/[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.33 by wakaba, Sat Jul 7 13:41:05 2007 UTC revision 1.184 by wakaba, Mon Sep 15 09:02:27 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6    ## NOTE: This module don't check all HTML5 parse errors; character
7    ## encoding related parse errors are expected to be handled by relevant
8    ## modules.
9    ## Parse errors for control characters that are not allowed in HTML5
10    ## documents, for surrogate code points, and for noncharacter code
11    ## points, as well as U+FFFD substitions for characters whose code points
12    ## is higher than U+10FFFF may be detected by combining the parser with
13    ## the checker implemented by Whatpm::Charset::UnicodeChecker (for its
14    ## usage example, see |t/HTML-tree.t| in the Whatpm package or the
15    ## WebHACC::Language::HTML module in the WebHACC package).
16    
17  ## ISSUE:  ## ISSUE:
18  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
19  ## doc.write ('');  ## doc.write ('');
20  ## alert (doc.compatMode);  ## alert (doc.compatMode);
21    
22  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  require IO::Handle;
23  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
24  ## is not yet clear.  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
25  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
26  ## "{U+FEFF}..." in GB18030?  my $SVG_NS = q<http://www.w3.org/2000/svg>;
27    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
28  my $permitted_slash_tag_name = {  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
29    base => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
30    link => 1,  
31    meta => 1,  sub A_EL () { 0b1 }
32    hr => 1,  sub ADDRESS_EL () { 0b10 }
33    br => 1,  sub BODY_EL () { 0b100 }
34    img=> 1,  sub BUTTON_EL () { 0b1000 }
35    embed => 1,  sub CAPTION_EL () { 0b10000 }
36    param => 1,  sub DD_EL () { 0b100000 }
37    area => 1,  sub DIV_EL () { 0b1000000 }
38    col => 1,  sub DT_EL () { 0b10000000 }
39    input => 1,  sub FORM_EL () { 0b100000000 }
40    sub FORMATTING_EL () { 0b1000000000 }
41    sub FRAMESET_EL () { 0b10000000000 }
42    sub HEADING_EL () { 0b100000000000 }
43    sub HTML_EL () { 0b1000000000000 }
44    sub LI_EL () { 0b10000000000000 }
45    sub NOBR_EL () { 0b100000000000000 }
46    sub OPTION_EL () { 0b1000000000000000 }
47    sub OPTGROUP_EL () { 0b10000000000000000 }
48    sub P_EL () { 0b100000000000000000 }
49    sub SELECT_EL () { 0b1000000000000000000 }
50    sub TABLE_EL () { 0b10000000000000000000 }
51    sub TABLE_CELL_EL () { 0b100000000000000000000 }
52    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
53    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
54    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
55    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
56    sub FOREIGN_EL () { 0b10000000000000000000000000 }
57    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
58    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
59    sub RUBY_EL () { 0b10000000000000000000000000000 }
60    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
61    
62    sub TABLE_ROWS_EL () {
63      TABLE_EL |
64      TABLE_ROW_EL |
65      TABLE_ROW_GROUP_EL
66    }
67    
68    ## NOTE: Used in "generate implied end tags" algorithm.
69    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
70    ## is used in "generate implied end tags" implementation (search for the
71    ## function mae).
72    sub END_TAG_OPTIONAL_EL () {
73      DD_EL |
74      DT_EL |
75      LI_EL |
76      P_EL |
77      RUBY_COMPONENT_EL
78    }
79    
80    ## NOTE: Used in </body> and EOF algorithms.
81    sub ALL_END_TAG_OPTIONAL_EL () {
82      DD_EL |
83      DT_EL |
84      LI_EL |
85      P_EL |
86    
87      BODY_EL |
88      HTML_EL |
89      TABLE_CELL_EL |
90      TABLE_ROW_EL |
91      TABLE_ROW_GROUP_EL
92    }
93    
94    sub SCOPING_EL () {
95      BUTTON_EL |
96      CAPTION_EL |
97      HTML_EL |
98      TABLE_EL |
99      TABLE_CELL_EL |
100      MISC_SCOPING_EL
101    }
102    
103    sub TABLE_SCOPING_EL () {
104      HTML_EL |
105      TABLE_EL
106    }
107    
108    sub TABLE_ROWS_SCOPING_EL () {
109      HTML_EL |
110      TABLE_ROW_GROUP_EL
111    }
112    
113    sub TABLE_ROW_SCOPING_EL () {
114      HTML_EL |
115      TABLE_ROW_EL
116    }
117    
118    sub SPECIAL_EL () {
119      ADDRESS_EL |
120      BODY_EL |
121      DIV_EL |
122    
123      DD_EL |
124      DT_EL |
125      LI_EL |
126      P_EL |
127    
128      FORM_EL |
129      FRAMESET_EL |
130      HEADING_EL |
131      OPTION_EL |
132      OPTGROUP_EL |
133      SELECT_EL |
134      TABLE_ROW_EL |
135      TABLE_ROW_GROUP_EL |
136      MISC_SPECIAL_EL
137    }
138    
139    my $el_category = {
140      a => A_EL | FORMATTING_EL,
141      address => ADDRESS_EL,
142      applet => MISC_SCOPING_EL,
143      area => MISC_SPECIAL_EL,
144      b => FORMATTING_EL,
145      base => MISC_SPECIAL_EL,
146      basefont => MISC_SPECIAL_EL,
147      bgsound => MISC_SPECIAL_EL,
148      big => FORMATTING_EL,
149      blockquote => MISC_SPECIAL_EL,
150      body => BODY_EL,
151      br => MISC_SPECIAL_EL,
152      button => BUTTON_EL,
153      caption => CAPTION_EL,
154      center => MISC_SPECIAL_EL,
155      col => MISC_SPECIAL_EL,
156      colgroup => MISC_SPECIAL_EL,
157      dd => DD_EL,
158      dir => MISC_SPECIAL_EL,
159      div => DIV_EL,
160      dl => MISC_SPECIAL_EL,
161      dt => DT_EL,
162      em => FORMATTING_EL,
163      embed => MISC_SPECIAL_EL,
164      fieldset => MISC_SPECIAL_EL,
165      font => FORMATTING_EL,
166      form => FORM_EL,
167      frame => MISC_SPECIAL_EL,
168      frameset => FRAMESET_EL,
169      h1 => HEADING_EL,
170      h2 => HEADING_EL,
171      h3 => HEADING_EL,
172      h4 => HEADING_EL,
173      h5 => HEADING_EL,
174      h6 => HEADING_EL,
175      head => MISC_SPECIAL_EL,
176      hr => MISC_SPECIAL_EL,
177      html => HTML_EL,
178      i => FORMATTING_EL,
179      iframe => MISC_SPECIAL_EL,
180      img => MISC_SPECIAL_EL,
181      input => MISC_SPECIAL_EL,
182      isindex => MISC_SPECIAL_EL,
183      li => LI_EL,
184      link => MISC_SPECIAL_EL,
185      listing => MISC_SPECIAL_EL,
186      marquee => MISC_SCOPING_EL,
187      menu => MISC_SPECIAL_EL,
188      meta => MISC_SPECIAL_EL,
189      nobr => NOBR_EL | FORMATTING_EL,
190      noembed => MISC_SPECIAL_EL,
191      noframes => MISC_SPECIAL_EL,
192      noscript => MISC_SPECIAL_EL,
193      object => MISC_SCOPING_EL,
194      ol => MISC_SPECIAL_EL,
195      optgroup => OPTGROUP_EL,
196      option => OPTION_EL,
197      p => P_EL,
198      param => MISC_SPECIAL_EL,
199      plaintext => MISC_SPECIAL_EL,
200      pre => MISC_SPECIAL_EL,
201      rp => RUBY_COMPONENT_EL,
202      rt => RUBY_COMPONENT_EL,
203      ruby => RUBY_EL,
204      s => FORMATTING_EL,
205      script => MISC_SPECIAL_EL,
206      select => SELECT_EL,
207      small => FORMATTING_EL,
208      spacer => MISC_SPECIAL_EL,
209      strike => FORMATTING_EL,
210      strong => FORMATTING_EL,
211      style => MISC_SPECIAL_EL,
212      table => TABLE_EL,
213      tbody => TABLE_ROW_GROUP_EL,
214      td => TABLE_CELL_EL,
215      textarea => MISC_SPECIAL_EL,
216      tfoot => TABLE_ROW_GROUP_EL,
217      th => TABLE_CELL_EL,
218      thead => TABLE_ROW_GROUP_EL,
219      title => MISC_SPECIAL_EL,
220      tr => TABLE_ROW_EL,
221      tt => FORMATTING_EL,
222      u => FORMATTING_EL,
223      ul => MISC_SPECIAL_EL,
224      wbr => MISC_SPECIAL_EL,
225    };
226    
227    my $el_category_f = {
228      $MML_NS => {
229        'annotation-xml' => MML_AXML_EL,
230        mi => FOREIGN_FLOW_CONTENT_EL,
231        mo => FOREIGN_FLOW_CONTENT_EL,
232        mn => FOREIGN_FLOW_CONTENT_EL,
233        ms => FOREIGN_FLOW_CONTENT_EL,
234        mtext => FOREIGN_FLOW_CONTENT_EL,
235      },
236      $SVG_NS => {
237        foreignObject => FOREIGN_FLOW_CONTENT_EL,
238        desc => FOREIGN_FLOW_CONTENT_EL,
239        title => FOREIGN_FLOW_CONTENT_EL,
240      },
241      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
242    };
243    
244    my $svg_attr_name = {
245      attributename => 'attributeName',
246      attributetype => 'attributeType',
247      basefrequency => 'baseFrequency',
248      baseprofile => 'baseProfile',
249      calcmode => 'calcMode',
250      clippathunits => 'clipPathUnits',
251      contentscripttype => 'contentScriptType',
252      contentstyletype => 'contentStyleType',
253      diffuseconstant => 'diffuseConstant',
254      edgemode => 'edgeMode',
255      externalresourcesrequired => 'externalResourcesRequired',
256      filterres => 'filterRes',
257      filterunits => 'filterUnits',
258      glyphref => 'glyphRef',
259      gradienttransform => 'gradientTransform',
260      gradientunits => 'gradientUnits',
261      kernelmatrix => 'kernelMatrix',
262      kernelunitlength => 'kernelUnitLength',
263      keypoints => 'keyPoints',
264      keysplines => 'keySplines',
265      keytimes => 'keyTimes',
266      lengthadjust => 'lengthAdjust',
267      limitingconeangle => 'limitingConeAngle',
268      markerheight => 'markerHeight',
269      markerunits => 'markerUnits',
270      markerwidth => 'markerWidth',
271      maskcontentunits => 'maskContentUnits',
272      maskunits => 'maskUnits',
273      numoctaves => 'numOctaves',
274      pathlength => 'pathLength',
275      patterncontentunits => 'patternContentUnits',
276      patterntransform => 'patternTransform',
277      patternunits => 'patternUnits',
278      pointsatx => 'pointsAtX',
279      pointsaty => 'pointsAtY',
280      pointsatz => 'pointsAtZ',
281      preservealpha => 'preserveAlpha',
282      preserveaspectratio => 'preserveAspectRatio',
283      primitiveunits => 'primitiveUnits',
284      refx => 'refX',
285      refy => 'refY',
286      repeatcount => 'repeatCount',
287      repeatdur => 'repeatDur',
288      requiredextensions => 'requiredExtensions',
289      requiredfeatures => 'requiredFeatures',
290      specularconstant => 'specularConstant',
291      specularexponent => 'specularExponent',
292      spreadmethod => 'spreadMethod',
293      startoffset => 'startOffset',
294      stddeviation => 'stdDeviation',
295      stitchtiles => 'stitchTiles',
296      surfacescale => 'surfaceScale',
297      systemlanguage => 'systemLanguage',
298      tablevalues => 'tableValues',
299      targetx => 'targetX',
300      targety => 'targetY',
301      textlength => 'textLength',
302      viewbox => 'viewBox',
303      viewtarget => 'viewTarget',
304      xchannelselector => 'xChannelSelector',
305      ychannelselector => 'yChannelSelector',
306      zoomandpan => 'zoomAndPan',
307  };  };
308    
309    my $foreign_attr_xname = {
310      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
311      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
312      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
313      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
314      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
315      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
316      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
317      'xml:base' => [$XML_NS, ['xml', 'base']],
318      'xml:lang' => [$XML_NS, ['xml', 'lang']],
319      'xml:space' => [$XML_NS, ['xml', 'space']],
320      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
321      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
322    };
323    
324    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
325    
326  my $c1_entity_char = {  my $c1_entity_char = {
327    0x80 => 0x20AC,    0x80 => 0x20AC,
328    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 358  my $c1_entity_char = {
358    0x9F => 0x0178,    0x9F => 0x0178,
359  }; # $c1_entity_char  }; # $c1_entity_char
360    
361  my $special_category = {  sub parse_byte_string ($$$$;$) {
362    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
363    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
364    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
365    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
366    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
367    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
368    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$$) {
369    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
370    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $self = ref $_[0] ? shift : shift->new;
371    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $charset_name = shift;
372  };    my $byte_stream = $_[0];
373  my $scoping_category = {  
374    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    my $onerror = $_[2] || sub {
375    table => 1, td => 1, th => 1,      my (%opt) = @_;
376  };      warn "Parse error ($opt{type})\n";
377  my $formatting_category = {    };
378    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,    $self->{parse_error} = $onerror; # updated later by parse_char_string
379    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
380  };    my $get_wrapper = $_[3] || sub ($) {
381  # $phrasing_category: all other elements      return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
382      };
383    
384      ## HTML5 encoding sniffing algorithm
385      require Message::Charset::Info;
386      my $charset;
387      my $buffer;
388      my ($char_stream, $e_status);
389    
390      SNIFFING: {
391        ## NOTE: By setting |allow_fallback| option true when the
392        ## |get_decode_handle| method is invoked, we ignore what the HTML5
393        ## spec requires, i.e. unsupported encoding should be ignored.
394          ## TODO: We should not do this unless the parser is invoked
395          ## in the conformance checking mode, in which this behavior
396          ## would be useful.
397    
398        ## Step 1
399        if (defined $charset_name) {
400          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
401              ## TODO: Is this ok?  Transfer protocol's parameter should be
402              ## interpreted in its semantics?
403    
404          ## ISSUE: Unsupported encoding is not ignored according to the spec.
405          ($char_stream, $e_status) = $charset->get_decode_handle
406              ($byte_stream, allow_error_reporting => 1,
407               allow_fallback => 1);
408          if ($char_stream) {
409            $self->{confident} = 1;
410            last SNIFFING;
411          } else {
412            ## TODO: unsupported error
413          }
414        }
415    
416        ## Step 2
417        my $byte_buffer = '';
418        for (1..1024) {
419          my $char = $byte_stream->getc;
420          last unless defined $char;
421          $byte_buffer .= $char;
422        } ## TODO: timeout
423    
424        ## Step 3
425        if ($byte_buffer =~ /^\xFE\xFF/) {
426          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
427          ($char_stream, $e_status) = $charset->get_decode_handle
428              ($byte_stream, allow_error_reporting => 1,
429               allow_fallback => 1, byte_buffer => \$byte_buffer);
430          $self->{confident} = 1;
431          last SNIFFING;
432        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
433          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
434          ($char_stream, $e_status) = $charset->get_decode_handle
435              ($byte_stream, allow_error_reporting => 1,
436               allow_fallback => 1, byte_buffer => \$byte_buffer);
437          $self->{confident} = 1;
438          last SNIFFING;
439        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
440          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($byte_stream, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          $self->{confident} = 1;
445          last SNIFFING;
446        }
447    
448        ## Step 4
449        ## TODO: <meta charset>
450    
451        ## Step 5
452        ## TODO: from history
453    
454        ## Step 6
455        require Whatpm::Charset::UniversalCharDet;
456        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
457            ($byte_buffer);
458        if (defined $charset_name) {
459          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
460    
461          ## ISSUE: Unsupported encoding is not ignored according to the spec.
462          require Whatpm::Charset::DecodeHandle;
463          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
464              ($byte_stream);
465          ($char_stream, $e_status) = $charset->get_decode_handle
466              ($buffer, allow_error_reporting => 1,
467               allow_fallback => 1, byte_buffer => \$byte_buffer);
468          if ($char_stream) {
469            $buffer->{buffer} = $byte_buffer;
470            !!!parse-error (type => 'sniffing:chardet',
471                            text => $charset_name,
472                            level => $self->{level}->{info},
473                            layer => 'encode',
474                            line => 1, column => 1);
475            $self->{confident} = 0;
476            last SNIFFING;
477          }
478        }
479    
480        ## Step 7: default
481        ## TODO: Make this configurable.
482        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
483            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
484            ## detectable in the step 6.
485        require Whatpm::Charset::DecodeHandle;
486        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
487            ($byte_stream);
488        ($char_stream, $e_status)
489            = $charset->get_decode_handle ($buffer,
490                                           allow_error_reporting => 1,
491                                           allow_fallback => 1,
492                                           byte_buffer => \$byte_buffer);
493        $buffer->{buffer} = $byte_buffer;
494        !!!parse-error (type => 'sniffing:default',
495                        text => 'windows-1252',
496                        level => $self->{level}->{info},
497                        line => 1, column => 1,
498                        layer => 'encode');
499        $self->{confident} = 0;
500      } # SNIFFING
501    
502      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
503        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
504        !!!parse-error (type => 'chardecode:fallback',
505                        #text => $self->{input_encoding},
506                        level => $self->{level}->{uncertain},
507                        line => 1, column => 1,
508                        layer => 'encode');
509      } elsif (not ($e_status &
510                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
511        $self->{input_encoding} = $charset->get_iana_name;
512        !!!parse-error (type => 'chardecode:no error',
513                        text => $self->{input_encoding},
514                        level => $self->{level}->{uncertain},
515                        line => 1, column => 1,
516                        layer => 'encode');
517      } else {
518        $self->{input_encoding} = $charset->get_iana_name;
519      }
520    
521      $self->{change_encoding} = sub {
522        my $self = shift;
523        $charset_name = shift;
524        my $token = shift;
525    
526  sub parse_string ($$$;$) {      $charset = Message::Charset::Info->get_by_html_name ($charset_name);
527    my $self = shift->new;      ($char_stream, $e_status) = $charset->get_decode_handle
528    my $s = \$_[0];          ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
529             byte_buffer => \ $buffer->{buffer});
530        
531        if ($char_stream) { # if supported
532          ## "Change the encoding" algorithm:
533    
534          ## Step 1    
535          if ($charset->{category} &
536              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
537            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
538            ($char_stream, $e_status) = $charset->get_decode_handle
539                ($byte_stream,
540                 byte_buffer => \ $buffer->{buffer});
541          }
542          $charset_name = $charset->get_iana_name;
543          
544          ## Step 2
545          if (defined $self->{input_encoding} and
546              $self->{input_encoding} eq $charset_name) {
547            !!!parse-error (type => 'charset label:matching',
548                            text => $charset_name,
549                            level => $self->{level}->{info});
550            $self->{confident} = 1;
551            return;
552          }
553    
554          !!!parse-error (type => 'charset label detected',
555                          text => $self->{input_encoding},
556                          value => $charset_name,
557                          level => $self->{level}->{warn},
558                          token => $token);
559          
560          ## Step 3
561          # if (can) {
562            ## change the encoding on the fly.
563            #$self->{confident} = 1;
564            #return;
565          # }
566          
567          ## Step 4
568          throw Whatpm::HTML::RestartParser ();
569        }
570      }; # $self->{change_encoding}
571    
572      my $char_onerror = sub {
573        my (undef, $type, %opt) = @_;
574        !!!parse-error (layer => 'encode',
575                        line => $self->{line}, column => $self->{column} + 1,
576                        %opt, type => $type);
577        if ($opt{octets}) {
578          ${$opt{octets}} = "\x{FFFD}"; # relacement character
579        }
580      };
581    
582      my $wrapped_char_stream = $get_wrapper->($char_stream);
583      $wrapped_char_stream->onerror ($char_onerror);
584    
585      my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
586      my $return;
587      try {
588        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
589      } catch Whatpm::HTML::RestartParser with {
590        ## NOTE: Invoked after {change_encoding}.
591    
592        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
593          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
594          !!!parse-error (type => 'chardecode:fallback',
595                          level => $self->{level}->{uncertain},
596                          #text => $self->{input_encoding},
597                          line => 1, column => 1,
598                          layer => 'encode');
599        } elsif (not ($e_status &
600                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
601          $self->{input_encoding} = $charset->get_iana_name;
602          !!!parse-error (type => 'chardecode:no error',
603                          text => $self->{input_encoding},
604                          level => $self->{level}->{uncertain},
605                          line => 1, column => 1,
606                          layer => 'encode');
607        } else {
608          $self->{input_encoding} = $charset->get_iana_name;
609        }
610        $self->{confident} = 1;
611    
612        $wrapped_char_stream = $get_wrapper->($char_stream);
613        $wrapped_char_stream->onerror ($char_onerror);
614    
615        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
616      };
617      return $return;
618    } # parse_byte_stream
619    
620    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
621    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
622    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
623    ## because the core part of our HTML parser expects a string of character,
624    ## not a string of bytes or code units or anything which might contain a BOM.
625    ## Therefore, any parser interface that accepts a string of bytes,
626    ## such as |parse_byte_string| in this module, must ensure that it does
627    ## strip the BOM and never strip any ZWNBSP.
628    
629    sub parse_char_string ($$$;$$) {
630      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
631      my $self = shift;
632      my $s = ref $_[0] ? $_[0] : \($_[0]);
633      require Whatpm::Charset::DecodeHandle;
634      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
635      return $self->parse_char_stream ($input, @_[1..$#_]);
636    } # parse_char_string
637    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
638    
639    sub parse_char_stream ($$$;$$) {
640      my $self = ref $_[0] ? shift : shift->new;
641      my $input = $_[0];
642    $self->{document} = $_[1];    $self->{document} = $_[1];
643      @{$self->{document}->child_nodes} = ();
644    
645    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
646    
647    my $i = 0;    $self->{confident} = 1 unless exists $self->{confident};
648    my $line = 1;    $self->{document}->input_encoding ($self->{input_encoding})
649    my $column = 0;        if defined $self->{input_encoding};
650    $self->{set_next_input_character} = sub {  ## TODO: |{input_encoding}| is needless?
651    
652      $self->{line_prev} = $self->{line} = 1;
653      $self->{column_prev} = -1;
654      $self->{column} = 0;
655      $self->{set_nc} = sub {
656      my $self = shift;      my $self = shift;
657    
658      pop @{$self->{prev_input_character}};      my $char = '';
659      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      if (defined $self->{next_nc}) {
660          $char = $self->{next_nc};
661          delete $self->{next_nc};
662          $self->{nc} = ord $char;
663        } else {
664          $self->{char_buffer} = '';
665          $self->{char_buffer_pos} = 0;
666    
667      $self->{next_input_character} = -1 and return if $i >= length $$s;        my $count = $input->manakai_read_until
668      $self->{next_input_character} = ord substr $$s, $i++, 1;           ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
669      $column++;        if ($count) {
670            $self->{line_prev} = $self->{line};
671            $self->{column_prev} = $self->{column};
672            $self->{column}++;
673            $self->{nc}
674                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
675            return;
676          }
677    
678          if ($input->read ($char, 1)) {
679            $self->{nc} = ord $char;
680          } else {
681            $self->{nc} = -1;
682            return;
683          }
684        }
685    
686        ($self->{line_prev}, $self->{column_prev})
687            = ($self->{line}, $self->{column});
688        $self->{column}++;
689            
690      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{nc} == 0x000A) { # LF
691        $line++;        !!!cp ('j1');
692        $column = 0;        $self->{line}++;
693      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
694        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{nc} == 0x000D) { # CR
695        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
696        $line++;  ## TODO: support for abort/streaming
697        $column = 0;        my $next = '';
698      } elsif ($self->{next_input_character} > 0x10FFFF) {        if ($input->read ($next, 1) and $next ne "\x0A") {
699        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_nc} = $next;
700      } elsif ($self->{next_input_character} == 0x0000) { # NULL        }
701          $self->{nc} = 0x000A; # LF # MUST
702          $self->{line}++;
703          $self->{column} = 0;
704        } elsif ($self->{nc} == 0x0000) { # NULL
705          !!!cp ('j4');
706        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
707        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
708      }      }
709    };    };
710    $self->{prev_input_character} = [-1, -1, -1];  
711    $self->{next_input_character} = -1;    $self->{read_until} = sub {
712        #my ($scalar, $specials_range, $offset) = @_;
713        return 0 if defined $self->{next_nc};
714    
715        my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
716        my $offset = $_[2] || 0;
717    
718        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
719          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
720          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
721            substr ($_[0], $offset)
722                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
723            my $count = $+[0] - $-[0];
724            if ($count) {
725              $self->{column} += $count;
726              $self->{char_buffer_pos} += $count;
727              $self->{line_prev} = $self->{line};
728              $self->{column_prev} = $self->{column} - 1;
729              $self->{nc} = -1;
730            }
731            return $count;
732          } else {
733            return 0;
734          }
735        } else {
736          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
737          if ($count) {
738            $self->{column} += $count;
739            $self->{line_prev} = $self->{line};
740            $self->{column_prev} = $self->{column} - 1;
741            $self->{nc} = -1;
742          }
743          return $count;
744        }
745      }; # $self->{read_until}
746    
747    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
748      my (%opt) = @_;      my (%opt) = @_;
749      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
750        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
751        warn "Parse error ($opt{type}) at line $line column $column\n";
752    };    };
753    $self->{parse_error} = sub {    $self->{parse_error} = sub {
754      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
755    };    };
756    
757      my $char_onerror = sub {
758        my (undef, $type, %opt) = @_;
759        !!!parse-error (layer => 'encode',
760                        line => $self->{line}, column => $self->{column} + 1,
761                        %opt, type => $type);
762      }; # $char_onerror
763    
764      if ($_[3]) {
765        $input = $_[3]->($input);
766        $input->onerror ($char_onerror);
767      } else {
768        $input->onerror ($char_onerror) unless defined $input->onerror;
769      }
770    
771    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
772    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
773    $self->_construct_tree;    $self->_construct_tree;
774    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
775    
776      delete $self->{parse_error}; # remove loop
777    
778    return $self->{document};    return $self->{document};
779  } # parse_string  } # parse_char_stream
780    
781  sub new ($) {  sub new ($) {
782    my $class = shift;    my $class = shift;
783    my $self = bless {}, $class;    my $self = bless {
784    $self->{set_next_input_character} = sub {      level => {must => 'm',
785      $self->{next_input_character} = -1;                should => 's',
786                  warn => 'w',
787                  info => 'i',
788                  uncertain => 'u'},
789      }, $class;
790      $self->{set_nc} = sub {
791        $self->{nc} = -1;
792    };    };
793    $self->{parse_error} = sub {    $self->{parse_error} = sub {
794      #      #
795    };    };
796      $self->{change_encoding} = sub {
797        # if ($_[0] is a supported encoding) {
798        #   run "change the encoding" algorithm;
799        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
800        # }
801      };
802      $self->{application_cache_selection} = sub {
803        #
804      };
805    return $self;    return $self;
806  } # new  } # new
807    
808    sub CM_ENTITY () { 0b001 } # & markup in data
809    sub CM_LIMITED_MARKUP () { 0b010 } # < markup in data (limited)
810    sub CM_FULL_MARKUP () { 0b100 } # < markup in data (any)
811    
812    sub PLAINTEXT_CONTENT_MODEL () { 0 }
813    sub CDATA_CONTENT_MODEL () { CM_LIMITED_MARKUP }
814    sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
815    sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
816    
817    sub DATA_STATE () { 0 }
818    #sub ENTITY_DATA_STATE () { 1 }
819    sub TAG_OPEN_STATE () { 2 }
820    sub CLOSE_TAG_OPEN_STATE () { 3 }
821    sub TAG_NAME_STATE () { 4 }
822    sub BEFORE_ATTRIBUTE_NAME_STATE () { 5 }
823    sub ATTRIBUTE_NAME_STATE () { 6 }
824    sub AFTER_ATTRIBUTE_NAME_STATE () { 7 }
825    sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 }
826    sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
827    sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
828    sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
829    #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
830    sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
831    sub COMMENT_START_STATE () { 14 }
832    sub COMMENT_START_DASH_STATE () { 15 }
833    sub COMMENT_STATE () { 16 }
834    sub COMMENT_END_STATE () { 17 }
835    sub COMMENT_END_DASH_STATE () { 18 }
836    sub BOGUS_COMMENT_STATE () { 19 }
837    sub DOCTYPE_STATE () { 20 }
838    sub BEFORE_DOCTYPE_NAME_STATE () { 21 }
839    sub DOCTYPE_NAME_STATE () { 22 }
840    sub AFTER_DOCTYPE_NAME_STATE () { 23 }
841    sub BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 24 }
842    sub DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE () { 25 }
843    sub DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE () { 26 }
844    sub AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE () { 27 }
845    sub BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 28 }
846    sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE () { 29 }
847    sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
848    sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
849    sub BOGUS_DOCTYPE_STATE () { 32 }
850    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
851    sub SELF_CLOSING_START_TAG_STATE () { 34 }
852    sub CDATA_SECTION_STATE () { 35 }
853    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
854    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
855    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
856    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
857    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
858    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
859    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
860    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
861    ## NOTE: "Entity data state", "entity in attribute value state", and
862    ## "consume a character reference" algorithm are jointly implemented
863    ## using the following six states:
864    sub ENTITY_STATE () { 44 }
865    sub ENTITY_HASH_STATE () { 45 }
866    sub NCR_NUM_STATE () { 46 }
867    sub HEXREF_X_STATE () { 47 }
868    sub HEXREF_HEX_STATE () { 48 }
869    sub ENTITY_NAME_STATE () { 49 }
870    
871    sub DOCTYPE_TOKEN () { 1 }
872    sub COMMENT_TOKEN () { 2 }
873    sub START_TAG_TOKEN () { 3 }
874    sub END_TAG_TOKEN () { 4 }
875    sub END_OF_FILE_TOKEN () { 5 }
876    sub CHARACTER_TOKEN () { 6 }
877    
878    sub AFTER_HTML_IMS () { 0b100 }
879    sub HEAD_IMS ()       { 0b1000 }
880    sub BODY_IMS ()       { 0b10000 }
881    sub BODY_TABLE_IMS () { 0b100000 }
882    sub TABLE_IMS ()      { 0b1000000 }
883    sub ROW_IMS ()        { 0b10000000 }
884    sub BODY_AFTER_IMS () { 0b100000000 }
885    sub FRAME_IMS ()      { 0b1000000000 }
886    sub SELECT_IMS ()     { 0b10000000000 }
887    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
888        ## NOTE: "in foreign content" insertion mode is special; it is combined
889        ## with the secondary insertion mode.  In this parser, they are stored
890        ## together in the bit-or'ed form.
891    
892    ## NOTE: "initial" and "before html" insertion modes have no constants.
893    
894    ## NOTE: "after after body" insertion mode.
895    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
896    
897    ## NOTE: "after after frameset" insertion mode.
898    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
899    
900    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
901    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
902    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
903    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
904    sub IN_BODY_IM () { BODY_IMS }
905    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
906    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
907    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
908    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
909    sub IN_TABLE_IM () { TABLE_IMS }
910    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
911    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
912    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
913    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
914    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
915    sub IN_COLUMN_GROUP_IM () { 0b10 }
916    
917  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
918    
919  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
920    my $self = shift;    my $self = shift;
921    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
922    $self->{content_model_flag} = 'PCDATA'; # be    #$self->{s_kwd}; # state keyword - initialized when used
923    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    #$self->{entity__value}; # initialized when used
924    undef $self->{current_attribute};    #$self->{entity__match}; # initialized when used
925    undef $self->{last_emitted_start_tag_name};    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
926    undef $self->{last_attribute_value_state};    undef $self->{ct}; # current token
927    $self->{char} = [];    undef $self->{ca}; # current attribute
928    # $self->{next_input_character}    undef $self->{last_stag_name}; # last emitted start tag name
929      #$self->{prev_state}; # initialized when used
930      delete $self->{self_closing};
931      $self->{char_buffer} = '';
932      $self->{char_buffer_pos} = 0;
933      $self->{nc} = -1; # next input character
934      #$self->{next_nc}
935    !!!next-input-character;    !!!next-input-character;
936    $self->{token} = [];    $self->{token} = [];
937    # $self->{escape}    # $self->{escape}
938  } # _initialize_tokenizer  } # _initialize_tokenizer
939    
940  ## A token has:  ## A token has:
941  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
942  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
943  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
944  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
945  ##   ->{system_identifier} (DOCTYPE)  ##   ->{pubid} (DOCTYPE_TOKEN)
946  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{sysid} (DOCTYPE_TOKEN)
947  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
948  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
949    ##        ->{name}
950    ##        ->{value}
951    ##        ->{has_reference} == 1 or 0
952    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
953    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
954    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
955    ##     while the token is pushed back to the stack.
956    
957  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
958    
# Line 185  sub _initialize_tokenizer ($) { Line 962  sub _initialize_tokenizer ($) {
962  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
963  ## and removed from the list.  ## and removed from the list.
964    
965    ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
966    ## (This requirement was dropped from HTML5 spec, unfortunately.)
967    
968  sub _get_next_token ($) {  sub _get_next_token ($) {
969    my $self = shift;    my $self = shift;
970    
971      if ($self->{self_closing}) {
972        !!!parse-error (type => 'nestc', token => $self->{ct});
973        ## NOTE: The |self_closing| flag is only set by start tag token.
974        ## In addition, when a start tag token is emitted, it is always set to
975        ## |ct|.
976        delete $self->{self_closing};
977      }
978    
979    if (@{$self->{token}}) {    if (@{$self->{token}}) {
980        $self->{self_closing} = $self->{token}->[0]->{self_closing};
981      return shift @{$self->{token}};      return shift @{$self->{token}};
982    }    }
983    
984    A: {    A: {
985      if ($self->{state} eq 'data') {      if ($self->{state} == DATA_STATE) {
986        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{nc} == 0x0026) { # &
987          if ($self->{content_model_flag} eq 'PCDATA' or          delete $self->{s_kwd};
988              $self->{content_model_flag} eq 'RCDATA') {          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
989            $self->{state} = 'entity data';              not $self->{escape}) {
990              !!!cp (1);
991              ## NOTE: In the spec, the tokenizer is switched to the
992              ## "entity data state".  In this implementation, the tokenizer
993              ## is switched to the |ENTITY_STATE|, which is an implementation
994              ## of the "consume a character reference" algorithm.
995              $self->{entity_add} = -1;
996              $self->{prev_state} = DATA_STATE;
997              $self->{state} = ENTITY_STATE;
998            !!!next-input-character;            !!!next-input-character;
999            redo A;            redo A;
1000          } else {          } else {
1001              !!!cp (2);
1002            #            #
1003          }          }
1004        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
1005          if ($self->{content_model_flag} eq 'RCDATA' or          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1006              $self->{content_model_flag} eq 'CDATA') {            if (defined $self->{s_kwd}) {
1007            unless ($self->{escape}) {              !!!cp (2.1);
1008              if ($self->{prev_input_character}->[0] == 0x002D and # -              $self->{s_kwd} .= '-';
1009                  $self->{prev_input_character}->[1] == 0x0021 and # !            } else {
1010                  $self->{prev_input_character}->[2] == 0x003C) { # <              !!!cp (2.2);
1011                $self->{escape} = 1;              $self->{s_kwd} = '-';
1012              }            }
1013    
1014              if ($self->{s_kwd} eq '<!--') {
1015                !!!cp (3);
1016                $self->{escape} = 1; # unless $self->{escape};
1017                $self->{s_kwd} = '--';
1018                #
1019              } elsif ($self->{s_kwd} eq '---') {
1020                !!!cp (4);
1021                $self->{s_kwd} = '--';
1022                #
1023              } else {
1024                !!!cp (5);
1025                #
1026            }            }
1027          }          }
1028                    
1029          #          #
1030        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{nc} == 0x0021) { # !
1031          if ($self->{content_model_flag} eq 'PCDATA' or          if (defined $self->{s_kwd}) {
1032              (($self->{content_model_flag} eq 'CDATA' or            !!!cp (5.1);
1033                $self->{content_model_flag} eq 'RCDATA') and            $self->{s_kwd} .= '!';
1034              #
1035            } else {
1036              !!!cp (5.2);
1037              #
1038            }
1039            #
1040          } elsif ($self->{nc} == 0x003C) { # <
1041            delete $self->{s_kwd};
1042            if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1043                (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1044               not $self->{escape})) {               not $self->{escape})) {
1045            $self->{state} = 'tag open';            !!!cp (6);
1046              $self->{state} = TAG_OPEN_STATE;
1047            !!!next-input-character;            !!!next-input-character;
1048            redo A;            redo A;
1049          } else {          } else {
1050              !!!cp (7);
1051            #            #
1052          }          }
1053        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1054          if ($self->{escape} and          if ($self->{escape} and
1055              ($self->{content_model_flag} eq 'RCDATA' or              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1056               $self->{content_model_flag} eq 'CDATA')) {            if (defined $self->{s_kwd} and $self->{s_kwd} eq '--') {
1057            if ($self->{prev_input_character}->[0] == 0x002D and # -              !!!cp (8);
               $self->{prev_input_character}->[1] == 0x002D) { # -  
1058              delete $self->{escape};              delete $self->{escape};
1059              } else {
1060                !!!cp (9);
1061            }            }
1062            } else {
1063              !!!cp (10);
1064          }          }
1065                    
1066            delete $self->{s_kwd};
1067          #          #
1068        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1069          !!!emit ({type => 'end-of-file'});          !!!cp (11);
1070            delete $self->{s_kwd};
1071            !!!emit ({type => END_OF_FILE_TOKEN,
1072                      line => $self->{line}, column => $self->{column}});
1073          last A; ## TODO: ok?          last A; ## TODO: ok?
1074          } else {
1075            !!!cp (12);
1076            delete $self->{s_kwd};
1077            #
1078        }        }
1079    
1080        # Anything else        # Anything else
1081        my $token = {type => 'character',        my $token = {type => CHARACTER_TOKEN,
1082                     data => chr $self->{next_input_character}};                     data => chr $self->{nc},
1083                       line => $self->{line}, column => $self->{column},
1084                      };
1085          if ($self->{read_until}->($token->{data}, q[-!<>&],
1086                                    length $token->{data})) {
1087            delete $self->{s_kwd};
1088          }
1089    
1090        ## Stay in the data state        ## Stay in the data state
1091        !!!next-input-character;        !!!next-input-character;
   
1092        !!!emit ($token);        !!!emit ($token);
   
1093        redo A;        redo A;
1094      } elsif ($self->{state} eq 'entity data') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1095        ## (cannot happen in CDATA state)        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1096                  if ($self->{nc} == 0x002F) { # /
1097        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);            !!!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) { # /  
1098            !!!next-input-character;            !!!next-input-character;
1099            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1100            redo A;            redo A;
1101            } elsif ($self->{nc} == 0x0021) { # !
1102              !!!cp (15.1);
1103              $self->{s_kwd} = '<' unless $self->{escape};
1104              #
1105          } else {          } else {
1106            ## reconsume            !!!cp (16);
1107            $self->{state} = 'data';            #
   
           !!!emit ({type => 'character', data => '<'});  
   
           redo A;  
1108          }          }
1109        } elsif ($self->{content_model_flag} eq 'PCDATA') {  
1110          if ($self->{next_input_character} == 0x0021) { # !          ## reconsume
1111            $self->{state} = 'markup declaration open';          $self->{state} = DATA_STATE;
1112            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1113                      line => $self->{line_prev},
1114                      column => $self->{column_prev},
1115                     });
1116            redo A;
1117          } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1118            if ($self->{nc} == 0x0021) { # !
1119              !!!cp (17);
1120              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1121            !!!next-input-character;            !!!next-input-character;
1122            redo A;            redo A;
1123          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1124            $self->{state} = 'close tag open';            !!!cp (18);
1125              $self->{state} = CLOSE_TAG_OPEN_STATE;
1126            !!!next-input-character;            !!!next-input-character;
1127            redo A;            redo A;
1128          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{nc} and
1129                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1130            $self->{current_token}            !!!cp (19);
1131              = {type => 'start tag',            $self->{ct}
1132                 tag_name => chr ($self->{next_input_character} + 0x0020)};              = {type => START_TAG_TOKEN,
1133            $self->{state} = 'tag name';                 tag_name => chr ($self->{nc} + 0x0020),
1134                   line => $self->{line_prev},
1135                   column => $self->{column_prev}};
1136              $self->{state} = TAG_NAME_STATE;
1137            !!!next-input-character;            !!!next-input-character;
1138            redo A;            redo A;
1139          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{nc} and
1140                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1141            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1142                              tag_name => chr ($self->{next_input_character})};            $self->{ct} = {type => START_TAG_TOKEN,
1143            $self->{state} = 'tag name';                                      tag_name => chr ($self->{nc}),
1144                                        line => $self->{line_prev},
1145                                        column => $self->{column_prev}};
1146              $self->{state} = TAG_NAME_STATE;
1147            !!!next-input-character;            !!!next-input-character;
1148            redo A;            redo A;
1149          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1150            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1151            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1152                              line => $self->{line_prev},
1153                              column => $self->{column_prev});
1154              $self->{state} = DATA_STATE;
1155            !!!next-input-character;            !!!next-input-character;
1156    
1157            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1158                        line => $self->{line_prev},
1159                        column => $self->{column_prev},
1160                       });
1161    
1162            redo A;            redo A;
1163          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1164            !!!parse-error (type => 'pio');            !!!cp (22);
1165            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1166            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1167                              column => $self->{column_prev});
1168              $self->{state} = BOGUS_COMMENT_STATE;
1169              $self->{ct} = {type => COMMENT_TOKEN, data => '',
1170                                        line => $self->{line_prev},
1171                                        column => $self->{column_prev},
1172                                       };
1173              ## $self->{nc} is intentionally left as is
1174            redo A;            redo A;
1175          } else {          } else {
1176            !!!parse-error (type => 'bare stago');            !!!cp (23);
1177            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1178                              line => $self->{line_prev},
1179                              column => $self->{column_prev});
1180              $self->{state} = DATA_STATE;
1181            ## reconsume            ## reconsume
1182    
1183            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1184                        line => $self->{line_prev},
1185                        column => $self->{column_prev},
1186                       });
1187    
1188            redo A;            redo A;
1189          }          }
1190        } else {        } else {
1191          die "$0: $self->{content_model_flag}: Unknown content model flag";          die "$0: $self->{content_model} in tag open";
1192        }        }
1193      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1194        if ($self->{content_model_flag} eq 'RCDATA' or        ## NOTE: The "close tag open state" in the spec is implemented as
1195            $self->{content_model_flag} eq 'CDATA') {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1196          if (defined $self->{last_emitted_start_tag_name}) {  
1197            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1198            my @next_char;        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1199            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {          if (defined $self->{last_stag_name}) {
1200              push @next_char, $self->{next_input_character};            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1201              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);            $self->{s_kwd} = '';
1202              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;            ## Reconsume.
1203              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {            redo A;
               !!!next-input-character;  
               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 => '</'});  
             redo A;  
           } else {  
             $self->{next_input_character} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1204          } else {          } else {
1205            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1206            # next-input-character is already done            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1207            $self->{state} = 'data';            !!!cp (28);
1208            !!!emit ({type => 'character', data => '</'});            $self->{state} = DATA_STATE;
1209              ## Reconsume.
1210              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1211                        line => $l, column => $c,
1212                       });
1213            redo A;            redo A;
1214          }          }
1215        }        }
1216          
1217        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{nc} and
1218            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1219          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1220                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{ct}
1221          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1222          !!!next-input-character;                 tag_name => chr ($self->{nc} + 0x0020),
1223          redo A;                 line => $l, column => $c};
1224        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1225                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1226          $self->{current_token} = {type => 'end tag',          redo A;
1227                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{nc} and
1228          $self->{state} = 'tag name';                 $self->{nc} <= 0x007A) { # a..z
1229          !!!next-input-character;          !!!cp (30);
1230          redo A;          $self->{ct} = {type => END_TAG_TOKEN,
1231        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{nc}),
1232          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1233          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1234            !!!next-input-character;
1235            redo A;
1236          } elsif ($self->{nc} == 0x003E) { # >
1237            !!!cp (31);
1238            !!!parse-error (type => 'empty end tag',
1239                            line => $self->{line_prev}, ## "<" in "</>"
1240                            column => $self->{column_prev} - 1);
1241            $self->{state} = DATA_STATE;
1242          !!!next-input-character;          !!!next-input-character;
1243          redo A;          redo A;
1244        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1245            !!!cp (32);
1246          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1247          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1248          # reconsume          # reconsume
1249    
1250          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1251                      line => $l, column => $c,
1252                     });
1253    
1254          redo A;          redo A;
1255        } else {        } else {
1256            !!!cp (33);
1257          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1258          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1259          ## $self->{next_input_character} is intentionally left as is          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1260          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1261                                      column => $self->{column_prev} - 1,
1262                                     };
1263            ## NOTE: $self->{nc} is intentionally left as is.
1264            ## Although the "anything else" case of the spec not explicitly
1265            ## states that the next input character is to be reconsumed,
1266            ## it will be included to the |data| of the comment token
1267            ## generated from the bogus end tag, as defined in the
1268            ## "bogus comment state" entry.
1269            redo A;
1270          }
1271        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1272          my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1273          if (length $ch) {
1274            my $CH = $ch;
1275            $ch =~ tr/a-z/A-Z/;
1276            my $nch = chr $self->{nc};
1277            if ($nch eq $ch or $nch eq $CH) {
1278              !!!cp (24);
1279              ## Stay in the state.
1280              $self->{s_kwd} .= $nch;
1281              !!!next-input-character;
1282              redo A;
1283            } else {
1284              !!!cp (25);
1285              $self->{state} = DATA_STATE;
1286              ## Reconsume.
1287              !!!emit ({type => CHARACTER_TOKEN,
1288                        data => '</' . $self->{s_kwd},
1289                        line => $self->{line_prev},
1290                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1291                       });
1292              redo A;
1293            }
1294          } else { # after "<{tag-name}"
1295            unless ({
1296                     0x0009 => 1, # HT
1297                     0x000A => 1, # LF
1298                     0x000B => 1, # VT
1299                     0x000C => 1, # FF
1300                     0x0020 => 1, # SP
1301                     0x003E => 1, # >
1302                     0x002F => 1, # /
1303                     -1 => 1, # EOF
1304                    }->{$self->{nc}}) {
1305              !!!cp (26);
1306              ## Reconsume.
1307              $self->{state} = DATA_STATE;
1308              !!!emit ({type => CHARACTER_TOKEN,
1309                        data => '</' . $self->{s_kwd},
1310                        line => $self->{line_prev},
1311                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1312                       });
1313              redo A;
1314            } else {
1315              !!!cp (27);
1316              $self->{ct}
1317                  = {type => END_TAG_TOKEN,
1318                     tag_name => $self->{last_stag_name},
1319                     line => $self->{line_prev},
1320                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1321              $self->{state} = TAG_NAME_STATE;
1322              ## Reconsume.
1323              redo A;
1324            }
1325        }        }
1326      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
1327        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1328            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1329            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1330            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1331            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1332          $self->{state} = 'before attribute name';          !!!cp (34);
1333          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1334          redo A;          !!!next-input-character;
1335        } elsif ($self->{next_input_character} == 0x003E) { # >          redo A;
1336          if ($self->{current_token}->{type} eq 'start tag') {        } elsif ($self->{nc} == 0x003E) { # >
1337            $self->{current_token}->{first_start_tag}          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1338                = not defined $self->{last_emitted_start_tag_name};            !!!cp (35);
1339            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1340          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1341            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1342            if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1343              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1344            }            #  !!! cp (36);
1345              #  !!! parse-error (type => 'end tag attribute');
1346              #} else {
1347                !!!cp (37);
1348              #}
1349          } else {          } else {
1350            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1351          }          }
1352          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1353          !!!next-input-character;          !!!next-input-character;
1354    
1355          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1356    
1357          redo A;          redo A;
1358        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1359                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1360          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1361            $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1362            # start tag or end tag            # start tag or end tag
1363          ## Stay in this state          ## Stay in this state
1364          !!!next-input-character;          !!!next-input-character;
1365          redo A;          redo A;
1366        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1367          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1368          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1369            $self->{current_token}->{first_start_tag}            !!!cp (39);
1370                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1371            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1372          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1373            $self->{content_model_flag} = 'PCDATA'; # MUST            #if ($self->{ct}->{attributes}) {
1374            if ($self->{current_token}->{attributes}) {            #  ## NOTE: This state should never be reached.
1375              !!!parse-error (type => 'end tag attribute');            #  !!! cp (40);
1376            }            #  !!! parse-error (type => 'end tag attribute');
1377              #} else {
1378                !!!cp (41);
1379              #}
1380          } else {          } else {
1381            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1382          }          }
1383          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1384          # reconsume          # reconsume
1385    
1386          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1387    
1388          redo A;          redo A;
1389        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1390            !!!cp (42);
1391            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1392          !!!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  
1393          redo A;          redo A;
1394        } else {        } else {
1395          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1396            $self->{ct}->{tag_name} .= chr $self->{nc};
1397            # start tag or end tag            # start tag or end tag
1398          ## Stay in the state          ## Stay in the state
1399          !!!next-input-character;          !!!next-input-character;
1400          redo A;          redo A;
1401        }        }
1402      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1403        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1404            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1405            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1406            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1407            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1408            !!!cp (45);
1409          ## Stay in the state          ## Stay in the state
1410          !!!next-input-character;          !!!next-input-character;
1411          redo A;          redo A;
1412        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1413          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1414            $self->{current_token}->{first_start_tag}            !!!cp (46);
1415                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1416            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1417          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1418            $self->{content_model_flag} = 'PCDATA'; # MUST            if ($self->{ct}->{attributes}) {
1419            if ($self->{current_token}->{attributes}) {              !!!cp (47);
1420              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1421              } else {
1422                !!!cp (48);
1423            }            }
1424          } else {          } else {
1425            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1426          }          }
1427          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1428          !!!next-input-character;          !!!next-input-character;
1429    
1430          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1431    
1432          redo A;          redo A;
1433        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1434                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1435          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1436                                value => ''};          $self->{ca}
1437          $self->{state} = 'attribute name';              = {name => chr ($self->{nc} + 0x0020),
1438                   value => '',
1439                   line => $self->{line}, column => $self->{column}};
1440            $self->{state} = ATTRIBUTE_NAME_STATE;
1441          !!!next-input-character;          !!!next-input-character;
1442          redo A;          redo A;
1443        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1444            !!!cp (50);
1445            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1446          !!!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');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1447          redo A;          redo A;
1448        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1449          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1450          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1451            $self->{current_token}->{first_start_tag}            !!!cp (52);
1452                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1453            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1454          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1455            $self->{content_model_flag} = 'PCDATA'; # MUST            if ($self->{ct}->{attributes}) {
1456            if ($self->{current_token}->{attributes}) {              !!!cp (53);
1457              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1458              } else {
1459                !!!cp (54);
1460            }            }
1461          } else {          } else {
1462            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1463          }          }
1464          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1465          # reconsume          # reconsume
1466    
1467          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1468    
1469          redo A;          redo A;
1470        } else {        } else {
1471          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1472                                value => ''};               0x0022 => 1, # "
1473          $self->{state} = 'attribute name';               0x0027 => 1, # '
1474                 0x003D => 1, # =
1475                }->{$self->{nc}}) {
1476              !!!cp (55);
1477              !!!parse-error (type => 'bad attribute name');
1478            } else {
1479              !!!cp (56);
1480            }
1481            $self->{ca}
1482                = {name => chr ($self->{nc}),
1483                   value => '',
1484                   line => $self->{line}, column => $self->{column}};
1485            $self->{state} = ATTRIBUTE_NAME_STATE;
1486          !!!next-input-character;          !!!next-input-character;
1487          redo A;          redo A;
1488        }        }
1489      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1490        my $before_leave = sub {        my $before_leave = sub {
1491          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1492              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1493            !!!parse-error (type => 'dupulicate attribute');            !!!cp (57);
1494            ## Discard $self->{current_attribute} # MUST            !!!parse-error (type => 'duplicate attribute', text => $self->{ca}->{name}, line => $self->{ca}->{line}, column => $self->{ca}->{column});
1495              ## Discard $self->{ca} # MUST
1496          } else {          } else {
1497            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            !!!cp (58);
1498              = $self->{current_attribute};            $self->{ct}->{attributes}->{$self->{ca}->{name}}
1499                = $self->{ca};
1500          }          }
1501        }; # $before_leave        }; # $before_leave
1502    
1503        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1504            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1505            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1506            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1507            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1508            !!!cp (59);
1509          $before_leave->();          $before_leave->();
1510          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1511          !!!next-input-character;          !!!next-input-character;
1512          redo A;          redo A;
1513        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1514            !!!cp (60);
1515          $before_leave->();          $before_leave->();
1516          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1517          !!!next-input-character;          !!!next-input-character;
1518          redo A;          redo A;
1519        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1520          $before_leave->();          $before_leave->();
1521          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1522            $self->{current_token}->{first_start_tag}            !!!cp (61);
1523                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1524            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1525          } elsif ($self->{current_token}->{type} eq 'end tag') {            !!!cp (62);
1526            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1527            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1528              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1529            }            }
1530          } else {          } else {
1531            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1532          }          }
1533          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1534          !!!next-input-character;          !!!next-input-character;
1535    
1536          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1537    
1538          redo A;          redo A;
1539        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1540                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1541          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1542            $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1543          ## Stay in the state          ## Stay in the state
1544          !!!next-input-character;          !!!next-input-character;
1545          redo A;          redo A;
1546        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1547            !!!cp (64);
1548          $before_leave->();          $before_leave->();
1549            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1550          !!!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  
1551          redo A;          redo A;
1552        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1553          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1554          $before_leave->();          $before_leave->();
1555          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1556            $self->{current_token}->{first_start_tag}            !!!cp (66);
1557                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1558            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1559          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1560            $self->{content_model_flag} = 'PCDATA'; # MUST            if ($self->{ct}->{attributes}) {
1561            if ($self->{current_token}->{attributes}) {              !!!cp (67);
1562              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1563              } else {
1564                ## NOTE: This state should never be reached.
1565                !!!cp (68);
1566            }            }
1567          } else {          } else {
1568            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1569          }          }
1570          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1571          # reconsume          # reconsume
1572    
1573          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1574    
1575          redo A;          redo A;
1576        } else {        } else {
1577          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x0022 or # "
1578                $self->{nc} == 0x0027) { # '
1579              !!!cp (69);
1580              !!!parse-error (type => 'bad attribute name');
1581            } else {
1582              !!!cp (70);
1583            }
1584            $self->{ca}->{name} .= chr ($self->{nc});
1585          ## Stay in the state          ## Stay in the state
1586          !!!next-input-character;          !!!next-input-character;
1587          redo A;          redo A;
1588        }        }
1589      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1590        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1591            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1592            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1593            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1594            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1595            !!!cp (71);
1596          ## Stay in the state          ## Stay in the state
1597          !!!next-input-character;          !!!next-input-character;
1598          redo A;          redo A;
1599        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1600          $self->{state} = 'before attribute value';          !!!cp (72);
1601            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1602          !!!next-input-character;          !!!next-input-character;
1603          redo A;          redo A;
1604        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1605          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1606            $self->{current_token}->{first_start_tag}            !!!cp (73);
1607                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1608            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1609          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1610            $self->{content_model_flag} = 'PCDATA'; # MUST            if ($self->{ct}->{attributes}) {
1611            if ($self->{current_token}->{attributes}) {              !!!cp (74);
1612              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1613              } else {
1614                ## NOTE: This state should never be reached.
1615                !!!cp (75);
1616            }            }
1617          } else {          } else {
1618            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1619          }          }
1620          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1621          !!!next-input-character;          !!!next-input-character;
1622    
1623          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1624    
1625          redo A;          redo A;
1626        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1627                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1628          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1629                                value => ''};          $self->{ca}
1630          $self->{state} = 'attribute name';              = {name => chr ($self->{nc} + 0x0020),
1631                   value => '',
1632                   line => $self->{line}, column => $self->{column}};
1633            $self->{state} = ATTRIBUTE_NAME_STATE;
1634          !!!next-input-character;          !!!next-input-character;
1635          redo A;          redo A;
1636        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1637            !!!cp (77);
1638            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1639          !!!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');  
           ## TODO: Different error type for <aa / bb> than <aa/>  
         }  
         $self->{state} = 'before attribute name';  
         # next-input-character is already done  
1640          redo A;          redo A;
1641        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1642          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1643          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1644            $self->{current_token}->{first_start_tag}            !!!cp (79);
1645                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1646            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1647          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1648            $self->{content_model_flag} = 'PCDATA'; # MUST            if ($self->{ct}->{attributes}) {
1649            if ($self->{current_token}->{attributes}) {              !!!cp (80);
1650              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1651              } else {
1652                ## NOTE: This state should never be reached.
1653                !!!cp (81);
1654            }            }
1655          } else {          } else {
1656            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1657          }          }
1658          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1659          # reconsume          # reconsume
1660    
1661          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1662    
1663          redo A;          redo A;
1664        } else {        } else {
1665          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{nc} == 0x0022 or # "
1666                                value => ''};              $self->{nc} == 0x0027) { # '
1667          $self->{state} = 'attribute name';            !!!cp (78);
1668              !!!parse-error (type => 'bad attribute name');
1669            } else {
1670              !!!cp (82);
1671            }
1672            $self->{ca}
1673                = {name => chr ($self->{nc}),
1674                   value => '',
1675                   line => $self->{line}, column => $self->{column}};
1676            $self->{state} = ATTRIBUTE_NAME_STATE;
1677          !!!next-input-character;          !!!next-input-character;
1678          redo A;                  redo A;        
1679        }        }
1680      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1681        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1682            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1683            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1684            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1685            $self->{next_input_character} == 0x0020) { # SP                  $self->{nc} == 0x0020) { # SP      
1686            !!!cp (83);
1687          ## Stay in the state          ## Stay in the state
1688          !!!next-input-character;          !!!next-input-character;
1689          redo A;          redo A;
1690        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
1691          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1692            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1693          !!!next-input-character;          !!!next-input-character;
1694          redo A;          redo A;
1695        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1696          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1697            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1698          ## reconsume          ## reconsume
1699          redo A;          redo A;
1700        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
1701          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1702            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1703          !!!next-input-character;          !!!next-input-character;
1704          redo A;          redo A;
1705        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1706          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1707            $self->{current_token}->{first_start_tag}          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1708                = not defined $self->{last_emitted_start_tag_name};            !!!cp (87);
1709            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1710          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1711            $self->{content_model_flag} = 'PCDATA'; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1712            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1713                !!!cp (88);
1714              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1715              } else {
1716                ## NOTE: This state should never be reached.
1717                !!!cp (89);
1718            }            }
1719          } else {          } else {
1720            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1721          }          }
1722          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1723          !!!next-input-character;          !!!next-input-character;
1724    
1725          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1726    
1727          redo A;          redo A;
1728        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1729          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1730          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1731            $self->{current_token}->{first_start_tag}            !!!cp (90);
1732                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1733            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1734          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1735            $self->{content_model_flag} = 'PCDATA'; # MUST            if ($self->{ct}->{attributes}) {
1736            if ($self->{current_token}->{attributes}) {              !!!cp (91);
1737              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1738              } else {
1739                ## NOTE: This state should never be reached.
1740                !!!cp (92);
1741            }            }
1742          } else {          } else {
1743            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1744          }          }
1745          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1746          ## reconsume          ## reconsume
1747    
1748          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1749    
1750          redo A;          redo A;
1751        } else {        } else {
1752          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x003D) { # =
1753          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1754              !!!parse-error (type => 'bad attribute value');
1755            } else {
1756              !!!cp (94);
1757            }
1758            $self->{ca}->{value} .= chr ($self->{nc});
1759            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1760          !!!next-input-character;          !!!next-input-character;
1761          redo A;          redo A;
1762        }        }
1763      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1764        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
1765          $self->{state} = 'before attribute name';          !!!cp (95);
1766            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1767          !!!next-input-character;          !!!next-input-character;
1768          redo A;          redo A;
1769        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1770          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1771          $self->{state} = 'entity in attribute value';          ## NOTE: In the spec, the tokenizer is switched to the
1772            ## "entity in attribute value state".  In this implementation, the
1773            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1774            ## implementation of the "consume a character reference" algorithm.
1775            $self->{prev_state} = $self->{state};
1776            $self->{entity_add} = 0x0022; # "
1777            $self->{state} = ENTITY_STATE;
1778          !!!next-input-character;          !!!next-input-character;
1779          redo A;          redo A;
1780        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1781          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1782          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1783            $self->{current_token}->{first_start_tag}            !!!cp (97);
1784                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1785            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1786          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1787            $self->{content_model_flag} = 'PCDATA'; # MUST            if ($self->{ct}->{attributes}) {
1788            if ($self->{current_token}->{attributes}) {              !!!cp (98);
1789              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1790              } else {
1791                ## NOTE: This state should never be reached.
1792                !!!cp (99);
1793            }            }
1794          } else {          } else {
1795            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1796          }          }
1797          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1798          ## reconsume          ## reconsume
1799    
1800          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1801    
1802          redo A;          redo A;
1803        } else {        } else {
1804          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1805            $self->{ca}->{value} .= chr ($self->{nc});
1806            $self->{read_until}->($self->{ca}->{value},
1807                                  q["&],
1808                                  length $self->{ca}->{value});
1809    
1810          ## Stay in the state          ## Stay in the state
1811          !!!next-input-character;          !!!next-input-character;
1812          redo A;          redo A;
1813        }        }
1814      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1815        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
1816          $self->{state} = 'before attribute name';          !!!cp (101);
1817            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1818            !!!next-input-character;
1819            redo A;
1820          } elsif ($self->{nc} == 0x0026) { # &
1821            !!!cp (102);
1822            ## NOTE: In the spec, the tokenizer is switched to the
1823            ## "entity in attribute value state".  In this implementation, the
1824            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1825            ## implementation of the "consume a character reference" algorithm.
1826            $self->{entity_add} = 0x0027; # '
1827            $self->{prev_state} = $self->{state};
1828            $self->{state} = ENTITY_STATE;
1829          !!!next-input-character;          !!!next-input-character;
1830          redo A;          redo A;
1831        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == -1) {
         $self->{last_attribute_value_state} = 'attribute value (single-quoted)';  
         $self->{state} = 'entity in attribute value';  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1832          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1833          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1834            $self->{current_token}->{first_start_tag}            !!!cp (103);
1835                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1836            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1837          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1838            $self->{content_model_flag} = 'PCDATA'; # MUST            if ($self->{ct}->{attributes}) {
1839            if ($self->{current_token}->{attributes}) {              !!!cp (104);
1840              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1841              } else {
1842                ## NOTE: This state should never be reached.
1843                !!!cp (105);
1844            }            }
1845          } else {          } else {
1846            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1847          }          }
1848          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1849          ## reconsume          ## reconsume
1850    
1851          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1852    
1853          redo A;          redo A;
1854        } else {        } else {
1855          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1856            $self->{ca}->{value} .= chr ($self->{nc});
1857            $self->{read_until}->($self->{ca}->{value},
1858                                  q['&],
1859                                  length $self->{ca}->{value});
1860    
1861          ## Stay in the state          ## Stay in the state
1862          !!!next-input-character;          !!!next-input-character;
1863          redo A;          redo A;
1864        }        }
1865      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1866        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1867            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1868            $self->{next_input_character} == 0x000B or # HT            $self->{nc} == 0x000B or # HT
1869            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1870            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1871          $self->{state} = 'before attribute name';          !!!cp (107);
1872          !!!next-input-character;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1873          redo A;          !!!next-input-character;
1874        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1875          $self->{last_attribute_value_state} = 'attribute value (unquoted)';        } elsif ($self->{nc} == 0x0026) { # &
1876          $self->{state} = 'entity in attribute value';          !!!cp (108);
1877          !!!next-input-character;          ## NOTE: In the spec, the tokenizer is switched to the
1878          redo A;          ## "entity in attribute value state".  In this implementation, the
1879        } elsif ($self->{next_input_character} == 0x003E) { # >          ## tokenizer is switched to the |ENTITY_STATE|, which is an
1880          if ($self->{current_token}->{type} eq 'start tag') {          ## implementation of the "consume a character reference" algorithm.
1881            $self->{current_token}->{first_start_tag}          $self->{entity_add} = -1;
1882                = not defined $self->{last_emitted_start_tag_name};          $self->{prev_state} = $self->{state};
1883            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          $self->{state} = ENTITY_STATE;
1884          } elsif ($self->{current_token}->{type} eq 'end tag') {          !!!next-input-character;
1885            $self->{content_model_flag} = 'PCDATA'; # MUST          redo A;
1886            if ($self->{current_token}->{attributes}) {        } elsif ($self->{nc} == 0x003E) { # >
1887            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1888              !!!cp (109);
1889              $self->{last_stag_name} = $self->{ct}->{tag_name};
1890            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1891              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1892              if ($self->{ct}->{attributes}) {
1893                !!!cp (110);
1894              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1895              } else {
1896                ## NOTE: This state should never be reached.
1897                !!!cp (111);
1898            }            }
1899          } else {          } else {
1900            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1901          }          }
1902          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1903          !!!next-input-character;          !!!next-input-character;
1904    
1905          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1906    
1907          redo A;          redo A;
1908        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1909          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1910          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1911            $self->{current_token}->{first_start_tag}            !!!cp (112);
1912                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1913            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1914          } elsif ($self->{current_token}->{type} eq 'end tag') {            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1915            $self->{content_model_flag} = 'PCDATA'; # MUST            if ($self->{ct}->{attributes}) {
1916            if ($self->{current_token}->{attributes}) {              !!!cp (113);
1917              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1918              } else {
1919                ## NOTE: This state should never be reached.
1920                !!!cp (114);
1921            }            }
1922          } else {          } else {
1923            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1924          }          }
1925          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1926          ## reconsume          ## reconsume
1927    
1928          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1929    
1930          redo A;          redo A;
1931        } else {        } else {
1932          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1933                 0x0022 => 1, # "
1934                 0x0027 => 1, # '
1935                 0x003D => 1, # =
1936                }->{$self->{nc}}) {
1937              !!!cp (115);
1938              !!!parse-error (type => 'bad attribute value');
1939            } else {
1940              !!!cp (116);
1941            }
1942            $self->{ca}->{value} .= chr ($self->{nc});
1943            $self->{read_until}->($self->{ca}->{value},
1944                                  q["'=& >],
1945                                  length $self->{ca}->{value});
1946    
1947          ## Stay in the state          ## Stay in the state
1948          !!!next-input-character;          !!!next-input-character;
1949          redo A;          redo A;
1950        }        }
1951      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1952        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        if ($self->{nc} == 0x0009 or # HT
1953              $self->{nc} == 0x000A or # LF
1954              $self->{nc} == 0x000B or # VT
1955              $self->{nc} == 0x000C or # FF
1956              $self->{nc} == 0x0020) { # SP
1957            !!!cp (118);
1958            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1959            !!!next-input-character;
1960            redo A;
1961          } elsif ($self->{nc} == 0x003E) { # >
1962            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1963              !!!cp (119);
1964              $self->{last_stag_name} = $self->{ct}->{tag_name};
1965            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1966              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1967              if ($self->{ct}->{attributes}) {
1968                !!!cp (120);
1969                !!!parse-error (type => 'end tag attribute');
1970              } else {
1971                ## NOTE: This state should never be reached.
1972                !!!cp (121);
1973              }
1974            } else {
1975              die "$0: $self->{ct}->{type}: Unknown token type";
1976            }
1977            $self->{state} = DATA_STATE;
1978            !!!next-input-character;
1979    
1980        unless (defined $token) {          !!!emit ($self->{ct}); # start tag or end tag
1981          $self->{current_attribute}->{value} .= '&';  
1982            redo A;
1983          } elsif ($self->{nc} == 0x002F) { # /
1984            !!!cp (122);
1985            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1986            !!!next-input-character;
1987            redo A;
1988          } elsif ($self->{nc} == -1) {
1989            !!!parse-error (type => 'unclosed tag');
1990            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1991              !!!cp (122.3);
1992              $self->{last_stag_name} = $self->{ct}->{tag_name};
1993            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1994              if ($self->{ct}->{attributes}) {
1995                !!!cp (122.1);
1996                !!!parse-error (type => 'end tag attribute');
1997              } else {
1998                ## NOTE: This state should never be reached.
1999                !!!cp (122.2);
2000              }
2001            } else {
2002              die "$0: $self->{ct}->{type}: Unknown token type";
2003            }
2004            $self->{state} = DATA_STATE;
2005            ## Reconsume.
2006            !!!emit ($self->{ct}); # start tag or end tag
2007            redo A;
2008        } else {        } else {
2009          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
2010          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
2011            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2012            ## reconsume
2013            redo A;
2014        }        }
2015        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
2016          if ($self->{nc} == 0x003E) { # >
2017            if ($self->{ct}->{type} == END_TAG_TOKEN) {
2018              !!!cp ('124.2');
2019              !!!parse-error (type => 'nestc', token => $self->{ct});
2020              ## TODO: Different type than slash in start tag
2021              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2022              if ($self->{ct}->{attributes}) {
2023                !!!cp ('124.4');
2024                !!!parse-error (type => 'end tag attribute');
2025              } else {
2026                !!!cp ('124.5');
2027              }
2028              ## TODO: Test |<title></title/>|
2029            } else {
2030              !!!cp ('124.3');
2031              $self->{self_closing} = 1;
2032            }
2033    
2034        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
2035        # 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  
2036    
2037            !!!emit ($token);          !!!emit ($self->{ct}); # start tag or end tag
2038    
2039            redo A;          redo A;
2040          } elsif ($self->{nc} == -1) {
2041            !!!parse-error (type => 'unclosed tag');
2042            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2043              !!!cp (124.7);
2044              $self->{last_stag_name} = $self->{ct}->{tag_name};
2045            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2046              if ($self->{ct}->{attributes}) {
2047                !!!cp (124.5);
2048                !!!parse-error (type => 'end tag attribute');
2049              } else {
2050                ## NOTE: This state should never be reached.
2051                !!!cp (124.6);
2052              }
2053          } else {          } else {
2054            $token->{data} .= chr ($self->{next_input_character});            die "$0: $self->{ct}->{type}: Unknown token type";
           !!!next-input-character;  
           redo BC;  
2055          }          }
2056        } # BC          $self->{state} = DATA_STATE;
2057      } elsif ($self->{state} eq 'markup declaration open') {          ## Reconsume.
2058            !!!emit ($self->{ct}); # start tag or end tag
2059            redo A;
2060          } else {
2061            !!!cp ('124.4');
2062            !!!parse-error (type => 'nestc');
2063            ## TODO: This error type is wrong.
2064            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2065            ## Reconsume.
2066            redo A;
2067          }
2068        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2069        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2070    
2071        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2072        push @next_char, $self->{next_input_character};        ## consumes characters one-by-one basis.
2073                
2074        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x003E) { # >
2075            !!!cp (124);
2076            $self->{state} = DATA_STATE;
2077          !!!next-input-character;          !!!next-input-character;
2078          push @next_char, $self->{next_input_character};  
2079          if ($self->{next_input_character} == 0x002D) { # -          !!!emit ($self->{ct}); # comment
2080            $self->{current_token} = {type => 'comment', data => ''};          redo A;
2081            $self->{state} = 'comment start';        } elsif ($self->{nc} == -1) {
2082            !!!next-input-character;          !!!cp (125);
2083            redo A;          $self->{state} = DATA_STATE;
2084          }          ## reconsume
2085        } elsif ($self->{next_input_character} == 0x0044 or # D  
2086                 $self->{next_input_character} == 0x0064) { # d          !!!emit ($self->{ct}); # comment
2087            redo A;
2088          } else {
2089            !!!cp (126);
2090            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2091            $self->{read_until}->($self->{ct}->{data},
2092                                  q[>],
2093                                  length $self->{ct}->{data});
2094    
2095            ## Stay in the state.
2096          !!!next-input-character;          !!!next-input-character;
2097          push @next_char, $self->{next_input_character};          redo A;
2098          if ($self->{next_input_character} == 0x004F or # O        }
2099              $self->{next_input_character} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2100            !!!next-input-character;        ## (only happen if PCDATA state)
2101            push @next_char, $self->{next_input_character};        
2102            if ($self->{next_input_character} == 0x0043 or # C        if ($self->{nc} == 0x002D) { # -
2103                $self->{next_input_character} == 0x0063) { # c          !!!cp (133);
2104              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2105              push @next_char, $self->{next_input_character};          !!!next-input-character;
2106              if ($self->{next_input_character} == 0x0054 or # T          redo A;
2107                  $self->{next_input_character} == 0x0074) { # t        } elsif ($self->{nc} == 0x0044 or # D
2108                !!!next-input-character;                 $self->{nc} == 0x0064) { # d
2109                push @next_char, $self->{next_input_character};          ## ASCII case-insensitive.
2110                if ($self->{next_input_character} == 0x0059 or # Y          !!!cp (130);
2111                    $self->{next_input_character} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2112                  !!!next-input-character;          $self->{s_kwd} = chr $self->{nc};
2113                  push @next_char, $self->{next_input_character};          !!!next-input-character;
2114                  if ($self->{next_input_character} == 0x0050 or # P          redo A;
2115                      $self->{next_input_character} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2116                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2117                    push @next_char, $self->{next_input_character};                 $self->{nc} == 0x005B) { # [
2118                    if ($self->{next_input_character} == 0x0045 or # E          !!!cp (135.4);                
2119                        $self->{next_input_character} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2120                      ## ISSUE: What a stupid code this is!          $self->{s_kwd} = '[';
2121                      $self->{state} = 'DOCTYPE';          !!!next-input-character;
2122                      !!!next-input-character;          redo A;
2123                      redo A;        } else {
2124                    }          !!!cp (136);
                 }  
               }  
             }  
           }  
         }  
2125        }        }
2126    
2127        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2128        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2129        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2130        $self->{state} = 'bogus comment';        ## Reconsume.
2131          $self->{state} = BOGUS_COMMENT_STATE;
2132          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2133                                    line => $self->{line_prev},
2134                                    column => $self->{column_prev} - 1,
2135                                   };
2136        redo A;        redo A;
2137              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2138        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2139        ## 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);
2140      } elsif ($self->{state} eq 'comment start') {          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2141        if ($self->{next_input_character} == 0x002D) { # -                                    line => $self->{line_prev},
2142          $self->{state} = 'comment start dash';                                    column => $self->{column_prev} - 2,
2143                                     };
2144            $self->{state} = COMMENT_START_STATE;
2145            !!!next-input-character;
2146            redo A;
2147          } else {
2148            !!!cp (128);
2149            !!!parse-error (type => 'bogus comment',
2150                            line => $self->{line_prev},
2151                            column => $self->{column_prev} - 2);
2152            $self->{state} = BOGUS_COMMENT_STATE;
2153            ## Reconsume.
2154            $self->{ct} = {type => COMMENT_TOKEN,
2155                                      data => '-',
2156                                      line => $self->{line_prev},
2157                                      column => $self->{column_prev} - 2,
2158                                     };
2159            redo A;
2160          }
2161        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2162          ## ASCII case-insensitive.
2163          if ($self->{nc} == [
2164                undef,
2165                0x004F, # O
2166                0x0043, # C
2167                0x0054, # T
2168                0x0059, # Y
2169                0x0050, # P
2170              ]->[length $self->{s_kwd}] or
2171              $self->{nc} == [
2172                undef,
2173                0x006F, # o
2174                0x0063, # c
2175                0x0074, # t
2176                0x0079, # y
2177                0x0070, # p
2178              ]->[length $self->{s_kwd}]) {
2179            !!!cp (131);
2180            ## Stay in the state.
2181            $self->{s_kwd} .= chr $self->{nc};
2182            !!!next-input-character;
2183            redo A;
2184          } elsif ((length $self->{s_kwd}) == 6 and
2185                   ($self->{nc} == 0x0045 or # E
2186                    $self->{nc} == 0x0065)) { # e
2187            !!!cp (129);
2188            $self->{state} = DOCTYPE_STATE;
2189            $self->{ct} = {type => DOCTYPE_TOKEN,
2190                                      quirks => 1,
2191                                      line => $self->{line_prev},
2192                                      column => $self->{column_prev} - 7,
2193                                     };
2194            !!!next-input-character;
2195            redo A;
2196          } else {
2197            !!!cp (132);        
2198            !!!parse-error (type => 'bogus comment',
2199                            line => $self->{line_prev},
2200                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2201            $self->{state} = BOGUS_COMMENT_STATE;
2202            ## Reconsume.
2203            $self->{ct} = {type => COMMENT_TOKEN,
2204                                      data => $self->{s_kwd},
2205                                      line => $self->{line_prev},
2206                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2207                                     };
2208            redo A;
2209          }
2210        } elsif ($self->{state} == MD_CDATA_STATE) {
2211          if ($self->{nc} == {
2212                '[' => 0x0043, # C
2213                '[C' => 0x0044, # D
2214                '[CD' => 0x0041, # A
2215                '[CDA' => 0x0054, # T
2216                '[CDAT' => 0x0041, # A
2217              }->{$self->{s_kwd}}) {
2218            !!!cp (135.1);
2219            ## Stay in the state.
2220            $self->{s_kwd} .= chr $self->{nc};
2221            !!!next-input-character;
2222            redo A;
2223          } elsif ($self->{s_kwd} eq '[CDATA' and
2224                   $self->{nc} == 0x005B) { # [
2225            !!!cp (135.2);
2226            $self->{ct} = {type => CHARACTER_TOKEN,
2227                                      data => '',
2228                                      line => $self->{line_prev},
2229                                      column => $self->{column_prev} - 7};
2230            $self->{state} = CDATA_SECTION_STATE;
2231          !!!next-input-character;          !!!next-input-character;
2232          redo A;          redo A;
2233        } elsif ($self->{next_input_character} == 0x003E) { # >        } else {
2234            !!!cp (135.3);
2235            !!!parse-error (type => 'bogus comment',
2236                            line => $self->{line_prev},
2237                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2238            $self->{state} = BOGUS_COMMENT_STATE;
2239            ## Reconsume.
2240            $self->{ct} = {type => COMMENT_TOKEN,
2241                                      data => $self->{s_kwd},
2242                                      line => $self->{line_prev},
2243                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2244                                     };
2245            redo A;
2246          }
2247        } elsif ($self->{state} == COMMENT_START_STATE) {
2248          if ($self->{nc} == 0x002D) { # -
2249            !!!cp (137);
2250            $self->{state} = COMMENT_START_DASH_STATE;
2251            !!!next-input-character;
2252            redo A;
2253          } elsif ($self->{nc} == 0x003E) { # >
2254            !!!cp (138);
2255          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2256          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2257          !!!next-input-character;          !!!next-input-character;
2258    
2259          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2260    
2261          redo A;          redo A;
2262        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2263            !!!cp (139);
2264          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2265          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2266          ## reconsume          ## reconsume
2267    
2268          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2269    
2270          redo A;          redo A;
2271        } else {        } else {
2272          $self->{current_token}->{data} # comment          !!!cp (140);
2273              .= chr ($self->{next_input_character});          $self->{ct}->{data} # comment
2274          $self->{state} = 'comment';              .= chr ($self->{nc});
2275            $self->{state} = COMMENT_STATE;
2276          !!!next-input-character;          !!!next-input-character;
2277          redo A;          redo A;
2278        }        }
2279      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2280        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2281          $self->{state} = 'comment end';          !!!cp (141);
2282            $self->{state} = COMMENT_END_STATE;
2283          !!!next-input-character;          !!!next-input-character;
2284          redo A;          redo A;
2285        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2286            !!!cp (142);
2287          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2288          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2289          !!!next-input-character;          !!!next-input-character;
2290    
2291          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2292    
2293          redo A;          redo A;
2294        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2295            !!!cp (143);
2296          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2297          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2298          ## reconsume          ## reconsume
2299    
2300          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2301    
2302          redo A;          redo A;
2303        } else {        } else {
2304          $self->{current_token}->{data} # comment          !!!cp (144);
2305              .= '-' . chr ($self->{next_input_character});          $self->{ct}->{data} # comment
2306          $self->{state} = 'comment';              .= '-' . chr ($self->{nc});
2307            $self->{state} = COMMENT_STATE;
2308          !!!next-input-character;          !!!next-input-character;
2309          redo A;          redo A;
2310        }        }
2311      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
2312        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2313          $self->{state} = 'comment end dash';          !!!cp (145);
2314            $self->{state} = COMMENT_END_DASH_STATE;
2315          !!!next-input-character;          !!!next-input-character;
2316          redo A;          redo A;
2317        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2318            !!!cp (146);
2319          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2320          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2321          ## reconsume          ## reconsume
2322    
2323          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2324    
2325          redo A;          redo A;
2326        } else {        } else {
2327          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2328            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2329            $self->{read_until}->($self->{ct}->{data},
2330                                  q[-],
2331                                  length $self->{ct}->{data});
2332    
2333          ## Stay in the state          ## Stay in the state
2334          !!!next-input-character;          !!!next-input-character;
2335          redo A;          redo A;
2336        }        }
2337      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2338        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2339          $self->{state} = 'comment end';          !!!cp (148);
2340            $self->{state} = COMMENT_END_STATE;
2341          !!!next-input-character;          !!!next-input-character;
2342          redo A;          redo A;
2343        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2344            !!!cp (149);
2345          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2346          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2347          ## reconsume          ## reconsume
2348    
2349          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2350    
2351          redo A;          redo A;
2352        } else {        } else {
2353          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2354          $self->{state} = 'comment';          $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2355            $self->{state} = COMMENT_STATE;
2356          !!!next-input-character;          !!!next-input-character;
2357          redo A;          redo A;
2358        }        }
2359      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2360        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2361          $self->{state} = 'data';          !!!cp (151);
2362            $self->{state} = DATA_STATE;
2363          !!!next-input-character;          !!!next-input-character;
2364    
2365          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2366    
2367          redo A;          redo A;
2368        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2369          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2370          $self->{current_token}->{data} .= '-'; # comment          !!!parse-error (type => 'dash in comment',
2371                            line => $self->{line_prev},
2372                            column => $self->{column_prev});
2373            $self->{ct}->{data} .= '-'; # comment
2374          ## Stay in the state          ## Stay in the state
2375          !!!next-input-character;          !!!next-input-character;
2376          redo A;          redo A;
2377        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2378            !!!cp (153);
2379          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2380          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2381          ## reconsume          ## reconsume
2382    
2383          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2384    
2385          redo A;          redo A;
2386        } else {        } else {
2387          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2388          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2389          $self->{state} = 'comment';                          line => $self->{line_prev},
2390                            column => $self->{column_prev});
2391            $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2392            $self->{state} = COMMENT_STATE;
2393          !!!next-input-character;          !!!next-input-character;
2394          redo A;          redo A;
2395        }        }
2396      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2397        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2398            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2399            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2400            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2401            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2402          $self->{state} = 'before DOCTYPE name';          !!!cp (155);
2403            $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2404          !!!next-input-character;          !!!next-input-character;
2405          redo A;          redo A;
2406        } else {        } else {
2407            !!!cp (156);
2408          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2409          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2410          ## reconsume          ## reconsume
2411          redo A;          redo A;
2412        }        }
2413      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2414        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2415            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2416            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2417            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2418            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2419            !!!cp (157);
2420          ## Stay in the state          ## Stay in the state
2421          !!!next-input-character;          !!!next-input-character;
2422          redo A;          redo A;
2423        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2424            !!!cp (158);
2425          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2426          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2427          !!!next-input-character;          !!!next-input-character;
2428    
2429          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2430    
2431          redo A;          redo A;
2432        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2433            !!!cp (159);
2434          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2435          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2436          ## reconsume          ## reconsume
2437    
2438          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2439    
2440          redo A;          redo A;
2441        } else {        } else {
2442          $self->{current_token}          !!!cp (160);
2443              = {type => 'DOCTYPE',          $self->{ct}->{name} = chr $self->{nc};
2444                 name => chr ($self->{next_input_character}),          delete $self->{ct}->{quirks};
                correct => 1};  
2445  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2446          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2447          !!!next-input-character;          !!!next-input-character;
2448          redo A;          redo A;
2449        }        }
2450      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2451  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2452        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2453            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2454            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2455            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2456            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2457          $self->{state} = 'after DOCTYPE name';          !!!cp (161);
2458            $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2459          !!!next-input-character;          !!!next-input-character;
2460          redo A;          redo A;
2461        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2462          $self->{state} = 'data';          !!!cp (162);
2463            $self->{state} = DATA_STATE;
2464          !!!next-input-character;          !!!next-input-character;
2465    
2466          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2467    
2468          redo A;          redo A;
2469        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2470            !!!cp (163);
2471          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2472          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2473          ## reconsume          ## reconsume
2474    
2475          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2476          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2477    
2478          redo A;          redo A;
2479        } else {        } else {
2480          $self->{current_token}->{name}          !!!cp (164);
2481            .= chr ($self->{next_input_character}); # DOCTYPE          $self->{ct}->{name}
2482              .= chr ($self->{nc}); # DOCTYPE
2483          ## Stay in the state          ## Stay in the state
2484          !!!next-input-character;          !!!next-input-character;
2485          redo A;          redo A;
2486        }        }
2487      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2488        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2489            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2490            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2491            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2492            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2493            !!!cp (165);
2494          ## Stay in the state          ## Stay in the state
2495          !!!next-input-character;          !!!next-input-character;
2496          redo A;          redo A;
2497        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2498          $self->{state} = 'data';          !!!cp (166);
2499            $self->{state} = DATA_STATE;
2500          !!!next-input-character;          !!!next-input-character;
2501    
2502          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2503    
2504          redo A;          redo A;
2505        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2506            !!!cp (167);
2507          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2508          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2509          ## reconsume          ## reconsume
2510    
2511          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2512          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2513    
2514          redo A;          redo A;
2515        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{nc} == 0x0050 or # P
2516                 $self->{next_input_character} == 0x0070) { # p                 $self->{nc} == 0x0070) { # p
2517            $self->{state} = PUBLIC_STATE;
2518            $self->{s_kwd} = chr $self->{nc};
2519          !!!next-input-character;          !!!next-input-character;
2520          if ($self->{next_input_character} == 0x0055 or # U          redo A;
2521              $self->{next_input_character} == 0x0075) { # u        } elsif ($self->{nc} == 0x0053 or # S
2522            !!!next-input-character;                 $self->{nc} == 0x0073) { # s
2523            if ($self->{next_input_character} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2524                $self->{next_input_character} == 0x0062) { # b          $self->{s_kwd} = chr $self->{nc};
             !!!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  
2525          !!!next-input-character;          !!!next-input-character;
2526          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;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
2527        } else {        } else {
2528            !!!cp (180);
2529            !!!parse-error (type => 'string after DOCTYPE name');
2530            $self->{ct}->{quirks} = 1;
2531    
2532            $self->{state} = BOGUS_DOCTYPE_STATE;
2533          !!!next-input-character;          !!!next-input-character;
2534          #          redo A;
2535        }        }
2536        } elsif ($self->{state} == PUBLIC_STATE) {
2537          ## ASCII case-insensitive
2538          if ($self->{nc} == [
2539                undef,
2540                0x0055, # U
2541                0x0042, # B
2542                0x004C, # L
2543                0x0049, # I
2544              ]->[length $self->{s_kwd}] or
2545              $self->{nc} == [
2546                undef,
2547                0x0075, # u
2548                0x0062, # b
2549                0x006C, # l
2550                0x0069, # i
2551              ]->[length $self->{s_kwd}]) {
2552            !!!cp (175);
2553            ## Stay in the state.
2554            $self->{s_kwd} .= chr $self->{nc};
2555            !!!next-input-character;
2556            redo A;
2557          } elsif ((length $self->{s_kwd}) == 5 and
2558                   ($self->{nc} == 0x0043 or # C
2559                    $self->{nc} == 0x0063)) { # c
2560            !!!cp (168);
2561            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2562            !!!next-input-character;
2563            redo A;
2564          } else {
2565            !!!cp (169);
2566            !!!parse-error (type => 'string after DOCTYPE name',
2567                            line => $self->{line_prev},
2568                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2569            $self->{ct}->{quirks} = 1;
2570    
2571            $self->{state} = BOGUS_DOCTYPE_STATE;
2572            ## Reconsume.
2573            redo A;
2574          }
2575        } elsif ($self->{state} == SYSTEM_STATE) {
2576          ## ASCII case-insensitive
2577          if ($self->{nc} == [
2578                undef,
2579                0x0059, # Y
2580                0x0053, # S
2581                0x0054, # T
2582                0x0045, # E
2583              ]->[length $self->{s_kwd}] or
2584              $self->{nc} == [
2585                undef,
2586                0x0079, # y
2587                0x0073, # s
2588                0x0074, # t
2589                0x0065, # e
2590              ]->[length $self->{s_kwd}]) {
2591            !!!cp (170);
2592            ## Stay in the state.
2593            $self->{s_kwd} .= chr $self->{nc};
2594            !!!next-input-character;
2595            redo A;
2596          } elsif ((length $self->{s_kwd}) == 5 and
2597                   ($self->{nc} == 0x004D or # M
2598                    $self->{nc} == 0x006D)) { # m
2599            !!!cp (171);
2600            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2601            !!!next-input-character;
2602            redo A;
2603          } else {
2604            !!!cp (172);
2605            !!!parse-error (type => 'string after DOCTYPE name',
2606                            line => $self->{line_prev},
2607                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2608            $self->{ct}->{quirks} = 1;
2609    
2610        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2611        $self->{state} = 'bogus DOCTYPE';          ## Reconsume.
2612        # next-input-character is already done          redo A;
2613        redo A;        }
2614      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2615        if ({        if ({
2616              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2617              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2618            }->{$self->{next_input_character}}) {            }->{$self->{nc}}) {
2619            !!!cp (181);
2620          ## Stay in the state          ## Stay in the state
2621          !!!next-input-character;          !!!next-input-character;
2622          redo A;          redo A;
2623        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{nc} eq 0x0022) { # "
2624          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          !!!cp (182);
2625          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{ct}->{pubid} = ''; # DOCTYPE
2626            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2627          !!!next-input-character;          !!!next-input-character;
2628          redo A;          redo A;
2629        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{nc} eq 0x0027) { # '
2630          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          !!!cp (183);
2631          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{ct}->{pubid} = ''; # DOCTYPE
2632            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2633          !!!next-input-character;          !!!next-input-character;
2634          redo A;          redo A;
2635        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{nc} eq 0x003E) { # >
2636            !!!cp (184);
2637          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2638    
2639          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2640          !!!next-input-character;          !!!next-input-character;
2641    
2642          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2643          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2644    
2645          redo A;          redo A;
2646        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2647            !!!cp (185);
2648          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2649    
2650          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2651          ## reconsume          ## reconsume
2652    
2653          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2654          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2655    
2656          redo A;          redo A;
2657        } else {        } else {
2658            !!!cp (186);
2659          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2660          $self->{state} = 'bogus DOCTYPE';          $self->{ct}->{quirks} = 1;
2661    
2662            $self->{state} = BOGUS_DOCTYPE_STATE;
2663          !!!next-input-character;          !!!next-input-character;
2664          redo A;          redo A;
2665        }        }
2666      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2667        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2668          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2669            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2670            !!!next-input-character;
2671            redo A;
2672          } elsif ($self->{nc} == 0x003E) { # >
2673            !!!cp (188);
2674            !!!parse-error (type => 'unclosed PUBLIC literal');
2675    
2676            $self->{state} = DATA_STATE;
2677          !!!next-input-character;          !!!next-input-character;
2678    
2679            $self->{ct}->{quirks} = 1;
2680            !!!emit ($self->{ct}); # DOCTYPE
2681    
2682          redo A;          redo A;
2683        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2684            !!!cp (189);
2685          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2686    
2687          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2688          ## reconsume          ## reconsume
2689    
2690          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2691          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2692    
2693          redo A;          redo A;
2694        } else {        } else {
2695          $self->{current_token}->{public_identifier} # DOCTYPE          !!!cp (190);
2696              .= chr $self->{next_input_character};          $self->{ct}->{pubid} # DOCTYPE
2697                .= chr $self->{nc};
2698            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2699                                  length $self->{ct}->{pubid});
2700    
2701          ## Stay in the state          ## Stay in the state
2702          !!!next-input-character;          !!!next-input-character;
2703          redo A;          redo A;
2704        }        }
2705      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2706        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2707          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2708            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2709          !!!next-input-character;          !!!next-input-character;
2710          redo A;          redo A;
2711        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2712            !!!cp (192);
2713          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2714    
2715          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2716            !!!next-input-character;
2717    
2718            $self->{ct}->{quirks} = 1;
2719            !!!emit ($self->{ct}); # DOCTYPE
2720    
2721            redo A;
2722          } elsif ($self->{nc} == -1) {
2723            !!!cp (193);
2724            !!!parse-error (type => 'unclosed PUBLIC literal');
2725    
2726            $self->{state} = DATA_STATE;
2727          ## reconsume          ## reconsume
2728    
2729          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2730          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2731    
2732          redo A;          redo A;
2733        } else {        } else {
2734          $self->{current_token}->{public_identifier} # DOCTYPE          !!!cp (194);
2735              .= chr $self->{next_input_character};          $self->{ct}->{pubid} # DOCTYPE
2736                .= chr $self->{nc};
2737            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2738                                  length $self->{ct}->{pubid});
2739    
2740          ## Stay in the state          ## Stay in the state
2741          !!!next-input-character;          !!!next-input-character;
2742          redo A;          redo A;
2743        }        }
2744      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2745        if ({        if ({
2746              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2747              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2748            }->{$self->{next_input_character}}) {            }->{$self->{nc}}) {
2749            !!!cp (195);
2750          ## Stay in the state          ## Stay in the state
2751          !!!next-input-character;          !!!next-input-character;
2752          redo A;          redo A;
2753        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2754          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (196);
2755          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{ct}->{sysid} = ''; # DOCTYPE
2756            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2757          !!!next-input-character;          !!!next-input-character;
2758          redo A;          redo A;
2759        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2760          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (197);
2761          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{ct}->{sysid} = ''; # DOCTYPE
2762            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2763          !!!next-input-character;          !!!next-input-character;
2764          redo A;          redo A;
2765        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2766          $self->{state} = 'data';          !!!cp (198);
2767            $self->{state} = DATA_STATE;
2768          !!!next-input-character;          !!!next-input-character;
2769    
2770          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2771    
2772          redo A;          redo A;
2773        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2774            !!!cp (199);
2775          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2776    
2777          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2778          ## reconsume          ## reconsume
2779    
2780          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2781          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2782    
2783          redo A;          redo A;
2784        } else {        } else {
2785            !!!cp (200);
2786          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2787          $self->{state} = 'bogus DOCTYPE';          $self->{ct}->{quirks} = 1;
2788    
2789            $self->{state} = BOGUS_DOCTYPE_STATE;
2790          !!!next-input-character;          !!!next-input-character;
2791          redo A;          redo A;
2792        }        }
2793      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2794        if ({        if ({
2795              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2796              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2797            }->{$self->{next_input_character}}) {            }->{$self->{nc}}) {
2798            !!!cp (201);
2799          ## Stay in the state          ## Stay in the state
2800          !!!next-input-character;          !!!next-input-character;
2801          redo A;          redo A;
2802        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2803          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (202);
2804          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{ct}->{sysid} = ''; # DOCTYPE
2805            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2806          !!!next-input-character;          !!!next-input-character;
2807          redo A;          redo A;
2808        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2809          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (203);
2810          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{ct}->{sysid} = ''; # DOCTYPE
2811            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2812          !!!next-input-character;          !!!next-input-character;
2813          redo A;          redo A;
2814        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2815            !!!cp (204);
2816          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2817          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2818          !!!next-input-character;          !!!next-input-character;
2819    
2820          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2821          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2822    
2823          redo A;          redo A;
2824        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2825            !!!cp (205);
2826          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2827    
2828          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2829          ## reconsume          ## reconsume
2830    
2831          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2832          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2833    
2834          redo A;          redo A;
2835        } else {        } else {
2836            !!!cp (206);
2837          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2838          $self->{state} = 'bogus DOCTYPE';          $self->{ct}->{quirks} = 1;
2839    
2840            $self->{state} = BOGUS_DOCTYPE_STATE;
2841          !!!next-input-character;          !!!next-input-character;
2842          redo A;          redo A;
2843        }        }
2844      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2845        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2846          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2847            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2848          !!!next-input-character;          !!!next-input-character;
2849          redo A;          redo A;
2850        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2851            !!!cp (208);
2852          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2853    
2854          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2855            !!!next-input-character;
2856    
2857            $self->{ct}->{quirks} = 1;
2858            !!!emit ($self->{ct}); # DOCTYPE
2859    
2860            redo A;
2861          } elsif ($self->{nc} == -1) {
2862            !!!cp (209);
2863            !!!parse-error (type => 'unclosed SYSTEM literal');
2864    
2865            $self->{state} = DATA_STATE;
2866          ## reconsume          ## reconsume
2867    
2868          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2869          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2870    
2871          redo A;          redo A;
2872        } else {        } else {
2873          $self->{current_token}->{system_identifier} # DOCTYPE          !!!cp (210);
2874              .= chr $self->{next_input_character};          $self->{ct}->{sysid} # DOCTYPE
2875                .= chr $self->{nc};
2876            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2877                                  length $self->{ct}->{sysid});
2878    
2879          ## Stay in the state          ## Stay in the state
2880          !!!next-input-character;          !!!next-input-character;
2881          redo A;          redo A;
2882        }        }
2883      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2884        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2885          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2886            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2887            !!!next-input-character;
2888            redo A;
2889          } elsif ($self->{nc} == 0x003E) { # >
2890            !!!cp (212);
2891            !!!parse-error (type => 'unclosed SYSTEM literal');
2892    
2893            $self->{state} = DATA_STATE;
2894          !!!next-input-character;          !!!next-input-character;
2895    
2896            $self->{ct}->{quirks} = 1;
2897            !!!emit ($self->{ct}); # DOCTYPE
2898    
2899          redo A;          redo A;
2900        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2901            !!!cp (213);
2902          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2903    
2904          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2905          ## reconsume          ## reconsume
2906    
2907          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2908          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2909    
2910          redo A;          redo A;
2911        } else {        } else {
2912          $self->{current_token}->{system_identifier} # DOCTYPE          !!!cp (214);
2913              .= chr $self->{next_input_character};          $self->{ct}->{sysid} # DOCTYPE
2914                .= chr $self->{nc};
2915            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2916                                  length $self->{ct}->{sysid});
2917    
2918          ## Stay in the state          ## Stay in the state
2919          !!!next-input-character;          !!!next-input-character;
2920          redo A;          redo A;
2921        }        }
2922      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2923        if ({        if ({
2924              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2925              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2926            }->{$self->{next_input_character}}) {            }->{$self->{nc}}) {
2927            !!!cp (215);
2928          ## Stay in the state          ## Stay in the state
2929          !!!next-input-character;          !!!next-input-character;
2930          redo A;          redo A;
2931        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2932          $self->{state} = 'data';          !!!cp (216);
2933            $self->{state} = DATA_STATE;
2934          !!!next-input-character;          !!!next-input-character;
2935    
2936          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2937    
2938          redo A;          redo A;
2939        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2940            !!!cp (217);
2941          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2942            $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2943          ## reconsume          ## reconsume
2944    
2945          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2946          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2947    
2948          redo A;          redo A;
2949        } else {        } else {
2950            !!!cp (218);
2951          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2952          $self->{state} = 'bogus DOCTYPE';          #$self->{ct}->{quirks} = 1;
2953    
2954            $self->{state} = BOGUS_DOCTYPE_STATE;
2955          !!!next-input-character;          !!!next-input-character;
2956          redo A;          redo A;
2957        }        }
2958      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2959        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2960          $self->{state} = 'data';          !!!cp (219);
2961            $self->{state} = DATA_STATE;
2962          !!!next-input-character;          !!!next-input-character;
2963    
2964          delete $self->{current_token}->{correct};          !!!emit ($self->{ct}); # DOCTYPE
         !!!emit ($self->{current_token}); # DOCTYPE  
2965    
2966          redo A;          redo A;
2967        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2968            !!!cp (220);
2969          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2970          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2971          ## reconsume          ## reconsume
2972    
2973          delete $self->{current_token}->{correct};          !!!emit ($self->{ct}); # DOCTYPE
         !!!emit ($self->{current_token}); # DOCTYPE  
2974    
2975          redo A;          redo A;
2976        } else {        } else {
2977            !!!cp (221);
2978            my $s = '';
2979            $self->{read_until}->($s, q[>], 0);
2980    
2981          ## Stay in the state          ## Stay in the state
2982          !!!next-input-character;          !!!next-input-character;
2983          redo A;          redo A;
2984        }        }
2985      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2986        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2987      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2988    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2989          
2990    die "$0: _get_next_token: unexpected case";        if ($self->{nc} == 0x005D) { # ]
2991  } # _get_next_token          !!!cp (221.1);
2992            $self->{state} = CDATA_SECTION_MSE1_STATE;
2993            !!!next-input-character;
2994            redo A;
2995          } elsif ($self->{nc} == -1) {
2996            $self->{state} = DATA_STATE;
2997            !!!next-input-character;
2998            if (length $self->{ct}->{data}) { # character
2999              !!!cp (221.2);
3000              !!!emit ($self->{ct}); # character
3001            } else {
3002              !!!cp (221.3);
3003              ## No token to emit. $self->{ct} is discarded.
3004            }        
3005            redo A;
3006          } else {
3007            !!!cp (221.4);
3008            $self->{ct}->{data} .= chr $self->{nc};
3009            $self->{read_until}->($self->{ct}->{data},
3010                                  q<]>,
3011                                  length $self->{ct}->{data});
3012    
3013  sub _tokenize_attempt_to_consume_an_entity ($$) {          ## Stay in the state.
3014    my ($self, $in_attr) = @_;          !!!next-input-character;
3015            redo A;
3016          }
3017    
3018    if ({        ## ISSUE: "text tokens" in spec.
3019         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
3020         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{nc} == 0x005D) { # ]
3021        }->{$self->{next_input_character}}) {          !!!cp (221.5);
3022      ## Don't consume          $self->{state} = CDATA_SECTION_MSE2_STATE;
3023      ## No error          !!!next-input-character;
3024      return undef;          redo A;
3025    } elsif ($self->{next_input_character} == 0x0023) { # #        } else {
3026      !!!next-input-character;          !!!cp (221.6);
3027      if ($self->{next_input_character} == 0x0078 or # x          $self->{ct}->{data} .= ']';
3028          $self->{next_input_character} == 0x0058) { # X          $self->{state} = CDATA_SECTION_STATE;
3029        my $code;          ## Reconsume.
3030        X: {          redo A;
3031          my $x_char = $self->{next_input_character};        }
3032          !!!next-input-character;      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3033          if (0x0030 <= $self->{next_input_character} and        if ($self->{nc} == 0x003E) { # >
3034              $self->{next_input_character} <= 0x0039) { # 0..9          $self->{state} = DATA_STATE;
3035            $code ||= 0;          !!!next-input-character;
3036            $code *= 0x10;          if (length $self->{ct}->{data}) { # character
3037            $code += $self->{next_input_character} - 0x0030;            !!!cp (221.7);
3038            redo X;            !!!emit ($self->{ct}); # character
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0066) { # a..f  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0046) { # A..F  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # 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;  
3039          } else {          } else {
3040            !!!parse-error (type => 'no refc');            !!!cp (221.8);
3041              ## No token to emit. $self->{ct} is discarded.
3042          }          }
3043            redo A;
3044          } elsif ($self->{nc} == 0x005D) { # ]
3045            !!!cp (221.9); # character
3046            $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3047            ## Stay in the state.
3048            !!!next-input-character;
3049            redo A;
3050          } else {
3051            !!!cp (221.11);
3052            $self->{ct}->{data} .= ']]'; # character
3053            $self->{state} = CDATA_SECTION_STATE;
3054            ## Reconsume.
3055            redo A;
3056          }
3057        } elsif ($self->{state} == ENTITY_STATE) {
3058          if ({
3059            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3060            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3061            $self->{entity_add} => 1,
3062          }->{$self->{nc}}) {
3063            !!!cp (1001);
3064            ## Don't consume
3065            ## No error
3066            ## Return nothing.
3067            #
3068          } elsif ($self->{nc} == 0x0023) { # #
3069            !!!cp (999);
3070            $self->{state} = ENTITY_HASH_STATE;
3071            $self->{s_kwd} = '#';
3072            !!!next-input-character;
3073            redo A;
3074          } elsif ((0x0041 <= $self->{nc} and
3075                    $self->{nc} <= 0x005A) or # A..Z
3076                   (0x0061 <= $self->{nc} and
3077                    $self->{nc} <= 0x007A)) { # a..z
3078            !!!cp (998);
3079            require Whatpm::_NamedEntityList;
3080            $self->{state} = ENTITY_NAME_STATE;
3081            $self->{s_kwd} = chr $self->{nc};
3082            $self->{entity__value} = $self->{s_kwd};
3083            $self->{entity__match} = 0;
3084            !!!next-input-character;
3085            redo A;
3086          } else {
3087            !!!cp (1027);
3088            !!!parse-error (type => 'bare ero');
3089            ## Return nothing.
3090            #
3091          }
3092    
3093          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3094            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## reference" algorithm.  In other word, there is an "&" character
3095            $code = 0xFFFD;        ## that does not introduce a character reference, which would be
3096          } elsif ($code > 0x10FFFF) {        ## appended to the parent element or the attribute value in later
3097            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        ## process of the tokenizer.
3098            $code = 0xFFFD;  
3099          } elsif ($code == 0x000D) {        if ($self->{prev_state} == DATA_STATE) {
3100            !!!parse-error (type => 'CR character reference');          !!!cp (997);
3101            $code = 0x000A;          $self->{state} = $self->{prev_state};
3102          } elsif (0x80 <= $code and $code <= 0x9F) {          ## Reconsume.
3103            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3104            $code = $c1_entity_char->{$code};                    line => $self->{line_prev},
3105          }                    column => $self->{column_prev},
3106                     });
3107          return {type => 'character', data => chr $code};          redo A;
3108        } # X        } else {
3109      } elsif (0x0030 <= $self->{next_input_character} and          !!!cp (996);
3110               $self->{next_input_character} <= 0x0039) { # 0..9          $self->{ca}->{value} .= '&';
3111        my $code = $self->{next_input_character} - 0x0030;          $self->{state} = $self->{prev_state};
3112        !!!next-input-character;          ## Reconsume.
3113                  redo A;
3114        while (0x0030 <= $self->{next_input_character} and        }
3115                  $self->{next_input_character} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3116          $code *= 10;        if ($self->{nc} == 0x0078 or # x
3117          $code += $self->{next_input_character} - 0x0030;            $self->{nc} == 0x0058) { # X
3118            !!!cp (995);
3119            $self->{state} = HEXREF_X_STATE;
3120            $self->{s_kwd} .= chr $self->{nc};
3121            !!!next-input-character;
3122            redo A;
3123          } elsif (0x0030 <= $self->{nc} and
3124                   $self->{nc} <= 0x0039) { # 0..9
3125            !!!cp (994);
3126            $self->{state} = NCR_NUM_STATE;
3127            $self->{s_kwd} = $self->{nc} - 0x0030;
3128            !!!next-input-character;
3129            redo A;
3130          } else {
3131            !!!parse-error (type => 'bare nero',
3132                            line => $self->{line_prev},
3133                            column => $self->{column_prev} - 1);
3134    
3135            ## NOTE: According to the spec algorithm, nothing is returned,
3136            ## and then "&#" is appended to the parent element or the attribute
3137            ## value in the later processing.
3138    
3139            if ($self->{prev_state} == DATA_STATE) {
3140              !!!cp (1019);
3141              $self->{state} = $self->{prev_state};
3142              ## Reconsume.
3143              !!!emit ({type => CHARACTER_TOKEN,
3144                        data => '&#',
3145                        line => $self->{line_prev},
3146                        column => $self->{column_prev} - 1,
3147                       });
3148              redo A;
3149            } else {
3150              !!!cp (993);
3151              $self->{ca}->{value} .= '&#';
3152              $self->{state} = $self->{prev_state};
3153              ## Reconsume.
3154              redo A;
3155            }
3156          }
3157        } elsif ($self->{state} == NCR_NUM_STATE) {
3158          if (0x0030 <= $self->{nc} and
3159              $self->{nc} <= 0x0039) { # 0..9
3160            !!!cp (1012);
3161            $self->{s_kwd} *= 10;
3162            $self->{s_kwd} += $self->{nc} - 0x0030;
3163                    
3164            ## Stay in the state.
3165          !!!next-input-character;          !!!next-input-character;
3166            redo A;
3167          } elsif ($self->{nc} == 0x003B) { # ;
3168            !!!cp (1013);
3169            !!!next-input-character;
3170            #
3171          } else {
3172            !!!cp (1014);
3173            !!!parse-error (type => 'no refc');
3174            ## Reconsume.
3175            #
3176        }        }
3177    
3178        if ($self->{next_input_character} == 0x003B) { # ;        my $code = $self->{s_kwd};
3179          my $l = $self->{line_prev};
3180          my $c = $self->{column_prev};
3181          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3182            !!!cp (1015);
3183            !!!parse-error (type => 'invalid character reference',
3184                            text => (sprintf 'U+%04X', $code),
3185                            line => $l, column => $c);
3186            $code = 0xFFFD;
3187          } elsif ($code > 0x10FFFF) {
3188            !!!cp (1016);
3189            !!!parse-error (type => 'invalid character reference',
3190                            text => (sprintf 'U-%08X', $code),
3191                            line => $l, column => $c);
3192            $code = 0xFFFD;
3193          } elsif ($code == 0x000D) {
3194            !!!cp (1017);
3195            !!!parse-error (type => 'CR character reference',
3196                            line => $l, column => $c);
3197            $code = 0x000A;
3198          } elsif (0x80 <= $code and $code <= 0x9F) {
3199            !!!cp (1018);
3200            !!!parse-error (type => 'C1 character reference',
3201                            text => (sprintf 'U+%04X', $code),
3202                            line => $l, column => $c);
3203            $code = $c1_entity_char->{$code};
3204          }
3205    
3206          if ($self->{prev_state} == DATA_STATE) {
3207            !!!cp (992);
3208            $self->{state} = $self->{prev_state};
3209            ## Reconsume.
3210            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3211                      line => $l, column => $c,
3212                     });
3213            redo A;
3214          } else {
3215            !!!cp (991);
3216            $self->{ca}->{value} .= chr $code;
3217            $self->{ca}->{has_reference} = 1;
3218            $self->{state} = $self->{prev_state};
3219            ## Reconsume.
3220            redo A;
3221          }
3222        } elsif ($self->{state} == HEXREF_X_STATE) {
3223          if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3224              (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3225              (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3226            # 0..9, A..F, a..f
3227            !!!cp (990);
3228            $self->{state} = HEXREF_HEX_STATE;
3229            $self->{s_kwd} = 0;
3230            ## Reconsume.
3231            redo A;
3232          } else {
3233            !!!parse-error (type => 'bare hcro',
3234                            line => $self->{line_prev},
3235                            column => $self->{column_prev} - 2);
3236    
3237            ## NOTE: According to the spec algorithm, nothing is returned,
3238            ## and then "&#" followed by "X" or "x" is appended to the parent
3239            ## element or the attribute value in the later processing.
3240    
3241            if ($self->{prev_state} == DATA_STATE) {
3242              !!!cp (1005);
3243              $self->{state} = $self->{prev_state};
3244              ## Reconsume.
3245              !!!emit ({type => CHARACTER_TOKEN,
3246                        data => '&' . $self->{s_kwd},
3247                        line => $self->{line_prev},
3248                        column => $self->{column_prev} - length $self->{s_kwd},
3249                       });
3250              redo A;
3251            } else {
3252              !!!cp (989);
3253              $self->{ca}->{value} .= '&' . $self->{s_kwd};
3254              $self->{state} = $self->{prev_state};
3255              ## Reconsume.
3256              redo A;
3257            }
3258          }
3259        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3260          if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3261            # 0..9
3262            !!!cp (1002);
3263            $self->{s_kwd} *= 0x10;
3264            $self->{s_kwd} += $self->{nc} - 0x0030;
3265            ## Stay in the state.
3266            !!!next-input-character;
3267            redo A;
3268          } elsif (0x0061 <= $self->{nc} and
3269                   $self->{nc} <= 0x0066) { # a..f
3270            !!!cp (1003);
3271            $self->{s_kwd} *= 0x10;
3272            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3273            ## Stay in the state.
3274            !!!next-input-character;
3275            redo A;
3276          } elsif (0x0041 <= $self->{nc} and
3277                   $self->{nc} <= 0x0046) { # A..F
3278            !!!cp (1004);
3279            $self->{s_kwd} *= 0x10;
3280            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3281            ## Stay in the state.
3282            !!!next-input-character;
3283            redo A;
3284          } elsif ($self->{nc} == 0x003B) { # ;
3285            !!!cp (1006);
3286          !!!next-input-character;          !!!next-input-character;
3287            #
3288        } else {        } else {
3289          !!!parse-error (type => 'no refc');          !!!cp (1007);
3290            !!!parse-error (type => 'no refc',
3291                            line => $self->{line},
3292                            column => $self->{column});
3293            ## Reconsume.
3294            #
3295        }        }
3296    
3297          my $code = $self->{s_kwd};
3298          my $l = $self->{line_prev};
3299          my $c = $self->{column_prev};
3300        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3301          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1008);
3302            !!!parse-error (type => 'invalid character reference',
3303                            text => (sprintf 'U+%04X', $code),
3304                            line => $l, column => $c);
3305          $code = 0xFFFD;          $code = 0xFFFD;
3306        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3307          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1009);
3308            !!!parse-error (type => 'invalid character reference',
3309                            text => (sprintf 'U-%08X', $code),
3310                            line => $l, column => $c);
3311          $code = 0xFFFD;          $code = 0xFFFD;
3312        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3313          !!!parse-error (type => 'CR character reference');          !!!cp (1010);
3314            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3315          $code = 0x000A;          $code = 0x000A;
3316        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3317          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1011);
3318            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3319          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3320        }        }
3321          
3322        return {type => 'character', data => chr $code};        if ($self->{prev_state} == DATA_STATE) {
3323      } else {          !!!cp (988);
3324        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3325        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3326        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3327        return undef;                    line => $l, column => $c,
3328      }                   });
3329    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
3330              $self->{next_input_character} <= 0x005A) or        } else {
3331             (0x0061 <= $self->{next_input_character} and          !!!cp (987);
3332              $self->{next_input_character} <= 0x007A)) {          $self->{ca}->{value} .= chr $code;
3333      my $entity_name = chr $self->{next_input_character};          $self->{ca}->{has_reference} = 1;
3334      !!!next-input-character;          $self->{state} = $self->{prev_state};
3335            ## Reconsume.
3336      my $value = $entity_name;          redo A;
3337      my $match;        }
3338      require Whatpm::_NamedEntityList;      } elsif ($self->{state} == ENTITY_NAME_STATE) {
3339      our $EntityChar;        if (length $self->{s_kwd} < 30 and
3340              ## NOTE: Some number greater than the maximum length of entity name
3341      while (length $entity_name < 10 and            ((0x0041 <= $self->{nc} and # a
3342             ## NOTE: Some number greater than the maximum length of entity name              $self->{nc} <= 0x005A) or # x
3343             ((0x0041 <= $self->{next_input_character} and # a             (0x0061 <= $self->{nc} and # a
3344               $self->{next_input_character} <= 0x005A) or # x              $self->{nc} <= 0x007A) or # z
3345              (0x0061 <= $self->{next_input_character} and # a             (0x0030 <= $self->{nc} and # 0
3346               $self->{next_input_character} <= 0x007A) or # z              $self->{nc} <= 0x0039) or # 9
3347              (0x0030 <= $self->{next_input_character} and # 0             $self->{nc} == 0x003B)) { # ;
3348               $self->{next_input_character} <= 0x0039) or # 9          our $EntityChar;
3349              $self->{next_input_character} == 0x003B)) { # ;          $self->{s_kwd} .= chr $self->{nc};
3350        $entity_name .= chr $self->{next_input_character};          if (defined $EntityChar->{$self->{s_kwd}}) {
3351        if (defined $EntityChar->{$entity_name}) {            if ($self->{nc} == 0x003B) { # ;
3352          if ($self->{next_input_character} == 0x003B) { # ;              !!!cp (1020);
3353            $value = $EntityChar->{$entity_name};              $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3354            $match = 1;              $self->{entity__match} = 1;
3355                !!!next-input-character;
3356                #
3357              } else {
3358                !!!cp (1021);
3359                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3360                $self->{entity__match} = -1;
3361                ## Stay in the state.
3362                !!!next-input-character;
3363                redo A;
3364              }
3365            } else {
3366              !!!cp (1022);
3367              $self->{entity__value} .= chr $self->{nc};
3368              $self->{entity__match} *= 2;
3369              ## Stay in the state.
3370            !!!next-input-character;            !!!next-input-character;
3371            last;            redo A;
3372          } elsif (not $in_attr) {          }
3373            $value = $EntityChar->{$entity_name};        }
3374            $match = -1;  
3375          my $data;
3376          my $has_ref;
3377          if ($self->{entity__match} > 0) {
3378            !!!cp (1023);
3379            $data = $self->{entity__value};
3380            $has_ref = 1;
3381            #
3382          } elsif ($self->{entity__match} < 0) {
3383            !!!parse-error (type => 'no refc');
3384            if ($self->{prev_state} != DATA_STATE and # in attribute
3385                $self->{entity__match} < -1) {
3386              !!!cp (1024);
3387              $data = '&' . $self->{s_kwd};
3388              #
3389          } else {          } else {
3390            $value .= chr $self->{next_input_character};            !!!cp (1025);
3391              $data = $self->{entity__value};
3392              $has_ref = 1;
3393              #
3394          }          }
3395        } else {        } else {
3396          $value .= chr $self->{next_input_character};          !!!cp (1026);
3397            !!!parse-error (type => 'bare ero',
3398                            line => $self->{line_prev},
3399                            column => $self->{column_prev} - length $self->{s_kwd});
3400            $data = '&' . $self->{s_kwd};
3401            #
3402          }
3403      
3404          ## NOTE: In these cases, when a character reference is found,
3405          ## it is consumed and a character token is returned, or, otherwise,
3406          ## nothing is consumed and returned, according to the spec algorithm.
3407          ## In this implementation, anything that has been examined by the
3408          ## tokenizer is appended to the parent element or the attribute value
3409          ## as string, either literal string when no character reference or
3410          ## entity-replaced string otherwise, in this stage, since any characters
3411          ## that would not be consumed are appended in the data state or in an
3412          ## appropriate attribute value state anyway.
3413    
3414          if ($self->{prev_state} == DATA_STATE) {
3415            !!!cp (986);
3416            $self->{state} = $self->{prev_state};
3417            ## Reconsume.
3418            !!!emit ({type => CHARACTER_TOKEN,
3419                      data => $data,
3420                      line => $self->{line_prev},
3421                      column => $self->{column_prev} + 1 - length $self->{s_kwd},
3422                     });
3423            redo A;
3424          } else {
3425            !!!cp (985);
3426            $self->{ca}->{value} .= $data;
3427            $self->{ca}->{has_reference} = 1 if $has_ref;
3428            $self->{state} = $self->{prev_state};
3429            ## Reconsume.
3430            redo A;
3431        }        }
       !!!next-input-character;  
     }  
       
     if ($match > 0) {  
       return {type => 'character', data => $value};  
     } elsif ($match < 0) {  
       !!!parse-error (type => 'no refc');  
       return {type => 'character', data => $value};  
3432      } else {      } else {
3433        !!!parse-error (type => 'bare ero');        die "$0: $self->{state}: Unknown state";
       ## NOTE: No characters are consumed in the spec.  
       return {type => 'character', data => '&'.$value};  
3434      }      }
3435    } else {    } # A  
3436      ## no characters are consumed  
3437      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3438      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3439    
3440  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3441    my $self = shift;    my $self = shift;
# Line 1773  sub _initialize_tree_constructor ($) { Line 3444  sub _initialize_tree_constructor ($) {
3444    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3445    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3446    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3447      $self->{document}->set_user_data (manakai_source_line => 1);
3448      $self->{document}->set_user_data (manakai_source_column => 1);
3449  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3450    
3451  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1799  sub _construct_tree ($) { Line 3472  sub _construct_tree ($) {
3472        
3473    !!!next-token;    !!!next-token;
3474    
   $self->{insertion_mode} = 'before head';  
3475    undef $self->{form_element};    undef $self->{form_element};
3476    undef $self->{head_element};    undef $self->{head_element};
3477    $self->{open_elements} = [];    $self->{open_elements} = [];
3478    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3479    
3480      ## NOTE: The "initial" insertion mode.
3481    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3482    
3483      ## NOTE: The "before html" insertion mode.
3484    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3485      $self->{insertion_mode} = BEFORE_HEAD_IM;
3486    
3487      ## NOTE: The "before head" insertion mode and so on.
3488    $self->_tree_construction_main;    $self->_tree_construction_main;
3489  } # _construct_tree  } # _construct_tree
3490    
3491  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3492    my $self = shift;    my $self = shift;
3493    
3494      ## NOTE: "initial" insertion mode
3495    
3496    INITIAL: {    INITIAL: {
3497      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3498        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3499        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
3500        ## language.        ## language.
3501        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3502        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3503        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3504        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3505            defined $token->{public_identifier} or            defined $token->{sysid}) {
3506            defined $token->{system_identifier}) {          !!!cp ('t1');
3507          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3508        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3509          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3510          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3511          } elsif (defined $token->{pubid}) {
3512            if ($token->{pubid} eq 'XSLT-compat') {
3513              !!!cp ('t1.2');
3514              !!!parse-error (type => 'XSLT-compat', token => $token,
3515                              level => $self->{level}->{should});
3516            } else {
3517              !!!parse-error (type => 'not HTML5', token => $token);
3518            }
3519          } else {
3520            !!!cp ('t3');
3521            #
3522        }        }
3523                
3524        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3525          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3526        $doctype->public_id ($token->{public_identifier})        ## NOTE: Default value for both |public_id| and |system_id| attributes
3527            if defined $token->{public_identifier};        ## are empty strings, so that we don't set any value in missing cases.
3528        $doctype->system_id ($token->{system_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3529            if defined $token->{system_identifier};        $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
3530        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
3531        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3532        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3533                
3534        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3535            !!!cp ('t4');
3536          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3537        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
3538          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
3539          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3540          if ({          my $prefix = [
3541            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3542            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3543            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3544            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3545            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3546            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3547            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3548            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3549            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3550            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3551            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3552            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3553            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3554            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3555            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3556            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3557            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3558            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3559            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3560            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3561            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3562            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3563            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3564            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3565            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3566            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3567            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3568            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3569            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3570            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3571            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3572            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3573            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3574            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3575            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3576            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3577            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3578            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3579            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3580            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3581            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3582            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3583            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3584            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3585            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3586            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3587            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3588            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3589            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3590            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3591            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3592            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3593            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3594            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3595            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3596            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3597            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3598            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3599            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3600            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3601            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3602            "-//W3C//DTD W3 HTML//EN" => 1,            }
3603            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3604            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3605            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3606            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3607            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3608            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3609            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3610          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3611                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3612            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
3613                !!!cp ('t6');
3614              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3615            } else {            } else {
3616                !!!cp ('t7');
3617              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3618            }            }
3619          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3620                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3621              !!!cp ('t8');
3622            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3623            } else {
3624              !!!cp ('t9');
3625          }          }
3626          } else {
3627            !!!cp ('t10');
3628        }        }
3629        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
3630          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
3631          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3632          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") {
3633              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3634              ## marked as quirks.
3635            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3636              !!!cp ('t11');
3637            } else {
3638              !!!cp ('t12');
3639          }          }
3640          } else {
3641            !!!cp ('t13');
3642        }        }
3643                
3644        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3645        !!!next-token;        !!!next-token;
3646        return;        return;
3647      } elsif ({      } elsif ({
3648                'start tag' => 1,                START_TAG_TOKEN, 1,
3649                'end tag' => 1,                END_TAG_TOKEN, 1,
3650                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
3651               }->{$token->{type}}) {               }->{$token->{type}}) {
3652        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3653          !!!parse-error (type => 'no DOCTYPE', token => $token);
3654        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3655        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3656        ## reprocess        ## reprocess
3657          !!!ack-later;
3658        return;        return;
3659      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3660        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3661          ## Ignore the token          ## Ignore the token
3662    
3663          unless (length $token->{data}) {          unless (length $token->{data}) {
3664            ## Stay in the phase            !!!cp ('t15');
3665              ## Stay in the insertion mode.
3666            !!!next-token;            !!!next-token;
3667            redo INITIAL;            redo INITIAL;
3668            } else {
3669              !!!cp ('t16');
3670          }          }
3671          } else {
3672            !!!cp ('t17');
3673        }        }
3674    
3675        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3676        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3677        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3678        ## reprocess        ## reprocess
3679        return;        return;
3680      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3681          !!!cp ('t18');
3682        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3683        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3684                
3685        ## Stay in the phase.        ## Stay in the insertion mode.
3686        !!!next-token;        !!!next-token;
3687        redo INITIAL;        redo INITIAL;
3688      } else {      } else {
3689        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
3690      }      }
3691    } # INITIAL    } # INITIAL
3692    
3693      die "$0: _tree_construction_initial: This should be never reached";
3694  } # _tree_construction_initial  } # _tree_construction_initial
3695    
3696  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3697    my $self = shift;    my $self = shift;
3698    
3699      ## NOTE: "before html" insertion mode.
3700        
3701    B: {    B: {
3702        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3703          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3704            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3705          ## Ignore the token          ## Ignore the token
3706          ## Stay in the phase          ## Stay in the insertion mode.
3707          !!!next-token;          !!!next-token;
3708          redo B;          redo B;
3709        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3710            !!!cp ('t20');
3711          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3712          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3713          ## Stay in the phase          ## Stay in the insertion mode.
3714          !!!next-token;          !!!next-token;
3715          redo B;          redo B;
3716        } elsif ($token->{type} eq 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3717          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3718            ## Ignore the token.            ## Ignore the token.
3719    
3720            unless (length $token->{data}) {            unless (length $token->{data}) {
3721              ## Stay in the phase              !!!cp ('t21');
3722                ## Stay in the insertion mode.
3723              !!!next-token;              !!!next-token;
3724              redo B;              redo B;
3725              } else {
3726                !!!cp ('t22');
3727            }            }
3728            } else {
3729              !!!cp ('t23');
3730          }          }
3731    
3732            $self->{application_cache_selection}->(undef);
3733    
3734          #          #
3735          } elsif ($token->{type} == START_TAG_TOKEN) {
3736            if ($token->{tag_name} eq 'html') {
3737              my $root_element;
3738              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3739              $self->{document}->append_child ($root_element);
3740              push @{$self->{open_elements}},
3741                  [$root_element, $el_category->{html}];
3742    
3743              if ($token->{attributes}->{manifest}) {
3744                !!!cp ('t24');
3745                $self->{application_cache_selection}
3746                    ->($token->{attributes}->{manifest}->{value});
3747                ## ISSUE: Spec is unclear on relative references.
3748                ## According to Hixie (#whatwg 2008-03-19), it should be
3749                ## resolved against the base URI of the document in HTML
3750                ## or xml:base of the element in XHTML.
3751              } else {
3752                !!!cp ('t25');
3753                $self->{application_cache_selection}->(undef);
3754              }
3755    
3756              !!!nack ('t25c');
3757    
3758              !!!next-token;
3759              return; ## Go to the "before head" insertion mode.
3760            } else {
3761              !!!cp ('t25.1');
3762              #
3763            }
3764        } elsif ({        } elsif ({
3765                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3766                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3767                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3768          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3769          #          #
3770        } else {        } else {
3771          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3772        }        }
3773        my $root_element; !!!create-element ($root_element, 'html');  
3774        $self->{document}->append_child ($root_element);      my $root_element;
3775        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3776        #$phase = 'main';      $self->{document}->append_child ($root_element);
3777        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3778        #redo B;  
3779        return;      $self->{application_cache_selection}->(undef);
3780    
3781        ## NOTE: Reprocess the token.
3782        !!!ack-later;
3783        return; ## Go to the "before head" insertion mode.
3784    
3785        ## ISSUE: There is an issue in the spec
3786    } # B    } # B
3787    
3788      die "$0: _tree_construction_root_element: This should never be reached";
3789  } # _tree_construction_root_element  } # _tree_construction_root_element
3790    
3791  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2037  sub _reset_insertion_mode ($) { Line 3800  sub _reset_insertion_mode ($) {
3800            
3801      ## Step 3      ## Step 3
3802      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
3803        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3804          $last = 1;          $last = 1;
3805          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3806            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3807                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3808              #          } else {
3809            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3810          }          }
3811        }        }
3812              
3813        ## Step 4..13        ## Step 4..14
3814        my $new_mode = {        my $new_mode;
3815                        select => 'in select',        if ($node->[1] & FOREIGN_EL) {
3816                        td => 'in cell',          !!!cp ('t28.1');
3817                        th => 'in cell',          ## NOTE: Strictly spaking, the line below only applies to MathML and
3818                        tr => 'in row',          ## SVG elements.  Currently the HTML syntax supports only MathML and
3819                        tbody => 'in table body',          ## SVG elements as foreigners.
3820                        thead => 'in table head',          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3821                        tfoot => 'in table foot',        } elsif ($node->[1] & TABLE_CELL_EL) {
3822                        caption => 'in caption',          if ($last) {
3823                        colgroup => 'in column group',            !!!cp ('t28.2');
3824                        table => 'in table',            #
3825                        head => 'in body', # not in head!          } else {
3826                        body => 'in body',            !!!cp ('t28.3');
3827                        frameset => 'in frameset',            $new_mode = IN_CELL_IM;
3828                       }->{$node->[1]};          }
3829          } else {
3830            !!!cp ('t28.4');
3831            $new_mode = {
3832                          select => IN_SELECT_IM,
3833                          ## NOTE: |option| and |optgroup| do not set
3834                          ## insertion mode to "in select" by themselves.
3835                          tr => IN_ROW_IM,
3836                          tbody => IN_TABLE_BODY_IM,
3837                          thead => IN_TABLE_BODY_IM,
3838                          tfoot => IN_TABLE_BODY_IM,
3839                          caption => IN_CAPTION_IM,
3840                          colgroup => IN_COLUMN_GROUP_IM,
3841                          table => IN_TABLE_IM,
3842                          head => IN_BODY_IM, # not in head!
3843                          body => IN_BODY_IM,
3844                          frameset => IN_FRAMESET_IM,
3845                         }->{$node->[0]->manakai_local_name};
3846          }
3847        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3848                
3849        ## Step 14        ## Step 15
3850        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3851          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3852            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3853              $self->{insertion_mode} = BEFORE_HEAD_IM;
3854          } else {          } else {
3855            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3856              !!!cp ('t30');
3857              $self->{insertion_mode} = AFTER_HEAD_IM;
3858          }          }
3859          return;          return;
3860          } else {
3861            !!!cp ('t31');
3862        }        }
3863                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3864        ## Step 16        ## Step 16
3865          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3866          
3867          ## Step 17
3868        $i--;        $i--;
3869        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3870                
3871        ## Step 17        ## Step 18
3872        redo S3;        redo S3;
3873      } # S3      } # S3
3874    
3875      die "$0: _reset_insertion_mode: This line should never be reached";
3876  } # _reset_insertion_mode  } # _reset_insertion_mode
3877    
3878  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3879    my $self = shift;    my $self = shift;
3880    
   my $phase = 'main';  
   
3881    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3882    
3883    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2115  sub _tree_construction_main ($) { Line 3894  sub _tree_construction_main ($) {
3894      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3895      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3896        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3897            !!!cp ('t32');
3898          return;          return;
3899        }        }
3900      }      }
# Line 2129  sub _tree_construction_main ($) { Line 3909  sub _tree_construction_main ($) {
3909    
3910        ## Step 6        ## Step 6
3911        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3912            !!!cp ('t33_1');
3913          #          #
3914        } else {        } else {
3915          my $in_open_elements;          my $in_open_elements;
3916          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3917            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3918                !!!cp ('t33');
3919              $in_open_elements = 1;              $in_open_elements = 1;
3920              last OE;              last OE;
3921            }            }
3922          }          }
3923          if ($in_open_elements) {          if ($in_open_elements) {
3924              !!!cp ('t34');
3925            #            #
3926          } else {          } else {
3927              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3928              !!!cp ('t35');
3929            redo S4;            redo S4;
3930          }          }
3931        }        }
# Line 2163  sub _tree_construction_main ($) { Line 3948  sub _tree_construction_main ($) {
3948    
3949        ## Step 11        ## Step 11
3950        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3951            !!!cp ('t36');
3952          ## Step 7'          ## Step 7'
3953          $i++;          $i++;
3954          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3955                    
3956          redo S7;          redo S7;
3957        }        }
3958    
3959          !!!cp ('t37');
3960      } # S7      } # S7
3961    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3962    
3963    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3964      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3965        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3966            !!!cp ('t38');
3967          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3968          return;          return;
3969        }        }
3970      }      }
3971    
3972        !!!cp ('t39');
3973    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3974    
3975    my $parse_rcdata = sub ($$) {    my $insert;
3976      my ($content_model_flag, $insert) = @_;  
3977      my $parse_rcdata = sub ($) {
3978        my ($content_model_flag) = @_;
3979    
3980      ## Step 1      ## Step 1
3981      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3982      my $el;      my $el;
3983      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3984    
3985      ## Step 2      ## Step 2
3986      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3987    
3988      ## Step 3      ## Step 3
3989      $self->{content_model_flag} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
3990      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3991    
3992      ## Step 4      ## Step 4
3993      my $text = '';      my $text = '';
3994        !!!nack ('t40.1');
3995      !!!next-token;      !!!next-token;
3996      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3997          !!!cp ('t40');
3998        $text .= $token->{data};        $text .= $token->{data};
3999        !!!next-token;        !!!next-token;
4000      }      }
4001    
4002      ## Step 5      ## Step 5
4003      if (length $text) {      if (length $text) {
4004          !!!cp ('t41');
4005        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
4006        $el->append_child ($text);        $el->append_child ($text);
4007      }      }
4008    
4009      ## Step 6      ## Step 6
4010      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
4011    
4012      ## Step 7      ## Step 7
4013      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
4014            $token->{tag_name} eq $start_tag_name) {
4015          !!!cp ('t42');
4016        ## Ignore the token        ## Ignore the token
4017      } else {      } else {
4018        !!!parse-error (type => 'in '.$content_model_flag.':#'.$token->{type});        ## NOTE: An end-of-file token.
4019          if ($content_model_flag == CDATA_CONTENT_MODEL) {
4020            !!!cp ('t43');
4021            !!!parse-error (type => 'in CDATA:#eof', token => $token);
4022          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4023            !!!cp ('t44');
4024            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4025          } else {
4026            die "$0: $content_model_flag in parse_rcdata";
4027          }
4028      }      }
4029      !!!next-token;      !!!next-token;
4030    }; # $parse_rcdata    }; # $parse_rcdata
4031    
4032    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
4033      my $script_el;      my $script_el;
4034      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4035      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4036    
4037      $self->{content_model_flag} = 'CDATA';      $self->{content_model} = CDATA_CONTENT_MODEL;
4038      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4039            
4040      my $text = '';      my $text = '';
4041        !!!nack ('t45.1');
4042      !!!next-token;      !!!next-token;
4043      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
4044          !!!cp ('t45');
4045        $text .= $token->{data};        $text .= $token->{data};
4046        !!!next-token;        !!!next-token;
4047      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
4048      if (length $text) {      if (length $text) {
4049          !!!cp ('t46');
4050        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
4051      }      }
4052                                
4053      $self->{content_model_flag} = 'PCDATA';      $self->{content_model} = PCDATA_CONTENT_MODEL;
4054    
4055      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
4056          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4057          !!!cp ('t47');
4058        ## Ignore the token        ## Ignore the token
4059      } else {      } else {
4060        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4061          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4062        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4063        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4064      }      }
4065            
4066      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4067          !!!cp ('t49');
4068        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4069      } else {      } else {
4070          !!!cp ('t50');
4071        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4072        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4073    
# Line 2268  sub _tree_construction_main ($) { Line 4081  sub _tree_construction_main ($) {
4081      !!!next-token;      !!!next-token;
4082    }; # $script_start_tag    }; # $script_start_tag
4083    
4084      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4085      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4086      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4087    
4088    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4089      my $tag_name = shift;      my $end_tag_token = shift;
4090        my $tag_name = $end_tag_token->{tag_name};
4091    
4092        ## NOTE: The adoption agency algorithm (AAA).
4093    
4094      FET: {      FET: {
4095        ## Step 1        ## Step 1
4096        my $formatting_element;        my $formatting_element;
4097        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4098        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4099          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4100              !!!cp ('t52');
4101              last AFE;
4102            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4103                         eq $tag_name) {
4104              !!!cp ('t51');
4105            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4106            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4107            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4108          }          }
4109        } # AFE        } # AFE
4110        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4111          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4112            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4113          ## Ignore the token          ## Ignore the token
4114          !!!next-token;          !!!next-token;
4115          return;          return;
# Line 2297  sub _tree_construction_main ($) { Line 4121  sub _tree_construction_main ($) {
4121          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4122          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4123            if ($in_scope) {            if ($in_scope) {
4124                !!!cp ('t54');
4125              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4126              last INSCOPE;              last INSCOPE;
4127            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4128              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4129                !!!parse-error (type => 'unmatched end tag',
4130                                text => $token->{tag_name},
4131                                token => $end_tag_token);
4132              ## Ignore the token              ## Ignore the token
4133              !!!next-token;              !!!next-token;
4134              return;              return;
4135            }            }
4136          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4137                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4138            $in_scope = 0;            $in_scope = 0;
4139          }          }
4140        } # INSCOPE        } # INSCOPE
4141        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4142          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4143            !!!parse-error (type => 'unmatched end tag',
4144                            text => $token->{tag_name},
4145                            token => $end_tag_token);
4146          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4147          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4148          return;          return;
4149        }        }
4150        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4151          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4152            !!!parse-error (type => 'not closed',
4153                            text => $self->{open_elements}->[-1]->[0]
4154                                ->manakai_local_name,
4155                            token => $end_tag_token);
4156        }        }
4157                
4158        ## Step 2        ## Step 2
# Line 2327  sub _tree_construction_main ($) { Line 4160  sub _tree_construction_main ($) {
4160        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4161        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4162          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4163          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4164              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4165              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4166               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4167              !!!cp ('t59');
4168            $furthest_block = $node;            $furthest_block = $node;
4169            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4170          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4171              !!!cp ('t60');
4172            last OE;            last OE;
4173          }          }
4174        } # OE        } # OE
4175                
4176        ## Step 3        ## Step 3
4177        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4178            !!!cp ('t61');
4179          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4180          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4181          !!!next-token;          !!!next-token;
# Line 2352  sub _tree_construction_main ($) { Line 4188  sub _tree_construction_main ($) {
4188        ## Step 5        ## Step 5
4189        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4190        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4191            !!!cp ('t62');
4192          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4193        }        }
4194                
# Line 2374  sub _tree_construction_main ($) { Line 4211  sub _tree_construction_main ($) {
4211          S7S2: {          S7S2: {
4212            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4213              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4214                  !!!cp ('t63');
4215                $node_i_in_active = $_;                $node_i_in_active = $_;
4216                last S7S2;                last S7S2;
4217              }              }
# Line 2387  sub _tree_construction_main ($) { Line 4225  sub _tree_construction_main ($) {
4225                    
4226          ## Step 4          ## Step 4
4227          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4228              !!!cp ('t64');
4229            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4230          }          }
4231                    
4232          ## Step 5          ## Step 5
4233          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4234              !!!cp ('t65');
4235            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4236            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4237            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2409  sub _tree_construction_main ($) { Line 4249  sub _tree_construction_main ($) {
4249        } # S7          } # S7  
4250                
4251        ## Step 8        ## Step 8
4252        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4253            my $foster_parent_element;
4254            my $next_sibling;
4255            OE: for (reverse 0..$#{$self->{open_elements}}) {
4256              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4257                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4258                                 if (defined $parent and $parent->node_type == 1) {
4259                                   !!!cp ('t65.1');
4260                                   $foster_parent_element = $parent;
4261                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4262                                 } else {
4263                                   !!!cp ('t65.2');
4264                                   $foster_parent_element
4265                                     = $self->{open_elements}->[$_ - 1]->[0];
4266                                 }
4267                                 last OE;
4268                               }
4269                             } # OE
4270                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4271                               unless defined $foster_parent_element;
4272            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4273            $open_tables->[-1]->[1] = 1; # tainted
4274          } else {
4275            !!!cp ('t65.3');
4276            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4277          }
4278                
4279        ## Step 9        ## Step 9
4280        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2426  sub _tree_construction_main ($) { Line 4291  sub _tree_construction_main ($) {
4291        my $i;        my $i;
4292        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4293          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4294              !!!cp ('t66');
4295            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4296            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4297          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4298              !!!cp ('t67');
4299            $i = $_;            $i = $_;
4300          }          }
4301        } # AFE        } # AFE
# Line 2438  sub _tree_construction_main ($) { Line 4305  sub _tree_construction_main ($) {
4305        undef $i;        undef $i;
4306        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4307          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4308              !!!cp ('t68');
4309            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4310            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4311          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4312              !!!cp ('t69');
4313            $i = $_;            $i = $_;
4314          }          }
4315        } # OE        } # OE
# Line 2451  sub _tree_construction_main ($) { Line 4320  sub _tree_construction_main ($) {
4320      } # FET      } # FET
4321    }; # $formatting_end_tag    }; # $formatting_end_tag
4322    
4323    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4324      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4325    }; # $insert_to_current    }; # $insert_to_current
4326    
4327    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4328                         my $child = shift;      my $child = shift;
4329                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4330                              table => 1, tbody => 1, tfoot => 1,        # MUST
4331                              thead => 1, tr => 1,        my $foster_parent_element;
4332                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4333                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4334                           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') {  
4335                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4336                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4337                                   !!!cp ('t70');
4338                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4339                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4340                               } else {                               } else {
4341                                   !!!cp ('t71');
4342                                 $foster_parent_element                                 $foster_parent_element
4343                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4344                               }                               }
# Line 2481  sub _tree_construction_main ($) { Line 4349  sub _tree_construction_main ($) {
4349                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4350                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4351                             ($child, $next_sibling);                             ($child, $next_sibling);
4352                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4353                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4354                         }        !!!cp ('t72');
4355          $self->{open_elements}->[-1]->[0]->append_child ($child);
4356        }
4357    }; # $insert_to_foster    }; # $insert_to_foster
4358    
4359    my $in_body = sub {    B: while (1) {
4360      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4361      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4362        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4363          ## NOTE: This is an "as if in head" code clone        ## Ignore the token
4364          $script_start_tag->($insert);        ## Stay in the phase
4365          return;        !!!next-token;
4366        } elsif ($token->{tag_name} eq 'style') {        next B;
4367          ## NOTE: This is an "as if in head" code clone      } elsif ($token->{type} == START_TAG_TOKEN and
4368          $parse_rcdata->('CDATA', $insert);               $token->{tag_name} eq 'html') {
4369          return;        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4370        } elsif ({          !!!cp ('t79');
4371                  base => 1, link => 1, meta => 1,          !!!parse-error (type => 'after html', text => 'html', token => $token);
4372                 }->{$token->{tag_name}}) {          $self->{insertion_mode} = AFTER_BODY_IM;
4373          ## NOTE: This is an "as if in head" code clone, only "-t" differs        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4374          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4375          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          !!!parse-error (type => 'after html', text => 'html', token => $token);
4376          !!!next-token;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4377          ## TODO: Extracting |charset| from |meta|.        } else {
4378          return;          !!!cp ('t81');
4379        } elsif ($token->{tag_name} eq 'title') {        }
4380          !!!parse-error (type => 'in body:title');  
4381          ## NOTE: This is an "as if in head" code clone        !!!cp ('t82');
4382          $parse_rcdata->('RCDATA', sub {        !!!parse-error (type => 'not first start tag', token => $token);
4383            if (defined $self->{head_element}) {        my $top_el = $self->{open_elements}->[0]->[0];
4384              $self->{head_element}->append_child ($_[0]);        for my $attr_name (keys %{$token->{attributes}}) {
4385            } else {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4386              $insert->($_[0]);            !!!cp ('t84');
4387            }            $top_el->set_attribute_ns
4388          });              (undef, [undef, $attr_name],
4389          return;               $token->{attributes}->{$attr_name}->{value});
       } 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});  
             }  
           }  
4390          }          }
4391          }
4392          !!!nack ('t84.1');
4393          !!!next-token;
4394          next B;
4395        } elsif ($token->{type} == COMMENT_TOKEN) {
4396          my $comment = $self->{document}->create_comment ($token->{data});
4397          if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4398            !!!cp ('t85');
4399            $self->{document}->append_child ($comment);
4400          } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4401            !!!cp ('t86');
4402            $self->{open_elements}->[0]->[0]->append_child ($comment);
4403          } else {
4404            !!!cp ('t87');
4405            $self->{open_elements}->[-1]->[0]->append_child ($comment);
4406          }
4407          !!!next-token;
4408          next B;
4409        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4410          if ($token->{type} == CHARACTER_TOKEN) {
4411            !!!cp ('t87.1');
4412            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4413          !!!next-token;          !!!next-token;
4414          return;          next B;
4415        } elsif ({        } elsif ($token->{type} == START_TAG_TOKEN) {
4416                  address => 1, blockquote => 1, center => 1, dir => 1,          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4417                  div => 1, dl => 1, fieldset => 1, listing => 1,               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4418                  menu => 1, ol => 1, p => 1, ul => 1,              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4419                  pre => 1,              ($token->{tag_name} eq 'svg' and
4420                 }->{$token->{tag_name}}) {               $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4421          ## has a p element in scope            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4422          INSCOPE: for (reverse @{$self->{open_elements}}) {            !!!cp ('t87.2');
4423            if ($_->[1] eq 'p') {            #
4424              !!!back-token;          } elsif ({
4425              $token = {type => 'end tag', tag_name => 'p'};                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4426              return;                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4427            } elsif ({                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4428                      table => 1, caption => 1, td => 1, th => 1,                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4429                      button => 1, marquee => 1, object => 1, html => 1,                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4430                     }->{$_->[1]}) {                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4431              last INSCOPE;                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4432            }                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4433          } # INSCOPE                   }->{$token->{tag_name}}) {
4434                        !!!cp ('t87.2');
4435          !!!insert-element-t ($token->{tag_name}, $token->{attributes});            !!!parse-error (type => 'not closed',
4436          if ($token->{tag_name} eq 'pre') {                            text => $self->{open_elements}->[-1]->[0]
4437            !!!next-token;                                ->manakai_local_name,
4438            if ($token->{type} eq 'character') {                            token => $token);
4439              $token->{data} =~ s/^\x0A//;  
4440              unless (length $token->{data}) {            pop @{$self->{open_elements}}
4441                !!!next-token;                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4442              }  
4443            }            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4444          } else {            ## Reprocess.
4445            !!!next-token;            next B;
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
4446          } else {          } else {
4447            ## has a p element in scope            my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4448            INSCOPE: for (reverse @{$self->{open_elements}}) {            my $tag_name = $token->{tag_name};
4449              if ($_->[1] eq 'p') {            if ($nsuri eq $SVG_NS) {
4450                !!!back-token;              $tag_name = {
4451                $token = {type => 'end tag', tag_name => 'p'};                 altglyph => 'altGlyph',
4452                return;                 altglyphdef => 'altGlyphDef',
4453              } elsif ({                 altglyphitem => 'altGlyphItem',
4454                        table => 1, caption => 1, td => 1, th => 1,                 animatecolor => 'animateColor',
4455                        button => 1, marquee => 1, object => 1, html => 1,                 animatemotion => 'animateMotion',
4456                       }->{$_->[1]}) {                 animatetransform => 'animateTransform',
4457                last INSCOPE;                 clippath => 'clipPath',
4458              }                 feblend => 'feBlend',
4459            } # INSCOPE                 fecolormatrix => 'feColorMatrix',
4460                               fecomponenttransfer => 'feComponentTransfer',
4461            !!!insert-element-t ($token->{tag_name}, $token->{attributes});                 fecomposite => 'feComposite',
4462            $self->{form_element} = $self->{open_elements}->[-1]->[0];                 feconvolvematrix => 'feConvolveMatrix',
4463            !!!next-token;                 fediffuselighting => 'feDiffuseLighting',
4464            return;                 fedisplacementmap => 'feDisplacementMap',
4465          }                 fedistantlight => 'feDistantLight',
4466        } elsif ($token->{tag_name} eq 'li') {                 feflood => 'feFlood',
4467          ## has a p element in scope                 fefunca => 'feFuncA',
4468          INSCOPE: for (reverse @{$self->{open_elements}}) {                 fefuncb => 'feFuncB',
4469            if ($_->[1] eq 'p') {                 fefuncg => 'feFuncG',
4470              !!!back-token;                 fefuncr => 'feFuncR',
4471              $token = {type => 'end tag', tag_name => 'p'};                 fegaussianblur => 'feGaussianBlur',
4472              return;                 feimage => 'feImage',
4473            } elsif ({                 femerge => 'feMerge',
4474                      table => 1, caption => 1, td => 1, th => 1,                 femergenode => 'feMergeNode',
4475                      button => 1, marquee => 1, object => 1, html => 1,                 femorphology => 'feMorphology',
4476                     }->{$_->[1]}) {                 feoffset => 'feOffset',
4477              last INSCOPE;                 fepointlight => 'fePointLight',
4478                   fespecularlighting => 'feSpecularLighting',
4479                   fespotlight => 'feSpotLight',
4480                   fetile => 'feTile',
4481                   feturbulence => 'feTurbulence',
4482                   foreignobject => 'foreignObject',
4483                   glyphref => 'glyphRef',
4484                   lineargradient => 'linearGradient',
4485                   radialgradient => 'radialGradient',
4486                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4487                   textpath => 'textPath',  
4488                }->{$tag_name} || $tag_name;
4489            }            }
         } # 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]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model_flag} = 'PLAINTEXT';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'a') {  
         AFE: for my $i (reverse 0..$#$active_formatting_elements) {  
           my $node = $active_formatting_elements->[$i];  
           if ($node->[1] eq 'a') {  
             !!!parse-error (type => 'in a:a');  
               
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'a'};  
             $formatting_end_tag->($token->{tag_name});  
               
             AFE2: for (reverse 0..$#$active_formatting_elements) {  
               if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {  
                 splice @$active_formatting_elements, $_, 1;  
                 last AFE2;  
               }  
             } # AFE2  
             OE: for (reverse 0..$#{$self->{open_elements}}) {  
               if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {  
                 splice @{$self->{open_elements}}, $_, 1;  
                 last OE;  
               }  
             } # OE  
             last AFE;  
           } elsif ($node->[0] eq '#marker') {  
             last AFE;  
           }  
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4490    
4491          !!!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];  
4492    
4493          !!!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);  
   
         ## has a |nobr| element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
4494    
4495          !!!next-token;            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
         return;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->('CDATA', $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
4496    
4497          ## NOTE: There is an "as if <br>" code clone.            if ($self->{self_closing}) {
4498          $reconstruct_active_formatting_elements->($insert_to_current);              pop @{$self->{open_elements}};
4499                        !!!ack ('t87.3');
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
4500            } else {            } else {
4501              push @tokens, {type => 'character',              !!!cp ('t87.4');
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
4502            }            }
4503            push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model_flag} = 'RCDATA';  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
           }  
         }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
4504            !!!next-token;            !!!next-token;
4505              next B;
4506          }          }
4507          if (length $text) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4508            $el->manakai_append_text ($text);          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4509          }          !!!cp ('t87.5');
4510                    #
4511          $self->{content_model_flag} = 'PCDATA';        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4512                    !!!cp ('t87.6');
4513          if ($token->{type} eq 'end tag' and          !!!parse-error (type => 'not closed',
4514              $token->{tag_name} eq $tag_name) {                          text => $self->{open_elements}->[-1]->[0]
4515            ## Ignore the token                              ->manakai_local_name,
4516          } else {                          token => $token);
4517            !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
4518          }          pop @{$self->{open_elements}}
4519          !!!next-token;              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4520          return;  
4521        } elsif ({          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4522                  iframe => 1,          ## Reprocess.
4523                  noembed => 1,          next B;
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         $parse_rcdata->('CDATA', $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
4524        } else {        } else {
4525          $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;  
4526        }        }
4527      } elsif ($token->{type} eq 'end tag') {      }
       if ($token->{tag_name} eq 'body') {  
         if (@{$self->{open_elements}} > 1 and  
             $self->{open_elements}->[1]->[1] eq 'body') {  
           for (@{$self->{open_elements}}) {  
             unless ({  
                        dd => 1, dt => 1, li => 1, p => 1, td => 1,  
                        th => 1, tr => 1, body => 1, html => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$_->[1]}) {  
               !!!parse-error (type => 'not closed:'.$_->[1]);  
             }  
           }  
4528    
4529            $self->{insertion_mode} = 'after body';      if ($self->{insertion_mode} & HEAD_IMS) {
4530            !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
4531            return;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4532          } else {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4533            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.2');
4534            ## Ignore the token              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4535            !!!next-token;              #
           return;  
         }  
       } elsif ($token->{tag_name} eq 'html') {  
         if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {  
           ## ISSUE: There is an issue in the spec.  
           if ($self->{open_elements}->[-1]->[1] ne 'body') {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);  
           }  
           $self->{insertion_mode} = 'after body';  
           ## reprocess  
           return;  
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
           !!!next-token;  
           return;  
         }  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, pre => 1, ul => 1,  
                 p => 1,  
                 dd => 1, dt => 1, li => 1,  
                 button => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE unless $token->{tag_name} eq 'p';  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           if (defined $i) {  
             !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4536            } else {            } else {
4537              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.1');
4538                ## Ignore the token.
4539                #
4540            }            }
4541          }            unless (length $token->{data}) {
4542                        !!!cp ('t88');
4543          if (defined $i) {              !!!next-token;
4544            splice @{$self->{open_elements}}, $i;              next B;
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4545            }            }
4546          } # 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]);  
4547          }          }
4548    
4549          undef $self->{form_element};          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4550          !!!next-token;            !!!cp ('t89');
4551          return;            ## As if <head>
4552        } elsif ({            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4553                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4554                 }->{$token->{tag_name}}) {            push @{$self->{open_elements}},
4555          ## has an element in scope                [$self->{head_element}, $el_category->{head}];
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
           
         splice @{$self->{open_elements}}, $i if defined $i;  
         !!!next-token;  
         return;  
       } elsif ({  
                 a => 1,  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 nobr => 1, s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         $formatting_end_tag->($token->{tag_name});  
         return;  
       } elsif ($token->{tag_name} eq 'br') {  
         !!!parse-error (type => 'unmatched end tag:br');  
4556    
4557          ## As if <br>            ## Reprocess in the "in head" insertion mode...
4558          $reconstruct_active_formatting_elements->($insert_to_current);            pop @{$self->{open_elements}};
           
         my $el;  
         !!!create-element ($el, 'br');  
         $insert->($el);  
           
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
4559    
4560          ## Step 2            ## Reprocess in the "after head" insertion mode...
4561          S2: {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4562            if ($node->[1] eq $token->{tag_name}) {            !!!cp ('t90');
4563              ## Step 1            ## As if </noscript>
4564              ## generate implied end tags            pop @{$self->{open_elements}};
4565              if ({            !!!parse-error (type => 'in noscript:#text', token => $token);
4566                   dd => 1, dt => 1, li => 1, p => 1,            
4567                   td => 1, th => 1, tr => 1,            ## Reprocess in the "in head" insertion mode...
4568                   tbody => 1, tfoot=> 1, thead => 1,            ## As if </head>
4569                  }->{$self->{open_elements}->[-1]->[1]}) {            pop @{$self->{open_elements}};
4570                !!!back-token;  
4571                $token = {type => 'end tag',            ## Reprocess in the "after head" insertion mode...
4572                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4573                return;            !!!cp ('t91');
4574              }            pop @{$self->{open_elements}};
4575            
4576              ## Step 2            ## Reprocess in the "after head" insertion mode...
4577              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {          } else {
4578                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!cp ('t92');
4579              }          }
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4580    
4581            ## "after head" insertion mode
4582            ## As if <body>
4583            !!!insert-element ('body',, $token);
4584            $self->{insertion_mode} = IN_BODY_IM;
4585            ## reprocess
4586            next B;
4587          } elsif ($token->{type} == START_TAG_TOKEN) {
4588            if ($token->{tag_name} eq 'head') {
4589              if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4590                !!!cp ('t93');
4591                !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4592                $self->{open_elements}->[-1]->[0]->append_child
4593                    ($self->{head_element});
4594                push @{$self->{open_elements}},
4595                    [$self->{head_element}, $el_category->{head}];
4596                $self->{insertion_mode} = IN_HEAD_IM;
4597                !!!nack ('t93.1');
4598              !!!next-token;              !!!next-token;
4599              last S2;              next B;
4600              } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4601                !!!cp ('t93.2');
4602                !!!parse-error (type => 'after head', text => 'head',
4603                                token => $token);
4604                ## Ignore the token
4605                !!!nack ('t93.3');
4606                !!!next-token;
4607                next B;
4608            } else {            } else {
4609              ## Step 3              !!!cp ('t95');
4610              if (not $formatting_category->{$node->[1]} and              !!!parse-error (type => 'in head:head',
4611                  #not $phrasing_category->{$node->[1]} and                              token => $token); # or in head noscript
4612                  ($special_category->{$node->[1]} or              ## Ignore the token
4613                   $scoping_category->{$node->[1]})) {              !!!nack ('t95.1');
4614                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!next-token;
4615                ## Ignore the token              next B;
               !!!next-token;  
               last S2;  
             }  
4616            }            }
4617                      } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4618            ## Step 4            !!!cp ('t96');
4619            $node_i--;            ## As if <head>
4620            $node = $self->{open_elements}->[$node_i];            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4621                        $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4622            ## Step 5;            push @{$self->{open_elements}},
4623            redo S2;                [$self->{head_element}, $el_category->{head}];
         } # S2  
         return;  
       }  
     }  
   }; # $in_body  
4624    
4625    B: {            $self->{insertion_mode} = IN_HEAD_IM;
4626      if ($phase eq 'main') {            ## Reprocess in the "in head" insertion mode...
4627        if ($token->{type} eq 'DOCTYPE') {          } else {
4628          !!!parse-error (type => 'in html:#DOCTYPE');            !!!cp ('t97');
         ## Ignore the token  
         ## Stay in the phase  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'start tag' and  
                $token->{tag_name} eq 'html') {  
 ## ISSUE: "aa<html>" is not a parse error.  
 ## ISSUE: "<html>" in fragment is not a parse error.  
         unless ($token->{first_start_tag}) {  
           !!!parse-error (type => 'not first start tag');  
         }  
         my $top_el = $self->{open_elements}->[0]->[0];  
         for my $attr_name (keys %{$token->{attributes}}) {  
           unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
             $top_el->set_attribute_ns  
               (undef, [undef, $attr_name],  
                $token->{attributes}->{$attr_name}->{value});  
           }  
         }  
         !!!next-token;  
         redo B;  
       } elsif ($token->{type} eq 'end-of-file') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4629          }          }
4630    
4631          ## Stop parsing              if ($token->{tag_name} eq 'base') {
4632          last B;                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4633                    !!!cp ('t98');
4634                    ## As if </noscript>
4635                    pop @{$self->{open_elements}};
4636                    !!!parse-error (type => 'in noscript', text => 'base',
4637                                    token => $token);
4638                  
4639                    $self->{insertion_mode} = IN_HEAD_IM;
4640                    ## Reprocess in the "in head" insertion mode...
4641                  } else {
4642                    !!!cp ('t99');
4643                  }
4644    
4645          ## ISSUE: There is an issue in the spec.                ## NOTE: There is a "as if in head" code clone.
4646        } else {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4647          if ($self->{insertion_mode} eq 'before head') {                  !!!cp ('t100');
4648            if ($token->{type} eq 'character') {                  !!!parse-error (type => 'after head',
4649              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                                  text => $token->{tag_name}, token => $token);
4650                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                  push @{$self->{open_elements}},
4651                unless (length $token->{data}) {                      [$self->{head_element}, $el_category->{head}];
4652                  !!!next-token;                } else {
4653                  redo B;                  !!!cp ('t101');
4654                }                }
4655              }                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4656              ## As if <head>                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4657              !!!create-element ($self->{head_element}, 'head');                pop @{$self->{open_elements}} # <head>
4658              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4659              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                !!!nack ('t101.1');
             $self->{insertion_mode} = 'in head';  
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  head => 1, body => 1, html => 1,  
                  p => 1, br => 1,  
                 }->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
4660                !!!next-token;                !!!next-token;
4661                redo B;                next B;
4662              }              } elsif ($token->{tag_name} eq 'link') {
           } else {  
             die "$0: $token->{type}: Unknown type";  
           }  
         } elsif ($self->{insertion_mode} eq 'in head' or  
                  $self->{insertion_mode} eq 'in head noscript' or  
                  $self->{insertion_mode} eq 'after head') {  
           if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
             }  
               
             #  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1, meta => 1}->{$token->{tag_name}}) {  
4663                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4664                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4665                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4666                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4667                                    text => $token->{tag_name}, token => $token);
4668                    push @{$self->{open_elements}},
4669                        [$self->{head_element}, $el_category->{head}];
4670                  } else {
4671                    !!!cp ('t103');
4672                }                }
4673                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4674                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4675                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
4676                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4677                    if $self->{insertion_mode} eq 'after head';                !!!ack ('t103.1');
4678                !!!next-token;                !!!next-token;
4679                redo B;                next B;
4680              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'meta') {
                      $self->{insertion_mode} eq 'in head') {  
4681                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4682                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4683                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4684                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4685                                    text => $token->{tag_name}, token => $token);
4686                    push @{$self->{open_elements}},
4687                        [$self->{head_element}, $el_category->{head}];
4688                  } else {
4689                    !!!cp ('t105');
4690                }                }
4691                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4692                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4693    
4694                  unless ($self->{confident}) {
4695                    if ($token->{attributes}->{charset}) {
4696                      !!!cp ('t106');
4697                      ## NOTE: Whether the encoding is supported or not is handled
4698                      ## in the {change_encoding} callback.
4699                      $self->{change_encoding}
4700                          ->($self, $token->{attributes}->{charset}->{value},
4701                             $token);
4702                      
4703                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4704                          ->set_user_data (manakai_has_reference =>
4705                                               $token->{attributes}->{charset}
4706                                                   ->{has_reference});
4707                    } elsif ($token->{attributes}->{content}) {
4708                      if ($token->{attributes}->{content}->{value}
4709                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4710                              [\x09-\x0D\x20]*=
4711                              [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4712                              ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4713                        !!!cp ('t107');
4714                        ## NOTE: Whether the encoding is supported or not is handled
4715                        ## in the {change_encoding} callback.
4716                        $self->{change_encoding}
4717                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4718                               $token);
4719                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4720                            ->set_user_data (manakai_has_reference =>
4721                                                 $token->{attributes}->{content}
4722                                                       ->{has_reference});
4723                      } else {
4724                        !!!cp ('t108');
4725                      }
4726                    }
4727                  } else {
4728                    if ($token->{attributes}->{charset}) {
4729                      !!!cp ('t109');
4730                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4731                          ->set_user_data (manakai_has_reference =>
4732                                               $token->{attributes}->{charset}
4733                                                   ->{has_reference});
4734                    }
4735                    if ($token->{attributes}->{content}) {
4736                      !!!cp ('t110');
4737                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4738                          ->set_user_data (manakai_has_reference =>
4739                                               $token->{attributes}->{content}
4740                                                   ->{has_reference});
4741                    }
4742                  }
4743    
4744                  pop @{$self->{open_elements}} # <head>
4745                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4746                  !!!ack ('t110.1');
4747                  !!!next-token;
4748                  next B;
4749                } elsif ($token->{tag_name} eq 'title') {
4750                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4751                    !!!cp ('t111');
4752                    ## As if </noscript>
4753                    pop @{$self->{open_elements}};
4754                    !!!parse-error (type => 'in noscript', text => 'title',
4755                                    token => $token);
4756                  
4757                    $self->{insertion_mode} = IN_HEAD_IM;
4758                    ## Reprocess in the "in head" insertion mode...
4759                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4760                    !!!cp ('t112');
4761                    !!!parse-error (type => 'after head',
4762                                    text => $token->{tag_name}, token => $token);
4763                    push @{$self->{open_elements}},
4764                        [$self->{head_element}, $el_category->{head}];
4765                  } else {
4766                    !!!cp ('t113');
4767                  }
4768    
4769                  ## NOTE: There is a "as if in head" code clone.
4770                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4771                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4772                $parse_rcdata->('RCDATA', sub { $parent->append_child ($_[0]) });                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4773                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4774                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4775                redo B;                next B;
4776              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4777                         $token->{tag_name} eq 'noframes') {
4778                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4779                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
4780                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4781                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4782                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4783                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4784                                    text => $token->{tag_name}, token => $token);
4785                    push @{$self->{open_elements}},
4786                        [$self->{head_element}, $el_category->{head}];
4787                  } else {
4788                    !!!cp ('t115');
4789                }                }
4790                $parse_rcdata->('CDATA', $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4791                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4792                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4793                redo B;                next B;
4794              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4795                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4796                    !!!cp ('t116');
4797                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4798                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4799                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4800                    !!!nack ('t116.1');
4801                  !!!next-token;                  !!!next-token;
4802                  redo B;                  next B;
4803                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4804                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4805                    !!!parse-error (type => 'in noscript', text => 'noscript',
4806                                    token => $token);
4807                  ## Ignore the token                  ## Ignore the token
4808                  redo B;                  !!!nack ('t117.1');
4809                    !!!next-token;
4810                    next B;
4811                } else {                } else {
4812                    !!!cp ('t118');
4813                  #                  #
4814                }                }
4815              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
4816                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4817                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
4818                ## Ignore the token                  ## As if </noscript>
4819                !!!next-token;                  pop @{$self->{open_elements}};
4820                redo B;                  !!!parse-error (type => 'in noscript', text => 'script',
4821              } elsif ($self->{insertion_mode} ne 'in head noscript' and                                  token => $token);
4822                       $token->{tag_name} eq 'script') {                
4823                if ($self->{insertion_mode} eq 'after head') {                  $self->{insertion_mode} = IN_HEAD_IM;
4824                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  ## Reprocess in the "in head" insertion mode...
4825                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4826                    !!!cp ('t120');
4827                    !!!parse-error (type => 'after head',
4828                                    text => $token->{tag_name}, token => $token);
4829                    push @{$self->{open_elements}},
4830                        [$self->{head_element}, $el_category->{head}];
4831                  } else {
4832                    !!!cp ('t121');
4833                }                }
4834    
4835                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4836                $script_start_tag->($insert_to_current);                $script_start_tag->();
4837                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4838                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4839                redo B;                next B;
4840              } elsif ($self->{insertion_mode} eq 'after head' and              } elsif ($token->{tag_name} eq 'body' or
                      $token->{tag_name} eq 'body') {  
               !!!insert-element ('body', $token->{attributes});  
               $self->{insertion_mode} = 'in body';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'after head' and  
4841                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4842                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4843                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
4844                    ## As if </noscript>
4845                    pop @{$self->{open_elements}};
4846                    !!!parse-error (type => 'in noscript',
4847                                    text => $token->{tag_name}, token => $token);
4848                    
4849                    ## Reprocess in the "in head" insertion mode...
4850                    ## As if </head>
4851                    pop @{$self->{open_elements}};
4852                    
4853                    ## Reprocess in the "after head" insertion mode...
4854                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4855                    !!!cp ('t124');
4856                    pop @{$self->{open_elements}};
4857                    
4858                    ## Reprocess in the "after head" insertion mode...
4859                  } else {
4860                    !!!cp ('t125');
4861                  }
4862    
4863                  ## "after head" insertion mode
4864                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4865                  if ($token->{tag_name} eq 'body') {
4866                    !!!cp ('t126');
4867                    $self->{insertion_mode} = IN_BODY_IM;
4868                  } elsif ($token->{tag_name} eq 'frameset') {
4869                    !!!cp ('t127');
4870                    $self->{insertion_mode} = IN_FRAMESET_IM;
4871                  } else {
4872                    die "$0: tag name: $self->{tag_name}";
4873                  }
4874                  !!!nack ('t127.1');
4875                !!!next-token;                !!!next-token;
4876                redo B;                next B;
4877              } else {              } else {
4878                  !!!cp ('t128');
4879                #                #
4880              }              }
4881            } elsif ($token->{type} eq 'end tag') {  
4882              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4883                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
4884                  ## As if </noscript>
4885                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4886                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/',
4887                !!!next-token;                                text => $token->{tag_name}, token => $token);
4888                redo B;                
4889              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## Reprocess in the "in head" insertion mode...
4890                  $token->{tag_name} eq 'noscript') {                ## As if </head>
4891                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
               $self->{insertion_mode} = 'in head';  
               !!!next-token;  
               redo B;  
             } elsif ($self->{insertion_mode} eq 'in head' and  
                      {  
                       body => 1, html => 1,  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                      {  
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} ne 'after head') {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
   
           ## As if </head> or </noscript> or <body>  
           if ($self->{insertion_mode} eq 'in head') {  
             pop @{$self->{open_elements}};  
             $self->{insertion_mode} = 'after head';  
           } elsif ($self->{insertion_mode} eq 'in head noscript') {  
             pop @{$self->{open_elements}};  
             !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));  
             $self->{insertion_mode} = 'in head';  
           } else { # 'after head'  
             !!!insert-element ('body');  
             $self->{insertion_mode} = 'in body';  
           }  
           ## reprocess  
           redo B;  
4892    
4893            ## ISSUE: An issue in the spec.                ## Reprocess in the "after head" insertion mode...
4894          } elsif ($self->{insertion_mode} eq 'in body') {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4895            if ($token->{type} eq 'character') {                !!!cp ('t130');
4896              ## NOTE: There is a code clone of "character in body".                ## As if </head>
4897              $reconstruct_active_formatting_elements->($insert_to_current);                pop @{$self->{open_elements}};
               
             $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});  
4898    
4899              !!!next-token;                ## Reprocess in the "after head" insertion mode...
4900              redo B;              } else {
4901            } elsif ($token->{type} eq 'comment') {                !!!cp ('t131');
             ## NOTE: There is a code clone of "comment in body".  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } else {  
             $in_body->($insert_to_current);  
             redo B;  
           }  
         } elsif ($self->{insertion_mode} eq 'in table') {  
           if ($token->{type} eq 'character') {  
             ## NOTE: There are "character in table" code clones.  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
                 
               unless (length $token->{data}) {  
                 !!!next-token;  
                 redo B;  
               }  
4902              }              }
4903    
4904              !!!parse-error (type => 'in table:#character');              ## "after head" insertion mode
4905                ## As if <body>
4906                !!!insert-element ('body',, $token);
4907                $self->{insertion_mode} = IN_BODY_IM;
4908                ## reprocess
4909                !!!ack-later;
4910                next B;
4911              } elsif ($token->{type} == END_TAG_TOKEN) {
4912                if ($token->{tag_name} eq 'head') {
4913                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4914                    !!!cp ('t132');
4915                    ## As if <head>
4916                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4917                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4918                    push @{$self->{open_elements}},
4919                        [$self->{head_element}, $el_category->{head}];
4920    
4921              ## As if in body, but insert into foster parent element                  ## Reprocess in the "in head" insertion mode...
4922              ## ISSUE: Spec says that "whenever a node would be inserted                  pop @{$self->{open_elements}};
4923              ## into the current node" while characters might not be                  $self->{insertion_mode} = AFTER_HEAD_IM;
4924              ## result in a new Text node.                  !!!next-token;
4925              $reconstruct_active_formatting_elements->($insert_to_foster);                  next B;
4926                              } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4927              if ({                  !!!cp ('t133');
4928                   table => 1, tbody => 1, tfoot => 1,                  ## As if </noscript>
4929                   thead => 1, tr => 1,                  pop @{$self->{open_elements}};
4930                  }->{$self->{open_elements}->[-1]->[1]}) {                  !!!parse-error (type => 'in noscript:/',
4931                # MUST                                  text => 'head', token => $token);
4932                my $foster_parent_element;                  
4933                my $next_sibling;                  ## Reprocess in the "in head" insertion mode...
4934                my $prev_sibling;                  pop @{$self->{open_elements}};
4935                OE: for (reverse 0..$#{$self->{open_elements}}) {                  $self->{insertion_mode} = AFTER_HEAD_IM;
4936                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  !!!next-token;
4937                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                  next B;
4938                    if (defined $parent and $parent->node_type == 1) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4939                      $foster_parent_element = $parent;                  !!!cp ('t134');
4940                      $next_sibling = $self->{open_elements}->[$_]->[0];                  pop @{$self->{open_elements}};
4941                      $prev_sibling = $next_sibling->previous_sibling;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4942                    } else {                  !!!next-token;
4943                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                  next B;
4944                      $prev_sibling = $foster_parent_element->last_child;                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4945                    }                  !!!cp ('t134.1');
4946                    last OE;                  !!!parse-error (type => 'unmatched end tag', text => 'head',
4947                  }                                  token => $token);
4948                } # OE                  ## Ignore the token
4949                $foster_parent_element = $self->{open_elements}->[0]->[0] and                  !!!next-token;
4950                $prev_sibling = $foster_parent_element->last_child                  next B;
                 unless defined $foster_parent_element;  
               if (defined $prev_sibling and  
                   $prev_sibling->node_type == 3) {  
                 $prev_sibling->manakai_append_text ($token->{data});  
4951                } else {                } else {
4952                  $foster_parent_element->insert_before                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                   ($self->{document}->create_text_node ($token->{data}),  
                    $next_sibling);  
4953                }                }
4954              } else {              } elsif ($token->{tag_name} eq 'noscript') {
4955                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4956              }                  !!!cp ('t136');
               
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'comment') {  
             my $comment = $self->{document}->create_comment ($token->{data});  
             $self->{open_elements}->[-1]->[0]->append_child ($comment);  
             !!!next-token;  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ({  
                  caption => 1,  
                  colgroup => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$token->{tag_name}}) {  
               ## Clear back to table context  
               while ($self->{open_elements}->[-1]->[1] ne 'table' and  
                      $self->{open_elements}->[-1]->[1] ne 'html') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4957                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4958                    $self->{insertion_mode} = IN_HEAD_IM;
4959                    !!!next-token;
4960                    next B;
4961                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4962                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4963                    !!!cp ('t137');
4964                    !!!parse-error (type => 'unmatched end tag',
4965                                    text => 'noscript', token => $token);
4966                    ## Ignore the token ## ISSUE: An issue in the spec.
4967                    !!!next-token;
4968                    next B;
4969                  } else {
4970                    !!!cp ('t138');
4971                    #
4972                }                }
   
               push @$active_formatting_elements, ['#marker', '']  
                 if $token->{tag_name} eq 'caption';  
   
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               $self->{insertion_mode} = {  
                                  caption => 'in caption',  
                                  colgroup => 'in column group',  
                                  tbody => 'in table body',  
                                  tfoot => 'in table body',  
                                  thead => 'in table body',  
                                 }->{$token->{tag_name}};  
               !!!next-token;  
               redo B;  
4973              } elsif ({              } elsif ({
4974                        col => 1,                        body => 1, html => 1,
                       td => 1, th => 1, tr => 1,  
4975                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4976                ## Clear back to table context                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4977                while ($self->{open_elements}->[-1]->[1] ne 'table' and                    $self->{insertion_mode} == IN_HEAD_IM or
4978                       $self->{open_elements}->[-1]->[1] ne 'html') {                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4979                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t140');
4980                  pop @{$self->{open_elements}};                  !!!parse-error (type => 'unmatched end tag',
4981                                    text => $token->{tag_name}, token => $token);
4982                    ## Ignore the token
4983                    !!!next-token;
4984                    next B;
4985                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4986                    !!!cp ('t140.1');
4987                    !!!parse-error (type => 'unmatched end tag',
4988                                    text => $token->{tag_name}, token => $token);
4989                    ## Ignore the token
4990                    !!!next-token;
4991                    next B;
4992                  } else {
4993                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4994                }                }
4995                } elsif ($token->{tag_name} eq 'p') {
4996                  !!!cp ('t142');
4997                  !!!parse-error (type => 'unmatched end tag',
4998                                  text => $token->{tag_name}, token => $token);
4999                  ## Ignore the token
5000                  !!!next-token;
5001                  next B;
5002                } elsif ($token->{tag_name} eq 'br') {
5003                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5004                    !!!cp ('t142.2');
5005                    ## (before head) as if <head>, (in head) as if </head>
5006                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5007                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
5008                    $self->{insertion_mode} = AFTER_HEAD_IM;
5009      
5010                    ## Reprocess in the "after head" insertion mode...
5011                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5012                    !!!cp ('t143.2');
5013                    ## As if </head>
5014                    pop @{$self->{open_elements}};
5015                    $self->{insertion_mode} = AFTER_HEAD_IM;
5016      
5017                    ## Reprocess in the "after head" insertion mode...
5018                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5019                    !!!cp ('t143.3');
5020                    ## ISSUE: Two parse errors for <head><noscript></br>
5021                    !!!parse-error (type => 'unmatched end tag',
5022                                    text => 'br', token => $token);
5023                    ## As if </noscript>
5024                    pop @{$self->{open_elements}};
5025                    $self->{insertion_mode} = IN_HEAD_IM;
5026    
5027                !!!insert-element ($token->{tag_name} eq 'col' ? 'colgroup' : 'tbody');                  ## Reprocess in the "in head" insertion mode...
5028                $self->{insertion_mode} = $token->{tag_name} eq 'col'                  ## As if </head>
5029                  ? 'in column group' : 'in table body';                  pop @{$self->{open_elements}};
5030                ## reprocess                  $self->{insertion_mode} = AFTER_HEAD_IM;
               redo B;  
             } elsif ($token->{tag_name} eq 'table') {  
               ## NOTE: There are code clones for this "table in table"  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5031    
5032                ## As if </table>                  ## Reprocess in the "after head" insertion mode...
5033                ## have a table element in table scope                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5034                my $i;                  !!!cp ('t143.4');
5035                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  #
5036                  my $node = $self->{open_elements}->[$_];                } else {
5037                  if ($node->[1] eq 'table') {                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:table');  
                 ## Ignore tokens </table><table>  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5038                }                }
5039    
5040                if ($self->{open_elements}->[-1]->[1] ne 'table') {                ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5041                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'unmatched end tag',
5042                }                                text => 'br', token => $token);
5043                  ## Ignore the token
5044                  !!!next-token;
5045                  next B;
5046                } else {
5047                  !!!cp ('t145');
5048                  !!!parse-error (type => 'unmatched end tag',
5049                                  text => $token->{tag_name}, token => $token);
5050                  ## Ignore the token
5051                  !!!next-token;
5052                  next B;
5053                }
5054    
5055                splice @{$self->{open_elements}}, $i;              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5056                  !!!cp ('t146');
5057                  ## As if </noscript>
5058                  pop @{$self->{open_elements}};
5059                  !!!parse-error (type => 'in noscript:/',
5060                                  text => $token->{tag_name}, token => $token);
5061                  
5062                  ## Reprocess in the "in head" insertion mode...
5063                  ## As if </head>
5064                  pop @{$self->{open_elements}};
5065    
5066                $self->_reset_insertion_mode;                ## Reprocess in the "after head" insertion mode...
5067                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5068                  !!!cp ('t147');
5069                  ## As if </head>
5070                  pop @{$self->{open_elements}};
5071    
5072                ## reprocess                ## Reprocess in the "after head" insertion mode...
5073                redo B;              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5074    ## ISSUE: This case cannot be reached?
5075                  !!!cp ('t148');
5076                  !!!parse-error (type => 'unmatched end tag',
5077                                  text => $token->{tag_name}, token => $token);
5078                  ## Ignore the token ## ISSUE: An issue in the spec.
5079                  !!!next-token;
5080                  next B;
5081              } else {              } else {
5082                #                !!!cp ('t149');
5083              }              }
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'table') {  
               ## have a table element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
                 }  
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
               }  
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
5084    
5085                if ($self->{open_elements}->[-1]->[1] ne 'table') {              ## "after head" insertion mode
5086                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              ## As if <body>
5087                }              !!!insert-element ('body',, $token);
5088                $self->{insertion_mode} = IN_BODY_IM;
5089                ## reprocess
5090                next B;
5091          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5092            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5093              !!!cp ('t149.1');
5094    
5095              ## NOTE: As if <head>
5096              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5097              $self->{open_elements}->[-1]->[0]->append_child
5098                  ($self->{head_element});
5099              #push @{$self->{open_elements}},
5100              #    [$self->{head_element}, $el_category->{head}];
5101              #$self->{insertion_mode} = IN_HEAD_IM;
5102              ## NOTE: Reprocess.
5103    
5104              ## NOTE: As if </head>
5105              #pop @{$self->{open_elements}};
5106              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5107              ## NOTE: Reprocess.
5108              
5109              #
5110            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5111              !!!cp ('t149.2');
5112    
5113                splice @{$self->{open_elements}}, $i;            ## NOTE: As if </head>
5114              pop @{$self->{open_elements}};
5115              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5116              ## NOTE: Reprocess.
5117    
5118                $self->_reset_insertion_mode;            #
5119            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5120              !!!cp ('t149.3');
5121    
5122                !!!next-token;            !!!parse-error (type => 'in noscript:#eof', token => $token);
5123                redo B;  
5124              } elsif ({            ## As if </noscript>
5125                        body => 1, caption => 1, col => 1, colgroup => 1,            pop @{$self->{open_elements}};
5126                        html => 1, tbody => 1, td => 1, tfoot => 1, th => 1,            #$self->{insertion_mode} = IN_HEAD_IM;
5127                        thead => 1, tr => 1,            ## NOTE: Reprocess.
                      }->{$token->{tag_name}}) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               redo B;  
             } else {  
               #  
             }  
           } else {  
             #  
           }  
5128    
5129            !!!parse-error (type => 'in table:'.$token->{tag_name});            ## NOTE: As if </head>
5130            $in_body->($insert_to_foster);            pop @{$self->{open_elements}};
5131            redo B;            #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5132          } elsif ($self->{insertion_mode} eq 'in caption') {            ## NOTE: Reprocess.
5133            if ($token->{type} eq 'character') {  
5134              ## NOTE: This is a code clone of "character in body".            #
5135            } else {
5136              !!!cp ('t149.4');
5137              #
5138            }
5139    
5140            ## NOTE: As if <body>
5141            !!!insert-element ('body',, $token);
5142            $self->{insertion_mode} = IN_BODY_IM;
5143            ## NOTE: Reprocess.
5144            next B;
5145          } else {
5146            die "$0: $token->{type}: Unknown token type";
5147          }
5148    
5149              ## ISSUE: An issue in the spec.
5150        } elsif ($self->{insertion_mode} & BODY_IMS) {
5151              if ($token->{type} == CHARACTER_TOKEN) {
5152                !!!cp ('t150');
5153                ## NOTE: There is a code clone of "character in body".
5154              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5155                            
5156              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5157    
5158              !!!next-token;              !!!next-token;
5159              redo B;              next B;
5160            } 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') {  
5161              if ({              if ({
5162                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
5163                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
5164                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5165                !!!parse-error (type => 'not closed:caption');                if ($self->{insertion_mode} == IN_CELL_IM) {
5166                    ## have an element in table scope
5167                ## As if </caption>                  for (reverse 0..$#{$self->{open_elements}}) {
5168                ## have a table element in table scope                    my $node = $self->{open_elements}->[$_];
5169                my $i;                    if ($node->[1] & TABLE_CELL_EL) {
5170                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                      !!!cp ('t151');
5171                  my $node = $self->{open_elements}->[$_];  
5172                  if ($node->[1] eq 'caption') {                      ## Close the cell
5173                    $i = $_;                      !!!back-token; # <x>
5174                    last INSCOPE;                      $token = {type => END_TAG_TOKEN,
5175                  } elsif ({                                tag_name => $node->[0]->manakai_local_name,
5176                            table => 1, html => 1,                                line => $token->{line},
5177                           }->{$node->[1]}) {                                column => $token->{column}};
5178                    last INSCOPE;                      next B;
5179                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5180                        !!!cp ('t152');
5181                        ## ISSUE: This case can never be reached, maybe.
5182                        last;
5183                      }
5184                  }                  }
5185                } # INSCOPE  
5186                unless (defined $i) {                  !!!cp ('t153');
5187                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'start tag not allowed',
5188                        text => $token->{tag_name}, token => $token);
5189                  ## Ignore the token                  ## Ignore the token
5190                    !!!nack ('t153.1');
5191                  !!!next-token;                  !!!next-token;
5192                  redo B;                  next B;
5193                }                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5194                                  !!!parse-error (type => 'not closed', text => 'caption',
5195                ## generate implied end tags                                  token => $token);
5196                if ({                  
5197                     dd => 1, dt => 1, li => 1, p => 1,                  ## NOTE: As if </caption>.
5198                     td => 1, th => 1, tr => 1,                  ## have a table element in table scope
5199                     tbody => 1, tfoot=> 1, thead => 1,                  my $i;
5200                    }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: {
5201                  !!!back-token; # <?>                    for (reverse 0..$#{$self->{open_elements}}) {
5202                  $token = {type => 'end tag', tag_name => 'caption'};                      my $node = $self->{open_elements}->[$_];
5203                  !!!back-token;                      if ($node->[1] & CAPTION_EL) {
5204                  $token = {type => 'end tag',                        !!!cp ('t155');
5205                            tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                        $i = $_;
5206                  redo B;                        last INSCOPE;
5207                }                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5208                          !!!cp ('t156');
5209                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                        last;
5210                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      }
5211                }                    }
   
               splice @{$self->{open_elements}}, $i;  
   
               $clear_up_to_marker->();  
5212    
5213                $self->{insertion_mode} = 'in table';                    !!!cp ('t157');
5214                      !!!parse-error (type => 'start tag not allowed',
5215                                      text => $token->{tag_name}, token => $token);
5216                      ## Ignore the token
5217                      !!!nack ('t157.1');
5218                      !!!next-token;
5219                      next B;
5220                    } # INSCOPE
5221                    
5222                    ## generate implied end tags
5223                    while ($self->{open_elements}->[-1]->[1]
5224                               & END_TAG_OPTIONAL_EL) {
5225                      !!!cp ('t158');
5226                      pop @{$self->{open_elements}};
5227                    }
5228    
5229                ## reprocess                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5230                redo B;                    !!!cp ('t159');
5231                      !!!parse-error (type => 'not closed',
5232                                      text => $self->{open_elements}->[-1]->[0]
5233                                          ->manakai_local_name,
5234                                      token => $token);
5235                    } else {
5236                      !!!cp ('t160');
5237                    }
5238                    
5239                    splice @{$self->{open_elements}}, $i;
5240                    
5241                    $clear_up_to_marker->();
5242                    
5243                    $self->{insertion_mode} = IN_TABLE_IM;
5244                    
5245                    ## reprocess
5246                    !!!ack-later;
5247                    next B;
5248                  } else {
5249                    !!!cp ('t161');
5250                    #
5251                  }
5252              } else {              } else {
5253                  !!!cp ('t162');
5254                #                #
5255              }              }
5256            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5257              if ($token->{tag_name} eq 'caption') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
5258                ## have a table element in table scope                if ($self->{insertion_mode} == IN_CELL_IM) {
5259                my $i;                  ## have an element in table scope
5260                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  my $i;
5261                  my $node = $self->{open_elements}->[$_];                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5262                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5263                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5264                    last INSCOPE;                      !!!cp ('t163');
5265                  } elsif ({                      $i = $_;
5266                            table => 1, html => 1,                      last INSCOPE;
5267                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5268                    last INSCOPE;                      !!!cp ('t164');
5269                        last INSCOPE;
5270                      }
5271                    } # INSCOPE
5272                      unless (defined $i) {
5273                        !!!cp ('t165');
5274                        !!!parse-error (type => 'unmatched end tag',
5275                                        text => $token->{tag_name},
5276                                        token => $token);
5277                        ## Ignore the token
5278                        !!!next-token;
5279                        next B;
5280                      }
5281                    
5282                    ## generate implied end tags
5283                    while ($self->{open_elements}->[-1]->[1]
5284                               & END_TAG_OPTIONAL_EL) {
5285                      !!!cp ('t166');
5286                      pop @{$self->{open_elements}};
5287                  }                  }
5288                } # INSCOPE  
5289                unless (defined $i) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5290                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                          ne $token->{tag_name}) {
5291                      !!!cp ('t167');
5292                      !!!parse-error (type => 'not closed',
5293                                      text => $self->{open_elements}->[-1]->[0]
5294                                          ->manakai_local_name,
5295                                      token => $token);
5296                    } else {
5297                      !!!cp ('t168');
5298                    }
5299                    
5300                    splice @{$self->{open_elements}}, $i;
5301                    
5302                    $clear_up_to_marker->();
5303                    
5304                    $self->{insertion_mode} = IN_ROW_IM;
5305                    
5306                    !!!next-token;
5307                    next B;
5308                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5309                    !!!cp ('t169');
5310                    !!!parse-error (type => 'unmatched end tag',
5311                                    text => $token->{tag_name}, token => $token);
5312                  ## Ignore the token                  ## Ignore the token
5313                  !!!next-token;                  !!!next-token;
5314                  redo B;                  next B;
5315                }                } else {
5316                                  !!!cp ('t170');
5317                ## generate implied end tags                  #
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5318                }                }
5319                } elsif ($token->{tag_name} eq 'caption') {
5320                  if ($self->{insertion_mode} == IN_CAPTION_IM) {
5321                    ## have a table element in table scope
5322                    my $i;
5323                    INSCOPE: {
5324                      for (reverse 0..$#{$self->{open_elements}}) {
5325                        my $node = $self->{open_elements}->[$_];
5326                        if ($node->[1] & CAPTION_EL) {
5327                          !!!cp ('t171');
5328                          $i = $_;
5329                          last INSCOPE;
5330                        } elsif ($node->[1] & TABLE_SCOPING_EL) {
5331                          !!!cp ('t172');
5332                          last;
5333                        }
5334                      }
5335    
5336                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                    !!!cp ('t173');
5337                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'unmatched end tag',
5338                                      text => $token->{tag_name}, token => $token);
5339                      ## Ignore the token
5340                      !!!next-token;
5341                      next B;
5342                    } # INSCOPE
5343                    
5344                    ## generate implied end tags
5345                    while ($self->{open_elements}->[-1]->[1]
5346                               & END_TAG_OPTIONAL_EL) {
5347                      !!!cp ('t174');
5348                      pop @{$self->{open_elements}};
5349                    }
5350                    
5351                    unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5352                      !!!cp ('t175');
5353                      !!!parse-error (type => 'not closed',
5354                                      text => $self->{open_elements}->[-1]->[0]
5355                                          ->manakai_local_name,
5356                                      token => $token);
5357                    } else {
5358                      !!!cp ('t176');
5359                    }
5360                    
5361                    splice @{$self->{open_elements}}, $i;
5362                    
5363                    $clear_up_to_marker->();
5364                    
5365                    $self->{insertion_mode} = IN_TABLE_IM;
5366                    
5367                    !!!next-token;
5368                    next B;
5369                  } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5370                    !!!cp ('t177');
5371                    !!!parse-error (type => 'unmatched end tag',
5372                                    text => $token->{tag_name}, token => $token);
5373                    ## Ignore the token
5374                    !!!next-token;
5375                    next B;
5376                  } else {
5377                    !!!cp ('t178');
5378                    #
5379                }                }
5380                } elsif ({
5381                          table => 1, tbody => 1, tfoot => 1,
5382                          thead => 1, tr => 1,
5383                         }->{$token->{tag_name}} and
5384                         $self->{insertion_mode} == IN_CELL_IM) {
5385                  ## have an element in table scope
5386                  my $i;
5387                  my $tn;
5388                  INSCOPE: {
5389                    for (reverse 0..$#{$self->{open_elements}}) {
5390                      my $node = $self->{open_elements}->[$_];
5391                      if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5392                        !!!cp ('t179');
5393                        $i = $_;
5394    
5395                        ## Close the cell
5396                        !!!back-token; # </x>
5397                        $token = {type => END_TAG_TOKEN, tag_name => $tn,
5398                                  line => $token->{line},
5399                                  column => $token->{column}};
5400                        next B;
5401                      } elsif ($node->[1] & TABLE_CELL_EL) {
5402                        !!!cp ('t180');
5403                        $tn = $node->[0]->manakai_local_name;
5404                        ## NOTE: There is exactly one |td| or |th| element
5405                        ## in scope in the stack of open elements by definition.
5406                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5407                        ## ISSUE: Can this be reached?
5408                        !!!cp ('t181');
5409                        last;
5410                      }
5411                    }
5412    
5413                splice @{$self->{open_elements}}, $i;                  !!!cp ('t182');
5414                    !!!parse-error (type => 'unmatched end tag',
5415                $clear_up_to_marker->();                      text => $token->{tag_name}, token => $token);
5416                    ## Ignore the token
5417                $self->{insertion_mode} = 'in table';                  !!!next-token;
5418                    next B;
5419                !!!next-token;                } # INSCOPE
5420                redo B;              } elsif ($token->{tag_name} eq 'table' and
5421              } elsif ($token->{tag_name} eq 'table') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5422                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5423                                  token => $token);
5424    
5425                ## As if </caption>                ## As if </caption>
5426                ## have a table element in table scope                ## have a table element in table scope
5427                my $i;                my $i;
5428                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5429                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5430                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5431                      !!!cp ('t184');
5432                    $i = $_;                    $i = $_;
5433                    last INSCOPE;                    last INSCOPE;
5434                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5435                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5436                    last INSCOPE;                    last INSCOPE;
5437                  }                  }
5438                } # INSCOPE                } # INSCOPE
5439                unless (defined $i) {                unless (defined $i) {
5440                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5441                    !!!parse-error (type => 'unmatched end tag',
5442                                    text => 'caption', token => $token);
5443                  ## Ignore the token                  ## Ignore the token
5444                  !!!next-token;                  !!!next-token;
5445                  redo B;                  next B;
5446                }                }
5447                                
5448                ## generate implied end tags                ## generate implied end tags
5449                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5450                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5451                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # </table>  
                 $token = {type => 'end tag', tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5452                }                }
5453    
5454                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5455                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5456                    !!!parse-error (type => 'not closed',
5457                                    text => $self->{open_elements}->[-1]->[0]
5458                                        ->manakai_local_name,
5459                                    token => $token);
5460                  } else {
5461                    !!!cp ('t189');
5462                }                }
5463    
5464                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5465    
5466                $clear_up_to_marker->();                $clear_up_to_marker->();
5467    
5468                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5469    
5470                ## reprocess                ## reprocess
5471                redo B;                next B;
5472              } elsif ({              } elsif ({
5473                        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,  
5474                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5475                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5476                ## Ignore the token                  !!!cp ('t190');
5477                redo B;                  !!!parse-error (type => 'unmatched end tag',
5478              } 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');  
5479                  ## Ignore the token                  ## Ignore the token
5480                  !!!next-token;                  !!!next-token;
5481                  redo B;                  next B;
5482                } else {                } else {
5483                  pop @{$self->{open_elements}}; # colgroup                  !!!cp ('t191');
5484                  $self->{insertion_mode} = 'in table';                  #
                 !!!next-token;  
                 redo B;              
5485                }                }
5486              } elsif ($token->{tag_name} eq 'col') {              } elsif ({
5487                !!!parse-error (type => 'unmatched end tag:col');                        tbody => 1, tfoot => 1,
5488                          thead => 1, tr => 1,
5489                         }->{$token->{tag_name}} and
5490                         $self->{insertion_mode} == IN_CAPTION_IM) {
5491                  !!!cp ('t192');
5492                  !!!parse-error (type => 'unmatched end tag',
5493                                  text => $token->{tag_name}, token => $token);
5494                ## Ignore the token                ## Ignore the token
5495                !!!next-token;                !!!next-token;
5496                redo B;                next B;
5497              } else {              } else {
5498                #                !!!cp ('t193');
5499                  #
5500              }              }
5501            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5502              #          for my $entry (@{$self->{open_elements}}) {
5503              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5504                !!!cp ('t75');
5505                !!!parse-error (type => 'in body:#eof', token => $token);
5506                last;
5507            }            }
5508            }
5509    
5510            ## As if </colgroup>          ## Stop parsing.
5511            if ($self->{open_elements}->[-1]->[1] eq 'html') {          last B;
5512              !!!parse-error (type => 'unmatched end tag:colgroup');        } else {
5513              ## Ignore the token          die "$0: $token->{type}: Unknown token type";
5514          }
5515    
5516          $insert = $insert_to_current;
5517          #
5518        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5519          if ($token->{type} == CHARACTER_TOKEN) {
5520            if (not $open_tables->[-1]->[1] and # tainted
5521                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5522              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5523                  
5524              unless (length $token->{data}) {
5525                !!!cp ('t194');
5526              !!!next-token;              !!!next-token;
5527              redo B;              next B;
5528            } else {            } else {
5529              pop @{$self->{open_elements}}; # colgroup              !!!cp ('t195');
             $self->{insertion_mode} = 'in table';  
             ## reprocess  
             redo B;  
5530            }            }
5531          } 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;  
               }  
             }  
5532    
5533              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5534    
5535              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5536              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
5537              ## into the current node" while characters might not be              ## into the current node" while characters might not be
5538              ## result in a new Text node.              ## result in a new Text node.
5539              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5540                
5541              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]}) {  
5542                # MUST                # MUST
5543                my $foster_parent_element;                my $foster_parent_element;
5544                my $next_sibling;                my $next_sibling;
5545                my $prev_sibling;                my $prev_sibling;
5546                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5547                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5548                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5549                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5550                        !!!cp ('t196');
5551                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5552                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5553                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5554                    } else {                    } else {
5555                        !!!cp ('t197');
5556                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5557                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5558                    }                    }
# Line 4108  sub _tree_construction_main ($) { Line 5564  sub _tree_construction_main ($) {
5564                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5565                if (defined $prev_sibling and                if (defined $prev_sibling and
5566                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5567                    !!!cp ('t198');
5568                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5569                } else {                } else {
5570                    !!!cp ('t199');
5571                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5572                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5573                     $next_sibling);                     $next_sibling);
5574                }                }
5575              } else {            $open_tables->[-1]->[1] = 1; # tainted
5576                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5577              !!!cp ('t200');
5578              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5579            }
5580                
5581            !!!next-token;
5582            next B;
5583          } elsif ($token->{type} == START_TAG_TOKEN) {
5584            if ({
5585                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5586                 th => 1, td => 1,
5587                }->{$token->{tag_name}}) {
5588              if ($self->{insertion_mode} == IN_TABLE_IM) {
5589                ## Clear back to table context
5590                while (not ($self->{open_elements}->[-1]->[1]
5591                                & TABLE_SCOPING_EL)) {
5592                  !!!cp ('t201');
5593                  pop @{$self->{open_elements}};
5594              }              }
5595                            
5596              !!!next-token;              !!!insert-element ('tbody',, $token);
5597              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5598            } elsif ($token->{type} eq 'comment') {              ## reprocess in the "in table body" insertion mode...
5599              ## Copied from 'in table'            }
5600              my $comment = $self->{document}->create_comment ($token->{data});            
5601              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5602              !!!next-token;              unless ($token->{tag_name} eq 'tr') {
5603              redo B;                !!!cp ('t202');
5604            } elsif ($token->{type} eq 'start tag') {                !!!parse-error (type => 'missing start tag:tr', token => $token);
5605              if ({              }
5606                   tr => 1,                  
5607                   th => 1, td => 1,              ## Clear back to table body context
5608                  }->{$token->{tag_name}}) {              while (not ($self->{open_elements}->[-1]->[1]
5609                unless ($token->{tag_name} eq 'tr') {                              & TABLE_ROWS_SCOPING_EL)) {
5610                  !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t203');
5611                  ## ISSUE: Can this case be reached?
5612                  pop @{$self->{open_elements}};
5613                }
5614                    
5615                    $self->{insertion_mode} = IN_ROW_IM;
5616                    if ($token->{tag_name} eq 'tr') {
5617                      !!!cp ('t204');
5618                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5619                      !!!nack ('t204');
5620                      !!!next-token;
5621                      next B;
5622                    } else {
5623                      !!!cp ('t205');
5624                      !!!insert-element ('tr',, $token);
5625                      ## reprocess in the "in row" insertion mode
5626                    }
5627                  } else {
5628                    !!!cp ('t206');
5629                }                }
5630    
5631                ## Clear back to table body context                ## Clear back to table row context
5632                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5633                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5634                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5635                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5636                }                }
5637                                
5638                $self->{insertion_mode} = 'in row';                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5639                if ($token->{tag_name} eq 'tr') {                $self->{insertion_mode} = IN_CELL_IM;
5640                  !!!insert-element ($token->{tag_name}, $token->{attributes});  
5641                  !!!next-token;                push @$active_formatting_elements, ['#marker', ''];
5642                } else {                
5643                  !!!insert-element ('tr');                !!!nack ('t207.1');
5644                  ## reprocess                !!!next-token;
5645                }                next B;
               redo B;  
5646              } elsif ({              } elsif ({
5647                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5648                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5649                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5650                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5651                ## have an element in table scope                if ($self->{insertion_mode} == IN_ROW_IM) {
5652                my $i;                  ## As if </tr>
5653                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
5654                  my $node = $self->{open_elements}->[$_];                  my $i;
5655                  if ({                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5656                       tbody => 1, thead => 1, tfoot => 1,                    my $node = $self->{open_elements}->[$_];
5657                      }->{$node->[1]}) {                    if ($node->[1] & TABLE_ROW_EL) {
5658                    $i = $_;                      !!!cp ('t208');
5659                    last INSCOPE;                      $i = $_;
5660                  } elsif ({                      last INSCOPE;
5661                            table => 1, html => 1,                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5662                           }->{$node->[1]}) {                      !!!cp ('t209');
5663                    last INSCOPE;                      last INSCOPE;
5664                      }
5665                    } # INSCOPE
5666                    unless (defined $i) {
5667                      !!!cp ('t210');
5668    ## TODO: This type is wrong.
5669                      !!!parse-error (type => 'unmacthed end tag',
5670                                      text => $token->{tag_name}, token => $token);
5671                      ## Ignore the token
5672                      !!!nack ('t210.1');
5673                      !!!next-token;
5674                      next B;
5675                    }
5676                    
5677                    ## Clear back to table row context
5678                    while (not ($self->{open_elements}->[-1]->[1]
5679                                    & TABLE_ROW_SCOPING_EL)) {
5680                      !!!cp ('t211');
5681                      ## ISSUE: Can this case be reached?
5682                      pop @{$self->{open_elements}};
5683                    }
5684                    
5685                    pop @{$self->{open_elements}}; # tr
5686                    $self->{insertion_mode} = IN_TABLE_BODY_IM;
5687                    if ($token->{tag_name} eq 'tr') {
5688                      !!!cp ('t212');
5689                      ## reprocess
5690                      !!!ack-later;
5691                      next B;
5692                    } else {
5693                      !!!cp ('t213');
5694                      ## reprocess in the "in table body" insertion mode...
5695                  }                  }
               } # INSCOPE  
               unless (defined $i) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5696                }                }
5697    
5698                ## Clear back to table body context                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5699                while (not {                  ## have an element in table scope
5700                  tbody => 1, tfoot => 1, thead => 1, html => 1,                  my $i;
5701                }->{$self->{open_elements}->[-1]->[1]}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5702                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    my $node = $self->{open_elements}->[$_];
5703                      if ($node->[1] & TABLE_ROW_GROUP_EL) {
5704                        !!!cp ('t214');
5705                        $i = $_;
5706                        last INSCOPE;
5707                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5708                        !!!cp ('t215');
5709                        last INSCOPE;
5710                      }
5711                    } # INSCOPE
5712                    unless (defined $i) {
5713                      !!!cp ('t216');
5714    ## TODO: This erorr type is wrong.
5715                      !!!parse-error (type => 'unmatched end tag',
5716                                      text => $token->{tag_name}, token => $token);
5717                      ## Ignore the token
5718                      !!!nack ('t216.1');
5719                      !!!next-token;
5720                      next B;
5721                    }
5722    
5723                    ## Clear back to table body context
5724                    while (not ($self->{open_elements}->[-1]->[1]
5725                                    & TABLE_ROWS_SCOPING_EL)) {
5726                      !!!cp ('t217');
5727                      ## ISSUE: Can this state be reached?
5728                      pop @{$self->{open_elements}};
5729                    }
5730                    
5731                    ## As if <{current node}>
5732                    ## have an element in table scope
5733                    ## true by definition
5734                    
5735                    ## Clear back to table body context
5736                    ## nop by definition
5737                    
5738                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5739                    $self->{insertion_mode} = IN_TABLE_IM;
5740                    ## reprocess in "in table" insertion mode...
5741                  } else {
5742                    !!!cp ('t218');
5743                }                }
5744    
5745                ## As if <{current node}>                if ($token->{tag_name} eq 'col') {
5746                ## have an element in table scope                  ## Clear back to table context
5747                ## true by definition                  while (not ($self->{open_elements}->[-1]->[1]
5748                                    & TABLE_SCOPING_EL)) {
5749                ## Clear back to table body context                    !!!cp ('t219');
5750                ## nop by definition                    ## ISSUE: Can this state be reached?
5751                      pop @{$self->{open_elements}};
5752                pop @{$self->{open_elements}};                  }
5753                $self->{insertion_mode} = 'in table';                  
5754                ## reprocess                  !!!insert-element ('colgroup',, $token);
5755                redo B;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5756                    ## reprocess
5757                    !!!ack-later;
5758                    next B;
5759                  } elsif ({
5760                            caption => 1,
5761                            colgroup => 1,
5762                            tbody => 1, tfoot => 1, thead => 1,
5763                           }->{$token->{tag_name}}) {
5764                    ## Clear back to table context
5765                    while (not ($self->{open_elements}->[-1]->[1]
5766                                    & TABLE_SCOPING_EL)) {
5767                      !!!cp ('t220');
5768                      ## ISSUE: Can this state be reached?
5769                      pop @{$self->{open_elements}};
5770                    }
5771                    
5772                    push @$active_formatting_elements, ['#marker', '']
5773                        if $token->{tag_name} eq 'caption';
5774                    
5775                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5776                    $self->{insertion_mode} = {
5777                                               caption => IN_CAPTION_IM,
5778                                               colgroup => IN_COLUMN_GROUP_IM,
5779                                               tbody => IN_TABLE_BODY_IM,
5780                                               tfoot => IN_TABLE_BODY_IM,
5781                                               thead => IN_TABLE_BODY_IM,
5782                                              }->{$token->{tag_name}};
5783                    !!!next-token;
5784                    !!!nack ('t220.1');
5785                    next B;
5786                  } else {
5787                    die "$0: in table: <>: $token->{tag_name}";
5788                  }
5789              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5790                ## NOTE: This is a code clone of "table in table"                !!!parse-error (type => 'not closed',
5791                !!!parse-error (type => 'not closed:table');                                text => $self->{open_elements}->[-1]->[0]
5792                                      ->manakai_local_name,
5793                                  token => $token);
5794    
5795                ## As if </table>                ## As if </table>
5796                ## have a table element in table scope                ## have a table element in table scope
5797                my $i;                my $i;
5798                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5799                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5800                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5801                      !!!cp ('t221');
5802                    $i = $_;                    $i = $_;
5803                    last INSCOPE;                    last INSCOPE;
5804                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5805                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5806                    last INSCOPE;                    last INSCOPE;
5807                  }                  }
5808                } # INSCOPE                } # INSCOPE
5809                unless (defined $i) {                unless (defined $i) {
5810                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5811    ## TODO: The following is wrong, maybe.
5812                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5813                                    token => $token);
5814                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5815                    !!!nack ('t223.1');
5816                  !!!next-token;                  !!!next-token;
5817                  redo B;                  next B;
5818                }                }
5819                                
5820    ## TODO: Followings are removed from the latest spec.
5821                ## generate implied end tags                ## generate implied end tags
5822                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5823                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5824                     td => 1, th => 1, tr => 1,                  pop @{$self->{open_elements}};
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5825                }                }
5826    
5827                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5828                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5829                    ## NOTE: |<table><tr><table>|
5830                    !!!parse-error (type => 'not closed',
5831                                    text => $self->{open_elements}->[-1]->[0]
5832                                        ->manakai_local_name,
5833                                    token => $token);
5834                  } else {
5835                    !!!cp ('t226');
5836                }                }
5837    
5838                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5839                  pop @{$open_tables};
5840    
5841                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5842    
5843                ## reprocess            ## reprocess
5844                redo B;            !!!ack-later;
5845              } else {            next B;
5846                #          } elsif ($token->{tag_name} eq 'style') {
5847              }            if (not $open_tables->[-1]->[1]) { # tainted
5848            } elsif ($token->{type} eq 'end tag') {              !!!cp ('t227.8');
5849              if ({              ## NOTE: This is a "as if in head" code clone.
5850                   tbody => 1, tfoot => 1, thead => 1,              $parse_rcdata->(CDATA_CONTENT_MODEL);
5851                  }->{$token->{tag_name}}) {              next B;
5852                ## have an element in table scope            } else {
5853                my $i;              !!!cp ('t227.7');
5854                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              #
5855                  my $node = $self->{open_elements}->[$_];            }
5856                  if ($node->[1] eq $token->{tag_name}) {          } elsif ($token->{tag_name} eq 'script') {
5857                    $i = $_;            if (not $open_tables->[-1]->[1]) { # tainted
5858                    last INSCOPE;              !!!cp ('t227.6');
5859                  } elsif ({              ## NOTE: This is a "as if in head" code clone.
5860                            table => 1, html => 1,              $script_start_tag->();
5861                           }->{$node->[1]}) {              next B;
5862                    last INSCOPE;            } else {
5863                  }              !!!cp ('t227.5');
5864                } # INSCOPE              #
5865                unless (defined $i) {            }
5866                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'input') {
5867                  ## Ignore the token            if (not $open_tables->[-1]->[1]) { # tainted
5868                  !!!next-token;              if ($token->{attributes}->{type}) { ## TODO: case
5869                  redo B;                my $type = lc $token->{attributes}->{type}->{value};
5870                }                if ($type eq 'hidden') {
5871                    !!!cp ('t227.3');
5872                    !!!parse-error (type => 'in table',
5873                                    text => $token->{tag_name}, token => $token);
5874    
5875                ## 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}};  
               }  
5876    
5877                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;  
               }  
5878    
               ## 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]);  
5879                  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  
5880    
5881                pop @{$self->{open_elements}};                  !!!next-token;
5882                $self->{insertion_mode} = 'in table';                  !!!ack ('t227.2.1');
5883                ## reprocess                  next B;
5884                redo B;                } else {
5885              } elsif ({                  !!!cp ('t227.2');
5886                        body => 1, caption => 1, col => 1, colgroup => 1,                  #
5887                        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;  
5888              } else {              } else {
5889                  !!!cp ('t227.1');
5890                #                #
5891              }              }
5892            } else {            } else {
5893                !!!cp ('t227.4');
5894              #              #
5895            }            }
5896                      } else {
5897            ## As if in table            !!!cp ('t227');
5898            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
5899            $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;  
               }  
             }  
5900    
5901              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table', text => $token->{tag_name},
5902                            token => $token);
5903    
5904              ## As if in body, but insert into foster parent element          $insert = $insert_to_foster;
5905              ## ISSUE: Spec says that "whenever a node would be inserted          #
5906              ## into the current node" while characters might not be        } elsif ($token->{type} == END_TAG_TOKEN) {
5907              ## result in a new Text node.              if ($token->{tag_name} eq 'tr' and
5908              $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>  
5909                ## have an element in table scope                ## have an element in table scope
5910                my $i;                my $i;
5911                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5912                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5913                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_ROW_EL) {
5914                      !!!cp ('t228');
5915                    $i = $_;                    $i = $_;
5916                    last INSCOPE;                    last INSCOPE;
5917                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5918                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5919                    last INSCOPE;                    last INSCOPE;
5920                  }                  }
5921                } # INSCOPE                } # INSCOPE
5922                unless (defined $i) {                unless (defined $i) {
5923                  !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                  !!!cp ('t230');
5924                    !!!parse-error (type => 'unmatched end tag',
5925                                    text => $token->{tag_name}, token => $token);
5926                  ## Ignore the token                  ## Ignore the token
5927                    !!!nack ('t230.1');
5928                  !!!next-token;                  !!!next-token;
5929                  redo B;                  next B;
5930                  } else {
5931                    !!!cp ('t232');
5932                }                }
5933    
5934                ## Clear back to table row context                ## Clear back to table row context
5935                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5936                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5937                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5938                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5939                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5940                }                }
5941    
5942                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5943                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5944                ## reprocess                !!!next-token;
5945                redo B;                !!!nack ('t231.1');
5946                  next B;
5947              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5948                ## NOTE: This is a code clone of "table in table"                if ($self->{insertion_mode} == IN_ROW_IM) {
5949                !!!parse-error (type => 'not closed:table');                  ## As if </tr>
5950                    ## have an element in table scope
5951                ## As if </table>                  my $i;
5952                ## have a table element in table scope                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5953                my $i;                    my $node = $self->{open_elements}->[$_];
5954                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    if ($node->[1] & TABLE_ROW_EL) {
5955                  my $node = $self->{open_elements}->[$_];                      !!!cp ('t233');
5956                  if ($node->[1] eq 'table') {                      $i = $_;
5957                    $i = $_;                      last INSCOPE;
5958                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5959                  } elsif ({                      !!!cp ('t234');
5960                            table => 1, html => 1,                      last INSCOPE;
5961                           }->{$node->[1]}) {                    }
5962                    last INSCOPE;                  } # INSCOPE
5963                    unless (defined $i) {
5964                      !!!cp ('t235');
5965    ## TODO: The following is wrong.
5966                      !!!parse-error (type => 'unmatched end tag',
5967                                      text => $token->{type}, token => $token);
5968                      ## Ignore the token
5969                      !!!nack ('t236.1');
5970                      !!!next-token;
5971                      next B;
5972                  }                  }
5973                } # INSCOPE                  
5974                unless (defined $i) {                  ## Clear back to table row context
5975                  !!!parse-error (type => 'unmatched end tag:table');                  while (not ($self->{open_elements}->[-1]->[1]
5976                  ## Ignore tokens </table><table>                                  & TABLE_ROW_SCOPING_EL)) {
5977                  !!!next-token;                    !!!cp ('t236');
5978                  redo B;  ## ISSUE: Can this state be reached?
5979                }                    pop @{$self->{open_elements}};
                 
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token; # <table>  
                 $token = {type => 'end tag', tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               }  
   
               splice @{$self->{open_elements}}, $i;  
   
               $self->_reset_insertion_mode;  
   
               ## reprocess  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'tr') {  
               ## have an element in table scope  
               my $i;  
               INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
                 my $node = $self->{open_elements}->[$_];  
                 if ($node->[1] eq $token->{tag_name}) {  
                   $i = $_;  
                   last INSCOPE;  
                 } elsif ({  
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
                   last INSCOPE;  
5980                  }                  }
5981                } # INSCOPE                  
5982                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
5983                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5984                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
5985                  !!!next-token;                }
5986                  redo B;  
5987                }                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5988                    ## have an element in table scope
5989                ## Clear back to table row context                  my $i;
5990                while (not {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5991                  tr => 1, html => 1,                    my $node = $self->{open_elements}->[$_];
5992                }->{$self->{open_elements}->[-1]->[1]}) {                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5993                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                      !!!cp ('t237');
5994                        $i = $_;
5995                        last INSCOPE;
5996                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5997                        !!!cp ('t238');
5998                        last INSCOPE;
5999                      }
6000                    } # INSCOPE
6001                    unless (defined $i) {
6002                      !!!cp ('t239');
6003                      !!!parse-error (type => 'unmatched end tag',
6004                                      text => $token->{tag_name}, token => $token);
6005                      ## Ignore the token
6006                      !!!nack ('t239.1');
6007                      !!!next-token;
6008                      next B;
6009                    }
6010                    
6011                    ## Clear back to table body context
6012                    while (not ($self->{open_elements}->[-1]->[1]
6013                                    & TABLE_ROWS_SCOPING_EL)) {
6014                      !!!cp ('t240');
6015                      pop @{$self->{open_elements}};
6016                    }
6017                    
6018                    ## As if <{current node}>
6019                    ## have an element in table scope
6020                    ## true by definition
6021                    
6022                    ## Clear back to table body context
6023                    ## nop by definition
6024                    
6025                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6026                    $self->{insertion_mode} = IN_TABLE_IM;
6027                    ## reprocess in the "in table" insertion mode...
6028                }                }
6029    
6030                pop @{$self->{open_elements}}; # tr                ## NOTE: </table> in the "in table" insertion mode.
6031                $self->{insertion_mode} = 'in table body';                ## When you edit the code fragment below, please ensure that
6032                !!!next-token;                ## the code for <table> in the "in table" insertion mode
6033                redo B;                ## is synced with it.
6034              } elsif ($token->{tag_name} eq 'table') {  
6035                ## As if </tr>                ## have a table element in table scope
               ## have an element in table scope  
6036                my $i;                my $i;
6037                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6038                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6039                  if ($node->[1] eq 'tr') {                  if ($node->[1] & TABLE_EL) {
6040                      !!!cp ('t241');
6041                    $i = $_;                    $i = $_;
6042                    last INSCOPE;                    last INSCOPE;
6043                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6044                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
6045                    last INSCOPE;                    last INSCOPE;
6046                  }                  }
6047                } # INSCOPE                } # INSCOPE
6048                unless (defined $i) {                unless (defined $i) {
6049                  !!!parse-error (type => 'unmatched end tag:'.$token->{type});                  !!!cp ('t243');
6050                    !!!parse-error (type => 'unmatched end tag',
6051                                    text => $token->{tag_name}, token => $token);
6052                  ## Ignore the token                  ## Ignore the token
6053                    !!!nack ('t243.1');
6054                  !!!next-token;                  !!!next-token;
6055                  redo B;                  next B;
6056                }                }
6057                    
6058                ## Clear back to table row context                splice @{$self->{open_elements}}, $i;
6059                while (not {                pop @{$open_tables};
6060                  tr => 1, html => 1,                
6061                }->{$self->{open_elements}->[-1]->[1]}) {                $self->_reset_insertion_mode;
6062                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                
6063                  pop @{$self->{open_elements}};                !!!next-token;
6064                }                next B;
   
               pop @{$self->{open_elements}}; # tr  
               $self->{insertion_mode} = 'in table body';  
               ## reprocess  
               redo B;  
6065              } elsif ({              } elsif ({
6066                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6067                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}} and
6068                ## have an element in table scope                       $self->{insertion_mode} & ROW_IMS) {
6069                my $i;                if ($self->{insertion_mode} == IN_ROW_IM) {
6070                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  ## have an element in table scope
6071                  my $node = $self->{open_elements}->[$_];                  my $i;
6072                  if ($node->[1] eq $token->{tag_name}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6073                    $i = $_;                    my $node = $self->{open_elements}->[$_];
6074                    last INSCOPE;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6075                  } elsif ({                      !!!cp ('t247');
6076                            table => 1, html => 1,                      $i = $_;
6077                           }->{$node->[1]}) {                      last INSCOPE;
6078                    last INSCOPE;                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6079                        !!!cp ('t248');
6080                        last INSCOPE;
6081                      }
6082                    } # INSCOPE
6083                      unless (defined $i) {
6084                        !!!cp ('t249');
6085                        !!!parse-error (type => 'unmatched end tag',
6086                                        text => $token->{tag_name}, token => $token);
6087                        ## Ignore the token
6088                        !!!nack ('t249.1');
6089                        !!!next-token;
6090                        next B;
6091                      }
6092                    
6093                    ## As if </tr>
6094                    ## have an element in table scope
6095                    my $i;
6096                    INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6097                      my $node = $self->{open_elements}->[$_];
6098                      if ($node->[1] & TABLE_ROW_EL) {
6099                        !!!cp ('t250');
6100                        $i = $_;
6101                        last INSCOPE;
6102                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
6103                        !!!cp ('t251');
6104                        last INSCOPE;
6105                      }
6106                    } # INSCOPE
6107                      unless (defined $i) {
6108                        !!!cp ('t252');
6109                        !!!parse-error (type => 'unmatched end tag',
6110                                        text => 'tr', token => $token);
6111                        ## Ignore the token
6112                        !!!nack ('t252.1');
6113                        !!!next-token;
6114                        next B;
6115                      }
6116                    
6117                    ## Clear back to table row context
6118                    while (not ($self->{open_elements}->[-1]->[1]
6119                                    & TABLE_ROW_SCOPING_EL)) {
6120                      !!!cp ('t253');
6121    ## ISSUE: Can this case be reached?
6122                      pop @{$self->{open_elements}};
6123                  }                  }
6124                } # INSCOPE                  
6125                unless (defined $i) {                  pop @{$self->{open_elements}}; # tr
6126                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
6127                  ## Ignore the token                  ## reprocess in the "in table body" insertion mode...
                 !!!next-token;  
                 redo B;  
6128                }                }
6129    
               ## As if </tr>  
6130                ## have an element in table scope                ## have an element in table scope
6131                my $i;                my $i;
6132                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6133                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6134                  if ($node->[1] eq 'tr') {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6135                      !!!cp ('t254');
6136                    $i = $_;                    $i = $_;
6137                    last INSCOPE;                    last INSCOPE;
6138                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6139                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6140                    last INSCOPE;                    last INSCOPE;
6141                  }                  }
6142                } # INSCOPE                } # INSCOPE
6143                unless (defined $i) {                unless (defined $i) {
6144                  !!!parse-error (type => 'unmatched end tag:tr');                  !!!cp ('t256');
6145                    !!!parse-error (type => 'unmatched end tag',
6146                                    text => $token->{tag_name}, token => $token);
6147                  ## Ignore the token                  ## Ignore the token
6148                    !!!nack ('t256.1');
6149                  !!!next-token;                  !!!next-token;
6150                  redo B;                  next B;
6151                }                }
6152    
6153                ## Clear back to table row context                ## Clear back to table body context
6154                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6155                  tr => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6156                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6157                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6158                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6159                }                }
6160    
6161                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}};
6162                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_IM;
6163                ## reprocess                !!!nack ('t257.1');
6164                redo B;                !!!next-token;
6165                  next B;
6166              } elsif ({              } elsif ({
6167                        body => 1, caption => 1, col => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6168                        colgroup => 1, html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6169                          tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6170                          tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6171                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6172                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6173                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6174                !!!next-token;                            text => $token->{tag_name}, token => $token);
6175                redo B;            ## Ignore the token
6176              } else {            !!!nack ('t258.1');
6177                #             !!!next-token;
6178              }            next B;
6179            } else {          } else {
6180              #            !!!cp ('t259');
6181            }            !!!parse-error (type => 'in table:/',
6182                              text => $token->{tag_name}, token => $token);
6183    
6184            ## As if in table            $insert = $insert_to_foster;
6185            !!!parse-error (type => 'in table:'.$token->{tag_name});            #
6186            $in_body->($insert_to_foster);          }
6187            redo B;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6188          } elsif ($self->{insertion_mode} eq 'in cell') {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6189            if ($token->{type} eq 'character') {                  @{$self->{open_elements}} == 1) { # redundant, maybe
6190              ## NOTE: This is a code clone of "character in body".            !!!parse-error (type => 'in body:#eof', token => $token);
6191              $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t259.1');
6192                          #
6193              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
6194              !!!cp ('t259.2');
6195              #
6196            }
6197    
6198              !!!next-token;          ## Stop parsing
6199              redo B;          last B;
6200            } elsif ($token->{type} eq 'comment') {        } else {
6201              ## NOTE: This is a code clone of "comment in body".          die "$0: $token->{type}: Unknown token type";
6202              my $comment = $self->{document}->create_comment ($token->{data});        }
6203              $self->{open_elements}->[-1]->[0]->append_child ($comment);      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6204              !!!next-token;            if ($token->{type} == CHARACTER_TOKEN) {
6205              redo B;              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6206            } elsif ($token->{type} eq 'start tag') {                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6207              if ({                unless (length $token->{data}) {
6208                   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  
6209                  !!!next-token;                  !!!next-token;
6210                  redo B;                  next B;
6211                }                }
6212                }
6213                ## Close the cell              
6214                !!!back-token; # <?>              !!!cp ('t261');
6215                $token = {type => 'end tag', tag_name => $tn};              #
6216                redo B;            } elsif ($token->{type} == START_TAG_TOKEN) {
6217              } else {              if ($token->{tag_name} eq 'col') {
6218                  !!!cp ('t262');
6219                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6220                  pop @{$self->{open_elements}};
6221                  !!!ack ('t262.1');
6222                  !!!next-token;
6223                  next B;
6224                } else {
6225                  !!!cp ('t263');
6226                #                #
6227              }              }
6228            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
6229              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'colgroup') {
6230                ## have an element in table scope                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6231                my $i;                  !!!cp ('t264');
6232                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  !!!parse-error (type => 'unmatched end tag',
6233                  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});  
6234                  ## Ignore the token                  ## Ignore the token
6235                  !!!next-token;                  !!!next-token;
6236                  redo B;                  next B;
6237                }                } else {
6238                                  !!!cp ('t265');
6239                ## generate implied end tags                  pop @{$self->{open_elements}}; # colgroup
6240                if ({                  $self->{insertion_mode} = IN_TABLE_IM;
6241                     dd => 1, dt => 1, li => 1, p => 1,                  !!!next-token;
6242                     td => ($token->{tag_name} eq 'th'),                  next B;            
                    th => ($token->{tag_name} eq 'td'),  
                    tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!back-token;  
                 $token = {type => 'end tag',  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
   
               if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6243                }                }
6244                } elsif ($token->{tag_name} eq 'col') {
6245                splice @{$self->{open_elements}}, $i;                !!!cp ('t266');
6246                  !!!parse-error (type => 'unmatched end tag',
6247                $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});  
6248                ## Ignore the token                ## Ignore the token
6249                !!!next-token;                !!!next-token;
6250                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;  
6251              } else {              } else {
6252                #                !!!cp ('t267');
6253                  #
6254              }              }
6255          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6256            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6257                @{$self->{open_elements}} == 1) { # redundant, maybe
6258              !!!cp ('t270.2');
6259              ## Stop parsing.
6260              last B;
6261            } else {
6262              ## NOTE: As if </colgroup>.
6263              !!!cp ('t270.1');
6264              pop @{$self->{open_elements}}; # colgroup
6265              $self->{insertion_mode} = IN_TABLE_IM;
6266              ## Reprocess.
6267              next B;
6268            }
6269          } else {
6270            die "$0: $token->{type}: Unknown token type";
6271          }
6272    
6273              ## As if </colgroup>
6274              if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6275                !!!cp ('t269');
6276    ## TODO: Wrong error type?
6277                !!!parse-error (type => 'unmatched end tag',
6278                                text => 'colgroup', token => $token);
6279                ## Ignore the token
6280                !!!nack ('t269.1');
6281                !!!next-token;
6282                next B;
6283            } else {            } else {
6284              #              !!!cp ('t270');
6285                pop @{$self->{open_elements}}; # colgroup
6286                $self->{insertion_mode} = IN_TABLE_IM;
6287                !!!ack-later;
6288                ## reprocess
6289                next B;
6290              }
6291        } elsif ($self->{insertion_mode} & SELECT_IMS) {
6292          if ($token->{type} == CHARACTER_TOKEN) {
6293            !!!cp ('t271');
6294            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6295            !!!next-token;
6296            next B;
6297          } elsif ($token->{type} == START_TAG_TOKEN) {
6298            if ($token->{tag_name} eq 'option') {
6299              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6300                !!!cp ('t272');
6301                ## As if </option>
6302                pop @{$self->{open_elements}};
6303              } else {
6304                !!!cp ('t273');
6305            }            }
             
           $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}};  
               }  
6306    
6307                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6308                !!!next-token;            !!!nack ('t273.1');
6309                redo B;            !!!next-token;
6310              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6311                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6312                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6313                  pop @{$self->{open_elements}};              !!!cp ('t274');
6314                }              ## As if </option>
6315                pop @{$self->{open_elements}};
6316              } else {
6317                !!!cp ('t275');
6318              }
6319    
6320                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6321                  ## As if </optgroup>              !!!cp ('t276');
6322                  pop @{$self->{open_elements}};              ## As if </optgroup>
6323                }              pop @{$self->{open_elements}};
6324              } else {
6325                !!!cp ('t277');
6326              }
6327    
6328                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6329                !!!next-token;            !!!nack ('t277.1');
6330                redo B;            !!!next-token;
6331              } elsif ($token->{tag_name} eq 'select') {            next B;
6332                !!!parse-error (type => 'not closed:select');          } elsif ({
6333                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6334                ## have an element in table scope                   }->{$token->{tag_name}} or
6335                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6336                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6337                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6338                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6339                    $i = $_;                     tr => 1, td => 1, th => 1,
6340                    last INSCOPE;                    }->{$token->{tag_name}})) {
6341                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6342                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6343                           }->{$node->[1]}) {                            token => $token);
6344                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6345                  }            ## as if there were </select> (otherwise).
6346                } # INSCOPE            ## have an element in table scope
6347                unless (defined $i) {            my $i;
6348                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6349                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6350                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6351                  redo B;                !!!cp ('t278');
6352                }                $i = $_;
6353                  last INSCOPE;
6354                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6355                  !!!cp ('t279');
6356                  last INSCOPE;
6357                }
6358              } # INSCOPE
6359              unless (defined $i) {
6360                !!!cp ('t280');
6361                !!!parse-error (type => 'unmatched end tag',
6362                                text => 'select', token => $token);
6363                ## Ignore the token
6364                !!!nack ('t280.1');
6365                !!!next-token;
6366                next B;
6367              }
6368                                
6369                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6370              splice @{$self->{open_elements}}, $i;
6371    
6372                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6373    
6374                !!!next-token;            if ($token->{tag_name} eq 'select') {
6375                redo B;              !!!nack ('t281.2');
6376              } else {              !!!next-token;
6377                #              next B;
6378              } else {
6379                !!!cp ('t281.1');
6380                !!!ack-later;
6381                ## Reprocess the token.
6382                next B;
6383              }
6384            } else {
6385              !!!cp ('t282');
6386              !!!parse-error (type => 'in select',
6387                              text => $token->{tag_name}, token => $token);
6388              ## Ignore the token
6389              !!!nack ('t282.1');
6390              !!!next-token;
6391              next B;
6392            }
6393          } elsif ($token->{type} == END_TAG_TOKEN) {
6394            if ($token->{tag_name} eq 'optgroup') {
6395              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6396                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6397                !!!cp ('t283');
6398                ## As if </option>
6399                splice @{$self->{open_elements}}, -2;
6400              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6401                !!!cp ('t284');
6402                pop @{$self->{open_elements}};
6403              } else {
6404                !!!cp ('t285');
6405                !!!parse-error (type => 'unmatched end tag',
6406                                text => $token->{tag_name}, token => $token);
6407                ## Ignore the token
6408              }
6409              !!!nack ('t285.1');
6410              !!!next-token;
6411              next B;
6412            } elsif ($token->{tag_name} eq 'option') {
6413              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6414                !!!cp ('t286');
6415                pop @{$self->{open_elements}};
6416              } else {
6417                !!!cp ('t287');
6418                !!!parse-error (type => 'unmatched end tag',
6419                                text => $token->{tag_name}, token => $token);
6420                ## Ignore the token
6421              }
6422              !!!nack ('t287.1');
6423              !!!next-token;
6424              next B;
6425            } elsif ($token->{tag_name} eq 'select') {
6426              ## have an element in table scope
6427              my $i;
6428              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6429                my $node = $self->{open_elements}->[$_];
6430                if ($node->[1] & SELECT_EL) {
6431                  !!!cp ('t288');
6432                  $i = $_;
6433                  last INSCOPE;
6434                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6435                  !!!cp ('t289');
6436                  last INSCOPE;
6437              }              }
6438            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6439              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6440                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6441                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6442                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6443                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6444                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6445                  pop @{$self->{open_elements}};              !!!next-token;
6446                } else {              next B;
6447                  !!!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;  
               }  
6448                                
6449                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6450              splice @{$self->{open_elements}}, $i;
6451    
6452                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6453    
6454                !!!next-token;            !!!nack ('t291.1');
6455                redo B;            !!!next-token;
6456              } elsif ({            next B;
6457                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6458                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6459                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6460                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6461                                   }->{$token->{tag_name}}) {
6462                ## have an element in table scope  ## TODO: The following is wrong?
6463                my $i;            !!!parse-error (type => 'unmatched end tag',
6464                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;  
               }  
6465                                
6466                ## As if </select>            ## have an element in table scope
6467                ## have an element in table scope            my $i;
6468                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6469                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6470                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6471                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6472                    $i = $_;                $i = $_;
6473                    last INSCOPE;                last INSCOPE;
6474                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6475                            table => 1, html => 1,                !!!cp ('t293');
6476                           }->{$node->[1]}) {                last INSCOPE;
6477                    last INSCOPE;              }
6478                  }            } # INSCOPE
6479                } # INSCOPE            unless (defined $i) {
6480                unless (defined $i) {              !!!cp ('t294');
6481                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6482                  ## Ignore the </select> token              !!!nack ('t294.1');
6483                  !!!next-token; ## TODO: ok?              !!!next-token;
6484                  redo B;              next B;
6485                }            }
6486                                
6487                splice @{$self->{open_elements}}, $i;            ## As if </select>
6488              ## have an element in table scope
6489                $self->_reset_insertion_mode;            undef $i;
6490              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6491                ## reprocess              my $node = $self->{open_elements}->[$_];
6492                redo B;              if ($node->[1] & SELECT_EL) {
6493              } else {                !!!cp ('t295');
6494                #                $i = $_;
6495                  last INSCOPE;
6496                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6497    ## ISSUE: Can this state be reached?
6498                  !!!cp ('t296');
6499                  last INSCOPE;
6500              }              }
6501            } else {            } # INSCOPE
6502              #            unless (defined $i) {
6503                !!!cp ('t297');
6504    ## TODO: The following error type is correct?
6505                !!!parse-error (type => 'unmatched end tag',
6506                                text => 'select', token => $token);
6507                ## Ignore the </select> token
6508                !!!nack ('t297.1');
6509                !!!next-token; ## TODO: ok?
6510                next B;
6511            }            }
6512                  
6513              !!!cp ('t298');
6514              splice @{$self->{open_elements}}, $i;
6515    
6516              $self->_reset_insertion_mode;
6517    
6518            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!ack-later;
6519              ## reprocess
6520              next B;
6521            } else {
6522              !!!cp ('t299');
6523              !!!parse-error (type => 'in select:/',
6524                              text => $token->{tag_name}, token => $token);
6525            ## Ignore the token            ## Ignore the token
6526              !!!nack ('t299.3');
6527            !!!next-token;            !!!next-token;
6528            redo B;            next B;
6529          } elsif ($self->{insertion_mode} eq 'after body') {          }
6530            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6531              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6532                ## As if in body                  @{$self->{open_elements}} == 1) { # redundant, maybe
6533                $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t299.1');
6534              !!!parse-error (type => 'in body:#eof', token => $token);
6535            } else {
6536              !!!cp ('t299.2');
6537            }
6538    
6539            ## Stop parsing.
6540            last B;
6541          } else {
6542            die "$0: $token->{type}: Unknown token type";
6543          }
6544        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6545          if ($token->{type} == CHARACTER_TOKEN) {
6546            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6547              my $data = $1;
6548              ## As if in body
6549              $reconstruct_active_formatting_elements->($insert_to_current);
6550                                
6551                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6552              
6553              unless (length $token->{data}) {
6554                !!!cp ('t300');
6555                !!!next-token;
6556                next B;
6557              }
6558            }
6559            
6560            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6561              !!!cp ('t301');
6562              !!!parse-error (type => 'after html:#text', token => $token);
6563    
6564                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
6565                  !!!next-token;          } else {
6566                  redo B;            !!!cp ('t302');
6567                }          }
6568              }          
6569                        ## "after body" insertion mode
6570              #          !!!parse-error (type => 'after body:#text', token => $token);
6571              !!!parse-error (type => 'after body:#'.$token->{type});  
6572            } elsif ($token->{type} eq 'comment') {          $self->{insertion_mode} = IN_BODY_IM;
6573              my $comment = $self->{document}->create_comment ($token->{data});          ## reprocess
6574              $self->{open_elements}->[0]->[0]->append_child ($comment);          next B;
6575          } elsif ($token->{type} == START_TAG_TOKEN) {
6576            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6577              !!!cp ('t303');
6578              !!!parse-error (type => 'after html',
6579                              text => $token->{tag_name}, token => $token);
6580              
6581              ## Reprocess in the "after body" insertion mode.
6582            } else {
6583              !!!cp ('t304');
6584            }
6585    
6586            ## "after body" insertion mode
6587            !!!parse-error (type => 'after body',
6588                            text => $token->{tag_name}, token => $token);
6589    
6590            $self->{insertion_mode} = IN_BODY_IM;
6591            !!!ack-later;
6592            ## reprocess
6593            next B;
6594          } elsif ($token->{type} == END_TAG_TOKEN) {
6595            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6596              !!!cp ('t305');
6597              !!!parse-error (type => 'after html:/',
6598                              text => $token->{tag_name}, token => $token);
6599              
6600              $self->{insertion_mode} = AFTER_BODY_IM;
6601              ## Reprocess in the "after body" insertion mode.
6602            } else {
6603              !!!cp ('t306');
6604            }
6605    
6606            ## "after body" insertion mode
6607            if ($token->{tag_name} eq 'html') {
6608              if (defined $self->{inner_html_node}) {
6609                !!!cp ('t307');
6610                !!!parse-error (type => 'unmatched end tag',
6611                                text => 'html', token => $token);
6612                ## Ignore the token
6613              !!!next-token;              !!!next-token;
6614              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});  
             }  
6615            } else {            } else {
6616              !!!parse-error (type => 'after body:#'.$token->{type});              !!!cp ('t308');
6617                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6618                !!!next-token;
6619                next B;
6620            }            }
6621            } else {
6622              !!!cp ('t309');
6623              !!!parse-error (type => 'after body:/',
6624                              text => $token->{tag_name}, token => $token);
6625    
6626            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6627            ## reprocess            ## reprocess
6628            redo B;            next B;
6629          } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6630            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6631              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          !!!cp ('t309.2');
6632                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          ## Stop parsing
6633            last B;
6634                unless (length $token->{data}) {        } else {
6635                  !!!next-token;          die "$0: $token->{type}: Unknown token type";
6636                  redo B;        }
6637                }      } elsif ($self->{insertion_mode} & FRAME_IMS) {
6638              }        if ($token->{type} == CHARACTER_TOKEN) {
6639            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6640              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6641              
6642              unless (length $token->{data}) {
6643                !!!cp ('t310');
6644                !!!next-token;
6645                next B;
6646              }
6647            }
6648            
6649            if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6650              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6651                !!!cp ('t311');
6652                !!!parse-error (type => 'in frameset:#text', token => $token);
6653              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6654                !!!cp ('t312');
6655                !!!parse-error (type => 'after frameset:#text', token => $token);
6656              } else { # "after after frameset"
6657                !!!cp ('t313');
6658                !!!parse-error (type => 'after html:#text', token => $token);
6659              }
6660              
6661              ## Ignore the token.
6662              if (length $token->{data}) {
6663                !!!cp ('t314');
6664                ## reprocess the rest of characters
6665              } else {
6666                !!!cp ('t315');
6667                !!!next-token;
6668              }
6669              next B;
6670            }
6671            
6672            die qq[$0: Character "$token->{data}"];
6673          } elsif ($token->{type} == START_TAG_TOKEN) {
6674            if ($token->{tag_name} eq 'frameset' and
6675                $self->{insertion_mode} == IN_FRAMESET_IM) {
6676              !!!cp ('t318');
6677              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6678              !!!nack ('t318.1');
6679              !!!next-token;
6680              next B;
6681            } elsif ($token->{tag_name} eq 'frame' and
6682                     $self->{insertion_mode} == IN_FRAMESET_IM) {
6683              !!!cp ('t319');
6684              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6685              pop @{$self->{open_elements}};
6686              !!!ack ('t319.1');
6687              !!!next-token;
6688              next B;
6689            } elsif ($token->{tag_name} eq 'noframes') {
6690              !!!cp ('t320');
6691              ## NOTE: As if in head.
6692              $parse_rcdata->(CDATA_CONTENT_MODEL);
6693              next B;
6694    
6695              #            ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6696            } elsif ($token->{type} eq 'comment') {            ## has no parse error.
6697              my $comment = $self->{document}->create_comment ($token->{data});          } else {
6698              $self->{open_elements}->[-1]->[0]->append_child ($comment);            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6699                !!!cp ('t321');
6700                !!!parse-error (type => 'in frameset',
6701                                text => $token->{tag_name}, token => $token);
6702              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6703                !!!cp ('t322');
6704                !!!parse-error (type => 'after frameset',
6705                                text => $token->{tag_name}, token => $token);
6706              } else { # "after after frameset"
6707                !!!cp ('t322.2');
6708                !!!parse-error (type => 'after after frameset',
6709                                text => $token->{tag_name}, token => $token);
6710              }
6711              ## Ignore the token
6712              !!!nack ('t322.1');
6713              !!!next-token;
6714              next B;
6715            }
6716          } elsif ($token->{type} == END_TAG_TOKEN) {
6717            if ($token->{tag_name} eq 'frameset' and
6718                $self->{insertion_mode} == IN_FRAMESET_IM) {
6719              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6720                  @{$self->{open_elements}} == 1) {
6721                !!!cp ('t325');
6722                !!!parse-error (type => 'unmatched end tag',
6723                                text => $token->{tag_name}, token => $token);
6724                ## Ignore the token
6725              !!!next-token;              !!!next-token;
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'frame') {  
               !!!insert-element ($token->{tag_name}, $token->{attributes});  
               pop @{$self->{open_elements}};  
               !!!next-token;  
               redo B;  
             } elsif ($token->{tag_name} eq 'noframes') {  
               $in_body->($insert_to_current);  
               redo B;  
             } else {  
               #  
             }  
           } elsif ($token->{type} eq 'end tag') {  
             if ($token->{tag_name} eq 'frameset') {  
               if ($self->{open_elements}->[-1]->[1] eq 'html' and  
                   @{$self->{open_elements}} == 1) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
               } else {  
                 pop @{$self->{open_elements}};  
                 !!!next-token;  
               }  
                 
               ## if not inner_html and  
               if ($self->{open_elements}->[-1]->[1] ne 'frameset') {  
                 $self->{insertion_mode} = 'after frameset';  
               }  
               redo B;  
             } else {  
               #  
             }  
6726            } else {            } else {
6727              #              !!!cp ('t326');
6728                pop @{$self->{open_elements}};
6729                !!!next-token;
6730            }            }
6731              
6732            if (defined $token->{tag_name}) {            if (not defined $self->{inner_html_node} and
6733              !!!parse-error (type => 'in frameset:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6734                !!!cp ('t327');
6735                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6736            } else {            } else {
6737              !!!parse-error (type => 'in frameset:#'.$token->{type});              !!!cp ('t328');
6738              }
6739              next B;
6740            } elsif ($token->{tag_name} eq 'html' and
6741                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6742              !!!cp ('t329');
6743              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6744              !!!next-token;
6745              next B;
6746            } else {
6747              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6748                !!!cp ('t330');
6749                !!!parse-error (type => 'in frameset:/',
6750                                text => $token->{tag_name}, token => $token);
6751              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6752                !!!cp ('t330.1');
6753                !!!parse-error (type => 'after frameset:/',
6754                                text => $token->{tag_name}, token => $token);
6755              } else { # "after after html"
6756                !!!cp ('t331');
6757                !!!parse-error (type => 'after after frameset:/',
6758                                text => $token->{tag_name}, token => $token);
6759            }            }
6760            ## Ignore the token            ## Ignore the token
6761            !!!next-token;            !!!next-token;
6762            redo B;            next B;
6763          } elsif ($self->{insertion_mode} eq 'after frameset') {          }
6764            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6765              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6766                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});                  @{$self->{open_elements}} == 1) { # redundant, maybe
6767              !!!cp ('t331.1');
6768              !!!parse-error (type => 'in body:#eof', token => $token);
6769            } else {
6770              !!!cp ('t331.2');
6771            }
6772            
6773            ## Stop parsing
6774            last B;
6775          } else {
6776            die "$0: $token->{type}: Unknown token type";
6777          }
6778    
6779                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6780                  !!!next-token;      } else {
6781                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6782                }      }
6783    
6784        ## "in body" insertion mode
6785        if ($token->{type} == START_TAG_TOKEN) {
6786          if ($token->{tag_name} eq 'script') {
6787            !!!cp ('t332');
6788            ## NOTE: This is an "as if in head" code clone
6789            $script_start_tag->();
6790            next B;
6791          } elsif ($token->{tag_name} eq 'style') {
6792            !!!cp ('t333');
6793            ## NOTE: This is an "as if in head" code clone
6794            $parse_rcdata->(CDATA_CONTENT_MODEL);
6795            next B;
6796          } elsif ({
6797                    base => 1, link => 1,
6798                   }->{$token->{tag_name}}) {
6799            !!!cp ('t334');
6800            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6801            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6802            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6803            !!!ack ('t334.1');
6804            !!!next-token;
6805            next B;
6806          } elsif ($token->{tag_name} eq 'meta') {
6807            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6808            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6809            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6810    
6811            unless ($self->{confident}) {
6812              if ($token->{attributes}->{charset}) {
6813                !!!cp ('t335');
6814                ## NOTE: Whether the encoding is supported or not is handled
6815                ## in the {change_encoding} callback.
6816                $self->{change_encoding}
6817                    ->($self, $token->{attributes}->{charset}->{value}, $token);
6818                
6819                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6820                    ->set_user_data (manakai_has_reference =>
6821                                         $token->{attributes}->{charset}
6822                                             ->{has_reference});
6823              } elsif ($token->{attributes}->{content}) {
6824                if ($token->{attributes}->{content}->{value}
6825                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6826                        [\x09-\x0D\x20]*=
6827                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6828                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6829                  !!!cp ('t336');
6830                  ## NOTE: Whether the encoding is supported or not is handled
6831                  ## in the {change_encoding} callback.
6832                  $self->{change_encoding}
6833                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6834                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6835                      ->set_user_data (manakai_has_reference =>
6836                                           $token->{attributes}->{content}
6837                                                 ->{has_reference});
6838              }              }
6839              }
6840            } else {
6841              if ($token->{attributes}->{charset}) {
6842                !!!cp ('t337');
6843                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6844                    ->set_user_data (manakai_has_reference =>
6845                                         $token->{attributes}->{charset}
6846                                             ->{has_reference});
6847              }
6848              if ($token->{attributes}->{content}) {
6849                !!!cp ('t338');
6850                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6851                    ->set_user_data (manakai_has_reference =>
6852                                         $token->{attributes}->{content}
6853                                             ->{has_reference});
6854              }
6855            }
6856    
6857              #          !!!ack ('t338.1');
6858            } elsif ($token->{type} eq 'comment') {          !!!next-token;
6859              my $comment = $self->{document}->create_comment ($token->{data});          next B;
6860              $self->{open_elements}->[-1]->[0]->append_child ($comment);        } elsif ($token->{tag_name} eq 'title') {
6861              !!!next-token;          !!!cp ('t341');
6862              redo B;          ## NOTE: This is an "as if in head" code clone
6863            } elsif ($token->{type} eq 'start tag') {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6864              if ($token->{tag_name} eq 'noframes') {          next B;
6865                $in_body->($insert_to_current);        } elsif ($token->{tag_name} eq 'body') {
6866                redo B;          !!!parse-error (type => 'in body', text => 'body', token => $token);
6867              } else {                
6868                #          if (@{$self->{open_elements}} == 1 or
6869                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6870              !!!cp ('t342');
6871              ## Ignore the token
6872            } else {
6873              my $body_el = $self->{open_elements}->[1]->[0];
6874              for my $attr_name (keys %{$token->{attributes}}) {
6875                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6876                  !!!cp ('t343');
6877                  $body_el->set_attribute_ns
6878                    (undef, [undef, $attr_name],
6879                     $token->{attributes}->{$attr_name}->{value});
6880              }              }
6881            } elsif ($token->{type} eq 'end tag') {            }
6882              if ($token->{tag_name} eq 'html') {          }
6883                $phase = 'trailing end';          !!!nack ('t343.1');
6884            !!!next-token;
6885            next B;
6886          } elsif ({
6887                    address => 1, blockquote => 1, center => 1, dir => 1,
6888                    div => 1, dl => 1, fieldset => 1,
6889                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6890                    menu => 1, ol => 1, p => 1, ul => 1,
6891                    pre => 1, listing => 1,
6892                    form => 1,
6893                    table => 1,
6894                    hr => 1,
6895                   }->{$token->{tag_name}}) {
6896            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6897              !!!cp ('t350');
6898              !!!parse-error (type => 'in form:form', token => $token);
6899              ## Ignore the token
6900              !!!nack ('t350.1');
6901              !!!next-token;
6902              next B;
6903            }
6904    
6905            ## has a p element in scope
6906            INSCOPE: for (reverse @{$self->{open_elements}}) {
6907              if ($_->[1] & P_EL) {
6908                !!!cp ('t344');
6909                !!!back-token; # <form>
6910                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6911                          line => $token->{line}, column => $token->{column}};
6912                next B;
6913              } elsif ($_->[1] & SCOPING_EL) {
6914                !!!cp ('t345');
6915                last INSCOPE;
6916              }
6917            } # INSCOPE
6918              
6919            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6920            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6921              !!!nack ('t346.1');
6922              !!!next-token;
6923              if ($token->{type} == CHARACTER_TOKEN) {
6924                $token->{data} =~ s/^\x0A//;
6925                unless (length $token->{data}) {
6926                  !!!cp ('t346');
6927                !!!next-token;                !!!next-token;
               redo B;  
6928              } else {              } else {
6929                #                !!!cp ('t349');
6930              }              }
6931            } else {            } else {
6932              #              !!!cp ('t348');
6933              }
6934            } elsif ($token->{tag_name} eq 'form') {
6935              !!!cp ('t347.1');
6936              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6937    
6938              !!!nack ('t347.2');
6939              !!!next-token;
6940            } elsif ($token->{tag_name} eq 'table') {
6941              !!!cp ('t382');
6942              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6943              
6944              $self->{insertion_mode} = IN_TABLE_IM;
6945    
6946              !!!nack ('t382.1');
6947              !!!next-token;
6948            } elsif ($token->{tag_name} eq 'hr') {
6949              !!!cp ('t386');
6950              pop @{$self->{open_elements}};
6951            
6952              !!!nack ('t386.1');
6953              !!!next-token;
6954            } else {
6955              !!!nack ('t347.1');
6956              !!!next-token;
6957            }
6958            next B;
6959          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6960            ## has a p element in scope
6961            INSCOPE: for (reverse @{$self->{open_elements}}) {
6962              if ($_->[1] & P_EL) {
6963                !!!cp ('t353');
6964                !!!back-token; # <x>
6965                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6966                          line => $token->{line}, column => $token->{column}};
6967                next B;
6968              } elsif ($_->[1] & SCOPING_EL) {
6969                !!!cp ('t354');
6970                last INSCOPE;
6971            }            }
6972            } # INSCOPE
6973                        
6974            if (defined $token->{tag_name}) {          ## Step 1
6975              !!!parse-error (type => 'after frameset:'.($token->{tag_name} eq 'end tag' ? '/' : '').$token->{tag_name});          my $i = -1;
6976            my $node = $self->{open_elements}->[$i];
6977            my $li_or_dtdd = {li => {li => 1},
6978                              dt => {dt => 1, dd => 1},
6979                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6980            LI: {
6981              ## Step 2
6982              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6983                if ($i != -1) {
6984                  !!!cp ('t355');
6985                  !!!parse-error (type => 'not closed',
6986                                  text => $self->{open_elements}->[-1]->[0]
6987                                      ->manakai_local_name,
6988                                  token => $token);
6989                } else {
6990                  !!!cp ('t356');
6991                }
6992                splice @{$self->{open_elements}}, $i;
6993                last LI;
6994            } else {            } else {
6995              !!!parse-error (type => 'after frameset:#'.$token->{type});              !!!cp ('t357');
6996              }
6997              
6998              ## Step 3
6999              if (not ($node->[1] & FORMATTING_EL) and
7000                  #not $phrasing_category->{$node->[1]} and
7001                  ($node->[1] & SPECIAL_EL or
7002                   $node->[1] & SCOPING_EL) and
7003                  not ($node->[1] & ADDRESS_EL) and
7004                  not ($node->[1] & DIV_EL)) {
7005                !!!cp ('t358');
7006                last LI;
7007            }            }
7008              
7009              !!!cp ('t359');
7010              ## Step 4
7011              $i--;
7012              $node = $self->{open_elements}->[$i];
7013              redo LI;
7014            } # LI
7015              
7016            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7017            !!!nack ('t359.1');
7018            !!!next-token;
7019            next B;
7020          } elsif ($token->{tag_name} eq 'plaintext') {
7021            ## has a p element in scope
7022            INSCOPE: for (reverse @{$self->{open_elements}}) {
7023              if ($_->[1] & P_EL) {
7024                !!!cp ('t367');
7025                !!!back-token; # <plaintext>
7026                $token = {type => END_TAG_TOKEN, tag_name => 'p',
7027                          line => $token->{line}, column => $token->{column}};
7028                next B;
7029              } elsif ($_->[1] & SCOPING_EL) {
7030                !!!cp ('t368');
7031                last INSCOPE;
7032              }
7033            } # INSCOPE
7034              
7035            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7036              
7037            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7038              
7039            !!!nack ('t368.1');
7040            !!!next-token;
7041            next B;
7042          } elsif ($token->{tag_name} eq 'a') {
7043            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7044              my $node = $active_formatting_elements->[$i];
7045              if ($node->[1] & A_EL) {
7046                !!!cp ('t371');
7047                !!!parse-error (type => 'in a:a', token => $token);
7048                
7049                !!!back-token; # <a>
7050                $token = {type => END_TAG_TOKEN, tag_name => 'a',
7051                          line => $token->{line}, column => $token->{column}};
7052                $formatting_end_tag->($token);
7053                
7054                AFE2: for (reverse 0..$#$active_formatting_elements) {
7055                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7056                    !!!cp ('t372');
7057                    splice @$active_formatting_elements, $_, 1;
7058                    last AFE2;
7059                  }
7060                } # AFE2
7061                OE: for (reverse 0..$#{$self->{open_elements}}) {
7062                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7063                    !!!cp ('t373');
7064                    splice @{$self->{open_elements}}, $_, 1;
7065                    last OE;
7066                  }
7067                } # OE
7068                last AFE;
7069              } elsif ($node->[0] eq '#marker') {
7070                !!!cp ('t374');
7071                last AFE;
7072              }
7073            } # AFE
7074              
7075            $reconstruct_active_formatting_elements->($insert_to_current);
7076    
7077            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7078            push @$active_formatting_elements, $self->{open_elements}->[-1];
7079    
7080            !!!nack ('t374.1');
7081            !!!next-token;
7082            next B;
7083          } elsif ($token->{tag_name} eq 'nobr') {
7084            $reconstruct_active_formatting_elements->($insert_to_current);
7085    
7086            ## has a |nobr| element in scope
7087            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7088              my $node = $self->{open_elements}->[$_];
7089              if ($node->[1] & NOBR_EL) {
7090                !!!cp ('t376');
7091                !!!parse-error (type => 'in nobr:nobr', token => $token);
7092                !!!back-token; # <nobr>
7093                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7094                          line => $token->{line}, column => $token->{column}};
7095                next B;
7096              } elsif ($node->[1] & SCOPING_EL) {
7097                !!!cp ('t377');
7098                last INSCOPE;
7099              }
7100            } # INSCOPE
7101            
7102            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7103            push @$active_formatting_elements, $self->{open_elements}->[-1];
7104            
7105            !!!nack ('t377.1');
7106            !!!next-token;
7107            next B;
7108          } elsif ($token->{tag_name} eq 'button') {
7109            ## has a button element in scope
7110            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7111              my $node = $self->{open_elements}->[$_];
7112              if ($node->[1] & BUTTON_EL) {
7113                !!!cp ('t378');
7114                !!!parse-error (type => 'in button:button', token => $token);
7115                !!!back-token; # <button>
7116                $token = {type => END_TAG_TOKEN, tag_name => 'button',
7117                          line => $token->{line}, column => $token->{column}};
7118                next B;
7119              } elsif ($node->[1] & SCOPING_EL) {
7120                !!!cp ('t379');
7121                last INSCOPE;
7122              }
7123            } # INSCOPE
7124              
7125            $reconstruct_active_formatting_elements->($insert_to_current);
7126              
7127            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7128    
7129            ## TODO: associate with $self->{form_element} if defined
7130    
7131            push @$active_formatting_elements, ['#marker', ''];
7132    
7133            !!!nack ('t379.1');
7134            !!!next-token;
7135            next B;
7136          } elsif ({
7137                    xmp => 1,
7138                    iframe => 1,
7139                    noembed => 1,
7140                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7141                    noscript => 0, ## TODO: 1 if scripting is enabled
7142                   }->{$token->{tag_name}}) {
7143            if ($token->{tag_name} eq 'xmp') {
7144              !!!cp ('t381');
7145              $reconstruct_active_formatting_elements->($insert_to_current);
7146            } else {
7147              !!!cp ('t399');
7148            }
7149            ## NOTE: There is an "as if in body" code clone.
7150            $parse_rcdata->(CDATA_CONTENT_MODEL);
7151            next B;
7152          } elsif ($token->{tag_name} eq 'isindex') {
7153            !!!parse-error (type => 'isindex', token => $token);
7154            
7155            if (defined $self->{form_element}) {
7156              !!!cp ('t389');
7157            ## Ignore the token            ## Ignore the token
7158              !!!nack ('t389'); ## NOTE: Not acknowledged.
7159            !!!next-token;            !!!next-token;
7160            redo B;            next B;
7161            } else {
7162              !!!ack ('t391.1');
7163    
7164            ## ISSUE: An issue in spec there            my $at = $token->{attributes};
7165              my $form_attrs;
7166              $form_attrs->{action} = $at->{action} if $at->{action};
7167              my $prompt_attr = $at->{prompt};
7168              $at->{name} = {name => 'name', value => 'isindex'};
7169              delete $at->{action};
7170              delete $at->{prompt};
7171              my @tokens = (
7172                            {type => START_TAG_TOKEN, tag_name => 'form',
7173                             attributes => $form_attrs,
7174                             line => $token->{line}, column => $token->{column}},
7175                            {type => START_TAG_TOKEN, tag_name => 'hr',
7176                             line => $token->{line}, column => $token->{column}},
7177                            {type => START_TAG_TOKEN, tag_name => 'p',
7178                             line => $token->{line}, column => $token->{column}},
7179                            {type => START_TAG_TOKEN, tag_name => 'label',
7180                             line => $token->{line}, column => $token->{column}},
7181                           );
7182              if ($prompt_attr) {
7183                !!!cp ('t390');
7184                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7185                               #line => $token->{line}, column => $token->{column},
7186                              };
7187              } else {
7188                !!!cp ('t391');
7189                push @tokens, {type => CHARACTER_TOKEN,
7190                               data => 'This is a searchable index. Insert your search keywords here: ',
7191                               #line => $token->{line}, column => $token->{column},
7192                              }; # SHOULD
7193                ## TODO: make this configurable
7194              }
7195              push @tokens,
7196                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7197                             line => $token->{line}, column => $token->{column}},
7198                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7199                            {type => END_TAG_TOKEN, tag_name => 'label',
7200                             line => $token->{line}, column => $token->{column}},
7201                            {type => END_TAG_TOKEN, tag_name => 'p',
7202                             line => $token->{line}, column => $token->{column}},
7203                            {type => START_TAG_TOKEN, tag_name => 'hr',
7204                             line => $token->{line}, column => $token->{column}},
7205                            {type => END_TAG_TOKEN, tag_name => 'form',
7206                             line => $token->{line}, column => $token->{column}};
7207              !!!back-token (@tokens);
7208              !!!next-token;
7209              next B;
7210            }
7211          } elsif ($token->{tag_name} eq 'textarea') {
7212            my $tag_name = $token->{tag_name};
7213            my $el;
7214            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7215            
7216            ## TODO: $self->{form_element} if defined
7217            $self->{content_model} = RCDATA_CONTENT_MODEL;
7218            delete $self->{escape}; # MUST
7219            
7220            $insert->($el);
7221            
7222            my $text = '';
7223            !!!nack ('t392.1');
7224            !!!next-token;
7225            if ($token->{type} == CHARACTER_TOKEN) {
7226              $token->{data} =~ s/^\x0A//;
7227              unless (length $token->{data}) {
7228                !!!cp ('t392');
7229                !!!next-token;
7230              } else {
7231                !!!cp ('t393');
7232              }
7233          } else {          } else {
7234            die "$0: $self->{insertion_mode}: Unknown insertion mode";            !!!cp ('t394');
7235            }
7236            while ($token->{type} == CHARACTER_TOKEN) {
7237              !!!cp ('t395');
7238              $text .= $token->{data};
7239              !!!next-token;
7240            }
7241            if (length $text) {
7242              !!!cp ('t396');
7243              $el->manakai_append_text ($text);
7244            }
7245            
7246            $self->{content_model} = PCDATA_CONTENT_MODEL;
7247            
7248            if ($token->{type} == END_TAG_TOKEN and
7249                $token->{tag_name} eq $tag_name) {
7250              !!!cp ('t397');
7251              ## Ignore the token
7252            } else {
7253              !!!cp ('t398');
7254              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7255          }          }
       }  
     } 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  
7256          !!!next-token;          !!!next-token;
7257          redo B;          next B;
7258        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{tag_name} eq 'rt' or
7259          my $comment = $self->{document}->create_comment ($token->{data});                 $token->{tag_name} eq 'rp') {
7260          $self->{document}->append_child ($comment);          ## has a |ruby| element in scope
7261            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7262              my $node = $self->{open_elements}->[$_];
7263              if ($node->[1] & RUBY_EL) {
7264                !!!cp ('t398.1');
7265                ## generate implied end tags
7266                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7267                  !!!cp ('t398.2');
7268                  pop @{$self->{open_elements}};
7269                }
7270                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7271                  !!!cp ('t398.3');
7272                  !!!parse-error (type => 'not closed',
7273                                  text => $self->{open_elements}->[-1]->[0]
7274                                      ->manakai_local_name,
7275                                  token => $token);
7276                  pop @{$self->{open_elements}}
7277                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7278                }
7279                last INSCOPE;
7280              } elsif ($node->[1] & SCOPING_EL) {
7281                !!!cp ('t398.4');
7282                last INSCOPE;
7283              }
7284            } # INSCOPE
7285    
7286            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7287    
7288            !!!nack ('t398.5');
7289          !!!next-token;          !!!next-token;
7290          redo B;          redo B;
7291        } elsif ($token->{type} eq 'character') {        } elsif ($token->{tag_name} eq 'math' or
7292          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {                 $token->{tag_name} eq 'svg') {
7293            my $data = $1;          $reconstruct_active_formatting_elements->($insert_to_current);
7294            ## As if in the main phase.  
7295            ## NOTE: The insertion mode in the main phase          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7296            ## just before the phase has been changed to the trailing  
7297            ## end phase is either "after body" or "after frameset".          ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7298            $reconstruct_active_formatting_elements->($insert_to_current)  
7299              if $phase eq 'main';          ## "adjust foreign attributes" - done in insert-element-f
7300            
7301            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7302            
7303            if ($self->{self_closing}) {
7304              pop @{$self->{open_elements}};
7305              !!!ack ('t398.1');
7306            } else {
7307              !!!cp ('t398.2');
7308              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7309              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7310              ## mode, "in body" (not "in foreign content") secondary insertion
7311              ## mode, maybe.
7312            }
7313    
7314            !!!next-token;
7315            next B;
7316          } elsif ({
7317                    caption => 1, col => 1, colgroup => 1, frame => 1,
7318                    frameset => 1, head => 1, option => 1, optgroup => 1,
7319                    tbody => 1, td => 1, tfoot => 1, th => 1,
7320                    thead => 1, tr => 1,
7321                   }->{$token->{tag_name}}) {
7322            !!!cp ('t401');
7323            !!!parse-error (type => 'in body',
7324                            text => $token->{tag_name}, token => $token);
7325            ## Ignore the token
7326            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7327            !!!next-token;
7328            next B;
7329            
7330            ## ISSUE: An issue on HTML5 new elements in the spec.
7331          } else {
7332            if ($token->{tag_name} eq 'image') {
7333              !!!cp ('t384');
7334              !!!parse-error (type => 'image', token => $token);
7335              $token->{tag_name} = 'img';
7336            } else {
7337              !!!cp ('t385');
7338            }
7339    
7340            ## NOTE: There is an "as if <br>" code clone.
7341            $reconstruct_active_formatting_elements->($insert_to_current);
7342            
7343            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7344    
7345            if ({
7346                 applet => 1, marquee => 1, object => 1,
7347                }->{$token->{tag_name}}) {
7348              !!!cp ('t380');
7349              push @$active_formatting_elements, ['#marker', ''];
7350              !!!nack ('t380.1');
7351            } elsif ({
7352                      b => 1, big => 1, em => 1, font => 1, i => 1,
7353                      s => 1, small => 1, strile => 1,
7354                      strong => 1, tt => 1, u => 1,
7355                     }->{$token->{tag_name}}) {
7356              !!!cp ('t375');
7357              push @$active_formatting_elements, $self->{open_elements}->[-1];
7358              !!!nack ('t375.1');
7359            } elsif ($token->{tag_name} eq 'input') {
7360              !!!cp ('t388');
7361              ## TODO: associate with $self->{form_element} if defined
7362              pop @{$self->{open_elements}};
7363              !!!ack ('t388.2');
7364            } elsif ({
7365                      area => 1, basefont => 1, bgsound => 1, br => 1,
7366                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7367                      #image => 1,
7368                     }->{$token->{tag_name}}) {
7369              !!!cp ('t388.1');
7370              pop @{$self->{open_elements}};
7371              !!!ack ('t388.3');
7372            } elsif ($token->{tag_name} eq 'select') {
7373              ## TODO: associate with $self->{form_element} if defined
7374            
7375              if ($self->{insertion_mode} & TABLE_IMS or
7376                  $self->{insertion_mode} & BODY_TABLE_IMS or
7377                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7378                !!!cp ('t400.1');
7379                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7380              } else {
7381                !!!cp ('t400.2');
7382                $self->{insertion_mode} = IN_SELECT_IM;
7383              }
7384              !!!nack ('t400.3');
7385            } else {
7386              !!!nack ('t402');
7387            }
7388            
7389            !!!next-token;
7390            next B;
7391          }
7392        } elsif ($token->{type} == END_TAG_TOKEN) {
7393          if ($token->{tag_name} eq 'body') {
7394            ## has a |body| element in scope
7395            my $i;
7396            INSCOPE: {
7397              for (reverse @{$self->{open_elements}}) {
7398                if ($_->[1] & BODY_EL) {
7399                  !!!cp ('t405');
7400                  $i = $_;
7401                  last INSCOPE;
7402                } elsif ($_->[1] & SCOPING_EL) {
7403                  !!!cp ('t405.1');
7404                  last;
7405                }
7406              }
7407    
7408              !!!parse-error (type => 'start tag not allowed',
7409                              text => $token->{tag_name}, token => $token);
7410              ## NOTE: Ignore the token.
7411              !!!next-token;
7412              next B;
7413            } # INSCOPE
7414    
7415            for (@{$self->{open_elements}}) {
7416              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7417                !!!cp ('t403');
7418                !!!parse-error (type => 'not closed',
7419                                text => $_->[0]->manakai_local_name,
7420                                token => $token);
7421                last;
7422              } else {
7423                !!!cp ('t404');
7424              }
7425            }
7426    
7427            $self->{insertion_mode} = AFTER_BODY_IM;
7428            !!!next-token;
7429            next B;
7430          } elsif ($token->{tag_name} eq 'html') {
7431            ## TODO: Update this code.  It seems that the code below is not
7432            ## up-to-date, though it has same effect as speced.
7433            if (@{$self->{open_elements}} > 1 and
7434                $self->{open_elements}->[1]->[1] & BODY_EL) {
7435              ## ISSUE: There is an issue in the spec.
7436              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7437                !!!cp ('t406');
7438                !!!parse-error (type => 'not closed',
7439                                text => $self->{open_elements}->[1]->[0]
7440                                    ->manakai_local_name,
7441                                token => $token);
7442              } else {
7443                !!!cp ('t407');
7444              }
7445              $self->{insertion_mode} = AFTER_BODY_IM;
7446              ## reprocess
7447              next B;
7448            } else {
7449              !!!cp ('t408');
7450              !!!parse-error (type => 'unmatched end tag',
7451                              text => $token->{tag_name}, token => $token);
7452              ## Ignore the token
7453              !!!next-token;
7454              next B;
7455            }
7456          } elsif ({
7457                    address => 1, blockquote => 1, center => 1, dir => 1,
7458                    div => 1, dl => 1, fieldset => 1, listing => 1,
7459                    menu => 1, ol => 1, pre => 1, ul => 1,
7460                    dd => 1, dt => 1, li => 1,
7461                    applet => 1, button => 1, marquee => 1, object => 1,
7462                   }->{$token->{tag_name}}) {
7463            ## has an element in scope
7464            my $i;
7465            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7466              my $node = $self->{open_elements}->[$_];
7467              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7468                !!!cp ('t410');
7469                $i = $_;
7470                last INSCOPE;
7471              } elsif ($node->[1] & SCOPING_EL) {
7472                !!!cp ('t411');
7473                last INSCOPE;
7474              }
7475            } # INSCOPE
7476    
7477            unless (defined $i) { # has an element in scope
7478              !!!cp ('t413');
7479              !!!parse-error (type => 'unmatched end tag',
7480                              text => $token->{tag_name}, token => $token);
7481              ## NOTE: Ignore the token.
7482            } else {
7483              ## Step 1. generate implied end tags
7484              while ({
7485                      ## END_TAG_OPTIONAL_EL
7486                      dd => ($token->{tag_name} ne 'dd'),
7487                      dt => ($token->{tag_name} ne 'dt'),
7488                      li => ($token->{tag_name} ne 'li'),
7489                      p => 1,
7490                      rt => 1,
7491                      rp => 1,
7492                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7493                !!!cp ('t409');
7494                pop @{$self->{open_elements}};
7495              }
7496    
7497              ## Step 2.
7498              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7499                      ne $token->{tag_name}) {
7500                !!!cp ('t412');
7501                !!!parse-error (type => 'not closed',
7502                                text => $self->{open_elements}->[-1]->[0]
7503                                    ->manakai_local_name,
7504                                token => $token);
7505              } else {
7506                !!!cp ('t414');
7507              }
7508    
7509              ## Step 3.
7510              splice @{$self->{open_elements}}, $i;
7511    
7512              ## Step 4.
7513              $clear_up_to_marker->()
7514                  if {
7515                    applet => 1, button => 1, marquee => 1, object => 1,
7516                  }->{$token->{tag_name}};
7517            }
7518            !!!next-token;
7519            next B;
7520          } elsif ($token->{tag_name} eq 'form') {
7521            undef $self->{form_element};
7522    
7523            ## has an element in scope
7524            my $i;
7525            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7526              my $node = $self->{open_elements}->[$_];
7527              if ($node->[1] & FORM_EL) {
7528                !!!cp ('t418');
7529                $i = $_;
7530                last INSCOPE;
7531              } elsif ($node->[1] & SCOPING_EL) {
7532                !!!cp ('t419');
7533                last INSCOPE;
7534              }
7535            } # INSCOPE
7536    
7537            unless (defined $i) { # has an element in scope
7538              !!!cp ('t421');
7539              !!!parse-error (type => 'unmatched end tag',
7540                              text => $token->{tag_name}, token => $token);
7541              ## NOTE: Ignore the token.
7542            } else {
7543              ## Step 1. generate implied end tags
7544              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7545                !!!cp ('t417');
7546                pop @{$self->{open_elements}};
7547              }
7548                        
7549            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);            ## Step 2.
7550              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7551                      ne $token->{tag_name}) {
7552                !!!cp ('t417.1');
7553                !!!parse-error (type => 'not closed',
7554                                text => $self->{open_elements}->[-1]->[0]
7555                                    ->manakai_local_name,
7556                                token => $token);
7557              } else {
7558                !!!cp ('t420');
7559              }  
7560                        
7561            unless (length $token->{data}) {            ## Step 3.
7562              !!!next-token;            splice @{$self->{open_elements}}, $i;
7563              redo B;          }
7564    
7565            !!!next-token;
7566            next B;
7567          } elsif ({
7568                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7569                   }->{$token->{tag_name}}) {
7570            ## has an element in scope
7571            my $i;
7572            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7573              my $node = $self->{open_elements}->[$_];
7574              if ($node->[1] & HEADING_EL) {
7575                !!!cp ('t423');
7576                $i = $_;
7577                last INSCOPE;
7578              } elsif ($node->[1] & SCOPING_EL) {
7579                !!!cp ('t424');
7580                last INSCOPE;
7581              }
7582            } # INSCOPE
7583    
7584            unless (defined $i) { # has an element in scope
7585              !!!cp ('t425.1');
7586              !!!parse-error (type => 'unmatched end tag',
7587                              text => $token->{tag_name}, token => $token);
7588              ## NOTE: Ignore the token.
7589            } else {
7590              ## Step 1. generate implied end tags
7591              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7592                !!!cp ('t422');
7593                pop @{$self->{open_elements}};
7594              }
7595              
7596              ## Step 2.
7597              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7598                      ne $token->{tag_name}) {
7599                !!!cp ('t425');
7600                !!!parse-error (type => 'unmatched end tag',
7601                                text => $token->{tag_name}, token => $token);
7602              } else {
7603                !!!cp ('t426');
7604            }            }
7605    
7606              ## Step 3.
7607              splice @{$self->{open_elements}}, $i;
7608          }          }
7609            
7610            !!!next-token;
7611            next B;
7612          } elsif ($token->{tag_name} eq 'p') {
7613            ## has an element in scope
7614            my $i;
7615            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7616              my $node = $self->{open_elements}->[$_];
7617              if ($node->[1] & P_EL) {
7618                !!!cp ('t410.1');
7619                $i = $_;
7620                last INSCOPE;
7621              } elsif ($node->[1] & SCOPING_EL) {
7622                !!!cp ('t411.1');
7623                last INSCOPE;
7624              }
7625            } # INSCOPE
7626    
7627          !!!parse-error (type => 'after html:#character');          if (defined $i) {
7628          $phase = 'main';            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7629          ## reprocess                    ne $token->{tag_name}) {
7630          redo B;              !!!cp ('t412.1');
7631        } elsif ($token->{type} eq 'start tag' or              !!!parse-error (type => 'not closed',
7632                 $token->{type} eq 'end tag') {                              text => $self->{open_elements}->[-1]->[0]
7633          !!!parse-error (type => 'after html:'.($token->{type} eq 'end tag' ? '/' : '').$token->{tag_name});                                  ->manakai_local_name,
7634          $phase = 'main';                              token => $token);
7635          ## reprocess            } else {
7636          redo B;              !!!cp ('t414.1');
7637        } elsif ($token->{type} eq 'end-of-file') {            }
7638          ## Stop parsing  
7639          last B;            splice @{$self->{open_elements}}, $i;
7640            } else {
7641              !!!cp ('t413.1');
7642              !!!parse-error (type => 'unmatched end tag',
7643                              text => $token->{tag_name}, token => $token);
7644    
7645              !!!cp ('t415.1');
7646              ## As if <p>, then reprocess the current token
7647              my $el;
7648              !!!create-element ($el, $HTML_NS, 'p',, $token);
7649              $insert->($el);
7650              ## NOTE: Not inserted into |$self->{open_elements}|.
7651            }
7652    
7653            !!!next-token;
7654            next B;
7655          } elsif ({
7656                    a => 1,
7657                    b => 1, big => 1, em => 1, font => 1, i => 1,
7658                    nobr => 1, s => 1, small => 1, strile => 1,
7659                    strong => 1, tt => 1, u => 1,
7660                   }->{$token->{tag_name}}) {
7661            !!!cp ('t427');
7662            $formatting_end_tag->($token);
7663            next B;
7664          } elsif ($token->{tag_name} eq 'br') {
7665            !!!cp ('t428');
7666            !!!parse-error (type => 'unmatched end tag',
7667                            text => 'br', token => $token);
7668    
7669            ## As if <br>
7670            $reconstruct_active_formatting_elements->($insert_to_current);
7671            
7672            my $el;
7673            !!!create-element ($el, $HTML_NS, 'br',, $token);
7674            $insert->($el);
7675            
7676            ## Ignore the token.
7677            !!!next-token;
7678            next B;
7679          } elsif ({
7680                    caption => 1, col => 1, colgroup => 1, frame => 1,
7681                    frameset => 1, head => 1, option => 1, optgroup => 1,
7682                    tbody => 1, td => 1, tfoot => 1, th => 1,
7683                    thead => 1, tr => 1,
7684                    area => 1, basefont => 1, bgsound => 1,
7685                    embed => 1, hr => 1, iframe => 1, image => 1,
7686                    img => 1, input => 1, isindex => 1, noembed => 1,
7687                    noframes => 1, param => 1, select => 1, spacer => 1,
7688                    table => 1, textarea => 1, wbr => 1,
7689                    noscript => 0, ## TODO: if scripting is enabled
7690                   }->{$token->{tag_name}}) {
7691            !!!cp ('t429');
7692            !!!parse-error (type => 'unmatched end tag',
7693                            text => $token->{tag_name}, token => $token);
7694            ## Ignore the token
7695            !!!next-token;
7696            next B;
7697            
7698            ## ISSUE: Issue on HTML5 new elements in spec
7699            
7700        } else {        } else {
7701          die "$0: $token->{type}: Unknown token";          ## Step 1
7702            my $node_i = -1;
7703            my $node = $self->{open_elements}->[$node_i];
7704    
7705            ## Step 2
7706            S2: {
7707              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7708                ## Step 1
7709                ## generate implied end tags
7710                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7711                  !!!cp ('t430');
7712                  ## NOTE: |<ruby><rt></ruby>|.
7713                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7714                  ## which seems wrong.
7715                  pop @{$self->{open_elements}};
7716                  $node_i++;
7717                }
7718            
7719                ## Step 2
7720                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7721                        ne $token->{tag_name}) {
7722                  !!!cp ('t431');
7723                  ## NOTE: <x><y></x>
7724                  !!!parse-error (type => 'not closed',
7725                                  text => $self->{open_elements}->[-1]->[0]
7726                                      ->manakai_local_name,
7727                                  token => $token);
7728                } else {
7729                  !!!cp ('t432');
7730                }
7731                
7732                ## Step 3
7733                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7734    
7735                !!!next-token;
7736                last S2;
7737              } else {
7738                ## Step 3
7739                if (not ($node->[1] & FORMATTING_EL) and
7740                    #not $phrasing_category->{$node->[1]} and
7741                    ($node->[1] & SPECIAL_EL or
7742                     $node->[1] & SCOPING_EL)) {
7743                  !!!cp ('t433');
7744                  !!!parse-error (type => 'unmatched end tag',
7745                                  text => $token->{tag_name}, token => $token);
7746                  ## Ignore the token
7747                  !!!next-token;
7748                  last S2;
7749                }
7750    
7751                !!!cp ('t434');
7752              }
7753              
7754              ## Step 4
7755              $node_i--;
7756              $node = $self->{open_elements}->[$node_i];
7757              
7758              ## Step 5;
7759              redo S2;
7760            } # S2
7761            next B;
7762        }        }
7763      }      }
7764        next B;
7765      } continue { # B
7766        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7767          ## NOTE: The code below is executed in cases where it does not have
7768          ## to be, but it it is harmless even in those cases.
7769          ## has an element in scope
7770          INSCOPE: {
7771            for (reverse 0..$#{$self->{open_elements}}) {
7772              my $node = $self->{open_elements}->[$_];
7773              if ($node->[1] & FOREIGN_EL) {
7774                last INSCOPE;
7775              } elsif ($node->[1] & SCOPING_EL) {
7776                last;
7777              }
7778            }
7779            
7780            ## NOTE: No foreign element in scope.
7781            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7782          } # INSCOPE
7783        }
7784    } # B    } # B
7785    
7786    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 5202  sub _tree_construction_main ($) { Line 7788  sub _tree_construction_main ($) {
7788    ## TODO: script stuffs    ## TODO: script stuffs
7789  } # _tree_construct_main  } # _tree_construct_main
7790    
7791  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7792    my $class = shift;    my $class = shift;
7793    my $node = shift;    my $node = shift;
7794    my $s = \$_[0];    #my $s = \$_[0];
7795    my $onerror = $_[1];    my $onerror = $_[1];
7796      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7797    
7798      ## ISSUE: Should {confident} be true?
7799    
7800    my $nt = $node->node_type;    my $nt = $node->node_type;
7801    if ($nt == 9) {    if ($nt == 9) {
# Line 5223  sub set_inner_html ($$$) { Line 7812  sub set_inner_html ($$$) {
7812      }      }
7813    
7814      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7815      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7816    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7817      ## TODO: If non-html element      ## TODO: If non-html element
7818    
7819      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7820    
7821    ## TODO: Support for $get_wrapper
7822    
7823      ## Step 1 # MUST      ## Step 1 # MUST
7824      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7825      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5236  sub set_inner_html ($$$) { Line 7827  sub set_inner_html ($$$) {
7827      my $p = $class->new;      my $p = $class->new;
7828      $p->{document} = $doc;      $p->{document} = $doc;
7829    
7830      ## Step 9 # MUST      ## Step 8 # MUST
7831      my $i = 0;      my $i = 0;
7832      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7833      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7834      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
7835        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7836        $input = $get_wrapper->($input);
7837        $p->{set_nc} = sub {
7838        my $self = shift;        my $self = shift;
7839    
7840        pop @{$self->{prev_input_character}};        my $char = '';
7841        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        if (defined $self->{next_nc}) {
7842            $char = $self->{next_nc};
7843            delete $self->{next_nc};
7844            $self->{nc} = ord $char;
7845          } else {
7846            $self->{char_buffer} = '';
7847            $self->{char_buffer_pos} = 0;
7848            
7849            my $count = $input->manakai_read_until
7850                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7851                 $self->{char_buffer_pos});
7852            if ($count) {
7853              $self->{line_prev} = $self->{line};
7854              $self->{column_prev} = $self->{column};
7855              $self->{column}++;
7856              $self->{nc}
7857                  = ord substr ($self->{char_buffer},
7858                                $self->{char_buffer_pos}++, 1);
7859              return;
7860            }
7861            
7862            if ($input->read ($char, 1)) {
7863              $self->{nc} = ord $char;
7864            } else {
7865              $self->{nc} = -1;
7866              return;
7867            }
7868          }
7869    
7870          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7871          $p->{column}++;
7872    
7873        $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($self->{nc} == 0x000A) { # LF
7874        $self->{next_input_character} = ord substr $$s, $i++, 1;          $p->{line}++;
7875        $column++;          $p->{column} = 0;
7876            !!!cp ('i1');
7877        if ($self->{next_input_character} == 0x000A) { # LF        } elsif ($self->{nc} == 0x000D) { # CR
7878          $line++;  ## TODO: support for abort/streaming
7879          $column = 0;          my $next = '';
7880        } elsif ($self->{next_input_character} == 0x000D) { # CR          if ($input->read ($next, 1) and $next ne "\x0A") {
7881          $i++ if substr ($$s, $i, 1) eq "\x0A";            $self->{next_nc} = $next;
7882          $self->{next_input_character} = 0x000A; # LF # MUST          }
7883          $line++;          $self->{nc} = 0x000A; # LF # MUST
7884          $column = 0;          $p->{line}++;
7885        } elsif ($self->{next_input_character} > 0x10FFFF) {          $p->{column} = 0;
7886          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i2');
7887        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{nc} == 0x0000) { # NULL
7888            !!!cp ('i4');
7889          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7890          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7891        }        }
7892      };      };
7893      $p->{prev_input_character} = [-1, -1, -1];  
7894      $p->{next_input_character} = -1;      $p->{read_until} = sub {
7895              #my ($scalar, $specials_range, $offset) = @_;
7896          return 0 if defined $p->{next_nc};
7897    
7898          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7899          my $offset = $_[2] || 0;
7900          
7901          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7902            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7903            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7904              substr ($_[0], $offset)
7905                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7906              my $count = $+[0] - $-[0];
7907              if ($count) {
7908                $p->{column} += $count;
7909                $p->{char_buffer_pos} += $count;
7910                $p->{line_prev} = $p->{line};
7911                $p->{column_prev} = $p->{column} - 1;
7912                $p->{nc} = -1;
7913              }
7914              return $count;
7915            } else {
7916              return 0;
7917            }
7918          } else {
7919            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7920            if ($count) {
7921              $p->{column} += $count;
7922              $p->{column_prev} += $count;
7923              $p->{nc} = -1;
7924            }
7925            return $count;
7926          }
7927        }; # $p->{read_until}
7928    
7929      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7930        my (%opt) = @_;        my (%opt) = @_;
7931        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7932          my $column = $opt{column};
7933          if (defined $opt{token} and defined $opt{token}->{line}) {
7934            $line = $opt{token}->{line};
7935            $column = $opt{token}->{column};
7936          }
7937          warn "Parse error ($opt{type}) at line $line column $column\n";
7938      };      };
7939      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7940        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7941      };      };
7942            
7943        my $char_onerror = sub {
7944          my (undef, $type, %opt) = @_;
7945          $ponerror->(layer => 'encode',
7946                      line => $p->{line}, column => $p->{column} + 1,
7947                      %opt, type => $type);
7948        }; # $char_onerror
7949        $input->onerror ($char_onerror);
7950    
7951      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7952      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7953    
7954      ## Step 2      ## Step 2
7955      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7956      $p->{content_model_flag} = {      $p->{content_model} = {
7957        title => 'RCDATA',        title => RCDATA_CONTENT_MODEL,
7958        textarea => 'RCDATA',        textarea => RCDATA_CONTENT_MODEL,
7959        style => 'CDATA',        style => CDATA_CONTENT_MODEL,
7960        script => 'CDATA',        script => CDATA_CONTENT_MODEL,
7961        xmp => 'CDATA',        xmp => CDATA_CONTENT_MODEL,
7962        iframe => 'CDATA',        iframe => CDATA_CONTENT_MODEL,
7963        noembed => 'CDATA',        noembed => CDATA_CONTENT_MODEL,
7964        noframes => 'CDATA',        noframes => CDATA_CONTENT_MODEL,
7965        noscript => 'CDATA',        noscript => CDATA_CONTENT_MODEL,
7966        plaintext => 'PLAINTEXT',        plaintext => PLAINTEXT_CONTENT_MODEL,
7967      }->{$node_ln} || 'PCDATA';      }->{$node_ln};
7968         ## ISSUE: What is "the name of the element"? local name?      $p->{content_model} = PCDATA_CONTENT_MODEL
7969            unless defined $p->{content_model};
7970            ## ISSUE: What is "the name of the element"? local name?
7971    
7972      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7973          ## TODO: Foreign element OK?
7974    
7975      ## Step 4      ## Step 3
7976      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7977        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7978    
7979      ## Step 5 # MUST      ## Step 4 # MUST
7980      $doc->append_child ($root);      $doc->append_child ($root);
7981    
7982      ## Step 6 # MUST      ## Step 5 # MUST
7983      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7984    
7985      undef $p->{head_element};      undef $p->{head_element};
7986    
7987      ## Step 7 # MUST      ## Step 6 # MUST
7988      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7989    
7990      ## Step 8 # MUST      ## Step 7 # MUST
7991      my $anode = $node;      my $anode = $node;
7992      AN: while (defined $anode) {      AN: while (defined $anode) {
7993        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7994          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7995          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7996            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7997                !!!cp ('i5');
7998              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7999              last AN;              last AN;
8000            }            }
# Line 5327  sub set_inner_html ($$$) { Line 8003  sub set_inner_html ($$$) {
8003        $anode = $anode->parent_node;        $anode = $anode->parent_node;
8004      } # AN      } # AN
8005            
8006      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
8007      {      {
8008        my $self = $p;        my $self = $p;
8009        !!!next-token;        !!!next-token;
8010      }      }
8011      $p->_tree_construction_main;      $p->_tree_construction_main;
8012    
8013      ## Step 11 # MUST      ## Step 10 # MUST
8014      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
8015      for (@cn) {      for (@cn) {
8016        $node->remove_child ($_);        $node->remove_child ($_);
8017      }      }
8018      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
8019    
8020      ## Step 12 # MUST      ## Step 11 # MUST
8021      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
8022      for (@cn) {      for (@cn) {
8023        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5351  sub set_inner_html ($$$) { Line 8026  sub set_inner_html ($$$) {
8026      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8027    
8028      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8029    
8030        delete $p->{parse_error}; # delete loop
8031    } else {    } else {
8032      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";
8033    }    }
# Line 5358  sub set_inner_html ($$$) { Line 8035  sub set_inner_html ($$$) {
8035    
8036  } # tree construction stage  } # tree construction stage
8037    
8038  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
8039    my (undef, $node, $on_error) = @_;  push our @ISA, 'Error';
   
   ## Step 1  
   my $s = '';  
   
   my $in_cdata;  
   my $parent = $node;  
   while (defined $parent) {  
     if ($parent->node_type == 1 and  
         $parent->namespace_uri eq 'http://www.w3.org/1999/xhtml' and  
         {  
           style => 1, script => 1, xmp => 1, iframe => 1,  
           noembed => 1, noframes => 1, noscript => 1,  
         }->{$parent->local_name}) { ## TODO: case thingy  
       $in_cdata = 1;  
     }  
     $parent = $parent->parent_node;  
   }  
   
   ## Step 2  
   my @node = @{$node->child_nodes};  
   C: while (@node) {  
     my $child = shift @node;  
     unless (ref $child) {  
       if ($child eq 'cdata-out') {  
         $in_cdata = 0;  
       } else {  
         $s .= $child; # end tag  
       }  
       next C;  
     }  
       
     my $nt = $child->node_type;  
     if ($nt == 1) { # Element  
       my $tag_name = $child->tag_name; ## TODO: manakai_tag_name  
       $s .= '<' . $tag_name;  
       ## NOTE: Non-HTML case:  
       ## <http://permalink.gmane.org/gmane.org.w3c.whatwg.discuss/11191>  
   
       my @attrs = @{$child->attributes}; # sort order MUST be stable  
       for my $attr (@attrs) { # order is implementation dependent  
         my $attr_name = $attr->name; ## TODO: manakai_name  
         $s .= ' ' . $attr_name . '="';  
         my $attr_value = $attr->value;  
         ## escape  
         $attr_value =~ s/&/&amp;/g;  
         $attr_value =~ s/</&lt;/g;  
         $attr_value =~ s/>/&gt;/g;  
         $attr_value =~ s/"/&quot;/g;  
         $s .= $attr_value . '"';  
       }  
       $s .= '>';  
         
       next C if {  
         area => 1, base => 1, basefont => 1, bgsound => 1,  
         br => 1, col => 1, embed => 1, frame => 1, hr => 1,  
         img => 1, input => 1, link => 1, meta => 1, param => 1,  
         spacer => 1, wbr => 1,  
       }->{$tag_name};  
   
       $s .= "\x0A" if $tag_name eq 'pre' or $tag_name eq 'textarea';  
   
       if (not $in_cdata and {  
         style => 1, script => 1, xmp => 1, iframe => 1,  
         noembed => 1, noframes => 1, noscript => 1,  
         plaintext => 1,  
       }->{$tag_name}) {  
         unshift @node, 'cdata-out';  
         $in_cdata = 1;  
       }  
   
       unshift @node, @{$child->child_nodes}, '</' . $tag_name . '>';  
     } elsif ($nt == 3 or $nt == 4) {  
       if ($in_cdata) {  
         $s .= $child->data;  
       } else {  
         my $value = $child->data;  
         $value =~ s/&/&amp;/g;  
         $value =~ s/</&lt;/g;  
         $value =~ s/>/&gt;/g;  
         $value =~ s/"/&quot;/g;  
         $s .= $value;  
       }  
     } elsif ($nt == 8) {  
       $s .= '<!--' . $child->data . '-->';  
     } elsif ($nt == 10) {  
       $s .= '<!DOCTYPE ' . $child->name . '>';  
     } elsif ($nt == 5) { # entrefs  
       push @node, @{$child->child_nodes};  
     } else {  
       $on_error->($child) if defined $on_error;  
     }  
     ## ISSUE: This code does not support PIs.  
   } # C  
     
   ## Step 3  
   return \$s;  
 } # get_inner_html  
8040    
8041  1;  1;
8042  # $Date$  # $Date$

Legend:
Removed from v.1.33  
changed lines
  Added in v.1.184

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24