/[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.48 by wakaba, Sat Jul 21 09:54:45 2007 UTC revision 1.187 by wakaba, Sat Sep 20 09:28:31 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        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
527        ($char_stream, $e_status) = $charset->get_decode_handle
528            ($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_string ($$$;$) {  sub parse_char_string ($$$;$$) {
630    my $self = shift->new;    #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
631    my $s = \$_[0];    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    
# Line 159  sub CDATA_CONTENT_MODEL () { CM_LIMITED_ Line 814  sub CDATA_CONTENT_MODEL () { CM_LIMITED_
814  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }  sub RCDATA_CONTENT_MODEL () { CM_ENTITY | CM_LIMITED_MARKUP }
815  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  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_RCDATA_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    sub PCDATA_STATE () { 50 } # "data state" in the spec
871    
872    sub DOCTYPE_TOKEN () { 1 }
873    sub COMMENT_TOKEN () { 2 }
874    sub START_TAG_TOKEN () { 3 }
875    sub END_TAG_TOKEN () { 4 }
876    sub END_OF_FILE_TOKEN () { 5 }
877    sub CHARACTER_TOKEN () { 6 }
878    
879    sub AFTER_HTML_IMS () { 0b100 }
880    sub HEAD_IMS ()       { 0b1000 }
881    sub BODY_IMS ()       { 0b10000 }
882    sub BODY_TABLE_IMS () { 0b100000 }
883    sub TABLE_IMS ()      { 0b1000000 }
884    sub ROW_IMS ()        { 0b10000000 }
885    sub BODY_AFTER_IMS () { 0b100000000 }
886    sub FRAME_IMS ()      { 0b1000000000 }
887    sub SELECT_IMS ()     { 0b10000000000 }
888    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
889        ## NOTE: "in foreign content" insertion mode is special; it is combined
890        ## with the secondary insertion mode.  In this parser, they are stored
891        ## together in the bit-or'ed form.
892    
893    ## NOTE: "initial" and "before html" insertion modes have no constants.
894    
895    ## NOTE: "after after body" insertion mode.
896    sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
897    
898    ## NOTE: "after after frameset" insertion mode.
899    sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
900    
901    sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
902    sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
903    sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
904    sub BEFORE_HEAD_IM () { HEAD_IMS | 0b11 }
905    sub IN_BODY_IM () { BODY_IMS }
906    sub IN_CELL_IM () { BODY_IMS | BODY_TABLE_IMS | 0b01 }
907    sub IN_CAPTION_IM () { BODY_IMS | BODY_TABLE_IMS | 0b10 }
908    sub IN_ROW_IM () { TABLE_IMS | ROW_IMS | 0b01 }
909    sub IN_TABLE_BODY_IM () { TABLE_IMS | ROW_IMS | 0b10 }
910    sub IN_TABLE_IM () { TABLE_IMS }
911    sub AFTER_BODY_IM () { BODY_AFTER_IMS }
912    sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
913    sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
914    sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
915    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
916    sub IN_COLUMN_GROUP_IM () { 0b10 }
917    
918  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
919    
920  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
921    my $self = shift;    my $self = shift;
922    $self->{state} = 'data'; # MUST    $self->{state} = DATA_STATE; # MUST
923      #$self->{s_kwd}; # state keyword - initialized when used
924      #$self->{entity__value}; # initialized when used
925      #$self->{entity__match}; # initialized when used
926    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
927    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{ct}; # current token
928    undef $self->{current_attribute};    undef $self->{ca}; # current attribute
929    undef $self->{last_emitted_start_tag_name};    undef $self->{last_stag_name}; # last emitted start tag name
930    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
931    $self->{char} = [];    delete $self->{self_closing};
932    # $self->{next_input_character}    $self->{char_buffer} = '';
933      $self->{char_buffer_pos} = 0;
934      $self->{nc} = -1; # next input character
935      #$self->{next_nc}
936    !!!next-input-character;    !!!next-input-character;
937    $self->{token} = [];    $self->{token} = [];
938    # $self->{escape}    # $self->{escape}
939  } # _initialize_tokenizer  } # _initialize_tokenizer
940    
941  ## A token has:  ## A token has:
942  ##   ->{type} eq 'DOCTYPE', 'start tag', 'end tag', 'comment',  ##   ->{type} == DOCTYPE_TOKEN, START_TAG_TOKEN, END_TAG_TOKEN, COMMENT_TOKEN,
943  ##       'character', or 'end-of-file'  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
944  ##   ->{name} (DOCTYPE, start tag (tag name), end tag (tag name))  ##   ->{name} (DOCTYPE_TOKEN)
945  ##   ->{public_identifier} (DOCTYPE)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
946  ##   ->{system_identifier} (DOCTYPE)  ##   ->{pubid} (DOCTYPE_TOKEN)
947  ##   ->{correct} == 1 or 0 (DOCTYPE)  ##   ->{sysid} (DOCTYPE_TOKEN)
948  ##   ->{attributes} isa HASH (start tag, end tag)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
949  ##   ->{data} (comment, character)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
950    ##        ->{name}
951    ##        ->{value}
952    ##        ->{has_reference} == 1 or 0
953    ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
954    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
955    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
956    ##     while the token is pushed back to the stack.
957    
958  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
959    
# Line 194  sub _initialize_tokenizer ($) { Line 963  sub _initialize_tokenizer ($) {
963  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
964  ## and removed from the list.  ## and removed from the list.
965    
966    ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
967    ## (This requirement was dropped from HTML5 spec, unfortunately.)
968    
969    my $is_space = {
970      0x0009 => 1, # CHARACTER TABULATION (HT)
971      0x000A => 1, # LINE FEED (LF)
972      #0x000B => 0, # LINE TABULATION (VT)
973      0x000C => 1, # FORM FEED (FF)
974      #0x000D => 1, # CARRIAGE RETURN (CR)
975      0x0020 => 1, # SPACE (SP)
976    };
977    
978  sub _get_next_token ($) {  sub _get_next_token ($) {
979    my $self = shift;    my $self = shift;
980    
981      if ($self->{self_closing}) {
982        !!!parse-error (type => 'nestc', token => $self->{ct});
983        ## NOTE: The |self_closing| flag is only set by start tag token.
984        ## In addition, when a start tag token is emitted, it is always set to
985        ## |ct|.
986        delete $self->{self_closing};
987      }
988    
989    if (@{$self->{token}}) {    if (@{$self->{token}}) {
990        $self->{self_closing} = $self->{token}->[0]->{self_closing};
991      return shift @{$self->{token}};      return shift @{$self->{token}};
992    }    }
993    
994    A: {    A: {
995      if ($self->{state} eq 'data') {      if ($self->{state} == PCDATA_STATE) {
996        if ($self->{next_input_character} == 0x0026) { # &        ## NOTE: Same as |DATA_STATE|, but only for |PCDATA| content model.
997          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA  
998            $self->{state} = 'entity data';        if ($self->{nc} == 0x0026) { # &
999            !!!cp (0.1);
1000            ## NOTE: In the spec, the tokenizer is switched to the
1001            ## "entity data state".  In this implementation, the tokenizer
1002            ## is switched to the |ENTITY_STATE|, which is an implementation
1003            ## of the "consume a character reference" algorithm.
1004            $self->{entity_add} = -1;
1005            $self->{prev_state} = DATA_STATE;
1006            $self->{state} = ENTITY_STATE;
1007            !!!next-input-character;
1008            redo A;
1009          } elsif ($self->{nc} == 0x003C) { # <
1010            !!!cp (0.2);
1011            $self->{state} = TAG_OPEN_STATE;
1012            !!!next-input-character;
1013            redo A;
1014          } elsif ($self->{nc} == -1) {
1015            !!!cp (0.3);
1016            !!!emit ({type => END_OF_FILE_TOKEN,
1017                      line => $self->{line}, column => $self->{column}});
1018            last A; ## TODO: ok?
1019          } else {
1020            !!!cp (0.4);
1021            #
1022          }
1023    
1024          # Anything else
1025          my $token = {type => CHARACTER_TOKEN,
1026                       data => chr $self->{nc},
1027                       line => $self->{line}, column => $self->{column},
1028                      };
1029          $self->{read_until}->($token->{data}, q[<&], length $token->{data});
1030    
1031          ## Stay in the state.
1032          !!!next-input-character;
1033          !!!emit ($token);
1034          redo A;
1035        } elsif ($self->{state} == DATA_STATE) {
1036          $self->{s_kwd} = '' unless defined $self->{s_kwd};
1037          if ($self->{nc} == 0x0026) { # &
1038            $self->{s_kwd} = '';
1039            if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
1040                not $self->{escape}) {
1041              !!!cp (1);
1042              ## NOTE: In the spec, the tokenizer is switched to the
1043              ## "entity data state".  In this implementation, the tokenizer
1044              ## is switched to the |ENTITY_STATE|, which is an implementation
1045              ## of the "consume a character reference" algorithm.
1046              $self->{entity_add} = -1;
1047              $self->{prev_state} = DATA_STATE;
1048              $self->{state} = ENTITY_STATE;
1049            !!!next-input-character;            !!!next-input-character;
1050            redo A;            redo A;
1051          } else {          } else {
1052              !!!cp (2);
1053            #            #
1054          }          }
1055        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
1056          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1057            unless ($self->{escape}) {            $self->{s_kwd} .= '-';
1058              if ($self->{prev_input_character}->[0] == 0x002D and # -            
1059                  $self->{prev_input_character}->[1] == 0x0021 and # !            if ($self->{s_kwd} eq '<!--') {
1060                  $self->{prev_input_character}->[2] == 0x003C) { # <              !!!cp (3);
1061                $self->{escape} = 1;              $self->{escape} = 1; # unless $self->{escape};
1062              }              $self->{s_kwd} = '--';
1063                #
1064              } elsif ($self->{s_kwd} eq '---') {
1065                !!!cp (4);
1066                $self->{s_kwd} = '--';
1067                #
1068              } else {
1069                !!!cp (5);
1070                #
1071            }            }
1072          }          }
1073                    
1074          #          #
1075        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{nc} == 0x0021) { # !
1076            if (length $self->{s_kwd}) {
1077              !!!cp (5.1);
1078              $self->{s_kwd} .= '!';
1079              #
1080            } else {
1081              !!!cp (5.2);
1082              #$self->{s_kwd} = '';
1083              #
1084            }
1085            #
1086          } elsif ($self->{nc} == 0x003C) { # <
1087          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1088              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1089               not $self->{escape})) {               not $self->{escape})) {
1090            $self->{state} = 'tag open';            !!!cp (6);
1091              $self->{state} = TAG_OPEN_STATE;
1092            !!!next-input-character;            !!!next-input-character;
1093            redo A;            redo A;
1094          } else {          } else {
1095              !!!cp (7);
1096              $self->{s_kwd} = '';
1097            #            #
1098          }          }
1099        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1100          if ($self->{escape} and          if ($self->{escape} and
1101              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1102            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{s_kwd} eq '--') {
1103                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
1104              delete $self->{escape};              delete $self->{escape};
1105              } else {
1106                !!!cp (9);
1107            }            }
1108            } else {
1109              !!!cp (10);
1110          }          }
1111                    
1112            $self->{s_kwd} = '';
1113          #          #
1114        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1115          !!!emit ({type => 'end-of-file'});          !!!cp (11);
1116            $self->{s_kwd} = '';
1117            !!!emit ({type => END_OF_FILE_TOKEN,
1118                      line => $self->{line}, column => $self->{column}});
1119          last A; ## TODO: ok?          last A; ## TODO: ok?
1120          } else {
1121            !!!cp (12);
1122            $self->{s_kwd} = '';
1123            #
1124        }        }
       # Anything else  
       my $token = {type => 'character',  
                    data => chr $self->{next_input_character}};  
       ## Stay in the data state  
       !!!next-input-character;  
1125    
1126        !!!emit ($token);        # Anything else
1127          my $token = {type => CHARACTER_TOKEN,
1128        redo A;                     data => chr $self->{nc},
1129      } elsif ($self->{state} eq 'entity data') {                     line => $self->{line}, column => $self->{column},
1130        ## (cannot happen in CDATA state)                    };
1131                if ($self->{read_until}->($token->{data}, q[-!<>&],
1132        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);                                  length $token->{data})) {
1133            $self->{s_kwd} = '';
1134        $self->{state} = 'data';        }
1135        # next-input-character is already done  
1136          ## Stay in the data state.
1137        unless (defined $token) {        if ($self->{content_model} == PCDATA_CONTENT_MODEL) {
1138          !!!emit ({type => 'character', data => '&'});          !!!cp (13);
1139            $self->{state} = PCDATA_STATE;
1140        } else {        } else {
1141          !!!emit ($token);          !!!cp (14);
1142            ## Stay in the state.
1143        }        }
1144          !!!next-input-character;
1145          !!!emit ($token);
1146        redo A;        redo A;
1147      } elsif ($self->{state} eq 'tag open') {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1148        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1149          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{nc} == 0x002F) { # /
1150              !!!cp (15);
1151            !!!next-input-character;            !!!next-input-character;
1152            $self->{state} = 'close tag open';            $self->{state} = CLOSE_TAG_OPEN_STATE;
1153            redo A;            redo A;
1154            } elsif ($self->{nc} == 0x0021) { # !
1155              !!!cp (15.1);
1156              $self->{s_kwd} = '<' unless $self->{escape};
1157              #
1158          } else {          } else {
1159            ## reconsume            !!!cp (16);
1160            $self->{state} = 'data';            #
   
           !!!emit ({type => 'character', data => '<'});  
   
           redo A;  
1161          }          }
1162    
1163            ## reconsume
1164            $self->{state} = DATA_STATE;
1165            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1166                      line => $self->{line_prev},
1167                      column => $self->{column_prev},
1168                     });
1169            redo A;
1170        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1171          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{nc} == 0x0021) { # !
1172            $self->{state} = 'markup declaration open';            !!!cp (17);
1173              $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1174            !!!next-input-character;            !!!next-input-character;
1175            redo A;            redo A;
1176          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1177            $self->{state} = 'close tag open';            !!!cp (18);
1178              $self->{state} = CLOSE_TAG_OPEN_STATE;
1179            !!!next-input-character;            !!!next-input-character;
1180            redo A;            redo A;
1181          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{nc} and
1182                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1183            $self->{current_token}            !!!cp (19);
1184              = {type => 'start tag',            $self->{ct}
1185                 tag_name => chr ($self->{next_input_character} + 0x0020)};              = {type => START_TAG_TOKEN,
1186            $self->{state} = 'tag name';                 tag_name => chr ($self->{nc} + 0x0020),
1187                   line => $self->{line_prev},
1188                   column => $self->{column_prev}};
1189              $self->{state} = TAG_NAME_STATE;
1190            !!!next-input-character;            !!!next-input-character;
1191            redo A;            redo A;
1192          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{nc} and
1193                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1194            $self->{current_token} = {type => 'start tag',            !!!cp (20);
1195                              tag_name => chr ($self->{next_input_character})};            $self->{ct} = {type => START_TAG_TOKEN,
1196            $self->{state} = 'tag name';                                      tag_name => chr ($self->{nc}),
1197                                        line => $self->{line_prev},
1198                                        column => $self->{column_prev}};
1199              $self->{state} = TAG_NAME_STATE;
1200            !!!next-input-character;            !!!next-input-character;
1201            redo A;            redo A;
1202          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1203            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1204            $self->{state} = 'data';            !!!parse-error (type => 'empty start tag',
1205                              line => $self->{line_prev},
1206                              column => $self->{column_prev});
1207              $self->{state} = DATA_STATE;
1208            !!!next-input-character;            !!!next-input-character;
1209    
1210            !!!emit ({type => 'character', data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1211                        line => $self->{line_prev},
1212                        column => $self->{column_prev},
1213                       });
1214    
1215            redo A;            redo A;
1216          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1217            !!!parse-error (type => 'pio');            !!!cp (22);
1218            $self->{state} = 'bogus comment';            !!!parse-error (type => 'pio',
1219            ## $self->{next_input_character} is intentionally left as is                            line => $self->{line_prev},
1220                              column => $self->{column_prev});
1221              $self->{state} = BOGUS_COMMENT_STATE;
1222              $self->{ct} = {type => COMMENT_TOKEN, data => '',
1223                                        line => $self->{line_prev},
1224                                        column => $self->{column_prev},
1225                                       };
1226              ## $self->{nc} is intentionally left as is
1227            redo A;            redo A;
1228          } else {          } else {
1229            !!!parse-error (type => 'bare stago');            !!!cp (23);
1230            $self->{state} = 'data';            !!!parse-error (type => 'bare stago',
1231                              line => $self->{line_prev},
1232                              column => $self->{column_prev});
1233              $self->{state} = DATA_STATE;
1234            ## reconsume            ## reconsume
1235    
1236            !!!emit ({type => 'character', data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1237                        line => $self->{line_prev},
1238                        column => $self->{column_prev},
1239                       });
1240    
1241            redo A;            redo A;
1242          }          }
1243        } else {        } else {
1244          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1245        }        }
1246      } elsif ($self->{state} eq 'close tag open') {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1247        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        ## NOTE: The "close tag open state" in the spec is implemented as
1248          if (defined $self->{last_emitted_start_tag_name}) {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_RCDATA_CLOSE_TAG_STATE|.
           ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>  
           my @next_char;  
           TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
             push @next_char, $self->{next_input_character};  
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               $self->{next_input_character} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = 'data';  
1249    
1250                !!!emit ({type => 'character', data => '</'});        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1251            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1252                redo A;          if (defined $self->{last_stag_name}) {
1253              }            $self->{state} = CDATA_RCDATA_CLOSE_TAG_STATE;
1254            }            $self->{s_kwd} = '';
1255            push @next_char, $self->{next_input_character};            ## Reconsume.
1256                    redo A;
           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...  
           }  
1257          } else {          } else {
1258            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1259            # next-input-character is already done            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1260            $self->{state} = 'data';            !!!cp (28);
1261            !!!emit ({type => 'character', data => '</'});            $self->{state} = DATA_STATE;
1262              ## Reconsume.
1263              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1264                        line => $l, column => $c,
1265                       });
1266            redo A;            redo A;
1267          }          }
1268        }        }
1269          
1270        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{nc} and
1271            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1272          $self->{current_token} = {type => 'end tag',          !!!cp (29);
1273                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{ct}
1274          $self->{state} = 'tag name';              = {type => END_TAG_TOKEN,
1275          !!!next-input-character;                 tag_name => chr ($self->{nc} + 0x0020),
1276          redo A;                 line => $l, column => $c};
1277        } elsif (0x0061 <= $self->{next_input_character} and          $self->{state} = TAG_NAME_STATE;
1278                 $self->{next_input_character} <= 0x007A) { # a..z          !!!next-input-character;
1279          $self->{current_token} = {type => 'end tag',          redo A;
1280                            tag_name => chr ($self->{next_input_character})};        } elsif (0x0061 <= $self->{nc} and
1281          $self->{state} = 'tag name';                 $self->{nc} <= 0x007A) { # a..z
1282          !!!next-input-character;          !!!cp (30);
1283          redo A;          $self->{ct} = {type => END_TAG_TOKEN,
1284        } elsif ($self->{next_input_character} == 0x003E) { # >                                    tag_name => chr ($self->{nc}),
1285          !!!parse-error (type => 'empty end tag');                                    line => $l, column => $c};
1286          $self->{state} = 'data';          $self->{state} = TAG_NAME_STATE;
1287            !!!next-input-character;
1288            redo A;
1289          } elsif ($self->{nc} == 0x003E) { # >
1290            !!!cp (31);
1291            !!!parse-error (type => 'empty end tag',
1292                            line => $self->{line_prev}, ## "<" in "</>"
1293                            column => $self->{column_prev} - 1);
1294            $self->{state} = DATA_STATE;
1295          !!!next-input-character;          !!!next-input-character;
1296          redo A;          redo A;
1297        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1298            !!!cp (32);
1299          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1300          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1301          # reconsume          # reconsume
1302    
1303          !!!emit ({type => 'character', data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1304                      line => $l, column => $c,
1305                     });
1306    
1307          redo A;          redo A;
1308        } else {        } else {
1309            !!!cp (33);
1310          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1311          $self->{state} = 'bogus comment';          $self->{state} = BOGUS_COMMENT_STATE;
1312          ## $self->{next_input_character} is intentionally left as is          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1313          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1314                                      column => $self->{column_prev} - 1,
1315                                     };
1316            ## NOTE: $self->{nc} is intentionally left as is.
1317            ## Although the "anything else" case of the spec not explicitly
1318            ## states that the next input character is to be reconsumed,
1319            ## it will be included to the |data| of the comment token
1320            ## generated from the bogus end tag, as defined in the
1321            ## "bogus comment state" entry.
1322            redo A;
1323          }
1324        } elsif ($self->{state} == CDATA_RCDATA_CLOSE_TAG_STATE) {
1325          my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1326          if (length $ch) {
1327            my $CH = $ch;
1328            $ch =~ tr/a-z/A-Z/;
1329            my $nch = chr $self->{nc};
1330            if ($nch eq $ch or $nch eq $CH) {
1331              !!!cp (24);
1332              ## Stay in the state.
1333              $self->{s_kwd} .= $nch;
1334              !!!next-input-character;
1335              redo A;
1336            } else {
1337              !!!cp (25);
1338              $self->{state} = DATA_STATE;
1339              ## Reconsume.
1340              !!!emit ({type => CHARACTER_TOKEN,
1341                        data => '</' . $self->{s_kwd},
1342                        line => $self->{line_prev},
1343                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1344                       });
1345              redo A;
1346            }
1347          } else { # after "<{tag-name}"
1348            unless ($is_space->{$self->{nc}} or
1349                    {
1350                     0x003E => 1, # >
1351                     0x002F => 1, # /
1352                     -1 => 1, # EOF
1353                    }->{$self->{nc}}) {
1354              !!!cp (26);
1355              ## Reconsume.
1356              $self->{state} = DATA_STATE;
1357              !!!emit ({type => CHARACTER_TOKEN,
1358                        data => '</' . $self->{s_kwd},
1359                        line => $self->{line_prev},
1360                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1361                       });
1362              redo A;
1363            } else {
1364              !!!cp (27);
1365              $self->{ct}
1366                  = {type => END_TAG_TOKEN,
1367                     tag_name => $self->{last_stag_name},
1368                     line => $self->{line_prev},
1369                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1370              $self->{state} = TAG_NAME_STATE;
1371              ## Reconsume.
1372              redo A;
1373            }
1374        }        }
1375      } elsif ($self->{state} eq 'tag name') {      } elsif ($self->{state} == TAG_NAME_STATE) {
1376        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1377            $self->{next_input_character} == 0x000A or # LF          !!!cp (34);
1378            $self->{next_input_character} == 0x000B or # VT          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1379            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
1380            $self->{next_input_character} == 0x0020) { # SP          redo A;
1381          $self->{state} = 'before attribute name';        } elsif ($self->{nc} == 0x003E) { # >
1382          !!!next-input-character;          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1383          redo A;            !!!cp (35);
1384        } elsif ($self->{next_input_character} == 0x003E) { # >            $self->{last_stag_name} = $self->{ct}->{tag_name};
1385          if ($self->{current_token}->{type} eq 'start tag') {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
1386            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1387            if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1388              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1389            }            #  !!! cp (36);
1390              #  !!! parse-error (type => 'end tag attribute');
1391              #} else {
1392                !!!cp (37);
1393              #}
1394          } else {          } else {
1395            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1396          }          }
1397          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1398          !!!next-input-character;          !!!next-input-character;
1399    
1400          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1401    
1402          redo A;          redo A;
1403        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1404                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1405          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1406            $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1407            # start tag or end tag            # start tag or end tag
1408          ## Stay in this state          ## Stay in this state
1409          !!!next-input-character;          !!!next-input-character;
1410          redo A;          redo A;
1411        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1412          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1413          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1414            $self->{current_token}->{first_start_tag}            !!!cp (39);
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) {
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
1417            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1418            if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1419              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1420            }            #  !!! cp (40);
1421              #  !!! parse-error (type => 'end tag attribute');
1422              #} else {
1423                !!!cp (41);
1424              #}
1425          } else {          } else {
1426            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1427          }          }
1428          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1429          # reconsume          # reconsume
1430    
1431          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1432    
1433          redo A;          redo A;
1434        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1435            !!!cp (42);
1436            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1437          !!!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  
1438          redo A;          redo A;
1439        } else {        } else {
1440          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1441            $self->{ct}->{tag_name} .= chr $self->{nc};
1442            # start tag or end tag            # start tag or end tag
1443          ## Stay in the state          ## Stay in the state
1444          !!!next-input-character;          !!!next-input-character;
1445          redo A;          redo A;
1446        }        }
1447      } elsif ($self->{state} eq 'before attribute name') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1448        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1449            $self->{next_input_character} == 0x000A or # LF          !!!cp (45);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
1450          ## Stay in the state          ## Stay in the state
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1454          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1455            $self->{current_token}->{first_start_tag}            !!!cp (46);
1456                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1457            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
1458            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1459            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1460                !!!cp (47);
1461              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1462              } else {
1463                !!!cp (48);
1464            }            }
1465          } else {          } else {
1466            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1467          }          }
1468          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1469          !!!next-input-character;          !!!next-input-character;
1470    
1471          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1472    
1473          redo A;          redo A;
1474        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1475                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1476          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1477                                value => ''};          $self->{ca}
1478          $self->{state} = 'attribute name';              = {name => chr ($self->{nc} + 0x0020),
1479                   value => '',
1480                   line => $self->{line}, column => $self->{column}};
1481            $self->{state} = ATTRIBUTE_NAME_STATE;
1482          !!!next-input-character;          !!!next-input-character;
1483          redo A;          redo A;
1484        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1485            !!!cp (50);
1486            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1487          !!!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  
1488          redo A;          redo A;
1489        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1490          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1491          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1492            $self->{current_token}->{first_start_tag}            !!!cp (52);
1493                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1494            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
1495            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1496            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1497                !!!cp (53);
1498              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1499              } else {
1500                !!!cp (54);
1501            }            }
1502          } else {          } else {
1503            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1504          }          }
1505          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1506          # reconsume          # reconsume
1507    
1508          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1509    
1510          redo A;          redo A;
1511        } else {        } else {
1512          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1513                                value => ''};               0x0022 => 1, # "
1514          $self->{state} = 'attribute name';               0x0027 => 1, # '
1515                 0x003D => 1, # =
1516                }->{$self->{nc}}) {
1517              !!!cp (55);
1518              !!!parse-error (type => 'bad attribute name');
1519            } else {
1520              !!!cp (56);
1521            }
1522            $self->{ca}
1523                = {name => chr ($self->{nc}),
1524                   value => '',
1525                   line => $self->{line}, column => $self->{column}};
1526            $self->{state} = ATTRIBUTE_NAME_STATE;
1527          !!!next-input-character;          !!!next-input-character;
1528          redo A;          redo A;
1529        }        }
1530      } elsif ($self->{state} eq 'attribute name') {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1531        my $before_leave = sub {        my $before_leave = sub {
1532          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1533              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1534            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1535            ## Discard $self->{current_attribute} # MUST            !!!parse-error (type => 'duplicate attribute', text => $self->{ca}->{name}, line => $self->{ca}->{line}, column => $self->{ca}->{column});
1536          } else {            ## Discard $self->{ca} # MUST
1537            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}          } else {
1538              = $self->{current_attribute};            !!!cp (58);
1539              $self->{ct}->{attributes}->{$self->{ca}->{name}}
1540                = $self->{ca};
1541          }          }
1542        }; # $before_leave        }; # $before_leave
1543    
1544        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1545            $self->{next_input_character} == 0x000A or # LF          !!!cp (59);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
1546          $before_leave->();          $before_leave->();
1547          $self->{state} = 'after attribute name';          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1548          !!!next-input-character;          !!!next-input-character;
1549          redo A;          redo A;
1550        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1551            !!!cp (60);
1552          $before_leave->();          $before_leave->();
1553          $self->{state} = 'before attribute value';          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1554          !!!next-input-character;          !!!next-input-character;
1555          redo A;          redo A;
1556        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1557          $before_leave->();          $before_leave->();
1558          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1559            $self->{current_token}->{first_start_tag}            !!!cp (61);
1560                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1561            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1562          } elsif ($self->{current_token}->{type} eq 'end tag') {            !!!cp (62);
1563            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1564            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1565              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
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          !!!next-input-character;          !!!next-input-character;
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        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1577                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1578          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1579            $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1580          ## Stay in the state          ## Stay in the state
1581          !!!next-input-character;          !!!next-input-character;
1582          redo A;          redo A;
1583        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1584            !!!cp (64);
1585          $before_leave->();          $before_leave->();
1586            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1587          !!!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  
1588          redo A;          redo A;
1589        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1590          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1591          $before_leave->();          $before_leave->();
1592          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1593            $self->{current_token}->{first_start_tag}            !!!cp (66);
1594                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1595            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
1596            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1597            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1598                !!!cp (67);
1599              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1600              } else {
1601                ## NOTE: This state should never be reached.
1602                !!!cp (68);
1603            }            }
1604          } else {          } else {
1605            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1606          }          }
1607          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1608          # reconsume          # reconsume
1609    
1610          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1611    
1612          redo A;          redo A;
1613        } else {        } else {
1614          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x0022 or # "
1615                $self->{nc} == 0x0027) { # '
1616              !!!cp (69);
1617              !!!parse-error (type => 'bad attribute name');
1618            } else {
1619              !!!cp (70);
1620            }
1621            $self->{ca}->{name} .= chr ($self->{nc});
1622          ## Stay in the state          ## Stay in the state
1623          !!!next-input-character;          !!!next-input-character;
1624          redo A;          redo A;
1625        }        }
1626      } elsif ($self->{state} eq 'after attribute name') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1627        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1628            $self->{next_input_character} == 0x000A or # LF          !!!cp (71);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
1629          ## Stay in the state          ## Stay in the state
1630          !!!next-input-character;          !!!next-input-character;
1631          redo A;          redo A;
1632        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1633          $self->{state} = 'before attribute value';          !!!cp (72);
1634            $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1635          !!!next-input-character;          !!!next-input-character;
1636          redo A;          redo A;
1637        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1638          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1639            $self->{current_token}->{first_start_tag}            !!!cp (73);
1640                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1641            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
1642            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1643            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1644                !!!cp (74);
1645              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1646              } else {
1647                ## NOTE: This state should never be reached.
1648                !!!cp (75);
1649            }            }
1650          } else {          } else {
1651            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1652          }          }
1653          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1654          !!!next-input-character;          !!!next-input-character;
1655    
1656          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1657    
1658          redo A;          redo A;
1659        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1660                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1661          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1662                                value => ''};          $self->{ca}
1663          $self->{state} = 'attribute name';              = {name => chr ($self->{nc} + 0x0020),
1664                   value => '',
1665                   line => $self->{line}, column => $self->{column}};
1666            $self->{state} = ATTRIBUTE_NAME_STATE;
1667          !!!next-input-character;          !!!next-input-character;
1668          redo A;          redo A;
1669        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1670            !!!cp (77);
1671            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1672          !!!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  
1673          redo A;          redo A;
1674        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1675          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1676          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1677            $self->{current_token}->{first_start_tag}            !!!cp (79);
1678                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1679            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
1680            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1681            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1682                !!!cp (80);
1683              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1684              } else {
1685                ## NOTE: This state should never be reached.
1686                !!!cp (81);
1687            }            }
1688          } else {          } else {
1689            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1690          }          }
1691          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1692          # reconsume          # reconsume
1693    
1694          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1695    
1696          redo A;          redo A;
1697        } else {        } else {
1698          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{nc} == 0x0022 or # "
1699                                value => ''};              $self->{nc} == 0x0027) { # '
1700          $self->{state} = 'attribute name';            !!!cp (78);
1701              !!!parse-error (type => 'bad attribute name');
1702            } else {
1703              !!!cp (82);
1704            }
1705            $self->{ca}
1706                = {name => chr ($self->{nc}),
1707                   value => '',
1708                   line => $self->{line}, column => $self->{column}};
1709            $self->{state} = ATTRIBUTE_NAME_STATE;
1710          !!!next-input-character;          !!!next-input-character;
1711          redo A;                  redo A;        
1712        }        }
1713      } elsif ($self->{state} eq 'before attribute value') {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1714        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1715            $self->{next_input_character} == 0x000A or # LF          !!!cp (83);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP        
1716          ## Stay in the state          ## Stay in the state
1717          !!!next-input-character;          !!!next-input-character;
1718          redo A;          redo A;
1719        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
1720          $self->{state} = 'attribute value (double-quoted)';          !!!cp (84);
1721            $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1722          !!!next-input-character;          !!!next-input-character;
1723          redo A;          redo A;
1724        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1725          $self->{state} = 'attribute value (unquoted)';          !!!cp (85);
1726            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1727          ## reconsume          ## reconsume
1728          redo A;          redo A;
1729        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
1730          $self->{state} = 'attribute value (single-quoted)';          !!!cp (86);
1731            $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1732          !!!next-input-character;          !!!next-input-character;
1733          redo A;          redo A;
1734        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1735          if ($self->{current_token}->{type} eq 'start tag') {          !!!parse-error (type => 'empty unquoted attribute value');
1736            $self->{current_token}->{first_start_tag}          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1737                = not defined $self->{last_emitted_start_tag_name};            !!!cp (87);
1738            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1739          } elsif ($self->{current_token}->{type} eq 'end tag') {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1740            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1741            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1742                !!!cp (88);
1743              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1744              } else {
1745                ## NOTE: This state should never be reached.
1746                !!!cp (89);
1747            }            }
1748          } else {          } else {
1749            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1750          }          }
1751          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1752          !!!next-input-character;          !!!next-input-character;
1753    
1754          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1755    
1756          redo A;          redo A;
1757        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1758          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1759          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1760            $self->{current_token}->{first_start_tag}            !!!cp (90);
1761                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1762            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
1763            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1764            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1765                !!!cp (91);
1766              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1767              } else {
1768                ## NOTE: This state should never be reached.
1769                !!!cp (92);
1770            }            }
1771          } else {          } else {
1772            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1773          }          }
1774          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1775          ## reconsume          ## reconsume
1776    
1777          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1778    
1779          redo A;          redo A;
1780        } else {        } else {
1781          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x003D) { # =
1782          $self->{state} = 'attribute value (unquoted)';            !!!cp (93);
1783              !!!parse-error (type => 'bad attribute value');
1784            } else {
1785              !!!cp (94);
1786            }
1787            $self->{ca}->{value} .= chr ($self->{nc});
1788            $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1789          !!!next-input-character;          !!!next-input-character;
1790          redo A;          redo A;
1791        }        }
1792      } elsif ($self->{state} eq 'attribute value (double-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1793        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
1794          $self->{state} = 'before attribute name';          !!!cp (95);
1795            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1796          !!!next-input-character;          !!!next-input-character;
1797          redo A;          redo A;
1798        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1799          $self->{last_attribute_value_state} = 'attribute value (double-quoted)';          !!!cp (96);
1800          $self->{state} = 'entity in attribute value';          ## NOTE: In the spec, the tokenizer is switched to the
1801            ## "entity in attribute value state".  In this implementation, the
1802            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1803            ## implementation of the "consume a character reference" algorithm.
1804            $self->{prev_state} = $self->{state};
1805            $self->{entity_add} = 0x0022; # "
1806            $self->{state} = ENTITY_STATE;
1807          !!!next-input-character;          !!!next-input-character;
1808          redo A;          redo A;
1809        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1810          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1811          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1812            $self->{current_token}->{first_start_tag}            !!!cp (97);
1813                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1814            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
1815            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1816            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1817                !!!cp (98);
1818              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1819              } else {
1820                ## NOTE: This state should never be reached.
1821                !!!cp (99);
1822            }            }
1823          } else {          } else {
1824            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1825          }          }
1826          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1827          ## reconsume          ## reconsume
1828    
1829          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1830    
1831          redo A;          redo A;
1832        } else {        } else {
1833          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1834            $self->{ca}->{value} .= chr ($self->{nc});
1835            $self->{read_until}->($self->{ca}->{value},
1836                                  q["&],
1837                                  length $self->{ca}->{value});
1838    
1839          ## Stay in the state          ## Stay in the state
1840          !!!next-input-character;          !!!next-input-character;
1841          redo A;          redo A;
1842        }        }
1843      } elsif ($self->{state} eq 'attribute value (single-quoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1844        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
1845          $self->{state} = 'before attribute name';          !!!cp (101);
1846            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1847            !!!next-input-character;
1848            redo A;
1849          } elsif ($self->{nc} == 0x0026) { # &
1850            !!!cp (102);
1851            ## NOTE: In the spec, the tokenizer is switched to the
1852            ## "entity in attribute value state".  In this implementation, the
1853            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1854            ## implementation of the "consume a character reference" algorithm.
1855            $self->{entity_add} = 0x0027; # '
1856            $self->{prev_state} = $self->{state};
1857            $self->{state} = ENTITY_STATE;
1858          !!!next-input-character;          !!!next-input-character;
1859          redo A;          redo A;
1860        } 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) {  
1861          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1862          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1863            $self->{current_token}->{first_start_tag}            !!!cp (103);
1864                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1865            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
1866            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1867            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1868                !!!cp (104);
1869              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1870              } else {
1871                ## NOTE: This state should never be reached.
1872                !!!cp (105);
1873            }            }
1874          } else {          } else {
1875            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1876          }          }
1877          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1878          ## reconsume          ## reconsume
1879    
1880          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1881    
1882          redo A;          redo A;
1883        } else {        } else {
1884          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1885            $self->{ca}->{value} .= chr ($self->{nc});
1886            $self->{read_until}->($self->{ca}->{value},
1887                                  q['&],
1888                                  length $self->{ca}->{value});
1889    
1890          ## Stay in the state          ## Stay in the state
1891          !!!next-input-character;          !!!next-input-character;
1892          redo A;          redo A;
1893        }        }
1894      } elsif ($self->{state} eq 'attribute value (unquoted)') {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1895        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1896            $self->{next_input_character} == 0x000A or # LF          !!!cp (107);
1897            $self->{next_input_character} == 0x000B or # HT          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1898            $self->{next_input_character} == 0x000C or # FF          !!!next-input-character;
1899            $self->{next_input_character} == 0x0020) { # SP          redo A;
1900          $self->{state} = 'before attribute name';        } elsif ($self->{nc} == 0x0026) { # &
1901          !!!next-input-character;          !!!cp (108);
1902          redo A;          ## NOTE: In the spec, the tokenizer is switched to the
1903        } elsif ($self->{next_input_character} == 0x0026) { # &          ## "entity in attribute value state".  In this implementation, the
1904          $self->{last_attribute_value_state} = 'attribute value (unquoted)';          ## tokenizer is switched to the |ENTITY_STATE|, which is an
1905          $self->{state} = 'entity in attribute value';          ## implementation of the "consume a character reference" algorithm.
1906          !!!next-input-character;          $self->{entity_add} = -1;
1907          redo A;          $self->{prev_state} = $self->{state};
1908        } elsif ($self->{next_input_character} == 0x003E) { # >          $self->{state} = ENTITY_STATE;
1909          if ($self->{current_token}->{type} eq 'start tag') {          !!!next-input-character;
1910            $self->{current_token}->{first_start_tag}          redo A;
1911                = not defined $self->{last_emitted_start_tag_name};        } elsif ($self->{nc} == 0x003E) { # >
1912            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1913          } elsif ($self->{current_token}->{type} eq 'end tag') {            !!!cp (109);
1914              $self->{last_stag_name} = $self->{ct}->{tag_name};
1915            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1916            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1917            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1918                !!!cp (110);
1919              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1920              } else {
1921                ## NOTE: This state should never be reached.
1922                !!!cp (111);
1923            }            }
1924          } else {          } else {
1925            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1926          }          }
1927          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1928          !!!next-input-character;          !!!next-input-character;
1929    
1930          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1931    
1932          redo A;          redo A;
1933        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1934          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1935          if ($self->{current_token}->{type} eq 'start tag') {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1936            $self->{current_token}->{first_start_tag}            !!!cp (112);
1937                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1938            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} eq 'end tag') {  
1939            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1940            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1941                !!!cp (113);
1942              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1943              } else {
1944                ## NOTE: This state should never be reached.
1945                !!!cp (114);
1946            }            }
1947          } else {          } else {
1948            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1949          }          }
1950          $self->{state} = 'data';          $self->{state} = DATA_STATE;
1951          ## reconsume          ## reconsume
1952    
1953          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1954    
1955          redo A;          redo A;
1956        } else {        } else {
1957          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1958                 0x0022 => 1, # "
1959                 0x0027 => 1, # '
1960                 0x003D => 1, # =
1961                }->{$self->{nc}}) {
1962              !!!cp (115);
1963              !!!parse-error (type => 'bad attribute value');
1964            } else {
1965              !!!cp (116);
1966            }
1967            $self->{ca}->{value} .= chr ($self->{nc});
1968            $self->{read_until}->($self->{ca}->{value},
1969                                  q["'=& >],
1970                                  length $self->{ca}->{value});
1971    
1972          ## Stay in the state          ## Stay in the state
1973          !!!next-input-character;          !!!next-input-character;
1974          redo A;          redo A;
1975        }        }
1976      } elsif ($self->{state} eq 'entity in attribute value') {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1977        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        if ($is_space->{$self->{nc}}) {
1978            !!!cp (118);
1979            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1980            !!!next-input-character;
1981            redo A;
1982          } elsif ($self->{nc} == 0x003E) { # >
1983            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1984              !!!cp (119);
1985              $self->{last_stag_name} = $self->{ct}->{tag_name};
1986            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1987              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1988              if ($self->{ct}->{attributes}) {
1989                !!!cp (120);
1990                !!!parse-error (type => 'end tag attribute');
1991              } else {
1992                ## NOTE: This state should never be reached.
1993                !!!cp (121);
1994              }
1995            } else {
1996              die "$0: $self->{ct}->{type}: Unknown token type";
1997            }
1998            $self->{state} = DATA_STATE;
1999            !!!next-input-character;
2000    
2001        unless (defined $token) {          !!!emit ($self->{ct}); # start tag or end tag
2002          $self->{current_attribute}->{value} .= '&';  
2003            redo A;
2004          } elsif ($self->{nc} == 0x002F) { # /
2005            !!!cp (122);
2006            $self->{state} = SELF_CLOSING_START_TAG_STATE;
2007            !!!next-input-character;
2008            redo A;
2009          } elsif ($self->{nc} == -1) {
2010            !!!parse-error (type => 'unclosed tag');
2011            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2012              !!!cp (122.3);
2013              $self->{last_stag_name} = $self->{ct}->{tag_name};
2014            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2015              if ($self->{ct}->{attributes}) {
2016                !!!cp (122.1);
2017                !!!parse-error (type => 'end tag attribute');
2018              } else {
2019                ## NOTE: This state should never be reached.
2020                !!!cp (122.2);
2021              }
2022            } else {
2023              die "$0: $self->{ct}->{type}: Unknown token type";
2024            }
2025            $self->{state} = DATA_STATE;
2026            ## Reconsume.
2027            !!!emit ($self->{ct}); # start tag or end tag
2028            redo A;
2029        } else {        } else {
2030          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
2031          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
2032            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2033            ## reconsume
2034            redo A;
2035        }        }
2036        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
2037          if ($self->{nc} == 0x003E) { # >
2038            if ($self->{ct}->{type} == END_TAG_TOKEN) {
2039              !!!cp ('124.2');
2040              !!!parse-error (type => 'nestc', token => $self->{ct});
2041              ## TODO: Different type than slash in start tag
2042              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2043              if ($self->{ct}->{attributes}) {
2044                !!!cp ('124.4');
2045                !!!parse-error (type => 'end tag attribute');
2046              } else {
2047                !!!cp ('124.5');
2048              }
2049              ## TODO: Test |<title></title/>|
2050            } else {
2051              !!!cp ('124.3');
2052              $self->{self_closing} = 1;
2053            }
2054    
2055        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
2056        # 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  
2057    
2058            !!!emit ($token);          !!!emit ($self->{ct}); # start tag or end tag
2059    
2060            redo A;          redo A;
2061          } elsif ($self->{nc} == -1) {
2062            !!!parse-error (type => 'unclosed tag');
2063            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2064              !!!cp (124.7);
2065              $self->{last_stag_name} = $self->{ct}->{tag_name};
2066            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2067              if ($self->{ct}->{attributes}) {
2068                !!!cp (124.5);
2069                !!!parse-error (type => 'end tag attribute');
2070              } else {
2071                ## NOTE: This state should never be reached.
2072                !!!cp (124.6);
2073              }
2074          } else {          } else {
2075            $token->{data} .= chr ($self->{next_input_character});            die "$0: $self->{ct}->{type}: Unknown token type";
           !!!next-input-character;  
           redo BC;  
2076          }          }
2077        } # BC          $self->{state} = DATA_STATE;
2078      } elsif ($self->{state} eq 'markup declaration open') {          ## Reconsume.
2079            !!!emit ($self->{ct}); # start tag or end tag
2080            redo A;
2081          } else {
2082            !!!cp ('124.4');
2083            !!!parse-error (type => 'nestc');
2084            ## TODO: This error type is wrong.
2085            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2086            ## Reconsume.
2087            redo A;
2088          }
2089        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2090        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2091    
2092        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2093        push @next_char, $self->{next_input_character};        ## consumes characters one-by-one basis.
2094                
2095        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x003E) { # >
2096            !!!cp (124);
2097            $self->{state} = DATA_STATE;
2098          !!!next-input-character;          !!!next-input-character;
2099          push @next_char, $self->{next_input_character};  
2100          if ($self->{next_input_character} == 0x002D) { # -          !!!emit ($self->{ct}); # comment
2101            $self->{current_token} = {type => 'comment', data => ''};          redo A;
2102            $self->{state} = 'comment start';        } elsif ($self->{nc} == -1) {
2103            !!!next-input-character;          !!!cp (125);
2104            redo A;          $self->{state} = DATA_STATE;
2105          }          ## reconsume
2106        } elsif ($self->{next_input_character} == 0x0044 or # D  
2107                 $self->{next_input_character} == 0x0064) { # d          !!!emit ($self->{ct}); # comment
2108            redo A;
2109          } else {
2110            !!!cp (126);
2111            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2112            $self->{read_until}->($self->{ct}->{data},
2113                                  q[>],
2114                                  length $self->{ct}->{data});
2115    
2116            ## Stay in the state.
2117          !!!next-input-character;          !!!next-input-character;
2118          push @next_char, $self->{next_input_character};          redo A;
2119          if ($self->{next_input_character} == 0x004F or # O        }
2120              $self->{next_input_character} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2121            !!!next-input-character;        ## (only happen if PCDATA state)
2122            push @next_char, $self->{next_input_character};        
2123            if ($self->{next_input_character} == 0x0043 or # C        if ($self->{nc} == 0x002D) { # -
2124                $self->{next_input_character} == 0x0063) { # c          !!!cp (133);
2125              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2126              push @next_char, $self->{next_input_character};          !!!next-input-character;
2127              if ($self->{next_input_character} == 0x0054 or # T          redo A;
2128                  $self->{next_input_character} == 0x0074) { # t        } elsif ($self->{nc} == 0x0044 or # D
2129                !!!next-input-character;                 $self->{nc} == 0x0064) { # d
2130                push @next_char, $self->{next_input_character};          ## ASCII case-insensitive.
2131                if ($self->{next_input_character} == 0x0059 or # Y          !!!cp (130);
2132                    $self->{next_input_character} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2133                  !!!next-input-character;          $self->{s_kwd} = chr $self->{nc};
2134                  push @next_char, $self->{next_input_character};          !!!next-input-character;
2135                  if ($self->{next_input_character} == 0x0050 or # P          redo A;
2136                      $self->{next_input_character} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2137                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2138                    push @next_char, $self->{next_input_character};                 $self->{nc} == 0x005B) { # [
2139                    if ($self->{next_input_character} == 0x0045 or # E          !!!cp (135.4);                
2140                        $self->{next_input_character} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2141                      ## ISSUE: What a stupid code this is!          $self->{s_kwd} = '[';
2142                      $self->{state} = 'DOCTYPE';          !!!next-input-character;
2143                      !!!next-input-character;          redo A;
2144                      redo A;        } else {
2145                    }          !!!cp (136);
                 }  
               }  
             }  
           }  
         }  
2146        }        }
2147    
2148        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2149        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2150        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2151        $self->{state} = 'bogus comment';        ## Reconsume.
2152          $self->{state} = BOGUS_COMMENT_STATE;
2153          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2154                                    line => $self->{line_prev},
2155                                    column => $self->{column_prev} - 1,
2156                                   };
2157        redo A;        redo A;
2158              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2159        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2160        ## 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);
2161      } elsif ($self->{state} eq 'comment start') {          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2162        if ($self->{next_input_character} == 0x002D) { # -                                    line => $self->{line_prev},
2163          $self->{state} = 'comment start dash';                                    column => $self->{column_prev} - 2,
2164                                     };
2165            $self->{state} = COMMENT_START_STATE;
2166            !!!next-input-character;
2167            redo A;
2168          } else {
2169            !!!cp (128);
2170            !!!parse-error (type => 'bogus comment',
2171                            line => $self->{line_prev},
2172                            column => $self->{column_prev} - 2);
2173            $self->{state} = BOGUS_COMMENT_STATE;
2174            ## Reconsume.
2175            $self->{ct} = {type => COMMENT_TOKEN,
2176                                      data => '-',
2177                                      line => $self->{line_prev},
2178                                      column => $self->{column_prev} - 2,
2179                                     };
2180            redo A;
2181          }
2182        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2183          ## ASCII case-insensitive.
2184          if ($self->{nc} == [
2185                undef,
2186                0x004F, # O
2187                0x0043, # C
2188                0x0054, # T
2189                0x0059, # Y
2190                0x0050, # P
2191              ]->[length $self->{s_kwd}] or
2192              $self->{nc} == [
2193                undef,
2194                0x006F, # o
2195                0x0063, # c
2196                0x0074, # t
2197                0x0079, # y
2198                0x0070, # p
2199              ]->[length $self->{s_kwd}]) {
2200            !!!cp (131);
2201            ## Stay in the state.
2202            $self->{s_kwd} .= chr $self->{nc};
2203            !!!next-input-character;
2204            redo A;
2205          } elsif ((length $self->{s_kwd}) == 6 and
2206                   ($self->{nc} == 0x0045 or # E
2207                    $self->{nc} == 0x0065)) { # e
2208            !!!cp (129);
2209            $self->{state} = DOCTYPE_STATE;
2210            $self->{ct} = {type => DOCTYPE_TOKEN,
2211                                      quirks => 1,
2212                                      line => $self->{line_prev},
2213                                      column => $self->{column_prev} - 7,
2214                                     };
2215            !!!next-input-character;
2216            redo A;
2217          } else {
2218            !!!cp (132);        
2219            !!!parse-error (type => 'bogus comment',
2220                            line => $self->{line_prev},
2221                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2222            $self->{state} = BOGUS_COMMENT_STATE;
2223            ## Reconsume.
2224            $self->{ct} = {type => COMMENT_TOKEN,
2225                                      data => $self->{s_kwd},
2226                                      line => $self->{line_prev},
2227                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2228                                     };
2229            redo A;
2230          }
2231        } elsif ($self->{state} == MD_CDATA_STATE) {
2232          if ($self->{nc} == {
2233                '[' => 0x0043, # C
2234                '[C' => 0x0044, # D
2235                '[CD' => 0x0041, # A
2236                '[CDA' => 0x0054, # T
2237                '[CDAT' => 0x0041, # A
2238              }->{$self->{s_kwd}}) {
2239            !!!cp (135.1);
2240            ## Stay in the state.
2241            $self->{s_kwd} .= chr $self->{nc};
2242            !!!next-input-character;
2243            redo A;
2244          } elsif ($self->{s_kwd} eq '[CDATA' and
2245                   $self->{nc} == 0x005B) { # [
2246            !!!cp (135.2);
2247            $self->{ct} = {type => CHARACTER_TOKEN,
2248                                      data => '',
2249                                      line => $self->{line_prev},
2250                                      column => $self->{column_prev} - 7};
2251            $self->{state} = CDATA_SECTION_STATE;
2252            !!!next-input-character;
2253            redo A;
2254          } else {
2255            !!!cp (135.3);
2256            !!!parse-error (type => 'bogus comment',
2257                            line => $self->{line_prev},
2258                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2259            $self->{state} = BOGUS_COMMENT_STATE;
2260            ## Reconsume.
2261            $self->{ct} = {type => COMMENT_TOKEN,
2262                                      data => $self->{s_kwd},
2263                                      line => $self->{line_prev},
2264                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2265                                     };
2266            redo A;
2267          }
2268        } elsif ($self->{state} == COMMENT_START_STATE) {
2269          if ($self->{nc} == 0x002D) { # -
2270            !!!cp (137);
2271            $self->{state} = COMMENT_START_DASH_STATE;
2272          !!!next-input-character;          !!!next-input-character;
2273          redo A;          redo A;
2274        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2275            !!!cp (138);
2276          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2277          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2278          !!!next-input-character;          !!!next-input-character;
2279    
2280          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2281    
2282          redo A;          redo A;
2283        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2284            !!!cp (139);
2285          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2286          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2287          ## reconsume          ## reconsume
2288    
2289          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2290    
2291          redo A;          redo A;
2292        } else {        } else {
2293          $self->{current_token}->{data} # comment          !!!cp (140);
2294              .= chr ($self->{next_input_character});          $self->{ct}->{data} # comment
2295          $self->{state} = 'comment';              .= chr ($self->{nc});
2296            $self->{state} = COMMENT_STATE;
2297          !!!next-input-character;          !!!next-input-character;
2298          redo A;          redo A;
2299        }        }
2300      } elsif ($self->{state} eq 'comment start dash') {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2301        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2302          $self->{state} = 'comment end';          !!!cp (141);
2303            $self->{state} = COMMENT_END_STATE;
2304          !!!next-input-character;          !!!next-input-character;
2305          redo A;          redo A;
2306        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2307            !!!cp (142);
2308          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2309          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2310          !!!next-input-character;          !!!next-input-character;
2311    
2312          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2313    
2314          redo A;          redo A;
2315        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2316            !!!cp (143);
2317          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2318          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2319          ## reconsume          ## reconsume
2320    
2321          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2322    
2323          redo A;          redo A;
2324        } else {        } else {
2325          $self->{current_token}->{data} # comment          !!!cp (144);
2326              .= '-' . chr ($self->{next_input_character});          $self->{ct}->{data} # comment
2327          $self->{state} = 'comment';              .= '-' . chr ($self->{nc});
2328            $self->{state} = COMMENT_STATE;
2329          !!!next-input-character;          !!!next-input-character;
2330          redo A;          redo A;
2331        }        }
2332      } elsif ($self->{state} eq 'comment') {      } elsif ($self->{state} == COMMENT_STATE) {
2333        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2334          $self->{state} = 'comment end dash';          !!!cp (145);
2335            $self->{state} = COMMENT_END_DASH_STATE;
2336          !!!next-input-character;          !!!next-input-character;
2337          redo A;          redo A;
2338        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2339            !!!cp (146);
2340          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2341          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2342          ## reconsume          ## reconsume
2343    
2344          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2345    
2346          redo A;          redo A;
2347        } else {        } else {
2348          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2349            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2350            $self->{read_until}->($self->{ct}->{data},
2351                                  q[-],
2352                                  length $self->{ct}->{data});
2353    
2354          ## Stay in the state          ## Stay in the state
2355          !!!next-input-character;          !!!next-input-character;
2356          redo A;          redo A;
2357        }        }
2358      } elsif ($self->{state} eq 'comment end dash') {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2359        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2360          $self->{state} = 'comment end';          !!!cp (148);
2361            $self->{state} = COMMENT_END_STATE;
2362          !!!next-input-character;          !!!next-input-character;
2363          redo A;          redo A;
2364        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2365            !!!cp (149);
2366          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2367          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2368          ## reconsume          ## reconsume
2369    
2370          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2371    
2372          redo A;          redo A;
2373        } else {        } else {
2374          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2375          $self->{state} = 'comment';          $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2376            $self->{state} = COMMENT_STATE;
2377          !!!next-input-character;          !!!next-input-character;
2378          redo A;          redo A;
2379        }        }
2380      } elsif ($self->{state} eq 'comment end') {      } elsif ($self->{state} == COMMENT_END_STATE) {
2381        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2382          $self->{state} = 'data';          !!!cp (151);
2383            $self->{state} = DATA_STATE;
2384          !!!next-input-character;          !!!next-input-character;
2385    
2386          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2387    
2388          redo A;          redo A;
2389        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2390          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2391          $self->{current_token}->{data} .= '-'; # comment          !!!parse-error (type => 'dash in comment',
2392                            line => $self->{line_prev},
2393                            column => $self->{column_prev});
2394            $self->{ct}->{data} .= '-'; # comment
2395          ## Stay in the state          ## Stay in the state
2396          !!!next-input-character;          !!!next-input-character;
2397          redo A;          redo A;
2398        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2399            !!!cp (153);
2400          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2401          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2402          ## reconsume          ## reconsume
2403    
2404          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2405    
2406          redo A;          redo A;
2407        } else {        } else {
2408          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2409          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2410          $self->{state} = 'comment';                          line => $self->{line_prev},
2411                            column => $self->{column_prev});
2412            $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2413            $self->{state} = COMMENT_STATE;
2414          !!!next-input-character;          !!!next-input-character;
2415          redo A;          redo A;
2416        }        }
2417      } elsif ($self->{state} eq 'DOCTYPE') {      } elsif ($self->{state} == DOCTYPE_STATE) {
2418        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
2419            $self->{next_input_character} == 0x000A or # LF          !!!cp (155);
2420            $self->{next_input_character} == 0x000B or # VT          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         $self->{state} = 'before DOCTYPE name';  
2421          !!!next-input-character;          !!!next-input-character;
2422          redo A;          redo A;
2423        } else {        } else {
2424            !!!cp (156);
2425          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2426          $self->{state} = 'before DOCTYPE name';          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2427          ## reconsume          ## reconsume
2428          redo A;          redo A;
2429        }        }
2430      } elsif ($self->{state} eq 'before DOCTYPE name') {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2431        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
2432            $self->{next_input_character} == 0x000A or # LF          !!!cp (157);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
2433          ## Stay in the state          ## Stay in the state
2434          !!!next-input-character;          !!!next-input-character;
2435          redo A;          redo A;
2436        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2437            !!!cp (158);
2438          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2439          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2440          !!!next-input-character;          !!!next-input-character;
2441    
2442          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2443    
2444          redo A;          redo A;
2445        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2446            !!!cp (159);
2447          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2448          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2449          ## reconsume          ## reconsume
2450    
2451          !!!emit ({type => 'DOCTYPE'}); # incorrect          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2452    
2453          redo A;          redo A;
2454        } else {        } else {
2455          $self->{current_token}          !!!cp (160);
2456              = {type => 'DOCTYPE',          $self->{ct}->{name} = chr $self->{nc};
2457                 name => chr ($self->{next_input_character}),          delete $self->{ct}->{quirks};
                correct => 1};  
2458  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2459          $self->{state} = 'DOCTYPE name';          $self->{state} = DOCTYPE_NAME_STATE;
2460          !!!next-input-character;          !!!next-input-character;
2461          redo A;          redo A;
2462        }        }
2463      } elsif ($self->{state} eq 'DOCTYPE name') {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2464  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2465        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
2466            $self->{next_input_character} == 0x000A or # LF          !!!cp (161);
2467            $self->{next_input_character} == 0x000B or # VT          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
         $self->{state} = 'after DOCTYPE name';  
2468          !!!next-input-character;          !!!next-input-character;
2469          redo A;          redo A;
2470        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2471          $self->{state} = 'data';          !!!cp (162);
2472            $self->{state} = DATA_STATE;
2473          !!!next-input-character;          !!!next-input-character;
2474    
2475          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2476    
2477          redo A;          redo A;
2478        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2479            !!!cp (163);
2480          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2481          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2482          ## reconsume          ## reconsume
2483    
2484          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2485          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2486    
2487          redo A;          redo A;
2488        } else {        } else {
2489          $self->{current_token}->{name}          !!!cp (164);
2490            .= chr ($self->{next_input_character}); # DOCTYPE          $self->{ct}->{name}
2491              .= chr ($self->{nc}); # DOCTYPE
2492          ## Stay in the state          ## Stay in the state
2493          !!!next-input-character;          !!!next-input-character;
2494          redo A;          redo A;
2495        }        }
2496      } elsif ($self->{state} eq 'after DOCTYPE name') {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2497        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
2498            $self->{next_input_character} == 0x000A or # LF          !!!cp (165);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
2499          ## Stay in the state          ## Stay in the state
2500          !!!next-input-character;          !!!next-input-character;
2501          redo A;          redo A;
2502        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2503          $self->{state} = 'data';          !!!cp (166);
2504            $self->{state} = DATA_STATE;
2505          !!!next-input-character;          !!!next-input-character;
2506    
2507          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2508    
2509          redo A;          redo A;
2510        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2511            !!!cp (167);
2512          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2513          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2514          ## reconsume          ## reconsume
2515    
2516          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2517          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2518    
2519          redo A;          redo A;
2520        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{nc} == 0x0050 or # P
2521                 $self->{next_input_character} == 0x0070) { # p                 $self->{nc} == 0x0070) { # p
2522            $self->{state} = PUBLIC_STATE;
2523            $self->{s_kwd} = chr $self->{nc};
2524          !!!next-input-character;          !!!next-input-character;
2525          if ($self->{next_input_character} == 0x0055 or # U          redo A;
2526              $self->{next_input_character} == 0x0075) { # u        } elsif ($self->{nc} == 0x0053 or # S
2527            !!!next-input-character;                 $self->{nc} == 0x0073) { # s
2528            if ($self->{next_input_character} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2529                $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  
2530          !!!next-input-character;          !!!next-input-character;
2531          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;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
2532        } else {        } else {
2533            !!!cp (180);
2534            !!!parse-error (type => 'string after DOCTYPE name');
2535            $self->{ct}->{quirks} = 1;
2536    
2537            $self->{state} = BOGUS_DOCTYPE_STATE;
2538          !!!next-input-character;          !!!next-input-character;
2539          #          redo A;
2540          }
2541        } elsif ($self->{state} == PUBLIC_STATE) {
2542          ## ASCII case-insensitive
2543          if ($self->{nc} == [
2544                undef,
2545                0x0055, # U
2546                0x0042, # B
2547                0x004C, # L
2548                0x0049, # I
2549              ]->[length $self->{s_kwd}] or
2550              $self->{nc} == [
2551                undef,
2552                0x0075, # u
2553                0x0062, # b
2554                0x006C, # l
2555                0x0069, # i
2556              ]->[length $self->{s_kwd}]) {
2557            !!!cp (175);
2558            ## Stay in the state.
2559            $self->{s_kwd} .= chr $self->{nc};
2560            !!!next-input-character;
2561            redo A;
2562          } elsif ((length $self->{s_kwd}) == 5 and
2563                   ($self->{nc} == 0x0043 or # C
2564                    $self->{nc} == 0x0063)) { # c
2565            !!!cp (168);
2566            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2567            !!!next-input-character;
2568            redo A;
2569          } else {
2570            !!!cp (169);
2571            !!!parse-error (type => 'string after DOCTYPE name',
2572                            line => $self->{line_prev},
2573                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2574            $self->{ct}->{quirks} = 1;
2575    
2576            $self->{state} = BOGUS_DOCTYPE_STATE;
2577            ## Reconsume.
2578            redo A;
2579        }        }
2580        } elsif ($self->{state} == SYSTEM_STATE) {
2581          ## ASCII case-insensitive
2582          if ($self->{nc} == [
2583                undef,
2584                0x0059, # Y
2585                0x0053, # S
2586                0x0054, # T
2587                0x0045, # E
2588              ]->[length $self->{s_kwd}] or
2589              $self->{nc} == [
2590                undef,
2591                0x0079, # y
2592                0x0073, # s
2593                0x0074, # t
2594                0x0065, # e
2595              ]->[length $self->{s_kwd}]) {
2596            !!!cp (170);
2597            ## Stay in the state.
2598            $self->{s_kwd} .= chr $self->{nc};
2599            !!!next-input-character;
2600            redo A;
2601          } elsif ((length $self->{s_kwd}) == 5 and
2602                   ($self->{nc} == 0x004D or # M
2603                    $self->{nc} == 0x006D)) { # m
2604            !!!cp (171);
2605            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2606            !!!next-input-character;
2607            redo A;
2608          } else {
2609            !!!cp (172);
2610            !!!parse-error (type => 'string after DOCTYPE name',
2611                            line => $self->{line_prev},
2612                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2613            $self->{ct}->{quirks} = 1;
2614    
2615        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2616        $self->{state} = 'bogus DOCTYPE';          ## Reconsume.
2617        # next-input-character is already done          redo A;
2618        redo A;        }
2619      } elsif ($self->{state} eq 'before DOCTYPE public identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2620        if ({        if ($is_space->{$self->{nc}}) {
2621              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,          !!!cp (181);
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
2622          ## Stay in the state          ## Stay in the state
2623          !!!next-input-character;          !!!next-input-character;
2624          redo A;          redo A;
2625        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{nc} eq 0x0022) { # "
2626          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          !!!cp (182);
2627          $self->{state} = 'DOCTYPE public identifier (double-quoted)';          $self->{ct}->{pubid} = ''; # DOCTYPE
2628            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2629          !!!next-input-character;          !!!next-input-character;
2630          redo A;          redo A;
2631        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{nc} eq 0x0027) { # '
2632          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          !!!cp (183);
2633          $self->{state} = 'DOCTYPE public identifier (single-quoted)';          $self->{ct}->{pubid} = ''; # DOCTYPE
2634            $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2635          !!!next-input-character;          !!!next-input-character;
2636          redo A;          redo A;
2637        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{nc} eq 0x003E) { # >
2638            !!!cp (184);
2639          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2640    
2641          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2642          !!!next-input-character;          !!!next-input-character;
2643    
2644          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2645          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2646    
2647          redo A;          redo A;
2648        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2649            !!!cp (185);
2650          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2651    
2652          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2653          ## reconsume          ## reconsume
2654    
2655          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2656          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2657    
2658          redo A;          redo A;
2659        } else {        } else {
2660            !!!cp (186);
2661          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2662          $self->{state} = 'bogus DOCTYPE';          $self->{ct}->{quirks} = 1;
2663    
2664            $self->{state} = BOGUS_DOCTYPE_STATE;
2665          !!!next-input-character;          !!!next-input-character;
2666          redo A;          redo A;
2667        }        }
2668      } elsif ($self->{state} eq 'DOCTYPE public identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2669        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2670          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (187);
2671            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2672            !!!next-input-character;
2673            redo A;
2674          } elsif ($self->{nc} == 0x003E) { # >
2675            !!!cp (188);
2676            !!!parse-error (type => 'unclosed PUBLIC literal');
2677    
2678            $self->{state} = DATA_STATE;
2679          !!!next-input-character;          !!!next-input-character;
2680    
2681            $self->{ct}->{quirks} = 1;
2682            !!!emit ($self->{ct}); # DOCTYPE
2683    
2684          redo A;          redo A;
2685        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2686            !!!cp (189);
2687          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2688    
2689          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2690          ## reconsume          ## reconsume
2691    
2692          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2693          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2694    
2695          redo A;          redo A;
2696        } else {        } else {
2697          $self->{current_token}->{public_identifier} # DOCTYPE          !!!cp (190);
2698              .= chr $self->{next_input_character};          $self->{ct}->{pubid} # DOCTYPE
2699                .= chr $self->{nc};
2700            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2701                                  length $self->{ct}->{pubid});
2702    
2703          ## Stay in the state          ## Stay in the state
2704          !!!next-input-character;          !!!next-input-character;
2705          redo A;          redo A;
2706        }        }
2707      } elsif ($self->{state} eq 'DOCTYPE public identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2708        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2709          $self->{state} = 'after DOCTYPE public identifier';          !!!cp (191);
2710            $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2711          !!!next-input-character;          !!!next-input-character;
2712          redo A;          redo A;
2713        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2714            !!!cp (192);
2715            !!!parse-error (type => 'unclosed PUBLIC literal');
2716    
2717            $self->{state} = DATA_STATE;
2718            !!!next-input-character;
2719    
2720            $self->{ct}->{quirks} = 1;
2721            !!!emit ($self->{ct}); # DOCTYPE
2722    
2723            redo A;
2724          } elsif ($self->{nc} == -1) {
2725            !!!cp (193);
2726          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2727    
2728          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2729          ## reconsume          ## reconsume
2730    
2731          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2732          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2733    
2734          redo A;          redo A;
2735        } else {        } else {
2736          $self->{current_token}->{public_identifier} # DOCTYPE          !!!cp (194);
2737              .= chr $self->{next_input_character};          $self->{ct}->{pubid} # DOCTYPE
2738                .= chr $self->{nc};
2739            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2740                                  length $self->{ct}->{pubid});
2741    
2742          ## Stay in the state          ## Stay in the state
2743          !!!next-input-character;          !!!next-input-character;
2744          redo A;          redo A;
2745        }        }
2746      } elsif ($self->{state} eq 'after DOCTYPE public identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2747        if ({        if ($is_space->{$self->{nc}}) {
2748              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,          !!!cp (195);
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
2749          ## Stay in the state          ## Stay in the state
2750          !!!next-input-character;          !!!next-input-character;
2751          redo A;          redo A;
2752        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2753          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (196);
2754          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{ct}->{sysid} = ''; # DOCTYPE
2755            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2756          !!!next-input-character;          !!!next-input-character;
2757          redo A;          redo A;
2758        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2759          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (197);
2760          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{ct}->{sysid} = ''; # DOCTYPE
2761            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2762          !!!next-input-character;          !!!next-input-character;
2763          redo A;          redo A;
2764        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2765          $self->{state} = 'data';          !!!cp (198);
2766            $self->{state} = DATA_STATE;
2767          !!!next-input-character;          !!!next-input-character;
2768    
2769          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2770    
2771          redo A;          redo A;
2772        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2773            !!!cp (199);
2774          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2775    
2776          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2777          ## reconsume          ## reconsume
2778    
2779          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2780          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2781    
2782          redo A;          redo A;
2783        } else {        } else {
2784            !!!cp (200);
2785          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2786          $self->{state} = 'bogus DOCTYPE';          $self->{ct}->{quirks} = 1;
2787    
2788            $self->{state} = BOGUS_DOCTYPE_STATE;
2789          !!!next-input-character;          !!!next-input-character;
2790          redo A;          redo A;
2791        }        }
2792      } elsif ($self->{state} eq 'before DOCTYPE system identifier') {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2793        if ({        if ($is_space->{$self->{nc}}) {
2794              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,          !!!cp (201);
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
2795          ## Stay in the state          ## Stay in the state
2796          !!!next-input-character;          !!!next-input-character;
2797          redo A;          redo A;
2798        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2799          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (202);
2800          $self->{state} = 'DOCTYPE system identifier (double-quoted)';          $self->{ct}->{sysid} = ''; # DOCTYPE
2801            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2802          !!!next-input-character;          !!!next-input-character;
2803          redo A;          redo A;
2804        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2805          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (203);
2806          $self->{state} = 'DOCTYPE system identifier (single-quoted)';          $self->{ct}->{sysid} = ''; # DOCTYPE
2807            $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2808          !!!next-input-character;          !!!next-input-character;
2809          redo A;          redo A;
2810        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2811            !!!cp (204);
2812          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2813          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2814          !!!next-input-character;          !!!next-input-character;
2815    
2816          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2817          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2818    
2819          redo A;          redo A;
2820        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2821            !!!cp (205);
2822          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2823    
2824          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2825          ## reconsume          ## reconsume
2826    
2827          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2828          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2829    
2830          redo A;          redo A;
2831        } else {        } else {
2832            !!!cp (206);
2833          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2834          $self->{state} = 'bogus DOCTYPE';          $self->{ct}->{quirks} = 1;
2835    
2836            $self->{state} = BOGUS_DOCTYPE_STATE;
2837          !!!next-input-character;          !!!next-input-character;
2838          redo A;          redo A;
2839        }        }
2840      } elsif ($self->{state} eq 'DOCTYPE system identifier (double-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2841        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2842          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (207);
2843            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2844          !!!next-input-character;          !!!next-input-character;
2845          redo A;          redo A;
2846        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2847            !!!cp (208);
2848          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2849    
2850          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2851            !!!next-input-character;
2852    
2853            $self->{ct}->{quirks} = 1;
2854            !!!emit ($self->{ct}); # DOCTYPE
2855    
2856            redo A;
2857          } elsif ($self->{nc} == -1) {
2858            !!!cp (209);
2859            !!!parse-error (type => 'unclosed SYSTEM literal');
2860    
2861            $self->{state} = DATA_STATE;
2862          ## reconsume          ## reconsume
2863    
2864          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2865          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2866    
2867          redo A;          redo A;
2868        } else {        } else {
2869          $self->{current_token}->{system_identifier} # DOCTYPE          !!!cp (210);
2870              .= chr $self->{next_input_character};          $self->{ct}->{sysid} # DOCTYPE
2871                .= chr $self->{nc};
2872            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2873                                  length $self->{ct}->{sysid});
2874    
2875          ## Stay in the state          ## Stay in the state
2876          !!!next-input-character;          !!!next-input-character;
2877          redo A;          redo A;
2878        }        }
2879      } elsif ($self->{state} eq 'DOCTYPE system identifier (single-quoted)') {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2880        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2881          $self->{state} = 'after DOCTYPE system identifier';          !!!cp (211);
2882            $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2883          !!!next-input-character;          !!!next-input-character;
2884          redo A;          redo A;
2885        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2886            !!!cp (212);
2887          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2888    
2889          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2890            !!!next-input-character;
2891    
2892            $self->{ct}->{quirks} = 1;
2893            !!!emit ($self->{ct}); # DOCTYPE
2894    
2895            redo A;
2896          } elsif ($self->{nc} == -1) {
2897            !!!cp (213);
2898            !!!parse-error (type => 'unclosed SYSTEM literal');
2899    
2900            $self->{state} = DATA_STATE;
2901          ## reconsume          ## reconsume
2902    
2903          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2904          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2905    
2906          redo A;          redo A;
2907        } else {        } else {
2908          $self->{current_token}->{system_identifier} # DOCTYPE          !!!cp (214);
2909              .= chr $self->{next_input_character};          $self->{ct}->{sysid} # DOCTYPE
2910                .= chr $self->{nc};
2911            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2912                                  length $self->{ct}->{sysid});
2913    
2914          ## Stay in the state          ## Stay in the state
2915          !!!next-input-character;          !!!next-input-character;
2916          redo A;          redo A;
2917        }        }
2918      } elsif ($self->{state} eq 'after DOCTYPE system identifier') {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2919        if ({        if ($is_space->{$self->{nc}}) {
2920              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,          !!!cp (215);
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
2921          ## Stay in the state          ## Stay in the state
2922          !!!next-input-character;          !!!next-input-character;
2923          redo A;          redo A;
2924        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2925          $self->{state} = 'data';          !!!cp (216);
2926            $self->{state} = DATA_STATE;
2927          !!!next-input-character;          !!!next-input-character;
2928    
2929          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2930    
2931          redo A;          redo A;
2932        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2933            !!!cp (217);
2934          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2935            $self->{state} = DATA_STATE;
         $self->{state} = 'data';  
2936          ## reconsume          ## reconsume
2937    
2938          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2939          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2940    
2941          redo A;          redo A;
2942        } else {        } else {
2943            !!!cp (218);
2944          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2945          $self->{state} = 'bogus DOCTYPE';          #$self->{ct}->{quirks} = 1;
2946    
2947            $self->{state} = BOGUS_DOCTYPE_STATE;
2948          !!!next-input-character;          !!!next-input-character;
2949          redo A;          redo A;
2950        }        }
2951      } elsif ($self->{state} eq 'bogus DOCTYPE') {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2952        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2953          $self->{state} = 'data';          !!!cp (219);
2954            $self->{state} = DATA_STATE;
2955          !!!next-input-character;          !!!next-input-character;
2956    
2957          delete $self->{current_token}->{correct};          !!!emit ($self->{ct}); # DOCTYPE
         !!!emit ($self->{current_token}); # DOCTYPE  
2958    
2959          redo A;          redo A;
2960        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2961            !!!cp (220);
2962          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2963          $self->{state} = 'data';          $self->{state} = DATA_STATE;
2964          ## reconsume          ## reconsume
2965    
2966          delete $self->{current_token}->{correct};          !!!emit ($self->{ct}); # DOCTYPE
         !!!emit ($self->{current_token}); # DOCTYPE  
2967    
2968          redo A;          redo A;
2969        } else {        } else {
2970            !!!cp (221);
2971            my $s = '';
2972            $self->{read_until}->($s, q[>], 0);
2973    
2974          ## Stay in the state          ## Stay in the state
2975          !!!next-input-character;          !!!next-input-character;
2976          redo A;          redo A;
2977        }        }
2978      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2979        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2980      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2981    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2982          
2983    die "$0: _get_next_token: unexpected case";        if ($self->{nc} == 0x005D) { # ]
2984  } # _get_next_token          !!!cp (221.1);
2985            $self->{state} = CDATA_SECTION_MSE1_STATE;
2986            !!!next-input-character;
2987            redo A;
2988          } elsif ($self->{nc} == -1) {
2989            $self->{state} = DATA_STATE;
2990            !!!next-input-character;
2991            if (length $self->{ct}->{data}) { # character
2992              !!!cp (221.2);
2993              !!!emit ($self->{ct}); # character
2994            } else {
2995              !!!cp (221.3);
2996              ## No token to emit. $self->{ct} is discarded.
2997            }        
2998            redo A;
2999          } else {
3000            !!!cp (221.4);
3001            $self->{ct}->{data} .= chr $self->{nc};
3002            $self->{read_until}->($self->{ct}->{data},
3003                                  q<]>,
3004                                  length $self->{ct}->{data});
3005    
3006  sub _tokenize_attempt_to_consume_an_entity ($$) {          ## Stay in the state.
3007    my ($self, $in_attr) = @_;          !!!next-input-character;
3008            redo A;
3009          }
3010    
3011    if ({        ## ISSUE: "text tokens" in spec.
3012         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
3013         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{nc} == 0x005D) { # ]
3014        }->{$self->{next_input_character}}) {          !!!cp (221.5);
3015      ## Don't consume          $self->{state} = CDATA_SECTION_MSE2_STATE;
3016      ## No error          !!!next-input-character;
3017      return undef;          redo A;
3018    } elsif ($self->{next_input_character} == 0x0023) { # #        } else {
3019      !!!next-input-character;          !!!cp (221.6);
3020      if ($self->{next_input_character} == 0x0078 or # x          $self->{ct}->{data} .= ']';
3021          $self->{next_input_character} == 0x0058) { # X          $self->{state} = CDATA_SECTION_STATE;
3022        my $code;          ## Reconsume.
3023        X: {          redo A;
3024          my $x_char = $self->{next_input_character};        }
3025          !!!next-input-character;      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3026          if (0x0030 <= $self->{next_input_character} and        if ($self->{nc} == 0x003E) { # >
3027              $self->{next_input_character} <= 0x0039) { # 0..9          $self->{state} = DATA_STATE;
3028            $code ||= 0;          !!!next-input-character;
3029            $code *= 0x10;          if (length $self->{ct}->{data}) { # character
3030            $code += $self->{next_input_character} - 0x0030;            !!!cp (221.7);
3031            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');  
           !!!back-next-input-character ($x_char, $self->{next_input_character});  
           $self->{next_input_character} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_input_character} == 0x003B) { # ;  
           !!!next-input-character;  
3032          } else {          } else {
3033            !!!parse-error (type => 'no refc');            !!!cp (221.8);
3034              ## No token to emit. $self->{ct} is discarded.
3035          }          }
3036            redo A;
3037          } elsif ($self->{nc} == 0x005D) { # ]
3038            !!!cp (221.9); # character
3039            $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3040            ## Stay in the state.
3041            !!!next-input-character;
3042            redo A;
3043          } else {
3044            !!!cp (221.11);
3045            $self->{ct}->{data} .= ']]'; # character
3046            $self->{state} = CDATA_SECTION_STATE;
3047            ## Reconsume.
3048            redo A;
3049          }
3050        } elsif ($self->{state} == ENTITY_STATE) {
3051          if ($is_space->{$self->{nc}} or
3052              {
3053                0x003C => 1, 0x0026 => 1, -1 => 1, # <, &
3054                $self->{entity_add} => 1,
3055              }->{$self->{nc}}) {
3056            !!!cp (1001);
3057            ## Don't consume
3058            ## No error
3059            ## Return nothing.
3060            #
3061          } elsif ($self->{nc} == 0x0023) { # #
3062            !!!cp (999);
3063            $self->{state} = ENTITY_HASH_STATE;
3064            $self->{s_kwd} = '#';
3065            !!!next-input-character;
3066            redo A;
3067          } elsif ((0x0041 <= $self->{nc} and
3068                    $self->{nc} <= 0x005A) or # A..Z
3069                   (0x0061 <= $self->{nc} and
3070                    $self->{nc} <= 0x007A)) { # a..z
3071            !!!cp (998);
3072            require Whatpm::_NamedEntityList;
3073            $self->{state} = ENTITY_NAME_STATE;
3074            $self->{s_kwd} = chr $self->{nc};
3075            $self->{entity__value} = $self->{s_kwd};
3076            $self->{entity__match} = 0;
3077            !!!next-input-character;
3078            redo A;
3079          } else {
3080            !!!cp (1027);
3081            !!!parse-error (type => 'bare ero');
3082            ## Return nothing.
3083            #
3084          }
3085    
3086          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3087            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## reference" algorithm.  In other word, there is an "&" character
3088            $code = 0xFFFD;        ## that does not introduce a character reference, which would be
3089          } elsif ($code > 0x10FFFF) {        ## appended to the parent element or the attribute value in later
3090            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        ## process of the tokenizer.
3091            $code = 0xFFFD;  
3092          } elsif ($code == 0x000D) {        if ($self->{prev_state} == DATA_STATE) {
3093            !!!parse-error (type => 'CR character reference');          !!!cp (997);
3094            $code = 0x000A;          $self->{state} = $self->{prev_state};
3095          } elsif (0x80 <= $code and $code <= 0x9F) {          ## Reconsume.
3096            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3097            $code = $c1_entity_char->{$code};                    line => $self->{line_prev},
3098          }                    column => $self->{column_prev},
3099                     });
3100          return {type => 'character', data => chr $code};          redo A;
3101        } # X        } else {
3102      } elsif (0x0030 <= $self->{next_input_character} and          !!!cp (996);
3103               $self->{next_input_character} <= 0x0039) { # 0..9          $self->{ca}->{value} .= '&';
3104        my $code = $self->{next_input_character} - 0x0030;          $self->{state} = $self->{prev_state};
3105        !!!next-input-character;          ## Reconsume.
3106                  redo A;
3107        while (0x0030 <= $self->{next_input_character} and        }
3108                  $self->{next_input_character} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3109          $code *= 10;        if ($self->{nc} == 0x0078 or # x
3110          $code += $self->{next_input_character} - 0x0030;            $self->{nc} == 0x0058) { # X
3111            !!!cp (995);
3112            $self->{state} = HEXREF_X_STATE;
3113            $self->{s_kwd} .= chr $self->{nc};
3114            !!!next-input-character;
3115            redo A;
3116          } elsif (0x0030 <= $self->{nc} and
3117                   $self->{nc} <= 0x0039) { # 0..9
3118            !!!cp (994);
3119            $self->{state} = NCR_NUM_STATE;
3120            $self->{s_kwd} = $self->{nc} - 0x0030;
3121            !!!next-input-character;
3122            redo A;
3123          } else {
3124            !!!parse-error (type => 'bare nero',
3125                            line => $self->{line_prev},
3126                            column => $self->{column_prev} - 1);
3127    
3128            ## NOTE: According to the spec algorithm, nothing is returned,
3129            ## and then "&#" is appended to the parent element or the attribute
3130            ## value in the later processing.
3131    
3132            if ($self->{prev_state} == DATA_STATE) {
3133              !!!cp (1019);
3134              $self->{state} = $self->{prev_state};
3135              ## Reconsume.
3136              !!!emit ({type => CHARACTER_TOKEN,
3137                        data => '&#',
3138                        line => $self->{line_prev},
3139                        column => $self->{column_prev} - 1,
3140                       });
3141              redo A;
3142            } else {
3143              !!!cp (993);
3144              $self->{ca}->{value} .= '&#';
3145              $self->{state} = $self->{prev_state};
3146              ## Reconsume.
3147              redo A;
3148            }
3149          }
3150        } elsif ($self->{state} == NCR_NUM_STATE) {
3151          if (0x0030 <= $self->{nc} and
3152              $self->{nc} <= 0x0039) { # 0..9
3153            !!!cp (1012);
3154            $self->{s_kwd} *= 10;
3155            $self->{s_kwd} += $self->{nc} - 0x0030;
3156                    
3157            ## Stay in the state.
3158          !!!next-input-character;          !!!next-input-character;
3159            redo A;
3160          } elsif ($self->{nc} == 0x003B) { # ;
3161            !!!cp (1013);
3162            !!!next-input-character;
3163            #
3164          } else {
3165            !!!cp (1014);
3166            !!!parse-error (type => 'no refc');
3167            ## Reconsume.
3168            #
3169        }        }
3170    
3171        if ($self->{next_input_character} == 0x003B) { # ;        my $code = $self->{s_kwd};
3172          my $l = $self->{line_prev};
3173          my $c = $self->{column_prev};
3174          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3175            !!!cp (1015);
3176            !!!parse-error (type => 'invalid character reference',
3177                            text => (sprintf 'U+%04X', $code),
3178                            line => $l, column => $c);
3179            $code = 0xFFFD;
3180          } elsif ($code > 0x10FFFF) {
3181            !!!cp (1016);
3182            !!!parse-error (type => 'invalid character reference',
3183                            text => (sprintf 'U-%08X', $code),
3184                            line => $l, column => $c);
3185            $code = 0xFFFD;
3186          } elsif ($code == 0x000D) {
3187            !!!cp (1017);
3188            !!!parse-error (type => 'CR character reference',
3189                            line => $l, column => $c);
3190            $code = 0x000A;
3191          } elsif (0x80 <= $code and $code <= 0x9F) {
3192            !!!cp (1018);
3193            !!!parse-error (type => 'C1 character reference',
3194                            text => (sprintf 'U+%04X', $code),
3195                            line => $l, column => $c);
3196            $code = $c1_entity_char->{$code};
3197          }
3198    
3199          if ($self->{prev_state} == DATA_STATE) {
3200            !!!cp (992);
3201            $self->{state} = $self->{prev_state};
3202            ## Reconsume.
3203            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3204                      line => $l, column => $c,
3205                     });
3206            redo A;
3207          } else {
3208            !!!cp (991);
3209            $self->{ca}->{value} .= chr $code;
3210            $self->{ca}->{has_reference} = 1;
3211            $self->{state} = $self->{prev_state};
3212            ## Reconsume.
3213            redo A;
3214          }
3215        } elsif ($self->{state} == HEXREF_X_STATE) {
3216          if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3217              (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3218              (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3219            # 0..9, A..F, a..f
3220            !!!cp (990);
3221            $self->{state} = HEXREF_HEX_STATE;
3222            $self->{s_kwd} = 0;
3223            ## Reconsume.
3224            redo A;
3225          } else {
3226            !!!parse-error (type => 'bare hcro',
3227                            line => $self->{line_prev},
3228                            column => $self->{column_prev} - 2);
3229    
3230            ## NOTE: According to the spec algorithm, nothing is returned,
3231            ## and then "&#" followed by "X" or "x" is appended to the parent
3232            ## element or the attribute value in the later processing.
3233    
3234            if ($self->{prev_state} == DATA_STATE) {
3235              !!!cp (1005);
3236              $self->{state} = $self->{prev_state};
3237              ## Reconsume.
3238              !!!emit ({type => CHARACTER_TOKEN,
3239                        data => '&' . $self->{s_kwd},
3240                        line => $self->{line_prev},
3241                        column => $self->{column_prev} - length $self->{s_kwd},
3242                       });
3243              redo A;
3244            } else {
3245              !!!cp (989);
3246              $self->{ca}->{value} .= '&' . $self->{s_kwd};
3247              $self->{state} = $self->{prev_state};
3248              ## Reconsume.
3249              redo A;
3250            }
3251          }
3252        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3253          if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3254            # 0..9
3255            !!!cp (1002);
3256            $self->{s_kwd} *= 0x10;
3257            $self->{s_kwd} += $self->{nc} - 0x0030;
3258            ## Stay in the state.
3259            !!!next-input-character;
3260            redo A;
3261          } elsif (0x0061 <= $self->{nc} and
3262                   $self->{nc} <= 0x0066) { # a..f
3263            !!!cp (1003);
3264            $self->{s_kwd} *= 0x10;
3265            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3266            ## Stay in the state.
3267            !!!next-input-character;
3268            redo A;
3269          } elsif (0x0041 <= $self->{nc} and
3270                   $self->{nc} <= 0x0046) { # A..F
3271            !!!cp (1004);
3272            $self->{s_kwd} *= 0x10;
3273            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3274            ## Stay in the state.
3275            !!!next-input-character;
3276            redo A;
3277          } elsif ($self->{nc} == 0x003B) { # ;
3278            !!!cp (1006);
3279          !!!next-input-character;          !!!next-input-character;
3280            #
3281        } else {        } else {
3282          !!!parse-error (type => 'no refc');          !!!cp (1007);
3283            !!!parse-error (type => 'no refc',
3284                            line => $self->{line},
3285                            column => $self->{column});
3286            ## Reconsume.
3287            #
3288        }        }
3289    
3290          my $code = $self->{s_kwd};
3291          my $l = $self->{line_prev};
3292          my $c = $self->{column_prev};
3293        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3294          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1008);
3295            !!!parse-error (type => 'invalid character reference',
3296                            text => (sprintf 'U+%04X', $code),
3297                            line => $l, column => $c);
3298          $code = 0xFFFD;          $code = 0xFFFD;
3299        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3300          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1009);
3301            !!!parse-error (type => 'invalid character reference',
3302                            text => (sprintf 'U-%08X', $code),
3303                            line => $l, column => $c);
3304          $code = 0xFFFD;          $code = 0xFFFD;
3305        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3306          !!!parse-error (type => 'CR character reference');          !!!cp (1010);
3307            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3308          $code = 0x000A;          $code = 0x000A;
3309        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3310          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1011);
3311            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3312          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3313        }        }
3314          
3315        return {type => 'character', data => chr $code};        if ($self->{prev_state} == DATA_STATE) {
3316      } else {          !!!cp (988);
3317        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3318        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3319        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3320        return undef;                    line => $l, column => $c,
3321      }                   });
3322    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
3323              $self->{next_input_character} <= 0x005A) or        } else {
3324             (0x0061 <= $self->{next_input_character} and          !!!cp (987);
3325              $self->{next_input_character} <= 0x007A)) {          $self->{ca}->{value} .= chr $code;
3326      my $entity_name = chr $self->{next_input_character};          $self->{ca}->{has_reference} = 1;
3327      !!!next-input-character;          $self->{state} = $self->{prev_state};
3328            ## Reconsume.
3329      my $value = $entity_name;          redo A;
3330      my $match = 0;        }
3331      require Whatpm::_NamedEntityList;      } elsif ($self->{state} == ENTITY_NAME_STATE) {
3332      our $EntityChar;        if (length $self->{s_kwd} < 30 and
3333              ## NOTE: Some number greater than the maximum length of entity name
3334      while (length $entity_name < 10 and            ((0x0041 <= $self->{nc} and # a
3335             ## NOTE: Some number greater than the maximum length of entity name              $self->{nc} <= 0x005A) or # x
3336             ((0x0041 <= $self->{next_input_character} and # a             (0x0061 <= $self->{nc} and # a
3337               $self->{next_input_character} <= 0x005A) or # x              $self->{nc} <= 0x007A) or # z
3338              (0x0061 <= $self->{next_input_character} and # a             (0x0030 <= $self->{nc} and # 0
3339               $self->{next_input_character} <= 0x007A) or # z              $self->{nc} <= 0x0039) or # 9
3340              (0x0030 <= $self->{next_input_character} and # 0             $self->{nc} == 0x003B)) { # ;
3341               $self->{next_input_character} <= 0x0039) or # 9          our $EntityChar;
3342              $self->{next_input_character} == 0x003B)) { # ;          $self->{s_kwd} .= chr $self->{nc};
3343        $entity_name .= chr $self->{next_input_character};          if (defined $EntityChar->{$self->{s_kwd}}) {
3344        if (defined $EntityChar->{$entity_name}) {            if ($self->{nc} == 0x003B) { # ;
3345          if ($self->{next_input_character} == 0x003B) { # ;              !!!cp (1020);
3346            $value = $EntityChar->{$entity_name};              $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3347            $match = 1;              $self->{entity__match} = 1;
3348            !!!next-input-character;              !!!next-input-character;
3349            last;              #
3350              } else {
3351                !!!cp (1021);
3352                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3353                $self->{entity__match} = -1;
3354                ## Stay in the state.
3355                !!!next-input-character;
3356                redo A;
3357              }
3358          } else {          } else {
3359            $value = $EntityChar->{$entity_name};            !!!cp (1022);
3360            $match = -1;            $self->{entity__value} .= chr $self->{nc};
3361              $self->{entity__match} *= 2;
3362              ## Stay in the state.
3363            !!!next-input-character;            !!!next-input-character;
3364              redo A;
3365          }          }
       } else {  
         $value .= chr $self->{next_input_character};  
         $match *= 2;  
         !!!next-input-character;  
3366        }        }
3367      }  
3368              my $data;
3369      if ($match > 0) {        my $has_ref;
3370        return {type => 'character', data => $value};        if ($self->{entity__match} > 0) {
3371      } elsif ($match < 0) {          !!!cp (1023);
3372        !!!parse-error (type => 'no refc');          $data = $self->{entity__value};
3373        if ($in_attr and $match < -1) {          $has_ref = 1;
3374          return {type => 'character', data => '&'.$entity_name};          #
3375          } elsif ($self->{entity__match} < 0) {
3376            !!!parse-error (type => 'no refc');
3377            if ($self->{prev_state} != DATA_STATE and # in attribute
3378                $self->{entity__match} < -1) {
3379              !!!cp (1024);
3380              $data = '&' . $self->{s_kwd};
3381              #
3382            } else {
3383              !!!cp (1025);
3384              $data = $self->{entity__value};
3385              $has_ref = 1;
3386              #
3387            }
3388        } else {        } else {
3389          return {type => 'character', data => $value};          !!!cp (1026);
3390            !!!parse-error (type => 'bare ero',
3391                            line => $self->{line_prev},
3392                            column => $self->{column_prev} - length $self->{s_kwd});
3393            $data = '&' . $self->{s_kwd};
3394            #
3395          }
3396      
3397          ## NOTE: In these cases, when a character reference is found,
3398          ## it is consumed and a character token is returned, or, otherwise,
3399          ## nothing is consumed and returned, according to the spec algorithm.
3400          ## In this implementation, anything that has been examined by the
3401          ## tokenizer is appended to the parent element or the attribute value
3402          ## as string, either literal string when no character reference or
3403          ## entity-replaced string otherwise, in this stage, since any characters
3404          ## that would not be consumed are appended in the data state or in an
3405          ## appropriate attribute value state anyway.
3406    
3407          if ($self->{prev_state} == DATA_STATE) {
3408            !!!cp (986);
3409            $self->{state} = $self->{prev_state};
3410            ## Reconsume.
3411            !!!emit ({type => CHARACTER_TOKEN,
3412                      data => $data,
3413                      line => $self->{line_prev},
3414                      column => $self->{column_prev} + 1 - length $self->{s_kwd},
3415                     });
3416            redo A;
3417          } else {
3418            !!!cp (985);
3419            $self->{ca}->{value} .= $data;
3420            $self->{ca}->{has_reference} = 1 if $has_ref;
3421            $self->{state} = $self->{prev_state};
3422            ## Reconsume.
3423            redo A;
3424        }        }
3425      } else {      } else {
3426        !!!parse-error (type => 'bare ero');        die "$0: $self->{state}: Unknown state";
       ## NOTE: No characters are consumed in the spec.  
       return {type => 'character', data => '&'.$value};  
3427      }      }
3428    } else {    } # A  
3429      ## no characters are consumed  
3430      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3431      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3432    
3433  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3434    my $self = shift;    my $self = shift;
# Line 1780  sub _initialize_tree_constructor ($) { Line 3437  sub _initialize_tree_constructor ($) {
3437    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3438    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3439    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3440      $self->{document}->set_user_data (manakai_source_line => 1);
3441      $self->{document}->set_user_data (manakai_source_column => 1);
3442  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3443    
3444  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1806  sub _construct_tree ($) { Line 3465  sub _construct_tree ($) {
3465        
3466    !!!next-token;    !!!next-token;
3467    
   $self->{insertion_mode} = 'before head';  
3468    undef $self->{form_element};    undef $self->{form_element};
3469    undef $self->{head_element};    undef $self->{head_element};
3470    $self->{open_elements} = [];    $self->{open_elements} = [];
3471    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3472    
3473      ## NOTE: The "initial" insertion mode.
3474    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3475    
3476      ## NOTE: The "before html" insertion mode.
3477    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3478      $self->{insertion_mode} = BEFORE_HEAD_IM;
3479    
3480      ## NOTE: The "before head" insertion mode and so on.
3481    $self->_tree_construction_main;    $self->_tree_construction_main;
3482  } # _construct_tree  } # _construct_tree
3483    
3484  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3485    my $self = shift;    my $self = shift;
3486    
3487      ## NOTE: "initial" insertion mode
3488    
3489    INITIAL: {    INITIAL: {
3490      if ($token->{type} eq 'DOCTYPE') {      if ($token->{type} == DOCTYPE_TOKEN) {
3491        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
3492        ## error, switch to a conformance checking mode for another        ## error, switch to a conformance checking mode for another
3493        ## language.        ## language.
3494        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3495        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3496        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3497        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3498            defined $token->{public_identifier} or            defined $token->{sysid}) {
3499            defined $token->{system_identifier}) {          !!!cp ('t1');
3500          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3501        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3502          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3503          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3504          } elsif (defined $token->{pubid}) {
3505            if ($token->{pubid} eq 'XSLT-compat') {
3506              !!!cp ('t1.2');
3507              !!!parse-error (type => 'XSLT-compat', token => $token,
3508                              level => $self->{level}->{should});
3509            } else {
3510              !!!parse-error (type => 'not HTML5', token => $token);
3511            }
3512          } else {
3513            !!!cp ('t3');
3514            #
3515        }        }
3516                
3517        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3518          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3519        $doctype->public_id ($token->{public_identifier})        ## NOTE: Default value for both |public_id| and |system_id| attributes
3520            if defined $token->{public_identifier};        ## are empty strings, so that we don't set any value in missing cases.
3521        $doctype->system_id ($token->{system_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3522            if defined $token->{system_identifier};        $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
3523        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
3524        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3525        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3526                
3527        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3528            !!!cp ('t4');
3529          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3530        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
3531          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
3532          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3533          if ({          my $prefix = [
3534            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3535            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3536            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3537            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3538            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3539            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3540            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3541            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3542            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3543            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3544            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3545            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3546            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3547            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3548            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3549            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3550            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3551            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3552            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3553            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3554            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3555            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3556            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3557            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3558            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3559            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3560            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3561            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3562            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3563            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3564            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3565            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3566            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3567            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3568            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3569            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3570            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3571            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3572            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3573            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3574            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3575            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3576            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3577            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3578            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3579            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3580            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3581            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3582            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3583            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3584            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3585            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3586            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3587            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3588            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3589            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3590            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3591            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3592            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3593            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3594            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3595            "-//W3C//DTD W3 HTML//EN" => 1,            }
3596            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3597            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3598            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3599            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3600            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3601            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3602            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3603          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3604                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3605            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
3606                !!!cp ('t6');
3607              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3608            } else {            } else {
3609                !!!cp ('t7');
3610              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3611            }            }
3612          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3613                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3614              !!!cp ('t8');
3615            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3616            } else {
3617              !!!cp ('t9');
3618          }          }
3619          } else {
3620            !!!cp ('t10');
3621        }        }
3622        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
3623          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
3624          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3625          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") {
3626              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3627              ## marked as quirks.
3628            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3629              !!!cp ('t11');
3630            } else {
3631              !!!cp ('t12');
3632          }          }
3633          } else {
3634            !!!cp ('t13');
3635        }        }
3636                
3637        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3638        !!!next-token;        !!!next-token;
3639        return;        return;
3640      } elsif ({      } elsif ({
3641                'start tag' => 1,                START_TAG_TOKEN, 1,
3642                'end tag' => 1,                END_TAG_TOKEN, 1,
3643                'end-of-file' => 1,                END_OF_FILE_TOKEN, 1,
3644               }->{$token->{type}}) {               }->{$token->{type}}) {
3645        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3646          !!!parse-error (type => 'no DOCTYPE', token => $token);
3647        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3648        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3649        ## reprocess        ## reprocess
3650          !!!ack-later;
3651        return;        return;
3652      } elsif ($token->{type} eq 'character') {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3653        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3654          ## Ignore the token          ## Ignore the token
3655    
3656          unless (length $token->{data}) {          unless (length $token->{data}) {
3657            ## Stay in the phase            !!!cp ('t15');
3658              ## Stay in the insertion mode.
3659            !!!next-token;            !!!next-token;
3660            redo INITIAL;            redo INITIAL;
3661            } else {
3662              !!!cp ('t16');
3663          }          }
3664          } else {
3665            !!!cp ('t17');
3666        }        }
3667    
3668        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3669        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3670        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3671        ## reprocess        ## reprocess
3672        return;        return;
3673      } elsif ($token->{type} eq 'comment') {      } elsif ($token->{type} == COMMENT_TOKEN) {
3674          !!!cp ('t18');
3675        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3676        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3677                
3678        ## Stay in the phase.        ## Stay in the insertion mode.
3679        !!!next-token;        !!!next-token;
3680        redo INITIAL;        redo INITIAL;
3681      } else {      } else {
3682        die "$0: $token->{type}: Unknown token";        die "$0: $token->{type}: Unknown token type";
3683      }      }
3684    } # INITIAL    } # INITIAL
3685    
3686      die "$0: _tree_construction_initial: This should be never reached";
3687  } # _tree_construction_initial  } # _tree_construction_initial
3688    
3689  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3690    my $self = shift;    my $self = shift;
3691    
3692      ## NOTE: "before html" insertion mode.
3693        
3694    B: {    B: {
3695        if ($token->{type} eq 'DOCTYPE') {        if ($token->{type} == DOCTYPE_TOKEN) {
3696          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3697            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3698          ## Ignore the token          ## Ignore the token
3699          ## Stay in the phase          ## Stay in the insertion mode.
3700          !!!next-token;          !!!next-token;
3701          redo B;          redo B;
3702        } elsif ($token->{type} eq 'comment') {        } elsif ($token->{type} == COMMENT_TOKEN) {
3703            !!!cp ('t20');
3704          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3705          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
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 'character') {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3710          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3711            ## Ignore the token.            ## Ignore the token.
3712    
3713            unless (length $token->{data}) {            unless (length $token->{data}) {
3714              ## Stay in the phase              !!!cp ('t21');
3715                ## Stay in the insertion mode.
3716              !!!next-token;              !!!next-token;
3717              redo B;              redo B;
3718              } else {
3719                !!!cp ('t22');
3720            }            }
3721            } else {
3722              !!!cp ('t23');
3723          }          }
3724    
3725            $self->{application_cache_selection}->(undef);
3726    
3727          #          #
3728          } elsif ($token->{type} == START_TAG_TOKEN) {
3729            if ($token->{tag_name} eq 'html') {
3730              my $root_element;
3731              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3732              $self->{document}->append_child ($root_element);
3733              push @{$self->{open_elements}},
3734                  [$root_element, $el_category->{html}];
3735    
3736              if ($token->{attributes}->{manifest}) {
3737                !!!cp ('t24');
3738                $self->{application_cache_selection}
3739                    ->($token->{attributes}->{manifest}->{value});
3740                ## ISSUE: Spec is unclear on relative references.
3741                ## According to Hixie (#whatwg 2008-03-19), it should be
3742                ## resolved against the base URI of the document in HTML
3743                ## or xml:base of the element in XHTML.
3744              } else {
3745                !!!cp ('t25');
3746                $self->{application_cache_selection}->(undef);
3747              }
3748    
3749              !!!nack ('t25c');
3750    
3751              !!!next-token;
3752              return; ## Go to the "before head" insertion mode.
3753            } else {
3754              !!!cp ('t25.1');
3755              #
3756            }
3757        } elsif ({        } elsif ({
3758                  'start tag' => 1,                  END_TAG_TOKEN, 1,
3759                  'end tag' => 1,                  END_OF_FILE_TOKEN, 1,
                 'end-of-file' => 1,  
3760                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3761          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3762          #          #
3763        } else {        } else {
3764          die "$0: $token->{type}: Unknown token";          die "$0: $token->{type}: Unknown token type";
3765        }        }
3766        my $root_element; !!!create-element ($root_element, 'html');  
3767        $self->{document}->append_child ($root_element);      my $root_element;
3768        push @{$self->{open_elements}}, [$root_element, 'html'];      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3769        ## reprocess      $self->{document}->append_child ($root_element);
3770        #redo B;      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3771        return; ## Go to the main phase.  
3772        $self->{application_cache_selection}->(undef);
3773    
3774        ## NOTE: Reprocess the token.
3775        !!!ack-later;
3776        return; ## Go to the "before head" insertion mode.
3777    
3778        ## ISSUE: There is an issue in the spec
3779    } # B    } # B
3780    
3781      die "$0: _tree_construction_root_element: This should never be reached";
3782  } # _tree_construction_root_element  } # _tree_construction_root_element
3783    
3784  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2043  sub _reset_insertion_mode ($) { Line 3793  sub _reset_insertion_mode ($) {
3793            
3794      ## Step 3      ## Step 3
3795      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"!?  
3796        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3797          $last = 1;          $last = 1;
3798          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3799            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3800                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3801              #          } else {
3802            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3803          }          }
3804        }        }
3805              
3806        ## Step 4..13        ## Step 4..14
3807        my $new_mode = {        my $new_mode;
3808                        select => 'in select',        if ($node->[1] & FOREIGN_EL) {
3809                        td => 'in cell',          !!!cp ('t28.1');
3810                        th => 'in cell',          ## NOTE: Strictly spaking, the line below only applies to MathML and
3811                        tr => 'in row',          ## SVG elements.  Currently the HTML syntax supports only MathML and
3812                        tbody => 'in table body',          ## SVG elements as foreigners.
3813                        thead => 'in table body',          $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3814                        tfoot => 'in table body',        } elsif ($node->[1] & TABLE_CELL_EL) {
3815                        caption => 'in caption',          if ($last) {
3816                        colgroup => 'in column group',            !!!cp ('t28.2');
3817                        table => 'in table',            #
3818                        head => 'in body', # not in head!          } else {
3819                        body => 'in body',            !!!cp ('t28.3');
3820                        frameset => 'in frameset',            $new_mode = IN_CELL_IM;
3821                       }->{$node->[1]};          }
3822          } else {
3823            !!!cp ('t28.4');
3824            $new_mode = {
3825                          select => IN_SELECT_IM,
3826                          ## NOTE: |option| and |optgroup| do not set
3827                          ## insertion mode to "in select" by themselves.
3828                          tr => IN_ROW_IM,
3829                          tbody => IN_TABLE_BODY_IM,
3830                          thead => IN_TABLE_BODY_IM,
3831                          tfoot => IN_TABLE_BODY_IM,
3832                          caption => IN_CAPTION_IM,
3833                          colgroup => IN_COLUMN_GROUP_IM,
3834                          table => IN_TABLE_IM,
3835                          head => IN_BODY_IM, # not in head!
3836                          body => IN_BODY_IM,
3837                          frameset => IN_FRAMESET_IM,
3838                         }->{$node->[0]->manakai_local_name};
3839          }
3840        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3841                
3842        ## Step 14        ## Step 15
3843        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3844          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3845            $self->{insertion_mode} = 'before head';            !!!cp ('t29');
3846              $self->{insertion_mode} = BEFORE_HEAD_IM;
3847          } else {          } else {
3848            $self->{insertion_mode} = 'after head';            ## ISSUE: Can this state be reached?
3849              !!!cp ('t30');
3850              $self->{insertion_mode} = AFTER_HEAD_IM;
3851          }          }
3852          return;          return;
3853          } else {
3854            !!!cp ('t31');
3855        }        }
3856                
       ## Step 15  
       $self->{insertion_mode} = 'in body' and return if $last;  
         
3857        ## Step 16        ## Step 16
3858          $self->{insertion_mode} = IN_BODY_IM and return if $last;
3859          
3860          ## Step 17
3861        $i--;        $i--;
3862        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3863                
3864        ## Step 17        ## Step 18
3865        redo S3;        redo S3;
3866      } # S3      } # S3
3867    
3868      die "$0: _reset_insertion_mode: This line should never be reached";
3869  } # _reset_insertion_mode  } # _reset_insertion_mode
3870    
3871  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
3872    my $self = shift;    my $self = shift;
3873    
   my $previous_insertion_mode;  
   
3874    my $active_formatting_elements = [];    my $active_formatting_elements = [];
3875    
3876    my $reconstruct_active_formatting_elements = sub { # MUST    my $reconstruct_active_formatting_elements = sub { # MUST
# Line 2121  sub _tree_construction_main ($) { Line 3887  sub _tree_construction_main ($) {
3887      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3888      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3889        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3890            !!!cp ('t32');
3891          return;          return;
3892        }        }
3893      }      }
# Line 2135  sub _tree_construction_main ($) { Line 3902  sub _tree_construction_main ($) {
3902    
3903        ## Step 6        ## Step 6
3904        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3905            !!!cp ('t33_1');
3906          #          #
3907        } else {        } else {
3908          my $in_open_elements;          my $in_open_elements;
3909          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3910            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3911                !!!cp ('t33');
3912              $in_open_elements = 1;              $in_open_elements = 1;
3913              last OE;              last OE;
3914            }            }
3915          }          }
3916          if ($in_open_elements) {          if ($in_open_elements) {
3917              !!!cp ('t34');
3918            #            #
3919          } else {          } else {
3920              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3921              !!!cp ('t35');
3922            redo S4;            redo S4;
3923          }          }
3924        }        }
# Line 2169  sub _tree_construction_main ($) { Line 3941  sub _tree_construction_main ($) {
3941    
3942        ## Step 11        ## Step 11
3943        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3944            !!!cp ('t36');
3945          ## Step 7'          ## Step 7'
3946          $i++;          $i++;
3947          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3948                    
3949          redo S7;          redo S7;
3950        }        }
3951    
3952          !!!cp ('t37');
3953      } # S7      } # S7
3954    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3955    
3956    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3957      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3958        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3959            !!!cp ('t38');
3960          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3961          return;          return;
3962        }        }
3963      }      }
3964    
3965        !!!cp ('t39');
3966    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3967    
3968    my $parse_rcdata = sub ($$) {    my $insert;
3969      my ($content_model_flag, $insert) = @_;  
3970      my $parse_rcdata = sub ($) {
3971        my ($content_model_flag) = @_;
3972    
3973      ## Step 1      ## Step 1
3974      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3975      my $el;      my $el;
3976      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3977    
3978      ## Step 2      ## Step 2
3979      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3980    
3981      ## Step 3      ## Step 3
3982      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2204  sub _tree_construction_main ($) { Line 3984  sub _tree_construction_main ($) {
3984    
3985      ## Step 4      ## Step 4
3986      my $text = '';      my $text = '';
3987        !!!nack ('t40.1');
3988      !!!next-token;      !!!next-token;
3989      while ($token->{type} eq 'character') { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3990          !!!cp ('t40');
3991        $text .= $token->{data};        $text .= $token->{data};
3992        !!!next-token;        !!!next-token;
3993      }      }
3994    
3995      ## Step 5      ## Step 5
3996      if (length $text) {      if (length $text) {
3997          !!!cp ('t41');
3998        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3999        $el->append_child ($text);        $el->append_child ($text);
4000      }      }
# Line 2220  sub _tree_construction_main ($) { Line 4003  sub _tree_construction_main ($) {
4003      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
4004    
4005      ## Step 7      ## Step 7
4006      if ($token->{type} eq 'end tag' and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
4007            $token->{tag_name} eq $start_tag_name) {
4008          !!!cp ('t42');
4009        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
4010      } else {      } else {
4011        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
4012          if ($content_model_flag == CDATA_CONTENT_MODEL) {
4013            !!!cp ('t43');
4014            !!!parse-error (type => 'in CDATA:#eof', token => $token);
4015          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4016            !!!cp ('t44');
4017            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4018          } else {
4019            die "$0: $content_model_flag in parse_rcdata";
4020          }
4021      }      }
4022      !!!next-token;      !!!next-token;
4023    }; # $parse_rcdata    }; # $parse_rcdata
4024    
4025    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
4026      my $script_el;      my $script_el;
4027      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4028      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4029    
4030      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
4031      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4032            
4033      my $text = '';      my $text = '';
4034        !!!nack ('t45.1');
4035      !!!next-token;      !!!next-token;
4036      while ($token->{type} eq 'character') {      while ($token->{type} == CHARACTER_TOKEN) {
4037          !!!cp ('t45');
4038        $text .= $token->{data};        $text .= $token->{data};
4039        !!!next-token;        !!!next-token;
4040      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
4041      if (length $text) {      if (length $text) {
4042          !!!cp ('t46');
4043        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
4044      }      }
4045                                
4046      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
4047    
4048      if ($token->{type} eq 'end tag' and      if ($token->{type} == END_TAG_TOKEN and
4049          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4050          !!!cp ('t47');
4051        ## Ignore the token        ## Ignore the token
4052      } else {      } else {
4053        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4054          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4055        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4056        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4057      }      }
4058            
4059      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4060          !!!cp ('t49');
4061        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4062      } else {      } else {
4063          !!!cp ('t50');
4064        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4065        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4066    
# Line 2278  sub _tree_construction_main ($) { Line 4074  sub _tree_construction_main ($) {
4074      !!!next-token;      !!!next-token;
4075    }; # $script_start_tag    }; # $script_start_tag
4076    
4077      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4078      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4079      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4080    
4081    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4082      my $tag_name = shift;      my $end_tag_token = shift;
4083        my $tag_name = $end_tag_token->{tag_name};
4084    
4085        ## NOTE: The adoption agency algorithm (AAA).
4086    
4087      FET: {      FET: {
4088        ## Step 1        ## Step 1
4089        my $formatting_element;        my $formatting_element;
4090        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4091        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4092          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4093              !!!cp ('t52');
4094              last AFE;
4095            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4096                         eq $tag_name) {
4097              !!!cp ('t51');
4098            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4099            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4100            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4101          }          }
4102        } # AFE        } # AFE
4103        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4104          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4105            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4106          ## Ignore the token          ## Ignore the token
4107          !!!next-token;          !!!next-token;
4108          return;          return;
# Line 2307  sub _tree_construction_main ($) { Line 4114  sub _tree_construction_main ($) {
4114          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4115          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4116            if ($in_scope) {            if ($in_scope) {
4117                !!!cp ('t54');
4118              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4119              last INSCOPE;              last INSCOPE;
4120            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4121              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4122                !!!parse-error (type => 'unmatched end tag',
4123                                text => $token->{tag_name},
4124                                token => $end_tag_token);
4125              ## Ignore the token              ## Ignore the token
4126              !!!next-token;              !!!next-token;
4127              return;              return;
4128            }            }
4129          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4130                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4131            $in_scope = 0;            $in_scope = 0;
4132          }          }
4133        } # INSCOPE        } # INSCOPE
4134        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4135          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4136            !!!parse-error (type => 'unmatched end tag',
4137                            text => $token->{tag_name},
4138                            token => $end_tag_token);
4139          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4140          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4141          return;          return;
4142        }        }
4143        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4144          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4145            !!!parse-error (type => 'not closed',
4146                            text => $self->{open_elements}->[-1]->[0]
4147                                ->manakai_local_name,
4148                            token => $end_tag_token);
4149        }        }
4150                
4151        ## Step 2        ## Step 2
# Line 2337  sub _tree_construction_main ($) { Line 4153  sub _tree_construction_main ($) {
4153        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4154        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4155          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4156          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4157              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4158              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4159               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4160              !!!cp ('t59');
4161            $furthest_block = $node;            $furthest_block = $node;
4162            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4163          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4164              !!!cp ('t60');
4165            last OE;            last OE;
4166          }          }
4167        } # OE        } # OE
4168                
4169        ## Step 3        ## Step 3
4170        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4171            !!!cp ('t61');
4172          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4173          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4174          !!!next-token;          !!!next-token;
# Line 2362  sub _tree_construction_main ($) { Line 4181  sub _tree_construction_main ($) {
4181        ## Step 5        ## Step 5
4182        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4183        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4184            !!!cp ('t62');
4185          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4186        }        }
4187                
# Line 2384  sub _tree_construction_main ($) { Line 4204  sub _tree_construction_main ($) {
4204          S7S2: {          S7S2: {
4205            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4206              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4207                  !!!cp ('t63');
4208                $node_i_in_active = $_;                $node_i_in_active = $_;
4209                last S7S2;                last S7S2;
4210              }              }
# Line 2397  sub _tree_construction_main ($) { Line 4218  sub _tree_construction_main ($) {
4218                    
4219          ## Step 4          ## Step 4
4220          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4221              !!!cp ('t64');
4222            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4223          }          }
4224                    
4225          ## Step 5          ## Step 5
4226          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4227              !!!cp ('t65');
4228            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4229            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4230            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2419  sub _tree_construction_main ($) { Line 4242  sub _tree_construction_main ($) {
4242        } # S7          } # S7  
4243                
4244        ## Step 8        ## Step 8
4245        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4246            my $foster_parent_element;
4247            my $next_sibling;
4248            OE: for (reverse 0..$#{$self->{open_elements}}) {
4249              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4250                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4251                                 if (defined $parent and $parent->node_type == 1) {
4252                                   !!!cp ('t65.1');
4253                                   $foster_parent_element = $parent;
4254                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4255                                 } else {
4256                                   !!!cp ('t65.2');
4257                                   $foster_parent_element
4258                                     = $self->{open_elements}->[$_ - 1]->[0];
4259                                 }
4260                                 last OE;
4261                               }
4262                             } # OE
4263                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4264                               unless defined $foster_parent_element;
4265            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4266            $open_tables->[-1]->[1] = 1; # tainted
4267          } else {
4268            !!!cp ('t65.3');
4269            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4270          }
4271                
4272        ## Step 9        ## Step 9
4273        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2436  sub _tree_construction_main ($) { Line 4284  sub _tree_construction_main ($) {
4284        my $i;        my $i;
4285        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4286          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4287              !!!cp ('t66');
4288            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4289            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4290          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4291              !!!cp ('t67');
4292            $i = $_;            $i = $_;
4293          }          }
4294        } # AFE        } # AFE
# Line 2448  sub _tree_construction_main ($) { Line 4298  sub _tree_construction_main ($) {
4298        undef $i;        undef $i;
4299        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4300          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4301              !!!cp ('t68');
4302            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4303            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4304          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4305              !!!cp ('t69');
4306            $i = $_;            $i = $_;
4307          }          }
4308        } # OE        } # OE
# Line 2461  sub _tree_construction_main ($) { Line 4313  sub _tree_construction_main ($) {
4313      } # FET      } # FET
4314    }; # $formatting_end_tag    }; # $formatting_end_tag
4315    
4316    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4317      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4318    }; # $insert_to_current    }; # $insert_to_current
4319    
4320    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4321                         my $child = shift;      my $child = shift;
4322                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4323                              table => 1, tbody => 1, tfoot => 1,        # MUST
4324                              thead => 1, tr => 1,        my $foster_parent_element;
4325                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4326                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4327                           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') {  
4328                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4329                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4330                                   !!!cp ('t70');
4331                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4332                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4333                               } else {                               } else {
4334                                   !!!cp ('t71');
4335                                 $foster_parent_element                                 $foster_parent_element
4336                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4337                               }                               }
# Line 2491  sub _tree_construction_main ($) { Line 4342  sub _tree_construction_main ($) {
4342                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4343                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4344                             ($child, $next_sibling);                             ($child, $next_sibling);
4345                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4346                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4347                         }        !!!cp ('t72');
4348          $self->{open_elements}->[-1]->[0]->append_child ($child);
4349        }
4350    }; # $insert_to_foster    }; # $insert_to_foster
4351    
4352    my $in_body = sub {    B: while (1) {
4353      my $insert = shift;      if ($token->{type} == DOCTYPE_TOKEN) {
4354      if ($token->{type} eq 'start tag') {        !!!cp ('t73');
4355        if ($token->{tag_name} eq 'script') {        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4356          ## NOTE: This is an "as if in head" code clone        ## Ignore the token
4357          $script_start_tag->($insert);        ## Stay in the phase
4358          return;        !!!next-token;
4359        } elsif ($token->{tag_name} eq 'style') {        next B;
4360          ## NOTE: This is an "as if in head" code clone      } elsif ($token->{type} == START_TAG_TOKEN and
4361          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);               $token->{tag_name} eq 'html') {
4362          return;        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4363        } elsif ({          !!!cp ('t79');
4364                  base => 1, link => 1,          !!!parse-error (type => 'after html', text => 'html', token => $token);
4365                 }->{$token->{tag_name}}) {          $self->{insertion_mode} = AFTER_BODY_IM;
4366          ## NOTE: This is an "as if in head" code clone, only "-t" differs        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4367          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!cp ('t80');
4368          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          !!!parse-error (type => 'after html', text => 'html', token => $token);
4369          !!!next-token;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4370          return;        } else {
4371        } elsif ($token->{tag_name} eq 'meta') {          !!!cp ('t81');
4372          ## NOTE: This is an "as if in head" code clone, only "-t" differs        }
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.  
   
         unless ($self->{confident}) {  
           my $charset;  
           if ($token->{attributes}->{charset}) { ## TODO: And if supported  
             $charset = $token->{attributes}->{charset}->{value};  
           }  
           if ($token->{attributes}->{'http-equiv'}) {  
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
             if ($token->{attributes}->{'http-equiv'}->{value}  
                 =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=  
                     [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|  
                     ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {  
               $charset = defined $1 ? $1 : defined $2 ? $2 : $3;  
             } ## TODO: And if supported  
           }  
           ## TODO: Change the encoding  
         }  
4373    
4374          !!!next-token;        !!!cp ('t82');
4375          return;        !!!parse-error (type => 'not first start tag', token => $token);
4376        } elsif ($token->{tag_name} eq 'title') {        my $top_el = $self->{open_elements}->[0]->[0];
4377          !!!parse-error (type => 'in body:title');        for my $attr_name (keys %{$token->{attributes}}) {
4378          ## NOTE: This is an "as if in head" code clone          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4379          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {            !!!cp ('t84');
4380            if (defined $self->{head_element}) {            $top_el->set_attribute_ns
4381              $self->{head_element}->append_child ($_[0]);              (undef, [undef, $attr_name],
4382            } else {               $token->{attributes}->{$attr_name}->{value});
             $insert->($_[0]);  
           }  
         });  
         return;  
       } elsif ($token->{tag_name} eq 'body') {  
         !!!parse-error (type => 'in body:body');  
                 
         if (@{$self->{open_elements}} == 1 or  
             $self->{open_elements}->[1]->[1] ne 'body') {  
           ## Ignore the token  
         } else {  
           my $body_el = $self->{open_elements}->[1]->[0];  
           for my $attr_name (keys %{$token->{attributes}}) {  
             unless ($body_el->has_attribute_ns (undef, $attr_name)) {  
               $body_el->set_attribute_ns  
                 (undef, [undef, $attr_name],  
                  $token->{attributes}->{$attr_name}->{value});  
             }  
           }  
         }  
         !!!next-token;  
         return;  
       } elsif ({  
                 address => 1, blockquote => 1, center => 1, dir => 1,  
                 div => 1, dl => 1, fieldset => 1, listing => 1,  
                 menu => 1, ol => 1, p => 1, ul => 1,  
                 pre => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         if ($token->{tag_name} eq 'pre') {  
           !!!next-token;  
           if ($token->{type} eq 'character') {  
             $token->{data} =~ s/^\x0A//;  
             unless (length $token->{data}) {  
               !!!next-token;  
             }  
           }  
         } else {  
           !!!next-token;  
         }  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         if (defined $self->{form_element}) {  
           !!!parse-error (type => 'in form:form');  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           ## has a p element in scope  
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => 'end tag', tag_name => 'p'};  
               return;  
             } elsif ({  
                       table => 1, caption => 1, td => 1, th => 1,  
                       button => 1, marquee => 1, object => 1, html => 1,  
                      }->{$_->[1]}) {  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
           !!!next-token;  
           return;  
4383          }          }
4384        } elsif ($token->{tag_name} eq 'li') {        }
4385          ## has a p element in scope        !!!nack ('t84.1');
4386          INSCOPE: for (reverse @{$self->{open_elements}}) {        !!!next-token;
4387            if ($_->[1] eq 'p') {        next B;
4388              !!!back-token;      } elsif ($token->{type} == COMMENT_TOKEN) {
4389              $token = {type => 'end tag', tag_name => 'p'};        my $comment = $self->{document}->create_comment ($token->{data});
4390              return;        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4391            } elsif ({          !!!cp ('t85');
4392                      table => 1, caption => 1, td => 1, th => 1,          $self->{document}->append_child ($comment);
4393                      button => 1, marquee => 1, object => 1, html => 1,        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4394                     }->{$_->[1]}) {          !!!cp ('t86');
4395              last INSCOPE;          $self->{open_elements}->[0]->[0]->append_child ($comment);
4396            }        } else {
4397          } # INSCOPE          !!!cp ('t87');
4398                      $self->{open_elements}->[-1]->[0]->append_child ($comment);
4399          ## Step 1        }
4400          my $i = -1;        !!!next-token;
4401          my $node = $self->{open_elements}->[$i];        next B;
4402          LI: {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4403            ## Step 2        if ($token->{type} == CHARACTER_TOKEN) {
4404            if ($node->[1] eq 'li') {          !!!cp ('t87.1');
4405              if ($i != -1) {          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'plaintext') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{content_model} = PLAINTEXT_CONTENT_MODEL;  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
4406          !!!next-token;          !!!next-token;
4407          return;          next B;
4408        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4409          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4410            my $node = $active_formatting_elements->[$i];               $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4411            if ($node->[1] eq 'a') {              not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4412              !!!parse-error (type => 'in a:a');              ($token->{tag_name} eq 'svg' and
4413                             $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4414              !!!back-token;            ## NOTE: "using the rules for secondary insertion mode"then"continue"
4415              $token = {type => 'end tag', tag_name => 'a'};            !!!cp ('t87.2');
4416              $formatting_end_tag->($token->{tag_name});            #
4417                        } elsif ({
4418              AFE2: for (reverse 0..$#$active_formatting_elements) {                    b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4419                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                    center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4420                  splice @$active_formatting_elements, $_, 1;                    em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4421                  last AFE2;                    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4422                }                    img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4423              } # AFE2                    nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4424              OE: for (reverse 0..$#{$self->{open_elements}}) {                    small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4425                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                    sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4426                  splice @{$self->{open_elements}}, $_, 1;                   }->{$token->{tag_name}}) {
4427                  last OE;            !!!cp ('t87.2');
4428                }            !!!parse-error (type => 'not closed',
4429              } # OE                            text => $self->{open_elements}->[-1]->[0]
4430              last AFE;                                ->manakai_local_name,
4431            } elsif ($node->[0] eq '#marker') {                            token => $token);
4432              last AFE;  
4433              pop @{$self->{open_elements}}
4434                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4435    
4436              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4437              ## Reprocess.
4438              next B;
4439            } else {
4440              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4441              my $tag_name = $token->{tag_name};
4442              if ($nsuri eq $SVG_NS) {
4443                $tag_name = {
4444                   altglyph => 'altGlyph',
4445                   altglyphdef => 'altGlyphDef',
4446                   altglyphitem => 'altGlyphItem',
4447                   animatecolor => 'animateColor',
4448                   animatemotion => 'animateMotion',
4449                   animatetransform => 'animateTransform',
4450                   clippath => 'clipPath',
4451                   feblend => 'feBlend',
4452                   fecolormatrix => 'feColorMatrix',
4453                   fecomponenttransfer => 'feComponentTransfer',
4454                   fecomposite => 'feComposite',
4455                   feconvolvematrix => 'feConvolveMatrix',
4456                   fediffuselighting => 'feDiffuseLighting',
4457                   fedisplacementmap => 'feDisplacementMap',
4458                   fedistantlight => 'feDistantLight',
4459                   feflood => 'feFlood',
4460                   fefunca => 'feFuncA',
4461                   fefuncb => 'feFuncB',
4462                   fefuncg => 'feFuncG',
4463                   fefuncr => 'feFuncR',
4464                   fegaussianblur => 'feGaussianBlur',
4465                   feimage => 'feImage',
4466                   femerge => 'feMerge',
4467                   femergenode => 'feMergeNode',
4468                   femorphology => 'feMorphology',
4469                   feoffset => 'feOffset',
4470                   fepointlight => 'fePointLight',
4471                   fespecularlighting => 'feSpecularLighting',
4472                   fespotlight => 'feSpotLight',
4473                   fetile => 'feTile',
4474                   feturbulence => 'feTurbulence',
4475                   foreignobject => 'foreignObject',
4476                   glyphref => 'glyphRef',
4477                   lineargradient => 'linearGradient',
4478                   radialgradient => 'radialGradient',
4479                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4480                   textpath => 'textPath',  
4481                }->{$tag_name} || $tag_name;
4482            }            }
         } # AFE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
4483    
4484          !!!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];  
4485    
4486          !!!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);  
4487    
4488          ## has a |nobr| element in scope            !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'nobr') {  
             !!!parse-error (type => 'not closed:nobr');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'nobr'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, $self->{open_elements}->[-1];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'button') {  
         ## has a button element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'button') {  
             !!!parse-error (type => 'in button:button');  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'button'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         $reconstruct_active_formatting_elements->($insert_to_current);  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
4489    
4490          !!!next-token;            if ($self->{self_closing}) {
4491          return;              pop @{$self->{open_elements}};
4492        } elsif ($token->{tag_name} eq 'marquee' or              !!!ack ('t87.3');
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = 'in table';  
             
         !!!next-token;  
         return;  
       } elsif ({  
                 area => 1, basefont => 1, bgsound => 1, br => 1,  
                 embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,  
                 image => 1,  
                }->{$token->{tag_name}}) {  
         if ($token->{tag_name} eq 'image') {  
           !!!parse-error (type => 'image');  
           $token->{tag_name} = 'img';  
         }  
   
         ## NOTE: There is an "as if <br>" code clone.  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => 'end tag', tag_name => 'p'};  
             return;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
             
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'input') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         ## TODO: associate with $self->{form_element} if defined  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'isindex') {  
         !!!parse-error (type => 'isindex');  
           
         if (defined $self->{form_element}) {  
           ## Ignore the token  
           !!!next-token;  
           return;  
         } else {  
           my $at = $token->{attributes};  
           my $form_attrs;  
           $form_attrs->{action} = $at->{action} if $at->{action};  
           my $prompt_attr = $at->{prompt};  
           $at->{name} = {name => 'name', value => 'isindex'};  
           delete $at->{action};  
           delete $at->{prompt};  
           my @tokens = (  
                         {type => 'start tag', tag_name => 'form',  
                          attributes => $form_attrs},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'start tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'label'},  
                        );  
           if ($prompt_attr) {  
             push @tokens, {type => 'character', data => $prompt_attr->{value}};  
4493            } else {            } else {
4494              push @tokens, {type => 'character',              !!!cp ('t87.4');
                            data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD  
             ## TODO: make this configurable  
           }  
           push @tokens,  
                         {type => 'start tag', tag_name => 'input', attributes => $at},  
                         #{type => 'character', data => ''}, # SHOULD  
                         {type => 'end tag', tag_name => 'label'},  
                         {type => 'end tag', tag_name => 'p'},  
                         {type => 'start tag', tag_name => 'hr'},  
                         {type => 'end tag', tag_name => 'form'};  
           $token = shift @tokens;  
           !!!back-token (@tokens);  
           return;  
         }  
       } elsif ($token->{tag_name} eq 'textarea') {  
         my $tag_name = $token->{tag_name};  
         my $el;  
         !!!create-element ($el, $token->{tag_name}, $token->{attributes});  
           
         ## TODO: $self->{form_element} if defined  
         $self->{content_model} = RCDATA_CONTENT_MODEL;  
         delete $self->{escape}; # MUST  
           
         $insert->($el);  
           
         my $text = '';  
         !!!next-token;  
         if ($token->{type} eq 'character') {  
           $token->{data} =~ s/^\x0A//;  
           unless (length $token->{data}) {  
             !!!next-token;  
4495            }            }
4496          }  
         while ($token->{type} eq 'character') {  
           $text .= $token->{data};  
4497            !!!next-token;            !!!next-token;
4498              next B;
4499          }          }
4500          if (length $text) {        } elsif ($token->{type} == END_TAG_TOKEN) {
4501            $el->manakai_append_text ($text);          ## NOTE: "using the rules for secondary insertion mode" then "continue"
4502          }          !!!cp ('t87.5');
4503                    #
4504          $self->{content_model} = PCDATA_CONTENT_MODEL;        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4505                    !!!cp ('t87.6');
4506          if ($token->{type} eq 'end tag' and          !!!parse-error (type => 'not closed',
4507              $token->{tag_name} eq $tag_name) {                          text => $self->{open_elements}->[-1]->[0]
4508            ## Ignore the token                              ->manakai_local_name,
4509          } else {                          token => $token);
4510            !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
4511          }          pop @{$self->{open_elements}}
4512          !!!next-token;              while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4513          return;  
4514        } elsif ({          $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4515                  iframe => 1,          ## Reprocess.
4516                  noembed => 1,          next B;
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There are two "as if in body" code clones.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         return;  
       } elsif ($token->{tag_name} eq 'select') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           
         $self->{insertion_mode} = 'in select';  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'in body:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: An issue on HTML5 new elements in the spec.  
4517        } else {        } else {
4518          $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;  
4519        }        }
4520      } 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]);  
             }  
           }  
4521    
4522            $self->{insertion_mode} = 'after body';      if ($self->{insertion_mode} & HEAD_IMS) {
4523            !!!next-token;        if ($token->{type} == CHARACTER_TOKEN) {
4524            return;          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4525          } else {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4526            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.2');
4527            ## Ignore the token              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4528            !!!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]);  
4529            } else {            } else {
4530              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t88.1');
4531            }              ## Ignore the token.
4532          }              #
           
         if (defined $i) {  
           splice @{$self->{open_elements}}, $i;  
         } elsif ($token->{tag_name} eq 'p') {  
           ## As if <p>, then reprocess the current token  
           my $el;  
           !!!create-element ($el, 'p');  
           $insert->($el);  
         }  
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
         !!!next-token;  
         return;  
       } elsif ($token->{tag_name} eq 'form') {  
         ## has an element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq $token->{tag_name}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4533            }            }
4534          } # INSCOPE            unless (length $token->{data}) {
4535                        !!!cp ('t88');
4536          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {              !!!next-token;
4537            pop @{$self->{open_elements}};              next B;
         } else {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         undef $self->{form_element};  
         !!!next-token;  
         return;  
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has an element in scope  
         my $i;  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ({  
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
             $i = $_;  
             last INSCOPE;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
4538            }            }
4539          } # INSCOPE  ## TODO: set $token->{column} appropriately
           
         if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
4540          }          }
           
         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');  
4541    
4542          ## As if <br>          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4543          $reconstruct_active_formatting_elements->($insert_to_current);            !!!cp ('t89');
4544                      ## As if <head>
4545          my $el;            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4546          !!!create-element ($el, 'br');            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4547          $insert->($el);            push @{$self->{open_elements}},
4548                          [$self->{head_element}, $el_category->{head}];
         ## Ignore the token.  
         !!!next-token;  
         return;  
       } elsif ({  
                 caption => 1, col => 1, colgroup => 1, frame => 1,  
                 frameset => 1, head => 1, option => 1, optgroup => 1,  
                 tbody => 1, td => 1, tfoot => 1, th => 1,  
                 thead => 1, tr => 1,  
                 area => 1, basefont => 1, bgsound => 1,  
                 embed => 1, hr => 1, iframe => 1, image => 1,  
                 img => 1, input => 1, isindex => 1, noembed => 1,  
                 noframes => 1, param => 1, select => 1, spacer => 1,  
                 table => 1, textarea => 1, wbr => 1,  
                 noscript => 0, ## TODO: if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
         ## Ignore the token  
         !!!next-token;  
         return;  
           
         ## ISSUE: Issue on HTML5 new elements in spec  
           
       } else {  
         ## Step 1  
         my $node_i = -1;  
         my $node = $self->{open_elements}->[$node_i];  
4549    
4550          ## Step 2            ## Reprocess in the "in head" insertion mode...
4551          S2: {            pop @{$self->{open_elements}};
           if ($node->[1] eq $token->{tag_name}) {  
             ## Step 1  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!back-token;  
               $token = {type => 'end tag',  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               return;  
             }  
           
             ## Step 2  
             if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {  
               !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
             }  
               
             ## Step 3  
             splice @{$self->{open_elements}}, $node_i;  
4552    
4553              !!!next-token;            ## Reprocess in the "after head" insertion mode...
4554              last S2;          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4555            } else {            !!!cp ('t90');
4556              ## Step 3            ## As if </noscript>
4557              if (not $formatting_category->{$node->[1]} and            pop @{$self->{open_elements}};
4558                  #not $phrasing_category->{$node->[1]} and            !!!parse-error (type => 'in noscript:#text', token => $token);
                 ($special_category->{$node->[1]} or  
                  $scoping_category->{$node->[1]})) {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token  
               !!!next-token;  
               last S2;  
             }  
           }  
             
           ## Step 4  
           $node_i--;  
           $node = $self->{open_elements}->[$node_i];  
4559                        
4560            ## Step 5;            ## Reprocess in the "in head" insertion mode...
4561            redo S2;            ## As if </head>
4562          } # S2            pop @{$self->{open_elements}};
         return;  
       }  
     }  
   }; # $in_body  
   
   B: {  
     if ($token->{type} eq 'DOCTYPE') {  
       !!!parse-error (type => 'DOCTYPE in the middle');  
       ## Ignore the token  
       ## Stay in the phase  
       !!!next-token;  
       redo B;  
     } elsif ($token->{type} eq 'end-of-file') {  
       if ($token->{insertion_mode} ne 'trailing end') {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!back-token;  
           $token = {type => 'end tag', tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
4563    
4564        ## Stop parsing            ## Reprocess in the "after head" insertion mode...
4565        last B;          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4566      } elsif ($token->{type} eq 'start tag' and            !!!cp ('t91');
4567               $token->{tag_name} eq 'html') {            pop @{$self->{open_elements}};
       if ($self->{insertion_mode} eq 'trailing end') {  
         ## Turn into the main phase  
         !!!parse-error (type => 'after html:html');  
         $self->{insertion_mode} = $previous_insertion_mode;  
       }  
4568    
4569  ## ISSUE: "aa<html>" is not a parse error.            ## Reprocess in the "after head" insertion mode...
4570  ## ISSUE: "<html>" in fragment is not a parse error.          } else {
4571        unless ($token->{first_start_tag}) {            !!!cp ('t92');
         !!!parse-error (type => 'not first start tag');  
       }  
       my $top_el = $self->{open_elements}->[0]->[0];  
       for my $attr_name (keys %{$token->{attributes}}) {  
         unless ($top_el->has_attribute_ns (undef, $attr_name)) {  
           $top_el->set_attribute_ns  
             (undef, [undef, $attr_name],  
              $token->{attributes}->{$attr_name}->{value});  
4572          }          }
4573        }  
4574        !!!next-token;          ## "after head" insertion mode
4575        redo B;          ## As if <body>
4576      } elsif ($token->{type} eq 'comment') {          !!!insert-element ('body',, $token);
4577        my $comment = $self->{document}->create_comment ($token->{data});          $self->{insertion_mode} = IN_BODY_IM;
4578        if ($self->{insertion_mode} eq 'trailing end') {          ## reprocess
4579          $self->{document}->append_child ($comment);          next B;
4580        } elsif ($self->{insertion_mode} eq 'after body') {        } elsif ($token->{type} == START_TAG_TOKEN) {
4581          $self->{open_elements}->[0]->[0]->append_child ($comment);          if ($token->{tag_name} eq 'head') {
4582        } else {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4583          $self->{open_elements}->[-1]->[0]->append_child ($comment);              !!!cp ('t93');
4584        }              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4585        !!!next-token;              $self->{open_elements}->[-1]->[0]->append_child
4586        redo B;                  ($self->{head_element});
4587      } elsif ($self->{insertion_mode} eq 'before head') {              push @{$self->{open_elements}},
4588            if ($token->{type} eq 'character') {                  [$self->{head_element}, $el_category->{head}];
4589              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $self->{insertion_mode} = IN_HEAD_IM;
4590                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              !!!nack ('t93.1');
4591                unless (length $token->{data}) {              !!!next-token;
4592                  !!!next-token;              next B;
4593                  redo B;            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4594                }              !!!cp ('t93.2');
4595              }              !!!parse-error (type => 'after head', text => 'head',
4596              ## As if <head>                              token => $token);
4597              !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4598              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t93.3');
4599              push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4600              $self->{insertion_mode} = 'in head';              next B;
             ## reprocess  
             redo B;  
           } elsif ($token->{type} eq 'start tag') {  
             my $attr = $token->{tag_name} eq 'head' ? $token->{attributes} : {};  
             !!!create-element ($self->{head_element}, 'head', $attr);  
             $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
             push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
             $self->{insertion_mode} = 'in head';  
             if ($token->{tag_name} eq 'head') {  
               !!!next-token;  
             #} elsif ({  
             #          base => 1, link => 1, meta => 1,  
             #          script => 1, style => 1, title => 1,  
             #         }->{$token->{tag_name}}) {  
             #  ## reprocess  
             } else {  
               ## reprocess  
             }  
             redo B;  
           } elsif ($token->{type} eq 'end tag') {  
             if ({  
                  head => 1, body => 1, html => 1,  
                  p => 1, br => 1,  
                 }->{$token->{tag_name}}) {  
               ## As if <head>  
               !!!create-element ($self->{head_element}, 'head');  
               $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
               push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
               $self->{insertion_mode} = 'in head';  
               ## reprocess  
               redo B;  
             } else {  
               !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
               ## Ignore the token ## ISSUE: An issue in the spec.  
               !!!next-token;  
               redo B;  
             }  
4601            } else {            } else {
4602              die "$0: $token->{type}: Unknown type";              !!!cp ('t95');
4603                !!!parse-error (type => 'in head:head',
4604                                token => $token); # or in head noscript
4605                ## Ignore the token
4606                !!!nack ('t95.1');
4607                !!!next-token;
4608                next B;
4609            }            }
4610          } elsif ($self->{insertion_mode} eq 'in head' or          } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4611                   $self->{insertion_mode} eq 'in head noscript' or            !!!cp ('t96');
4612                   $self->{insertion_mode} eq 'after head') {            ## As if <head>
4613            if ($token->{type} eq 'character') {            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4614              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4615                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            push @{$self->{open_elements}},
4616                unless (length $token->{data}) {                [$self->{head_element}, $el_category->{head}];
4617                  !!!next-token;  
4618                  redo B;            $self->{insertion_mode} = IN_HEAD_IM;
4619              ## Reprocess in the "in head" insertion mode...
4620            } else {
4621              !!!cp ('t97');
4622            }
4623    
4624                if ($token->{tag_name} eq 'base') {
4625                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4626                    !!!cp ('t98');
4627                    ## As if </noscript>
4628                    pop @{$self->{open_elements}};
4629                    !!!parse-error (type => 'in noscript', text => 'base',
4630                                    token => $token);
4631                  
4632                    $self->{insertion_mode} = IN_HEAD_IM;
4633                    ## Reprocess in the "in head" insertion mode...
4634                  } else {
4635                    !!!cp ('t99');
4636                }                }
4637              }  
               
             #  
           } elsif ($token->{type} eq 'start tag') {  
             if ({base => ($self->{insertion_mode} eq 'in head' or  
                           $self->{insertion_mode} eq 'after head'),  
                  link => 1}->{$token->{tag_name}}) {  
4638                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4639                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4640                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4641                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4642                                    text => $token->{tag_name}, token => $token);
4643                    push @{$self->{open_elements}},
4644                        [$self->{head_element}, $el_category->{head}];
4645                  } else {
4646                    !!!cp ('t101');
4647                }                }
4648                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4649                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4650                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4651                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4652                  !!!nack ('t101.1');
4653                !!!next-token;                !!!next-token;
4654                redo B;                next B;
4655              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'link') {
4656                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4657                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4658                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4659                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4660                                    text => $token->{tag_name}, token => $token);
4661                    push @{$self->{open_elements}},
4662                        [$self->{head_element}, $el_category->{head}];
4663                  } else {
4664                    !!!cp ('t103');
4665                }                }
4666                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4667                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4668                  pop @{$self->{open_elements}} # <head>
4669                      if $self->{insertion_mode} == AFTER_HEAD_IM;
4670                  !!!ack ('t103.1');
4671                  !!!next-token;
4672                  next B;
4673                } elsif ($token->{tag_name} eq 'meta') {
4674                  ## NOTE: There is a "as if in head" code clone.
4675                  if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4676                    !!!cp ('t104');
4677                    !!!parse-error (type => 'after head',
4678                                    text => $token->{tag_name}, token => $token);
4679                    push @{$self->{open_elements}},
4680                        [$self->{head_element}, $el_category->{head}];
4681                  } else {
4682                    !!!cp ('t105');
4683                  }
4684                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4685                  my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4686    
4687                unless ($self->{confident}) {                unless ($self->{confident}) {
4688                  my $charset;                  if ($token->{attributes}->{charset}) {
4689                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4690                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4691                      ## in the {change_encoding} callback.
4692                      $self->{change_encoding}
4693                          ->($self, $token->{attributes}->{charset}->{value},
4694                             $token);
4695                      
4696                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4697                          ->set_user_data (manakai_has_reference =>
4698                                               $token->{attributes}->{charset}
4699                                                   ->{has_reference});
4700                    } elsif ($token->{attributes}->{content}) {
4701                      if ($token->{attributes}->{content}->{value}
4702                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4703                              [\x09\x0A\x0C\x0D\x20]*=
4704                              [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4705                              ([^"'\x09\x0A\x0C\x0D\x20]
4706                               [^\x09\x0A\x0C\x0D\x20\x3B]*))/x) {
4707                        !!!cp ('t107');
4708                        ## NOTE: Whether the encoding is supported or not is handled
4709                        ## in the {change_encoding} callback.
4710                        $self->{change_encoding}
4711                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4712                               $token);
4713                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4714                            ->set_user_data (manakai_has_reference =>
4715                                                 $token->{attributes}->{content}
4716                                                       ->{has_reference});
4717                      } else {
4718                        !!!cp ('t108');
4719                      }
4720                  }                  }
4721                  if ($token->{attributes}->{'http-equiv'}) {                } else {
4722                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                  if ($token->{attributes}->{charset}) {
4723                    if ($token->{attributes}->{'http-equiv'}->{value}                    !!!cp ('t109');
4724                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4725                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                        ->set_user_data (manakai_has_reference =>
4726                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                                             $token->{attributes}->{charset}
4727                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                                                 ->{has_reference});
4728                    } ## TODO: And if supported                  }
4729                    if ($token->{attributes}->{content}) {
4730                      !!!cp ('t110');
4731                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4732                          ->set_user_data (manakai_has_reference =>
4733                                               $token->{attributes}->{content}
4734                                                   ->{has_reference});
4735                  }                  }
                 ## TODO: Change the encoding  
4736                }                }
4737    
4738                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
4739                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4740                    if $self->{insertion_mode} eq 'after head';                !!!ack ('t110.1');
4741                !!!next-token;                !!!next-token;
4742                redo B;                next B;
4743              } elsif ($token->{tag_name} eq 'title' and              } elsif ($token->{tag_name} eq 'title') {
4744                       $self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4745                ## NOTE: There is a "as if in head" code clone.                  !!!cp ('t111');
4746                if ($self->{insertion_mode} eq 'after head') {                  ## As if </noscript>
4747                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  pop @{$self->{open_elements}};
4748                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'in noscript', text => 'title',
4749                                    token => $token);
4750                  
4751                    $self->{insertion_mode} = IN_HEAD_IM;
4752                    ## Reprocess in the "in head" insertion mode...
4753                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4754                    !!!cp ('t112');
4755                    !!!parse-error (type => 'after head',
4756                                    text => $token->{tag_name}, token => $token);
4757                    push @{$self->{open_elements}},
4758                        [$self->{head_element}, $el_category->{head}];
4759                  } else {
4760                    !!!cp ('t113');
4761                }                }
4762    
4763                  ## NOTE: There is a "as if in head" code clone.
4764                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4765                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4766                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4767                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
4768                pop @{$self->{open_elements}}                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4769                    if $self->{insertion_mode} eq 'after head';                next B;
4770                redo B;              } elsif ($token->{tag_name} eq 'style' or
4771              } elsif ($token->{tag_name} eq 'style') {                       $token->{tag_name} eq 'noframes') {
4772                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4773                ## insertion mode 'in head')                ## insertion mode IN_HEAD_IM)
4774                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4775                if ($self->{insertion_mode} eq 'after head') {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4776                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4777                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4778                                    text => $token->{tag_name}, token => $token);
4779                    push @{$self->{open_elements}},
4780                        [$self->{head_element}, $el_category->{head}];
4781                  } else {
4782                    !!!cp ('t115');
4783                }                }
4784                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4785                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4786                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4787                redo B;                next B;
4788              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4789                if ($self->{insertion_mode} eq 'in head') {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4790                    !!!cp ('t116');
4791                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4792                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4793                  $self->{insertion_mode} = 'in head noscript';                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4794                    !!!nack ('t116.1');
4795                  !!!next-token;                  !!!next-token;
4796                  redo B;                  next B;
4797                } elsif ($self->{insertion_mode} eq 'in head noscript') {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4798                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4799                    !!!parse-error (type => 'in noscript', text => 'noscript',
4800                                    token => $token);
4801                  ## Ignore the token                  ## Ignore the token
4802                    !!!nack ('t117.1');
4803                  !!!next-token;                  !!!next-token;
4804                  redo B;                  next B;
4805                } else {                } else {
4806                    !!!cp ('t118');
4807                  #                  #
4808                }                }
4809              } elsif ($token->{tag_name} eq 'head' and              } elsif ($token->{tag_name} eq 'script') {
4810                       $self->{insertion_mode} ne 'after head') {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4811                !!!parse-error (type => 'in head:head'); # or in head noscript                  !!!cp ('t119');
4812                ## Ignore the token                  ## As if </noscript>
4813                !!!next-token;                  pop @{$self->{open_elements}};
4814                redo B;                  !!!parse-error (type => 'in noscript', text => 'script',
4815              } elsif ($self->{insertion_mode} ne 'in head noscript' and                                  token => $token);
4816                       $token->{tag_name} eq 'script') {                
4817                if ($self->{insertion_mode} eq 'after head') {                  $self->{insertion_mode} = IN_HEAD_IM;
4818                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  ## Reprocess in the "in head" insertion mode...
4819                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4820                    !!!cp ('t120');
4821                    !!!parse-error (type => 'after head',
4822                                    text => $token->{tag_name}, token => $token);
4823                    push @{$self->{open_elements}},
4824                        [$self->{head_element}, $el_category->{head}];
4825                  } else {
4826                    !!!cp ('t121');
4827                }                }
4828    
4829                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4830                $script_start_tag->($insert_to_current);                $script_start_tag->();
4831                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4832                    if $self->{insertion_mode} eq 'after head';                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4833                redo B;                next B;
4834              } 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  
4835                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4836                !!!insert-element ('frameset', $token->{attributes});                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4837                $self->{insertion_mode} = 'in frameset';                  !!!cp ('t122');
4838                    ## As if </noscript>
4839                    pop @{$self->{open_elements}};
4840                    !!!parse-error (type => 'in noscript',
4841                                    text => $token->{tag_name}, token => $token);
4842                    
4843                    ## Reprocess in the "in head" insertion mode...
4844                    ## As if </head>
4845                    pop @{$self->{open_elements}};
4846                    
4847                    ## Reprocess in the "after head" insertion mode...
4848                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4849                    !!!cp ('t124');
4850                    pop @{$self->{open_elements}};
4851                    
4852                    ## Reprocess in the "after head" insertion mode...
4853                  } else {
4854                    !!!cp ('t125');
4855                  }
4856    
4857                  ## "after head" insertion mode
4858                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4859                  if ($token->{tag_name} eq 'body') {
4860                    !!!cp ('t126');
4861                    $self->{insertion_mode} = IN_BODY_IM;
4862                  } elsif ($token->{tag_name} eq 'frameset') {
4863                    !!!cp ('t127');
4864                    $self->{insertion_mode} = IN_FRAMESET_IM;
4865                  } else {
4866                    die "$0: tag name: $self->{tag_name}";
4867                  }
4868                  !!!nack ('t127.1');
4869                !!!next-token;                !!!next-token;
4870                redo B;                next B;
4871              } else {              } else {
4872                  !!!cp ('t128');
4873                #                #
4874              }              }
4875            } elsif ($token->{type} eq 'end tag') {  
4876              if ($self->{insertion_mode} eq 'in head' and              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4877                  $token->{tag_name} eq 'head') {                !!!cp ('t129');
4878                  ## As if </noscript>
4879                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4880                $self->{insertion_mode} = 'after head';                !!!parse-error (type => 'in noscript:/',
4881                !!!next-token;                                text => $token->{tag_name}, token => $token);
4882                redo B;                
4883              } elsif ($self->{insertion_mode} eq 'in head noscript' and                ## Reprocess in the "in head" insertion mode...
4884                  $token->{tag_name} eq 'noscript') {                ## As if </head>
4885                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4886                $self->{insertion_mode} = 'in head';  
4887                !!!next-token;                ## Reprocess in the "after head" insertion mode...
4888                redo B;              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4889              } elsif ($self->{insertion_mode} eq 'in head' and                !!!cp ('t130');
4890                       {                ## As if </head>
4891                  pop @{$self->{open_elements}};
4892    
4893                  ## Reprocess in the "after head" insertion mode...
4894                } else {
4895                  !!!cp ('t131');
4896                }
4897    
4898                ## "after head" insertion mode
4899                ## As if <body>
4900                !!!insert-element ('body',, $token);
4901                $self->{insertion_mode} = IN_BODY_IM;
4902                ## reprocess
4903                !!!ack-later;
4904                next B;
4905              } elsif ($token->{type} == END_TAG_TOKEN) {
4906                if ($token->{tag_name} eq 'head') {
4907                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4908                    !!!cp ('t132');
4909                    ## As if <head>
4910                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4911                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4912                    push @{$self->{open_elements}},
4913                        [$self->{head_element}, $el_category->{head}];
4914    
4915                    ## Reprocess in the "in head" insertion mode...
4916                    pop @{$self->{open_elements}};
4917                    $self->{insertion_mode} = AFTER_HEAD_IM;
4918                    !!!next-token;
4919                    next B;
4920                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4921                    !!!cp ('t133');
4922                    ## As if </noscript>
4923                    pop @{$self->{open_elements}};
4924                    !!!parse-error (type => 'in noscript:/',
4925                                    text => 'head', token => $token);
4926                    
4927                    ## Reprocess in the "in head" insertion mode...
4928                    pop @{$self->{open_elements}};
4929                    $self->{insertion_mode} = AFTER_HEAD_IM;
4930                    !!!next-token;
4931                    next B;
4932                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4933                    !!!cp ('t134');
4934                    pop @{$self->{open_elements}};
4935                    $self->{insertion_mode} = AFTER_HEAD_IM;
4936                    !!!next-token;
4937                    next B;
4938                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4939                    !!!cp ('t134.1');
4940                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4941                                    token => $token);
4942                    ## Ignore the token
4943                    !!!next-token;
4944                    next B;
4945                  } else {
4946                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4947                  }
4948                } elsif ($token->{tag_name} eq 'noscript') {
4949                  if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4950                    !!!cp ('t136');
4951                    pop @{$self->{open_elements}};
4952                    $self->{insertion_mode} = IN_HEAD_IM;
4953                    !!!next-token;
4954                    next B;
4955                  } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4956                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4957                    !!!cp ('t137');
4958                    !!!parse-error (type => 'unmatched end tag',
4959                                    text => 'noscript', token => $token);
4960                    ## Ignore the token ## ISSUE: An issue in the spec.
4961                    !!!next-token;
4962                    next B;
4963                  } else {
4964                    !!!cp ('t138');
4965                    #
4966                  }
4967                } elsif ({
4968                        body => 1, html => 1,                        body => 1, html => 1,
                       p => 1, br => 1,  
                      }->{$token->{tag_name}}) {  
               #  
             } elsif ($self->{insertion_mode} eq 'in head noscript' and  
                      {  
                       p => 1, br => 1,  
4969                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4970                #                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4971              } elsif ($self->{insertion_mode} ne 'after head') {                    $self->{insertion_mode} == IN_HEAD_IM or
4972                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4973                    !!!cp ('t140');
4974                    !!!parse-error (type => 'unmatched end tag',
4975                                    text => $token->{tag_name}, token => $token);
4976                    ## Ignore the token
4977                    !!!next-token;
4978                    next B;
4979                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4980                    !!!cp ('t140.1');
4981                    !!!parse-error (type => 'unmatched end tag',
4982                                    text => $token->{tag_name}, token => $token);
4983                    ## Ignore the token
4984                    !!!next-token;
4985                    next B;
4986                  } else {
4987                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4988                  }
4989                } elsif ($token->{tag_name} eq 'p') {
4990                  !!!cp ('t142');
4991                  !!!parse-error (type => 'unmatched end tag',
4992                                  text => $token->{tag_name}, token => $token);
4993                ## Ignore the token                ## Ignore the token
4994                !!!next-token;                !!!next-token;
4995                redo B;                next B;
4996                } elsif ($token->{tag_name} eq 'br') {
4997                  if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4998                    !!!cp ('t142.2');
4999                    ## (before head) as if <head>, (in head) as if </head>
5000                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5001                    $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
5002                    $self->{insertion_mode} = AFTER_HEAD_IM;
5003      
5004                    ## Reprocess in the "after head" insertion mode...
5005                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5006                    !!!cp ('t143.2');
5007                    ## As if </head>
5008                    pop @{$self->{open_elements}};
5009                    $self->{insertion_mode} = AFTER_HEAD_IM;
5010      
5011                    ## Reprocess in the "after head" insertion mode...
5012                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5013                    !!!cp ('t143.3');
5014                    ## ISSUE: Two parse errors for <head><noscript></br>
5015                    !!!parse-error (type => 'unmatched end tag',
5016                                    text => 'br', token => $token);
5017                    ## As if </noscript>
5018                    pop @{$self->{open_elements}};
5019                    $self->{insertion_mode} = IN_HEAD_IM;
5020    
5021                    ## Reprocess in the "in head" insertion mode...
5022                    ## As if </head>
5023                    pop @{$self->{open_elements}};
5024                    $self->{insertion_mode} = AFTER_HEAD_IM;
5025    
5026                    ## Reprocess in the "after head" insertion mode...
5027                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5028                    !!!cp ('t143.4');
5029                    #
5030                  } else {
5031                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
5032                  }
5033    
5034                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5035                  !!!parse-error (type => 'unmatched end tag',
5036                                  text => 'br', token => $token);
5037                  ## Ignore the token
5038                  !!!next-token;
5039                  next B;
5040              } else {              } else {
5041                #                !!!cp ('t145');
5042                  !!!parse-error (type => 'unmatched end tag',
5043                                  text => $token->{tag_name}, token => $token);
5044                  ## Ignore the token
5045                  !!!next-token;
5046                  next B;
5047              }              }
           } else {  
             #  
           }  
5048    
5049            ## As if </head> or </noscript> or <body>              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5050            if ($self->{insertion_mode} eq 'in head') {                !!!cp ('t146');
5051              pop @{$self->{open_elements}};                ## As if </noscript>
5052              $self->{insertion_mode} = 'after head';                pop @{$self->{open_elements}};
5053            } elsif ($self->{insertion_mode} eq 'in head noscript') {                !!!parse-error (type => 'in noscript:/',
5054              pop @{$self->{open_elements}};                                text => $token->{tag_name}, token => $token);
5055              !!!parse-error (type => 'in noscript:'.(defined $token->{tag_name} ? ($token->{type} eq 'end tag' ? '/' : '') . $token->{tag_name} : '#' . $token->{type}));                
5056              $self->{insertion_mode} = 'in head';                ## Reprocess in the "in head" insertion mode...
5057            } else { # 'after head'                ## As if </head>
5058              !!!insert-element ('body');                pop @{$self->{open_elements}};
5059              $self->{insertion_mode} = 'in body';  
5060            }                ## Reprocess in the "after head" insertion mode...
5061            ## reprocess              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5062            redo B;                !!!cp ('t147');
5063                  ## As if </head>
5064                  pop @{$self->{open_elements}};
5065    
5066                  ## Reprocess in the "after head" insertion mode...
5067                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5068    ## ISSUE: This case cannot be reached?
5069                  !!!cp ('t148');
5070                  !!!parse-error (type => 'unmatched end tag',
5071                                  text => $token->{tag_name}, token => $token);
5072                  ## Ignore the token ## ISSUE: An issue in the spec.
5073                  !!!next-token;
5074                  next B;
5075                } else {
5076                  !!!cp ('t149');
5077                }
5078    
5079                ## "after head" insertion mode
5080                ## As if <body>
5081                !!!insert-element ('body',, $token);
5082                $self->{insertion_mode} = IN_BODY_IM;
5083                ## reprocess
5084                next B;
5085          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5086            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5087              !!!cp ('t149.1');
5088    
5089              ## NOTE: As if <head>
5090              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5091              $self->{open_elements}->[-1]->[0]->append_child
5092                  ($self->{head_element});
5093              #push @{$self->{open_elements}},
5094              #    [$self->{head_element}, $el_category->{head}];
5095              #$self->{insertion_mode} = IN_HEAD_IM;
5096              ## NOTE: Reprocess.
5097    
5098              ## NOTE: As if </head>
5099              #pop @{$self->{open_elements}};
5100              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5101              ## NOTE: Reprocess.
5102              
5103              #
5104            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5105              !!!cp ('t149.2');
5106    
5107              ## NOTE: As if </head>
5108              pop @{$self->{open_elements}};
5109              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5110              ## NOTE: Reprocess.
5111    
5112              #
5113            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5114              !!!cp ('t149.3');
5115    
5116              !!!parse-error (type => 'in noscript:#eof', token => $token);
5117    
5118              ## As if </noscript>
5119              pop @{$self->{open_elements}};
5120              #$self->{insertion_mode} = IN_HEAD_IM;
5121              ## NOTE: Reprocess.
5122    
5123              ## NOTE: As if </head>
5124              pop @{$self->{open_elements}};
5125              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5126              ## NOTE: Reprocess.
5127    
5128              #
5129            } else {
5130              !!!cp ('t149.4');
5131              #
5132            }
5133    
5134            ## NOTE: As if <body>
5135            !!!insert-element ('body',, $token);
5136            $self->{insertion_mode} = IN_BODY_IM;
5137            ## NOTE: Reprocess.
5138            next B;
5139          } else {
5140            die "$0: $token->{type}: Unknown token type";
5141          }
5142    
5143            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5144          } elsif ($self->{insertion_mode} eq 'in body' or      } elsif ($self->{insertion_mode} & BODY_IMS) {
5145                   $self->{insertion_mode} eq 'in cell' or            if ($token->{type} == CHARACTER_TOKEN) {
5146                   $self->{insertion_mode} eq 'in caption') {              !!!cp ('t150');
           if ($token->{type} eq 'character') {  
5147              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
5148              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5149                            
5150              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5151    
5152              !!!next-token;              !!!next-token;
5153              redo B;              next B;
5154            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
5155              if ({              if ({
5156                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
5157                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,                   td => 1, tfoot => 1, th => 1, thead => 1, tr => 1,
5158                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5159                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
5160                  ## have an element in table scope                  ## have an element in table scope
5161                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5162                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5163                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5164                      $tn = $node->[1];                      !!!cp ('t151');
5165                      last INSCOPE;  
5166                    } elsif ({                      ## Close the cell
5167                              table => 1, html => 1,                      !!!back-token; # <x>
5168                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
5169                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
5170                    }                                line => $token->{line},
5171                  } # INSCOPE                                column => $token->{column}};
5172                    unless (defined $tn) {                      next B;
5173                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5174                      ## Ignore the token                      !!!cp ('t152');
5175                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
5176                      redo B;                      last;
5177                    }                    }
5178                    }
5179    
5180                    !!!cp ('t153');
5181                    !!!parse-error (type => 'start tag not allowed',
5182                        text => $token->{tag_name}, token => $token);
5183                    ## Ignore the token
5184                    !!!nack ('t153.1');
5185                    !!!next-token;
5186                    next B;
5187                  } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5188                    !!!parse-error (type => 'not closed', text => 'caption',
5189                                    token => $token);
5190                                    
5191                  ## Close the cell                  ## NOTE: As if </caption>.
                 !!!back-token; # <?>  
                 $token = {type => 'end tag', tag_name => $tn};  
                 redo B;  
               } elsif ($self->{insertion_mode} eq 'in caption') {  
                 !!!parse-error (type => 'not closed:caption');  
                   
                 ## As if </caption>  
5192                  ## have a table element in table scope                  ## have a table element in table scope
5193                  my $i;                  my $i;
5194                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5195                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5196                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5197                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5198                      last INSCOPE;                        !!!cp ('t155');
5199                    } elsif ({                        $i = $_;
5200                              table => 1, html => 1,                        last INSCOPE;
5201                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5202                      last INSCOPE;                        !!!cp ('t156');
5203                          last;
5204                        }
5205                    }                    }
5206    
5207                      !!!cp ('t157');
5208                      !!!parse-error (type => 'start tag not allowed',
5209                                      text => $token->{tag_name}, token => $token);
5210                      ## Ignore the token
5211                      !!!nack ('t157.1');
5212                      !!!next-token;
5213                      next B;
5214                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5215                                    
5216                  ## generate implied end tags                  ## generate implied end tags
5217                  if ({                  while ($self->{open_elements}->[-1]->[1]
5218                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5219                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
5220                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => 'end tag', tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5221                  }                  }
5222    
5223                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5224                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
5225                      !!!parse-error (type => 'not closed',
5226                                      text => $self->{open_elements}->[-1]->[0]
5227                                          ->manakai_local_name,
5228                                      token => $token);
5229                    } else {
5230                      !!!cp ('t160');
5231                  }                  }
5232                                    
5233                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
5234                                    
5235                  $clear_up_to_marker->();                  $clear_up_to_marker->();
5236                                    
5237                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
5238                                    
5239                  ## reprocess                  ## reprocess
5240                  redo B;                  !!!ack-later;
5241                    next B;
5242                } else {                } else {
5243                    !!!cp ('t161');
5244                  #                  #
5245                }                }
5246              } else {              } else {
5247                  !!!cp ('t162');
5248                #                #
5249              }              }
5250            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
5251              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {              if ($token->{tag_name} eq 'td' or $token->{tag_name} eq 'th') {
5252                if ($self->{insertion_mode} eq 'in cell') {                if ($self->{insertion_mode} == IN_CELL_IM) {
5253                  ## have an element in table scope                  ## have an element in table scope
5254                  my $i;                  my $i;
5255                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5256                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5257                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5258                        !!!cp ('t163');
5259                      $i = $_;                      $i = $_;
5260                      last INSCOPE;                      last INSCOPE;
5261                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5262                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
5263                      last INSCOPE;                      last INSCOPE;
5264                    }                    }
5265                  } # INSCOPE                  } # INSCOPE
5266                    unless (defined $i) {                    unless (defined $i) {
5267                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
5268                        !!!parse-error (type => 'unmatched end tag',
5269                                        text => $token->{tag_name},
5270                                        token => $token);
5271                      ## Ignore the token                      ## Ignore the token
5272                      !!!next-token;                      !!!next-token;
5273                      redo B;                      next B;
5274                    }                    }
5275                                    
5276                  ## generate implied end tags                  ## generate implied end tags
5277                  if ({                  while ($self->{open_elements}->[-1]->[1]
5278                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5279                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
5280                       th => ($token->{tag_name} eq 'td'),                    pop @{$self->{open_elements}};
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5281                  }                  }
5282                    
5283                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5284                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
5285                      !!!cp ('t167');
5286                      !!!parse-error (type => 'not closed',
5287                                      text => $self->{open_elements}->[-1]->[0]
5288                                          ->manakai_local_name,
5289                                      token => $token);
5290                    } else {
5291                      !!!cp ('t168');
5292                  }                  }
5293                                    
5294                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
5295                                    
5296                  $clear_up_to_marker->();                  $clear_up_to_marker->();
5297                                    
5298                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
5299                                    
5300                  !!!next-token;                  !!!next-token;
5301                  redo B;                  next B;
5302                } elsif ($self->{insertion_mode} eq 'in caption') {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5303                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
5304                    !!!parse-error (type => 'unmatched end tag',
5305                                    text => $token->{tag_name}, token => $token);
5306                  ## Ignore the token                  ## Ignore the token
5307                  !!!next-token;                  !!!next-token;
5308                  redo B;                  next B;
5309                } else {                } else {
5310                    !!!cp ('t170');
5311                  #                  #
5312                }                }
5313              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
5314                if ($self->{insertion_mode} eq 'in caption') {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5315                  ## have a table element in table scope                  ## have a table element in table scope
5316                  my $i;                  my $i;
5317                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5318                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5319                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5320                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5321                      last INSCOPE;                        !!!cp ('t171');
5322                    } elsif ({                        $i = $_;
5323                              table => 1, html => 1,                        last INSCOPE;
5324                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5325                      last INSCOPE;                        !!!cp ('t172');
5326                          last;
5327                        }
5328                    }                    }
5329    
5330                      !!!cp ('t173');
5331                      !!!parse-error (type => 'unmatched end tag',
5332                                      text => $token->{tag_name}, token => $token);
5333                      ## Ignore the token
5334                      !!!next-token;
5335                      next B;
5336                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5337                                    
5338                  ## generate implied end tags                  ## generate implied end tags
5339                  if ({                  while ($self->{open_elements}->[-1]->[1]
5340                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5341                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
5342                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => 'end tag',  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5343                  }                  }
5344                                    
5345                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5346                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
5347                      !!!parse-error (type => 'not closed',
5348                                      text => $self->{open_elements}->[-1]->[0]
5349                                          ->manakai_local_name,
5350                                      token => $token);
5351                    } else {
5352                      !!!cp ('t176');
5353                  }                  }
5354                                    
5355                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
5356                                    
5357                  $clear_up_to_marker->();                  $clear_up_to_marker->();
5358                                    
5359                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
5360                                    
5361                  !!!next-token;                  !!!next-token;
5362                  redo B;                  next B;
5363                } elsif ($self->{insertion_mode} eq 'in cell') {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5364                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5365                    !!!parse-error (type => 'unmatched end tag',
5366                                    text => $token->{tag_name}, token => $token);
5367                  ## Ignore the token                  ## Ignore the token
5368                  !!!next-token;                  !!!next-token;
5369                  redo B;                  next B;
5370                } else {                } else {
5371                    !!!cp ('t178');
5372                  #                  #
5373                }                }
5374              } elsif ({              } elsif ({
5375                        table => 1, tbody => 1, tfoot => 1,                        table => 1, tbody => 1, tfoot => 1,
5376                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5377                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5378                       $self->{insertion_mode} eq 'in cell') {                       $self->{insertion_mode} == IN_CELL_IM) {
5379                ## have an element in table scope                ## have an element in table scope
5380                my $i;                my $i;
5381                my $tn;                my $tn;
5382                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5383                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5384                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5385                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5386                    last INSCOPE;                      !!!cp ('t179');
5387                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5388                    $tn = $node->[1];  
5389                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5390                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5391                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5392                            table => 1, html => 1,                                line => $token->{line},
5393                           }->{$node->[1]}) {                                column => $token->{column}};
5394                    last INSCOPE;                      next B;
5395                      } elsif ($node->[1] & TABLE_CELL_EL) {
5396                        !!!cp ('t180');
5397                        $tn = $node->[0]->manakai_local_name;
5398                        ## NOTE: There is exactly one |td| or |th| element
5399                        ## in scope in the stack of open elements by definition.
5400                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5401                        ## ISSUE: Can this be reached?
5402                        !!!cp ('t181');
5403                        last;
5404                      }
5405                  }                  }
5406                } # INSCOPE  
5407                unless (defined $i) {                  !!!cp ('t182');
5408                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5409                        text => $token->{tag_name}, token => $token);
5410                  ## Ignore the token                  ## Ignore the token
5411                  !!!next-token;                  !!!next-token;
5412                  redo B;                  next B;
5413                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => 'end tag', tag_name => $tn};  
               redo B;  
5414              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5415                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5416                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5417                                  token => $token);
5418    
5419                ## As if </caption>                ## As if </caption>
5420                ## have a table element in table scope                ## have a table element in table scope
5421                my $i;                my $i;
5422                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5423                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5424                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5425                      !!!cp ('t184');
5426                    $i = $_;                    $i = $_;
5427                    last INSCOPE;                    last INSCOPE;
5428                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5429                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5430                    last INSCOPE;                    last INSCOPE;
5431                  }                  }
5432                } # INSCOPE                } # INSCOPE
5433                unless (defined $i) {                unless (defined $i) {
5434                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5435                    !!!parse-error (type => 'unmatched end tag',
5436                                    text => 'caption', token => $token);
5437                  ## Ignore the token                  ## Ignore the token
5438                  !!!next-token;                  !!!next-token;
5439                  redo B;                  next B;
5440                }                }
5441                                
5442                ## generate implied end tags                ## generate implied end tags
5443                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5444                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5445                     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;  
5446                }                }
5447    
5448                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5449                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5450                    !!!parse-error (type => 'not closed',
5451                                    text => $self->{open_elements}->[-1]->[0]
5452                                        ->manakai_local_name,
5453                                    token => $token);
5454                  } else {
5455                    !!!cp ('t189');
5456                }                }
5457    
5458                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5459    
5460                $clear_up_to_marker->();                $clear_up_to_marker->();
5461    
5462                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
5463    
5464                ## reprocess                ## reprocess
5465                redo B;                next B;
5466              } elsif ({              } elsif ({
5467                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5468                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5469                if ($self->{insertion_mode} eq 'in cell' or                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5470                    $self->{insertion_mode} eq 'in caption') {                  !!!cp ('t190');
5471                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5472                                    text => $token->{tag_name}, token => $token);
5473                  ## Ignore the token                  ## Ignore the token
5474                  !!!next-token;                  !!!next-token;
5475                  redo B;                  next B;
5476                } else {                } else {
5477                    !!!cp ('t191');
5478                  #                  #
5479                }                }
5480              } elsif ({              } elsif ({
5481                        tbody => 1, tfoot => 1,                        tbody => 1, tfoot => 1,
5482                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5483                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5484                       $self->{insertion_mode} eq 'in caption') {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5485                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5486                  !!!parse-error (type => 'unmatched end tag',
5487                                  text => $token->{tag_name}, token => $token);
5488                ## Ignore the token                ## Ignore the token
5489                !!!next-token;                !!!next-token;
5490                redo B;                next B;
5491              } else {              } else {
5492                  !!!cp ('t193');
5493                #                #
5494              }              }
5495            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5496              #          for my $entry (@{$self->{open_elements}}) {
5497              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5498                !!!cp ('t75');
5499                !!!parse-error (type => 'in body:#eof', token => $token);
5500                last;
5501            }            }
5502                      }
5503            $in_body->($insert_to_current);  
5504            redo B;          ## Stop parsing.
5505          } elsif ($self->{insertion_mode} eq 'in row' or          last B;
5506                   $self->{insertion_mode} eq 'in table body' or        } else {
5507                   $self->{insertion_mode} eq 'in table') {          die "$0: $token->{type}: Unknown token type";
5508            if ($token->{type} eq 'character') {        }
5509              ## NOTE: There are "character in table" code clones.  
5510              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {        $insert = $insert_to_current;
5511                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);        #
5512        } elsif ($self->{insertion_mode} & TABLE_IMS) {
5513          if ($token->{type} == CHARACTER_TOKEN) {
5514            if (not $open_tables->[-1]->[1] and # tainted
5515                $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5516              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5517                                
5518                unless (length $token->{data}) {            unless (length $token->{data}) {
5519                  !!!next-token;              !!!cp ('t194');
5520                  redo B;              !!!next-token;
5521                }              next B;
5522              }            } else {
5523                !!!cp ('t195');
5524              }
5525            }
5526    
5527              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5528    
5529              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5530              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3992  sub _tree_construction_main ($) { Line 5532  sub _tree_construction_main ($) {
5532              ## result in a new Text node.              ## result in a new Text node.
5533              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5534                            
5535              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]}) {  
5536                # MUST                # MUST
5537                my $foster_parent_element;                my $foster_parent_element;
5538                my $next_sibling;                my $next_sibling;
5539                my $prev_sibling;                my $prev_sibling;
5540                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5541                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5542                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5543                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5544                        !!!cp ('t196');
5545                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5546                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5547                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5548                    } else {                    } else {
5549                        !!!cp ('t197');
5550                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5551                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5552                    }                    }
# Line 4019  sub _tree_construction_main ($) { Line 5558  sub _tree_construction_main ($) {
5558                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5559                if (defined $prev_sibling and                if (defined $prev_sibling and
5560                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5561                    !!!cp ('t198');
5562                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5563                } else {                } else {
5564                    !!!cp ('t199');
5565                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5566                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5567                     $next_sibling);                     $next_sibling);
5568                }                }
5569              } else {            $open_tables->[-1]->[1] = 1; # tainted
5570                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5571              !!!cp ('t200');
5572              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5573            }
5574                
5575            !!!next-token;
5576            next B;
5577          } elsif ($token->{type} == START_TAG_TOKEN) {
5578            if ({
5579                 tr => ($self->{insertion_mode} != IN_ROW_IM),
5580                 th => 1, td => 1,
5581                }->{$token->{tag_name}}) {
5582              if ($self->{insertion_mode} == IN_TABLE_IM) {
5583                ## Clear back to table context
5584                while (not ($self->{open_elements}->[-1]->[1]
5585                                & TABLE_SCOPING_EL)) {
5586                  !!!cp ('t201');
5587                  pop @{$self->{open_elements}};
5588              }              }
5589                            
5590              !!!next-token;              !!!insert-element ('tbody',, $token);
5591              redo B;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5592            } elsif ($token->{type} eq 'start tag') {              ## reprocess in the "in table body" insertion mode...
5593              if ({            }
5594                   tr => ($self->{insertion_mode} ne 'in row'),            
5595                   th => 1, td => 1,            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5596                  }->{$token->{tag_name}}) {              unless ($token->{tag_name} eq 'tr') {
5597                if ($self->{insertion_mode} eq 'in table') {                !!!cp ('t202');
5598                  ## Clear back to table context                !!!parse-error (type => 'missing start tag:tr', token => $token);
5599                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              }
                        $self->{open_elements}->[-1]->[1] ne 'html') {  
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
                   pop @{$self->{open_elements}};  
                 }  
                   
                 !!!insert-element ('tbody');  
                 $self->{insertion_mode} = 'in table body';  
                 ## reprocess in the "in table body" insertion mode...  
               }  
   
               if ($self->{insertion_mode} eq 'in table body') {  
                 unless ($token->{tag_name} eq 'tr') {  
                   !!!parse-error (type => 'missing start tag:tr');  
                 }  
5600                                    
5601                  ## Clear back to table body context              ## Clear back to table body context
5602                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5603                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5604                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5605                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## ISSUE: Can this case be reached?
5606                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5607                  }              }
5608                                    
5609                  $self->{insertion_mode} = 'in row';                  $self->{insertion_mode} = IN_ROW_IM;
5610                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5611                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5612                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5613                      !!!nack ('t204');
5614                    !!!next-token;                    !!!next-token;
5615                    redo B;                    next B;
5616                  } else {                  } else {
5617                    !!!insert-element ('tr');                    !!!cp ('t205');
5618                      !!!insert-element ('tr',, $token);
5619                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5620                  }                  }
5621                  } else {
5622                    !!!cp ('t206');
5623                }                }
5624    
5625                ## Clear back to table row context                ## Clear back to table row context
5626                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5627                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5628                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5629                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5630                }                }
5631                                
5632                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5633                $self->{insertion_mode} = 'in cell';                $self->{insertion_mode} = IN_CELL_IM;
5634    
5635                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5636                                
5637                  !!!nack ('t207.1');
5638                !!!next-token;                !!!next-token;
5639                redo B;                next B;
5640              } elsif ({              } elsif ({
5641                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5642                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5643                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5644                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5645                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
5646                  ## As if </tr>                  ## As if </tr>
5647                  ## have an element in table scope                  ## have an element in table scope
5648                  my $i;                  my $i;
5649                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5650                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5651                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5652                        !!!cp ('t208');
5653                      $i = $_;                      $i = $_;
5654                      last INSCOPE;                      last INSCOPE;
5655                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5656                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5657                      last INSCOPE;                      last INSCOPE;
5658                    }                    }
5659                  } # INSCOPE                  } # INSCOPE
5660                  unless (defined $i) {                  unless (defined $i) {
5661                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5662    ## TODO: This type is wrong.
5663                      !!!parse-error (type => 'unmacthed end tag',
5664                                      text => $token->{tag_name}, token => $token);
5665                    ## Ignore the token                    ## Ignore the token
5666                      !!!nack ('t210.1');
5667                    !!!next-token;                    !!!next-token;
5668                    redo B;                    next B;
5669                  }                  }
5670                                    
5671                  ## Clear back to table row context                  ## Clear back to table row context
5672                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5673                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5674                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5675                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5676                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5677                  }                  }
5678                                    
5679                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5680                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5681                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5682                      !!!cp ('t212');
5683                    ## reprocess                    ## reprocess
5684                    redo B;                    !!!ack-later;
5685                      next B;
5686                  } else {                  } else {
5687                      !!!cp ('t213');
5688                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5689                  }                  }
5690                }                }
5691    
5692                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5693                  ## have an element in table scope                  ## have an element in table scope
5694                  my $i;                  my $i;
5695                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5696                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5697                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5698                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5699                      $i = $_;                      $i = $_;
5700                      last INSCOPE;                      last INSCOPE;
5701                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5702                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5703                      last INSCOPE;                      last INSCOPE;
5704                    }                    }
5705                  } # INSCOPE                  } # INSCOPE
5706                  unless (defined $i) {                  unless (defined $i) {
5707                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5708    ## TODO: This erorr type is wrong.
5709                      !!!parse-error (type => 'unmatched end tag',
5710                                      text => $token->{tag_name}, token => $token);
5711                    ## Ignore the token                    ## Ignore the token
5712                      !!!nack ('t216.1');
5713                    !!!next-token;                    !!!next-token;
5714                    redo B;                    next B;
5715                  }                  }
5716    
5717                  ## Clear back to table body context                  ## Clear back to table body context
5718                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5719                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5720                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5721                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5722                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5723                  }                  }
5724                                    
# Line 4172  sub _tree_construction_main ($) { Line 5730  sub _tree_construction_main ($) {
5730                  ## nop by definition                  ## nop by definition
5731                                    
5732                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5733                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
5734                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5735                  } else {
5736                    !!!cp ('t218');
5737                }                }
5738    
5739                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5740                  ## Clear back to table context                  ## Clear back to table context
5741                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5742                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5743                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5744                      ## ISSUE: Can this state be reached?
5745                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5746                  }                  }
5747                                    
5748                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5749                  $self->{insertion_mode} = 'in column group';                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5750                  ## reprocess                  ## reprocess
5751                  redo B;                  !!!ack-later;
5752                    next B;
5753                } elsif ({                } elsif ({
5754                          caption => 1,                          caption => 1,
5755                          colgroup => 1,                          colgroup => 1,
5756                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5757                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5758                  ## Clear back to table context                  ## Clear back to table context
5759                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5760                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5761                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5762                      ## ISSUE: Can this state be reached?
5763                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5764                  }                  }
5765                                    
5766                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5767                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5768                                    
5769                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5770                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5771                                             caption => 'in caption',                                             caption => IN_CAPTION_IM,
5772                                             colgroup => 'in column group',                                             colgroup => IN_COLUMN_GROUP_IM,
5773                                             tbody => 'in table body',                                             tbody => IN_TABLE_BODY_IM,
5774                                             tfoot => 'in table body',                                             tfoot => IN_TABLE_BODY_IM,
5775                                             thead => 'in table body',                                             thead => IN_TABLE_BODY_IM,
5776                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5777                  !!!next-token;                  !!!next-token;
5778                  redo B;                  !!!nack ('t220.1');
5779                    next B;
5780                } else {                } else {
5781                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5782                }                }
5783              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5784                ## NOTE: There are code clones for this "table in table"                !!!parse-error (type => 'not closed',
5785                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
5786                                      ->manakai_local_name,
5787                                  token => $token);
5788    
5789                ## As if </table>                ## As if </table>
5790                ## have a table element in table scope                ## have a table element in table scope
5791                my $i;                my $i;
5792                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5793                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5794                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5795                      !!!cp ('t221');
5796                    $i = $_;                    $i = $_;
5797                    last INSCOPE;                    last INSCOPE;
5798                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5799                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5800                    last INSCOPE;                    last INSCOPE;
5801                  }                  }
5802                } # INSCOPE                } # INSCOPE
5803                unless (defined $i) {                unless (defined $i) {
5804                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5805    ## TODO: The following is wrong, maybe.
5806                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5807                                    token => $token);
5808                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5809                    !!!nack ('t223.1');
5810                  !!!next-token;                  !!!next-token;
5811                  redo B;                  next B;
5812                }                }
5813                                
5814    ## TODO: Followings are removed from the latest spec.
5815                ## generate implied end tags                ## generate implied end tags
5816                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5817                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5818                     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;  
5819                }                }
5820    
5821                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5822                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5823                    ## NOTE: |<table><tr><table>|
5824                    !!!parse-error (type => 'not closed',
5825                                    text => $self->{open_elements}->[-1]->[0]
5826                                        ->manakai_local_name,
5827                                    token => $token);
5828                  } else {
5829                    !!!cp ('t226');
5830                }                }
5831    
5832                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5833                  pop @{$open_tables};
5834    
5835                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5836    
5837                ## reprocess            ## reprocess
5838                redo B;            !!!ack-later;
5839              next B;
5840            } elsif ($token->{tag_name} eq 'style') {
5841              if (not $open_tables->[-1]->[1]) { # tainted
5842                !!!cp ('t227.8');
5843                ## NOTE: This is a "as if in head" code clone.
5844                $parse_rcdata->(CDATA_CONTENT_MODEL);
5845                next B;
5846              } else {
5847                !!!cp ('t227.7');
5848                #
5849              }
5850            } elsif ($token->{tag_name} eq 'script') {
5851              if (not $open_tables->[-1]->[1]) { # tainted
5852                !!!cp ('t227.6');
5853                ## NOTE: This is a "as if in head" code clone.
5854                $script_start_tag->();
5855                next B;
5856              } else {
5857                !!!cp ('t227.5');
5858                #
5859              }
5860            } elsif ($token->{tag_name} eq 'input') {
5861              if (not $open_tables->[-1]->[1]) { # tainted
5862                if ($token->{attributes}->{type}) { ## TODO: case
5863                  my $type = lc $token->{attributes}->{type}->{value};
5864                  if ($type eq 'hidden') {
5865                    !!!cp ('t227.3');
5866                    !!!parse-error (type => 'in table',
5867                                    text => $token->{tag_name}, token => $token);
5868    
5869                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5870    
5871                    ## TODO: form element pointer
5872    
5873                    pop @{$self->{open_elements}};
5874    
5875                    !!!next-token;
5876                    !!!ack ('t227.2.1');
5877                    next B;
5878                  } else {
5879                    !!!cp ('t227.2');
5880                    #
5881                  }
5882              } else {              } else {
5883                  !!!cp ('t227.1');
5884                #                #
5885              }              }
5886            } elsif ($token->{type} eq 'end tag') {            } else {
5887                !!!cp ('t227.4');
5888                #
5889              }
5890            } else {
5891              !!!cp ('t227');
5892              #
5893            }
5894    
5895            !!!parse-error (type => 'in table', text => $token->{tag_name},
5896                            token => $token);
5897    
5898            $insert = $insert_to_foster;
5899            #
5900          } elsif ($token->{type} == END_TAG_TOKEN) {
5901              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5902                  $self->{insertion_mode} eq 'in row') {                  $self->{insertion_mode} == IN_ROW_IM) {
5903                ## have an element in table scope                ## have an element in table scope
5904                my $i;                my $i;
5905                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5906                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5907                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5908                      !!!cp ('t228');
5909                    $i = $_;                    $i = $_;
5910                    last INSCOPE;                    last INSCOPE;
5911                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5912                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5913                    last INSCOPE;                    last INSCOPE;
5914                  }                  }
5915                } # INSCOPE                } # INSCOPE
5916                unless (defined $i) {                unless (defined $i) {
5917                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5918                    !!!parse-error (type => 'unmatched end tag',
5919                                    text => $token->{tag_name}, token => $token);
5920                  ## Ignore the token                  ## Ignore the token
5921                    !!!nack ('t230.1');
5922                  !!!next-token;                  !!!next-token;
5923                  redo B;                  next B;
5924                  } else {
5925                    !!!cp ('t232');
5926                }                }
5927    
5928                ## Clear back to table row context                ## Clear back to table row context
5929                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5930                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5931                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5932                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5933                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5934                }                }
5935    
5936                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5937                $self->{insertion_mode} = 'in table body';                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5938                !!!next-token;                !!!next-token;
5939                redo B;                !!!nack ('t231.1');
5940                  next B;
5941              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5942                if ($self->{insertion_mode} eq 'in row') {                if ($self->{insertion_mode} == IN_ROW_IM) {
5943                  ## As if </tr>                  ## As if </tr>
5944                  ## have an element in table scope                  ## have an element in table scope
5945                  my $i;                  my $i;
5946                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5947                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5948                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5949                        !!!cp ('t233');
5950                      $i = $_;                      $i = $_;
5951                      last INSCOPE;                      last INSCOPE;
5952                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5953                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5954                      last INSCOPE;                      last INSCOPE;
5955                    }                    }
5956                  } # INSCOPE                  } # INSCOPE
5957                  unless (defined $i) {                  unless (defined $i) {
5958                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5959    ## TODO: The following is wrong.
5960                      !!!parse-error (type => 'unmatched end tag',
5961                                      text => $token->{type}, token => $token);
5962                    ## Ignore the token                    ## Ignore the token
5963                      !!!nack ('t236.1');
5964                    !!!next-token;                    !!!next-token;
5965                    redo B;                    next B;
5966                  }                  }
5967                                    
5968                  ## Clear back to table row context                  ## Clear back to table row context
5969                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5970                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5971                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5972                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5973                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5974                  }                  }
5975                                    
5976                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5977                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5978                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5979                }                }
5980    
5981                if ($self->{insertion_mode} eq 'in table body') {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5982                  ## have an element in table scope                  ## have an element in table scope
5983                  my $i;                  my $i;
5984                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5985                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5986                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5987                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5988                      $i = $_;                      $i = $_;
5989                      last INSCOPE;                      last INSCOPE;
5990                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5991                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5992                      last INSCOPE;                      last INSCOPE;
5993                    }                    }
5994                  } # INSCOPE                  } # INSCOPE
5995                  unless (defined $i) {                  unless (defined $i) {
5996                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5997                      !!!parse-error (type => 'unmatched end tag',
5998                                      text => $token->{tag_name}, token => $token);
5999                    ## Ignore the token                    ## Ignore the token
6000                      !!!nack ('t239.1');
6001                    !!!next-token;                    !!!next-token;
6002                    redo B;                    next B;
6003                  }                  }
6004                                    
6005                  ## Clear back to table body context                  ## Clear back to table body context
6006                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6007                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
6008                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6009                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6010                  }                  }
6011                                    
# Line 4378  sub _tree_construction_main ($) { Line 6017  sub _tree_construction_main ($) {
6017                  ## nop by definition                  ## nop by definition
6018                                    
6019                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6020                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
6021                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
6022                }                }
6023    
6024                  ## NOTE: </table> in the "in table" insertion mode.
6025                  ## When you edit the code fragment below, please ensure that
6026                  ## the code for <table> in the "in table" insertion mode
6027                  ## is synced with it.
6028    
6029                ## have a table element in table scope                ## have a table element in table scope
6030                my $i;                my $i;
6031                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6032                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6033                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
6034                      !!!cp ('t241');
6035                    $i = $_;                    $i = $_;
6036                    last INSCOPE;                    last INSCOPE;
6037                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6038                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
6039                    last INSCOPE;                    last INSCOPE;
6040                  }                  }
6041                } # INSCOPE                } # INSCOPE
6042                unless (defined $i) {                unless (defined $i) {
6043                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
6044                    !!!parse-error (type => 'unmatched end tag',
6045                                    text => $token->{tag_name}, token => $token);
6046                  ## Ignore the token                  ## Ignore the token
6047                    !!!nack ('t243.1');
6048                  !!!next-token;                  !!!next-token;
6049                  redo B;                  next 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;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6050                }                }
6051                                    
6052                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
6053                  pop @{$open_tables};
6054                                
6055                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6056                                
6057                !!!next-token;                !!!next-token;
6058                redo B;                next B;
6059              } elsif ({              } elsif ({
6060                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6061                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
6062                       ($self->{insertion_mode} eq 'in row' or                       $self->{insertion_mode} & ROW_IMS) {
6063                        $self->{insertion_mode} eq 'in table body')) {                if ($self->{insertion_mode} == IN_ROW_IM) {
               if ($self->{insertion_mode} eq 'in row') {  
6064                  ## have an element in table scope                  ## have an element in table scope
6065                  my $i;                  my $i;
6066                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6067                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6068                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6069                        !!!cp ('t247');
6070                      $i = $_;                      $i = $_;
6071                      last INSCOPE;                      last INSCOPE;
6072                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6073                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
6074                      last INSCOPE;                      last INSCOPE;
6075                    }                    }
6076                  } # INSCOPE                  } # INSCOPE
6077                    unless (defined $i) {                    unless (defined $i) {
6078                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
6079                        !!!parse-error (type => 'unmatched end tag',
6080                                        text => $token->{tag_name}, token => $token);
6081                      ## Ignore the token                      ## Ignore the token
6082                        !!!nack ('t249.1');
6083                      !!!next-token;                      !!!next-token;
6084                      redo B;                      next B;
6085                    }                    }
6086                                    
6087                  ## As if </tr>                  ## As if </tr>
# Line 4455  sub _tree_construction_main ($) { Line 6089  sub _tree_construction_main ($) {
6089                  my $i;                  my $i;
6090                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6091                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6092                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6093                        !!!cp ('t250');
6094                      $i = $_;                      $i = $_;
6095                      last INSCOPE;                      last INSCOPE;
6096                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6097                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
6098                      last INSCOPE;                      last INSCOPE;
6099                    }                    }
6100                  } # INSCOPE                  } # INSCOPE
6101                    unless (defined $i) {                    unless (defined $i) {
6102                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
6103                        !!!parse-error (type => 'unmatched end tag',
6104                                        text => 'tr', token => $token);
6105                      ## Ignore the token                      ## Ignore the token
6106                        !!!nack ('t252.1');
6107                      !!!next-token;                      !!!next-token;
6108                      redo B;                      next B;
6109                    }                    }
6110                                    
6111                  ## Clear back to table row context                  ## Clear back to table row context
6112                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6113                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
6114                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
6115                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6116                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6117                  }                  }
6118                                    
6119                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
6120                  $self->{insertion_mode} = 'in table body';                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
6121                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
6122                }                }
6123    
# Line 4488  sub _tree_construction_main ($) { Line 6125  sub _tree_construction_main ($) {
6125                my $i;                my $i;
6126                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6127                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6128                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6129                      !!!cp ('t254');
6130                    $i = $_;                    $i = $_;
6131                    last INSCOPE;                    last INSCOPE;
6132                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6133                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6134                    last INSCOPE;                    last INSCOPE;
6135                  }                  }
6136                } # INSCOPE                } # INSCOPE
6137                unless (defined $i) {                unless (defined $i) {
6138                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
6139                    !!!parse-error (type => 'unmatched end tag',
6140                                    text => $token->{tag_name}, token => $token);
6141                  ## Ignore the token                  ## Ignore the token
6142                    !!!nack ('t256.1');
6143                  !!!next-token;                  !!!next-token;
6144                  redo B;                  next B;
6145                }                }
6146    
6147                ## Clear back to table body context                ## Clear back to table body context
6148                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6149                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6150                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6151                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6152                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6153                }                }
6154    
6155                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6156                $self->{insertion_mode} = 'in table';                $self->{insertion_mode} = IN_TABLE_IM;
6157                  !!!nack ('t257.1');
6158                !!!next-token;                !!!next-token;
6159                redo B;                next B;
6160              } elsif ({              } elsif ({
6161                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6162                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6163                        tr => 1, # $self->{insertion_mode} eq 'in row'                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6164                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} eq 'in table'                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6165                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6166                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6167                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6168                !!!next-token;                            text => $token->{tag_name}, token => $token);
6169                redo B;            ## Ignore the token
6170              } else {            !!!nack ('t258.1');
6171                #             !!!next-token;
6172              }            next B;
6173            } else {          } else {
6174              die "$0: $token->{type}: Unknown token type";            !!!cp ('t259');
6175            }            !!!parse-error (type => 'in table:/',
6176                              text => $token->{tag_name}, token => $token);
6177    
6178              $insert = $insert_to_foster;
6179              #
6180            }
6181          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6182            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6183                    @{$self->{open_elements}} == 1) { # redundant, maybe
6184              !!!parse-error (type => 'in body:#eof', token => $token);
6185              !!!cp ('t259.1');
6186              #
6187            } else {
6188              !!!cp ('t259.2');
6189              #
6190            }
6191    
6192            !!!parse-error (type => 'in table:'.$token->{tag_name});          ## Stop parsing
6193            $in_body->($insert_to_foster);          last B;
6194            redo B;        } else {
6195          } elsif ($self->{insertion_mode} eq 'in column group') {          die "$0: $token->{type}: Unknown token type";
6196            if ($token->{type} eq 'character') {        }
6197        } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6198              if ($token->{type} == CHARACTER_TOKEN) {
6199              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6200                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6201                unless (length $token->{data}) {                unless (length $token->{data}) {
6202                    !!!cp ('t260');
6203                  !!!next-token;                  !!!next-token;
6204                  redo B;                  next B;
6205                }                }
6206              }              }
6207                            
6208                !!!cp ('t261');
6209              #              #
6210            } elsif ($token->{type} eq 'start tag') {            } elsif ($token->{type} == START_TAG_TOKEN) {
6211              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6212                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
6213                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6214                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6215                  !!!ack ('t262.1');
6216                !!!next-token;                !!!next-token;
6217                redo B;                next B;
6218              } else {              } else {
6219                  !!!cp ('t263');
6220                #                #
6221              }              }
6222            } elsif ($token->{type} eq 'end tag') {            } elsif ($token->{type} == END_TAG_TOKEN) {
6223              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6224                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6225                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
6226                    !!!parse-error (type => 'unmatched end tag',
6227                                    text => 'colgroup', token => $token);
6228                  ## Ignore the token                  ## Ignore the token
6229                  !!!next-token;                  !!!next-token;
6230                  redo B;                  next B;
6231                } else {                } else {
6232                    !!!cp ('t265');
6233                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6234                  $self->{insertion_mode} = 'in table';                  $self->{insertion_mode} = IN_TABLE_IM;
6235                  !!!next-token;                  !!!next-token;
6236                  redo B;                              next B;            
6237                }                }
6238              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6239                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
6240                  !!!parse-error (type => 'unmatched end tag',
6241                                  text => 'col', token => $token);
6242                ## Ignore the token                ## Ignore the token
6243                !!!next-token;                !!!next-token;
6244                redo B;                next B;
6245              } else {              } else {
6246                  !!!cp ('t267');
6247                #                #
6248              }              }
6249            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6250              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6251            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6252              !!!cp ('t270.2');
6253              ## Stop parsing.
6254              last B;
6255            } else {
6256              ## NOTE: As if </colgroup>.
6257              !!!cp ('t270.1');
6258              pop @{$self->{open_elements}}; # colgroup
6259              $self->{insertion_mode} = IN_TABLE_IM;
6260              ## Reprocess.
6261              next B;
6262            }
6263          } else {
6264            die "$0: $token->{type}: Unknown token type";
6265          }
6266    
6267            ## As if </colgroup>            ## As if </colgroup>
6268            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6269              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
6270    ## TODO: Wrong error type?
6271                !!!parse-error (type => 'unmatched end tag',
6272                                text => 'colgroup', token => $token);
6273              ## Ignore the token              ## Ignore the token
6274                !!!nack ('t269.1');
6275              !!!next-token;              !!!next-token;
6276              redo B;              next B;
6277            } else {            } else {
6278                !!!cp ('t270');
6279              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6280              $self->{insertion_mode} = 'in table';              $self->{insertion_mode} = IN_TABLE_IM;
6281                !!!ack-later;
6282              ## reprocess              ## reprocess
6283              redo B;              next B;
6284              }
6285        } elsif ($self->{insertion_mode} & SELECT_IMS) {
6286          if ($token->{type} == CHARACTER_TOKEN) {
6287            !!!cp ('t271');
6288            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6289            !!!next-token;
6290            next B;
6291          } elsif ($token->{type} == START_TAG_TOKEN) {
6292            if ($token->{tag_name} eq 'option') {
6293              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6294                !!!cp ('t272');
6295                ## As if </option>
6296                pop @{$self->{open_elements}};
6297              } else {
6298                !!!cp ('t273');
6299            }            }
         } 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 'start tag') {  
             if ($token->{tag_name} eq 'option') {  
               if ($self->{open_elements}->[-1]->[1] eq 'option') {  
                 ## As if </option>  
                 pop @{$self->{open_elements}};  
               }  
6300    
6301                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6302                !!!next-token;            !!!nack ('t273.1');
6303                redo B;            !!!next-token;
6304              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6305                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6306                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6307                  pop @{$self->{open_elements}};              !!!cp ('t274');
6308                }              ## As if </option>
6309                pop @{$self->{open_elements}};
6310              } else {
6311                !!!cp ('t275');
6312              }
6313    
6314                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6315                  ## As if </optgroup>              !!!cp ('t276');
6316                  pop @{$self->{open_elements}};              ## As if </optgroup>
6317                }              pop @{$self->{open_elements}};
6318              } else {
6319                !!!cp ('t277');
6320              }
6321    
6322                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6323                !!!next-token;            !!!nack ('t277.1');
6324                redo B;            !!!next-token;
6325              } elsif ($token->{tag_name} eq 'select') {            next B;
6326                !!!parse-error (type => 'not closed:select');          } elsif ({
6327                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6328                ## have an element in table scope                   }->{$token->{tag_name}} or
6329                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6330                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6331                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6332                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6333                    $i = $_;                     tr => 1, td => 1, th => 1,
6334                    last INSCOPE;                    }->{$token->{tag_name}})) {
6335                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6336                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6337                           }->{$node->[1]}) {                            token => $token);
6338                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6339                  }            ## as if there were </select> (otherwise).
6340                } # INSCOPE            ## have an element in table scope
6341                unless (defined $i) {            my $i;
6342                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6343                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6344                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6345                  redo B;                !!!cp ('t278');
6346                }                $i = $_;
6347                  last INSCOPE;
6348                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6349                  !!!cp ('t279');
6350                  last INSCOPE;
6351                }
6352              } # INSCOPE
6353              unless (defined $i) {
6354                !!!cp ('t280');
6355                !!!parse-error (type => 'unmatched end tag',
6356                                text => 'select', token => $token);
6357                ## Ignore the token
6358                !!!nack ('t280.1');
6359                !!!next-token;
6360                next B;
6361              }
6362                                
6363                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6364              splice @{$self->{open_elements}}, $i;
6365    
6366                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6367    
6368                !!!next-token;            if ($token->{tag_name} eq 'select') {
6369                redo B;              !!!nack ('t281.2');
6370              } else {              !!!next-token;
6371                #              next B;
6372              } else {
6373                !!!cp ('t281.1');
6374                !!!ack-later;
6375                ## Reprocess the token.
6376                next B;
6377              }
6378            } else {
6379              !!!cp ('t282');
6380              !!!parse-error (type => 'in select',
6381                              text => $token->{tag_name}, token => $token);
6382              ## Ignore the token
6383              !!!nack ('t282.1');
6384              !!!next-token;
6385              next B;
6386            }
6387          } elsif ($token->{type} == END_TAG_TOKEN) {
6388            if ($token->{tag_name} eq 'optgroup') {
6389              if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6390                  $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6391                !!!cp ('t283');
6392                ## As if </option>
6393                splice @{$self->{open_elements}}, -2;
6394              } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6395                !!!cp ('t284');
6396                pop @{$self->{open_elements}};
6397              } else {
6398                !!!cp ('t285');
6399                !!!parse-error (type => 'unmatched end tag',
6400                                text => $token->{tag_name}, token => $token);
6401                ## Ignore the token
6402              }
6403              !!!nack ('t285.1');
6404              !!!next-token;
6405              next B;
6406            } elsif ($token->{tag_name} eq 'option') {
6407              if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6408                !!!cp ('t286');
6409                pop @{$self->{open_elements}};
6410              } else {
6411                !!!cp ('t287');
6412                !!!parse-error (type => 'unmatched end tag',
6413                                text => $token->{tag_name}, token => $token);
6414                ## Ignore the token
6415              }
6416              !!!nack ('t287.1');
6417              !!!next-token;
6418              next B;
6419            } elsif ($token->{tag_name} eq 'select') {
6420              ## have an element in table scope
6421              my $i;
6422              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6423                my $node = $self->{open_elements}->[$_];
6424                if ($node->[1] & SELECT_EL) {
6425                  !!!cp ('t288');
6426                  $i = $_;
6427                  last INSCOPE;
6428                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6429                  !!!cp ('t289');
6430                  last INSCOPE;
6431              }              }
6432            } elsif ($token->{type} eq 'end tag') {            } # INSCOPE
6433              if ($token->{tag_name} eq 'optgroup') {            unless (defined $i) {
6434                if ($self->{open_elements}->[-1]->[1] eq 'option' and              !!!cp ('t290');
6435                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {              !!!parse-error (type => 'unmatched end tag',
6436                  ## As if </option>                              text => $token->{tag_name}, token => $token);
6437                  splice @{$self->{open_elements}}, -2;              ## Ignore the token
6438                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              !!!nack ('t290.1');
6439                  pop @{$self->{open_elements}};              !!!next-token;
6440                } else {              next B;
6441                  !!!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;  
               }  
6442                                
6443                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6444              splice @{$self->{open_elements}}, $i;
6445    
6446                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6447    
6448                !!!next-token;            !!!nack ('t291.1');
6449                redo B;            !!!next-token;
6450              } elsif ({            next B;
6451                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6452                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6453                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6454                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6455                                   }->{$token->{tag_name}}) {
6456                ## have an element in table scope  ## TODO: The following is wrong?
6457                my $i;            !!!parse-error (type => 'unmatched end tag',
6458                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;  
               }  
6459                                
6460                ## As if </select>            ## have an element in table scope
6461                ## have an element in table scope            my $i;
6462                undef $i;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6463                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {              my $node = $self->{open_elements}->[$_];
6464                  my $node = $self->{open_elements}->[$_];              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6465                  if ($node->[1] eq 'select') {                !!!cp ('t292');
6466                    $i = $_;                $i = $_;
6467                    last INSCOPE;                last INSCOPE;
6468                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6469                            table => 1, html => 1,                !!!cp ('t293');
6470                           }->{$node->[1]}) {                last INSCOPE;
6471                    last INSCOPE;              }
6472                  }            } # INSCOPE
6473                } # INSCOPE            unless (defined $i) {
6474                unless (defined $i) {              !!!cp ('t294');
6475                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6476                  ## Ignore the </select> token              !!!nack ('t294.1');
6477                  !!!next-token; ## TODO: ok?              !!!next-token;
6478                  redo B;              next B;
6479                }            }
6480                                
6481                splice @{$self->{open_elements}}, $i;            ## As if </select>
6482              ## have an element in table scope
6483                $self->_reset_insertion_mode;            undef $i;
6484              INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6485                ## reprocess              my $node = $self->{open_elements}->[$_];
6486                redo B;              if ($node->[1] & SELECT_EL) {
6487              } else {                !!!cp ('t295');
6488                #                $i = $_;
6489                  last INSCOPE;
6490                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6491    ## ISSUE: Can this state be reached?
6492                  !!!cp ('t296');
6493                  last INSCOPE;
6494              }              }
6495            } else {            } # INSCOPE
6496              #            unless (defined $i) {
6497                !!!cp ('t297');
6498    ## TODO: The following error type is correct?
6499                !!!parse-error (type => 'unmatched end tag',
6500                                text => 'select', token => $token);
6501                ## Ignore the </select> token
6502                !!!nack ('t297.1');
6503                !!!next-token; ## TODO: ok?
6504                next B;
6505            }            }
6506                  
6507              !!!cp ('t298');
6508              splice @{$self->{open_elements}}, $i;
6509    
6510            !!!parse-error (type => 'in select:'.$token->{tag_name});            $self->_reset_insertion_mode;
6511    
6512              !!!ack-later;
6513              ## reprocess
6514              next B;
6515            } else {
6516              !!!cp ('t299');
6517              !!!parse-error (type => 'in select:/',
6518                              text => $token->{tag_name}, token => $token);
6519            ## Ignore the token            ## Ignore the token
6520              !!!nack ('t299.3');
6521            !!!next-token;            !!!next-token;
6522            redo B;            next B;
6523          } elsif ($self->{insertion_mode} eq 'after body') {          }
6524            if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6525              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6526                my $data = $1;                  @{$self->{open_elements}} == 1) { # redundant, maybe
6527                ## As if in body            !!!cp ('t299.1');
6528                $reconstruct_active_formatting_elements->($insert_to_current);            !!!parse-error (type => 'in body:#eof', token => $token);
6529            } else {
6530              !!!cp ('t299.2');
6531            }
6532    
6533            ## Stop parsing.
6534            last B;
6535          } else {
6536            die "$0: $token->{type}: Unknown token type";
6537          }
6538        } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6539          if ($token->{type} == CHARACTER_TOKEN) {
6540            if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6541              my $data = $1;
6542              ## As if in body
6543              $reconstruct_active_formatting_elements->($insert_to_current);
6544                                
6545                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6546              
6547              unless (length $token->{data}) {
6548                !!!cp ('t300');
6549                !!!next-token;
6550                next B;
6551              }
6552            }
6553            
6554            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6555              !!!cp ('t301');
6556              !!!parse-error (type => 'after html:#text', token => $token);
6557    
6558                unless (length $token->{data}) {            ## Reprocess in the "after body" insertion mode.
6559                  !!!next-token;          } else {
6560                  redo B;            !!!cp ('t302');
6561                }          }
6562              }          
6563                        ## "after body" insertion mode
6564              #          !!!parse-error (type => 'after body:#text', token => $token);
6565              !!!parse-error (type => 'after body:#character');  
6566            } elsif ($token->{type} eq 'start tag') {          $self->{insertion_mode} = IN_BODY_IM;
6567              !!!parse-error (type => 'after body:'.$token->{tag_name});          ## reprocess
6568              #          next B;
6569            } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == START_TAG_TOKEN) {
6570              if ($token->{tag_name} eq 'html') {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6571                if (defined $self->{inner_html_node}) {            !!!cp ('t303');
6572                  !!!parse-error (type => 'unmatched end tag:html');            !!!parse-error (type => 'after html',
6573                  ## Ignore the token                            text => $token->{tag_name}, token => $token);
6574                  !!!next-token;            
6575                  redo B;            ## Reprocess in the "after body" insertion mode.
6576                } else {          } else {
6577                  $previous_insertion_mode = $self->{insertion_mode};            !!!cp ('t304');
6578                  $self->{insertion_mode} = 'trailing end';          }
6579                  !!!next-token;  
6580                  redo B;          ## "after body" insertion mode
6581                }          !!!parse-error (type => 'after body',
6582              } else {                          text => $token->{tag_name}, token => $token);
6583                !!!parse-error (type => 'after body:/'.$token->{tag_name});  
6584              }          $self->{insertion_mode} = IN_BODY_IM;
6585            !!!ack-later;
6586            ## reprocess
6587            next B;
6588          } elsif ($token->{type} == END_TAG_TOKEN) {
6589            if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6590              !!!cp ('t305');
6591              !!!parse-error (type => 'after html:/',
6592                              text => $token->{tag_name}, token => $token);
6593              
6594              $self->{insertion_mode} = AFTER_BODY_IM;
6595              ## Reprocess in the "after body" insertion mode.
6596            } else {
6597              !!!cp ('t306');
6598            }
6599    
6600            ## "after body" insertion mode
6601            if ($token->{tag_name} eq 'html') {
6602              if (defined $self->{inner_html_node}) {
6603                !!!cp ('t307');
6604                !!!parse-error (type => 'unmatched end tag',
6605                                text => 'html', token => $token);
6606                ## Ignore the token
6607                !!!next-token;
6608                next B;
6609            } else {            } else {
6610              die "$0: $token->{type}: Unknown token type";              !!!cp ('t308');
6611                $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6612                !!!next-token;
6613                next B;
6614            }            }
6615            } else {
6616              !!!cp ('t309');
6617              !!!parse-error (type => 'after body:/',
6618                              text => $token->{tag_name}, token => $token);
6619    
6620            $self->{insertion_mode} = 'in body';            $self->{insertion_mode} = IN_BODY_IM;
6621            ## reprocess            ## reprocess
6622            redo B;            next B;
6623      } elsif ($self->{insertion_mode} eq 'in frameset') {          }
6624        if ($token->{type} eq 'character') {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6625            !!!cp ('t309.2');
6626            ## Stop parsing
6627            last B;
6628          } else {
6629            die "$0: $token->{type}: Unknown token type";
6630          }
6631        } elsif ($self->{insertion_mode} & FRAME_IMS) {
6632          if ($token->{type} == CHARACTER_TOKEN) {
6633          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6634            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6635              
6636            unless (length $token->{data}) {            unless (length $token->{data}) {
6637                !!!cp ('t310');
6638              !!!next-token;              !!!next-token;
6639              redo B;              next B;
6640            }            }
6641          }          }
6642            
6643          !!!parse-error (type => 'in frameset:#character');          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6644          ## Ignore the token            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6645          !!!next-token;              !!!cp ('t311');
6646          redo B;              !!!parse-error (type => 'in frameset:#text', token => $token);
6647        } elsif ($token->{type} eq 'start tag') {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6648          if ($token->{tag_name} eq 'frameset') {              !!!cp ('t312');
6649            !!!insert-element ($token->{tag_name}, $token->{attributes});              !!!parse-error (type => 'after frameset:#text', token => $token);
6650              } else { # "after after frameset"
6651                !!!cp ('t313');
6652                !!!parse-error (type => 'after html:#text', token => $token);
6653              }
6654              
6655              ## Ignore the token.
6656              if (length $token->{data}) {
6657                !!!cp ('t314');
6658                ## reprocess the rest of characters
6659              } else {
6660                !!!cp ('t315');
6661                !!!next-token;
6662              }
6663              next B;
6664            }
6665            
6666            die qq[$0: Character "$token->{data}"];
6667          } elsif ($token->{type} == START_TAG_TOKEN) {
6668            if ($token->{tag_name} eq 'frameset' and
6669                $self->{insertion_mode} == IN_FRAMESET_IM) {
6670              !!!cp ('t318');
6671              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6672              !!!nack ('t318.1');
6673            !!!next-token;            !!!next-token;
6674            redo B;            next B;
6675          } elsif ($token->{tag_name} eq 'frame') {          } elsif ($token->{tag_name} eq 'frame' and
6676            !!!insert-element ($token->{tag_name}, $token->{attributes});                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6677              !!!cp ('t319');
6678              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6679            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6680              !!!ack ('t319.1');
6681            !!!next-token;            !!!next-token;
6682            redo B;            next B;
6683          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6684            ## NOTE: As if in body.            !!!cp ('t320');
6685            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6686            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6687          } else {            next B;
6688            !!!parse-error (type => 'in frameset:'.$token->{tag_name});  
6689              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6690              ## has no parse error.
6691            } else {
6692              if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6693                !!!cp ('t321');
6694                !!!parse-error (type => 'in frameset',
6695                                text => $token->{tag_name}, token => $token);
6696              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6697                !!!cp ('t322');
6698                !!!parse-error (type => 'after frameset',
6699                                text => $token->{tag_name}, token => $token);
6700              } else { # "after after frameset"
6701                !!!cp ('t322.2');
6702                !!!parse-error (type => 'after after frameset',
6703                                text => $token->{tag_name}, token => $token);
6704              }
6705            ## Ignore the token            ## Ignore the token
6706              !!!nack ('t322.1');
6707            !!!next-token;            !!!next-token;
6708            redo B;            next B;
6709          }          }
6710        } elsif ($token->{type} eq 'end tag') {        } elsif ($token->{type} == END_TAG_TOKEN) {
6711          if ($token->{tag_name} eq 'frameset') {          if ($token->{tag_name} eq 'frameset' and
6712            if ($self->{open_elements}->[-1]->[1] eq 'html' and              $self->{insertion_mode} == IN_FRAMESET_IM) {
6713              if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6714                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6715              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6716                !!!parse-error (type => 'unmatched end tag',
6717                                text => $token->{tag_name}, token => $token);
6718              ## Ignore the token              ## Ignore the token
6719              !!!next-token;              !!!next-token;
6720            } else {            } else {
6721                !!!cp ('t326');
6722              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6723              !!!next-token;              !!!next-token;
6724            }            }
6725    
6726            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6727                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6728              $self->{insertion_mode} = 'after frameset';              !!!cp ('t327');
6729                $self->{insertion_mode} = AFTER_FRAMESET_IM;
6730              } else {
6731                !!!cp ('t328');
6732            }            }
6733            redo B;            next B;
6734            } elsif ($token->{tag_name} eq 'html' and
6735                     $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6736              !!!cp ('t329');
6737              $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6738              !!!next-token;
6739              next B;
6740          } else {          } else {
6741            !!!parse-error (type => 'in frameset:/'.$token->{tag_name});            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6742                !!!cp ('t330');
6743                !!!parse-error (type => 'in frameset:/',
6744                                text => $token->{tag_name}, token => $token);
6745              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6746                !!!cp ('t330.1');
6747                !!!parse-error (type => 'after frameset:/',
6748                                text => $token->{tag_name}, token => $token);
6749              } else { # "after after html"
6750                !!!cp ('t331');
6751                !!!parse-error (type => 'after after frameset:/',
6752                                text => $token->{tag_name}, token => $token);
6753              }
6754            ## Ignore the token            ## Ignore the token
6755            !!!next-token;            !!!next-token;
6756            redo B;            next B;
6757          }          }
6758          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6759            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6760                    @{$self->{open_elements}} == 1) { # redundant, maybe
6761              !!!cp ('t331.1');
6762              !!!parse-error (type => 'in body:#eof', token => $token);
6763            } else {
6764              !!!cp ('t331.2');
6765            }
6766            
6767            ## Stop parsing
6768            last B;
6769        } else {        } else {
6770          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6771        }        }
     } elsif ($self->{insertion_mode} eq 'after frameset') {  
       if ($token->{type} eq 'character') {  
             if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {  
               $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);  
6772    
6773                unless (length $token->{data}) {        ## ISSUE: An issue in spec here
6774                  !!!next-token;      } else {
6775                  redo B;        die "$0: $self->{insertion_mode}: Unknown insertion mode";
6776                }      }
             }  
6777    
6778              if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {      ## "in body" insertion mode
6779                !!!parse-error (type => 'after frameset:#character');      if ($token->{type} == START_TAG_TOKEN) {
6780          if ($token->{tag_name} eq 'script') {
6781            !!!cp ('t332');
6782            ## NOTE: This is an "as if in head" code clone
6783            $script_start_tag->();
6784            next B;
6785          } elsif ($token->{tag_name} eq 'style') {
6786            !!!cp ('t333');
6787            ## NOTE: This is an "as if in head" code clone
6788            $parse_rcdata->(CDATA_CONTENT_MODEL);
6789            next B;
6790          } elsif ({
6791                    base => 1, link => 1,
6792                   }->{$token->{tag_name}}) {
6793            !!!cp ('t334');
6794            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6795            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6796            pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6797            !!!ack ('t334.1');
6798            !!!next-token;
6799            next B;
6800          } elsif ($token->{tag_name} eq 'meta') {
6801            ## NOTE: This is an "as if in head" code clone, only "-t" differs
6802            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6803            my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6804    
6805                ## Ignore the token.          unless ($self->{confident}) {
6806                if (length $token->{data}) {            if ($token->{attributes}->{charset}) {
6807                  ## reprocess the rest of characters              !!!cp ('t335');
6808                } else {              ## NOTE: Whether the encoding is supported or not is handled
6809                  !!!next-token;              ## in the {change_encoding} callback.
6810                }              $self->{change_encoding}
6811                redo B;                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6812                
6813                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6814                    ->set_user_data (manakai_has_reference =>
6815                                         $token->{attributes}->{charset}
6816                                             ->{has_reference});
6817              } elsif ($token->{attributes}->{content}) {
6818                if ($token->{attributes}->{content}->{value}
6819                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6820                        [\x09-\x0D\x20]*=
6821                        [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6822                        ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6823                  !!!cp ('t336');
6824                  ## NOTE: Whether the encoding is supported or not is handled
6825                  ## in the {change_encoding} callback.
6826                  $self->{change_encoding}
6827                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6828                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6829                      ->set_user_data (manakai_has_reference =>
6830                                           $token->{attributes}->{content}
6831                                                 ->{has_reference});
6832              }              }
6833              }
6834            } else {
6835              if ($token->{attributes}->{charset}) {
6836                !!!cp ('t337');
6837                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6838                    ->set_user_data (manakai_has_reference =>
6839                                         $token->{attributes}->{charset}
6840                                             ->{has_reference});
6841              }
6842              if ($token->{attributes}->{content}) {
6843                !!!cp ('t338');
6844                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6845                    ->set_user_data (manakai_has_reference =>
6846                                         $token->{attributes}->{content}
6847                                             ->{has_reference});
6848              }
6849            }
6850    
6851          die qq[$0: Character "$token->{data}"];          !!!ack ('t338.1');
6852        } elsif ($token->{type} eq 'start tag') {          !!!next-token;
6853          if ($token->{tag_name} eq 'noframes') {          next B;
6854            ## NOTE: As if in body.        } elsif ($token->{tag_name} eq 'title') {
6855            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);          !!!cp ('t341');
6856            redo B;          ## NOTE: This is an "as if in head" code clone
6857            $parse_rcdata->(RCDATA_CONTENT_MODEL);
6858            next B;
6859          } elsif ($token->{tag_name} eq 'body') {
6860            !!!parse-error (type => 'in body', text => 'body', token => $token);
6861                  
6862            if (@{$self->{open_elements}} == 1 or
6863                not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6864              !!!cp ('t342');
6865              ## Ignore the token
6866          } else {          } else {
6867            !!!parse-error (type => 'after frameset:'.$token->{tag_name});            my $body_el = $self->{open_elements}->[1]->[0];
6868              for my $attr_name (keys %{$token->{attributes}}) {
6869                unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6870                  !!!cp ('t343');
6871                  $body_el->set_attribute_ns
6872                    (undef, [undef, $attr_name],
6873                     $token->{attributes}->{$attr_name}->{value});
6874                }
6875              }
6876            }
6877            !!!nack ('t343.1');
6878            !!!next-token;
6879            next B;
6880          } elsif ({
6881                    address => 1, blockquote => 1, center => 1, dir => 1,
6882                    div => 1, dl => 1, fieldset => 1,
6883                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6884                    menu => 1, ol => 1, p => 1, ul => 1,
6885                    pre => 1, listing => 1,
6886                    form => 1,
6887                    table => 1,
6888                    hr => 1,
6889                   }->{$token->{tag_name}}) {
6890            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6891              !!!cp ('t350');
6892              !!!parse-error (type => 'in form:form', token => $token);
6893            ## Ignore the token            ## Ignore the token
6894              !!!nack ('t350.1');
6895            !!!next-token;            !!!next-token;
6896            redo B;            next B;
6897          }          }
6898        } elsif ($token->{type} eq 'end tag') {  
6899          if ($token->{tag_name} eq 'html') {          ## has a p element in scope
6900            $previous_insertion_mode = $self->{insertion_mode};          INSCOPE: for (reverse @{$self->{open_elements}}) {
6901            $self->{insertion_mode} = 'trailing end';            if ($_->[1] & P_EL) {
6902                !!!cp ('t344');
6903                !!!back-token; # <form>
6904                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6905                          line => $token->{line}, column => $token->{column}};
6906                next B;
6907              } elsif ($_->[1] & SCOPING_EL) {
6908                !!!cp ('t345');
6909                last INSCOPE;
6910              }
6911            } # INSCOPE
6912              
6913            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6914            if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6915              !!!nack ('t346.1');
6916              !!!next-token;
6917              if ($token->{type} == CHARACTER_TOKEN) {
6918                $token->{data} =~ s/^\x0A//;
6919                unless (length $token->{data}) {
6920                  !!!cp ('t346');
6921                  !!!next-token;
6922                } else {
6923                  !!!cp ('t349');
6924                }
6925              } else {
6926                !!!cp ('t348');
6927              }
6928            } elsif ($token->{tag_name} eq 'form') {
6929              !!!cp ('t347.1');
6930              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6931    
6932              !!!nack ('t347.2');
6933              !!!next-token;
6934            } elsif ($token->{tag_name} eq 'table') {
6935              !!!cp ('t382');
6936              push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6937              
6938              $self->{insertion_mode} = IN_TABLE_IM;
6939    
6940              !!!nack ('t382.1');
6941              !!!next-token;
6942            } elsif ($token->{tag_name} eq 'hr') {
6943              !!!cp ('t386');
6944              pop @{$self->{open_elements}};
6945            
6946              !!!nack ('t386.1');
6947            !!!next-token;            !!!next-token;
           redo B;  
6948          } else {          } else {
6949            !!!parse-error (type => 'after frameset:/'.$token->{tag_name});            !!!nack ('t347.1');
           ## Ignore the token  
6950            !!!next-token;            !!!next-token;
           redo B;  
6951          }          }
6952        } else {          next B;
6953          die "$0: $token->{type}: Unknown token type";        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6954        }          ## has a p element in scope
6955            INSCOPE: for (reverse @{$self->{open_elements}}) {
6956              if ($_->[1] & P_EL) {
6957                !!!cp ('t353');
6958                !!!back-token; # <x>
6959                $token = {type => END_TAG_TOKEN, tag_name => 'p',
6960                          line => $token->{line}, column => $token->{column}};
6961                next B;
6962              } elsif ($_->[1] & SCOPING_EL) {
6963                !!!cp ('t354');
6964                last INSCOPE;
6965              }
6966            } # INSCOPE
6967              
6968            ## Step 1
6969            my $i = -1;
6970            my $node = $self->{open_elements}->[$i];
6971            my $li_or_dtdd = {li => {li => 1},
6972                              dt => {dt => 1, dd => 1},
6973                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6974            LI: {
6975              ## Step 2
6976              if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6977                if ($i != -1) {
6978                  !!!cp ('t355');
6979                  !!!parse-error (type => 'not closed',
6980                                  text => $self->{open_elements}->[-1]->[0]
6981                                      ->manakai_local_name,
6982                                  token => $token);
6983                } else {
6984                  !!!cp ('t356');
6985                }
6986                splice @{$self->{open_elements}}, $i;
6987                last LI;
6988              } else {
6989                !!!cp ('t357');
6990              }
6991              
6992              ## Step 3
6993              if (not ($node->[1] & FORMATTING_EL) and
6994                  #not $phrasing_category->{$node->[1]} and
6995                  ($node->[1] & SPECIAL_EL or
6996                   $node->[1] & SCOPING_EL) and
6997                  not ($node->[1] & ADDRESS_EL) and
6998                  not ($node->[1] & DIV_EL)) {
6999                !!!cp ('t358');
7000                last LI;
7001              }
7002              
7003              !!!cp ('t359');
7004              ## Step 4
7005              $i--;
7006              $node = $self->{open_elements}->[$i];
7007              redo LI;
7008            } # LI
7009              
7010            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7011            !!!nack ('t359.1');
7012            !!!next-token;
7013            next B;
7014          } elsif ($token->{tag_name} eq 'plaintext') {
7015            ## has a p element in scope
7016            INSCOPE: for (reverse @{$self->{open_elements}}) {
7017              if ($_->[1] & P_EL) {
7018                !!!cp ('t367');
7019                !!!back-token; # <plaintext>
7020                $token = {type => END_TAG_TOKEN, tag_name => 'p',
7021                          line => $token->{line}, column => $token->{column}};
7022                next B;
7023              } elsif ($_->[1] & SCOPING_EL) {
7024                !!!cp ('t368');
7025                last INSCOPE;
7026              }
7027            } # INSCOPE
7028              
7029            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7030              
7031            $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7032              
7033            !!!nack ('t368.1');
7034            !!!next-token;
7035            next B;
7036          } elsif ($token->{tag_name} eq 'a') {
7037            AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7038              my $node = $active_formatting_elements->[$i];
7039              if ($node->[1] & A_EL) {
7040                !!!cp ('t371');
7041                !!!parse-error (type => 'in a:a', token => $token);
7042                
7043                !!!back-token; # <a>
7044                $token = {type => END_TAG_TOKEN, tag_name => 'a',
7045                          line => $token->{line}, column => $token->{column}};
7046                $formatting_end_tag->($token);
7047                
7048                AFE2: for (reverse 0..$#$active_formatting_elements) {
7049                  if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7050                    !!!cp ('t372');
7051                    splice @$active_formatting_elements, $_, 1;
7052                    last AFE2;
7053                  }
7054                } # AFE2
7055                OE: for (reverse 0..$#{$self->{open_elements}}) {
7056                  if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7057                    !!!cp ('t373');
7058                    splice @{$self->{open_elements}}, $_, 1;
7059                    last OE;
7060                  }
7061                } # OE
7062                last AFE;
7063              } elsif ($node->[0] eq '#marker') {
7064                !!!cp ('t374');
7065                last AFE;
7066              }
7067            } # AFE
7068              
7069            $reconstruct_active_formatting_elements->($insert_to_current);
7070    
7071        ## ISSUE: An issue in spec here          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7072      } elsif ($self->{insertion_mode} eq 'trailing end') {          push @$active_formatting_elements, $self->{open_elements}->[-1];
7073        ## states in the main stage is preserved yet # MUST  
7074                  !!!nack ('t374.1');
7075        if ($token->{type} eq 'character') {          !!!next-token;
7076          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          next B;
7077            my $data = $1;        } elsif ($token->{tag_name} eq 'nobr') {
7078            ## As if in the main phase.          $reconstruct_active_formatting_elements->($insert_to_current);
7079            ## NOTE: The insertion mode in the main phase  
7080            ## just before the phase has been changed to the trailing          ## has a |nobr| element in scope
7081            ## end phase is either "after body" or "after frameset".          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7082            $reconstruct_active_formatting_elements->($insert_to_current);            my $node = $self->{open_elements}->[$_];
7083              if ($node->[1] & NOBR_EL) {
7084                !!!cp ('t376');
7085                !!!parse-error (type => 'in nobr:nobr', token => $token);
7086                !!!back-token; # <nobr>
7087                $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7088                          line => $token->{line}, column => $token->{column}};
7089                next B;
7090              } elsif ($node->[1] & SCOPING_EL) {
7091                !!!cp ('t377');
7092                last INSCOPE;
7093              }
7094            } # INSCOPE
7095            
7096            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7097            push @$active_formatting_elements, $self->{open_elements}->[-1];
7098            
7099            !!!nack ('t377.1');
7100            !!!next-token;
7101            next B;
7102          } elsif ($token->{tag_name} eq 'button') {
7103            ## has a button element in scope
7104            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7105              my $node = $self->{open_elements}->[$_];
7106              if ($node->[1] & BUTTON_EL) {
7107                !!!cp ('t378');
7108                !!!parse-error (type => 'in button:button', token => $token);
7109                !!!back-token; # <button>
7110                $token = {type => END_TAG_TOKEN, tag_name => 'button',
7111                          line => $token->{line}, column => $token->{column}};
7112                next B;
7113              } elsif ($node->[1] & SCOPING_EL) {
7114                !!!cp ('t379');
7115                last INSCOPE;
7116              }
7117            } # INSCOPE
7118                        
7119            $self->{open_elements}->[-1]->[0]->manakai_append_text ($data);          $reconstruct_active_formatting_elements->($insert_to_current);
7120                        
7121            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7122    
7123            ## TODO: associate with $self->{form_element} if defined
7124    
7125            push @$active_formatting_elements, ['#marker', ''];
7126    
7127            !!!nack ('t379.1');
7128            !!!next-token;
7129            next B;
7130          } elsif ({
7131                    xmp => 1,
7132                    iframe => 1,
7133                    noembed => 1,
7134                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7135                    noscript => 0, ## TODO: 1 if scripting is enabled
7136                   }->{$token->{tag_name}}) {
7137            if ($token->{tag_name} eq 'xmp') {
7138              !!!cp ('t381');
7139              $reconstruct_active_formatting_elements->($insert_to_current);
7140            } else {
7141              !!!cp ('t399');
7142            }
7143            ## NOTE: There is an "as if in body" code clone.
7144            $parse_rcdata->(CDATA_CONTENT_MODEL);
7145            next B;
7146          } elsif ($token->{tag_name} eq 'isindex') {
7147            !!!parse-error (type => 'isindex', token => $token);
7148            
7149            if (defined $self->{form_element}) {
7150              !!!cp ('t389');
7151              ## Ignore the token
7152              !!!nack ('t389'); ## NOTE: Not acknowledged.
7153              !!!next-token;
7154              next B;
7155            } else {
7156              !!!ack ('t391.1');
7157    
7158              my $at = $token->{attributes};
7159              my $form_attrs;
7160              $form_attrs->{action} = $at->{action} if $at->{action};
7161              my $prompt_attr = $at->{prompt};
7162              $at->{name} = {name => 'name', value => 'isindex'};
7163              delete $at->{action};
7164              delete $at->{prompt};
7165              my @tokens = (
7166                            {type => START_TAG_TOKEN, tag_name => 'form',
7167                             attributes => $form_attrs,
7168                             line => $token->{line}, column => $token->{column}},
7169                            {type => START_TAG_TOKEN, tag_name => 'hr',
7170                             line => $token->{line}, column => $token->{column}},
7171                            {type => START_TAG_TOKEN, tag_name => 'p',
7172                             line => $token->{line}, column => $token->{column}},
7173                            {type => START_TAG_TOKEN, tag_name => 'label',
7174                             line => $token->{line}, column => $token->{column}},
7175                           );
7176              if ($prompt_attr) {
7177                !!!cp ('t390');
7178                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7179                               #line => $token->{line}, column => $token->{column},
7180                              };
7181              } else {
7182                !!!cp ('t391');
7183                push @tokens, {type => CHARACTER_TOKEN,
7184                               data => 'This is a searchable index. Insert your search keywords here: ',
7185                               #line => $token->{line}, column => $token->{column},
7186                              }; # SHOULD
7187                ## TODO: make this configurable
7188              }
7189              push @tokens,
7190                            {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7191                             line => $token->{line}, column => $token->{column}},
7192                            #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7193                            {type => END_TAG_TOKEN, tag_name => 'label',
7194                             line => $token->{line}, column => $token->{column}},
7195                            {type => END_TAG_TOKEN, tag_name => 'p',
7196                             line => $token->{line}, column => $token->{column}},
7197                            {type => START_TAG_TOKEN, tag_name => 'hr',
7198                             line => $token->{line}, column => $token->{column}},
7199                            {type => END_TAG_TOKEN, tag_name => 'form',
7200                             line => $token->{line}, column => $token->{column}};
7201              !!!back-token (@tokens);
7202              !!!next-token;
7203              next B;
7204            }
7205          } elsif ($token->{tag_name} eq 'textarea') {
7206            my $tag_name = $token->{tag_name};
7207            my $el;
7208            !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7209            
7210            ## TODO: $self->{form_element} if defined
7211            $self->{content_model} = RCDATA_CONTENT_MODEL;
7212            delete $self->{escape}; # MUST
7213            
7214            $insert->($el);
7215            
7216            my $text = '';
7217            !!!nack ('t392.1');
7218            !!!next-token;
7219            if ($token->{type} == CHARACTER_TOKEN) {
7220              $token->{data} =~ s/^\x0A//;
7221            unless (length $token->{data}) {            unless (length $token->{data}) {
7222                !!!cp ('t392');
7223              !!!next-token;              !!!next-token;
7224              redo B;            } else {
7225                !!!cp ('t393');
7226            }            }
7227            } else {
7228              !!!cp ('t394');
7229          }          }
7230            while ($token->{type} == CHARACTER_TOKEN) {
7231              !!!cp ('t395');
7232              $text .= $token->{data};
7233              !!!next-token;
7234            }
7235            if (length $text) {
7236              !!!cp ('t396');
7237              $el->manakai_append_text ($text);
7238            }
7239            
7240            $self->{content_model} = PCDATA_CONTENT_MODEL;
7241            
7242            if ($token->{type} == END_TAG_TOKEN and
7243                $token->{tag_name} eq $tag_name) {
7244              !!!cp ('t397');
7245              ## Ignore the token
7246            } else {
7247              !!!cp ('t398');
7248              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7249            }
7250            !!!next-token;
7251            next B;
7252          } elsif ($token->{tag_name} eq 'rt' or
7253                   $token->{tag_name} eq 'rp') {
7254            ## has a |ruby| element in scope
7255            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7256              my $node = $self->{open_elements}->[$_];
7257              if ($node->[1] & RUBY_EL) {
7258                !!!cp ('t398.1');
7259                ## generate implied end tags
7260                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7261                  !!!cp ('t398.2');
7262                  pop @{$self->{open_elements}};
7263                }
7264                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7265                  !!!cp ('t398.3');
7266                  !!!parse-error (type => 'not closed',
7267                                  text => $self->{open_elements}->[-1]->[0]
7268                                      ->manakai_local_name,
7269                                  token => $token);
7270                  pop @{$self->{open_elements}}
7271                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7272                }
7273                last INSCOPE;
7274              } elsif ($node->[1] & SCOPING_EL) {
7275                !!!cp ('t398.4');
7276                last INSCOPE;
7277              }
7278            } # INSCOPE
7279    
7280          !!!parse-error (type => 'after html:#character');          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7281          $self->{insertion_mode} = $previous_insertion_mode;  
7282          ## reprocess          !!!nack ('t398.5');
7283          redo B;          !!!next-token;
       } elsif ($token->{type} eq 'start tag') {  
         !!!parse-error (type => 'after html:'.$token->{tag_name});  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
         redo B;  
       } elsif ($token->{type} eq 'end tag') {  
         !!!parse-error (type => 'after html:/'.$token->{tag_name});  
         $self->{insertion_mode} = $previous_insertion_mode;  
         ## reprocess  
7284          redo B;          redo B;
7285          } elsif ($token->{tag_name} eq 'math' or
7286                   $token->{tag_name} eq 'svg') {
7287            $reconstruct_active_formatting_elements->($insert_to_current);
7288    
7289            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7290    
7291            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7292    
7293            ## "adjust foreign attributes" - done in insert-element-f
7294            
7295            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7296            
7297            if ($self->{self_closing}) {
7298              pop @{$self->{open_elements}};
7299              !!!ack ('t398.1');
7300            } else {
7301              !!!cp ('t398.2');
7302              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7303              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7304              ## mode, "in body" (not "in foreign content") secondary insertion
7305              ## mode, maybe.
7306            }
7307    
7308            !!!next-token;
7309            next B;
7310          } elsif ({
7311                    caption => 1, col => 1, colgroup => 1, frame => 1,
7312                    frameset => 1, head => 1, option => 1, optgroup => 1,
7313                    tbody => 1, td => 1, tfoot => 1, th => 1,
7314                    thead => 1, tr => 1,
7315                   }->{$token->{tag_name}}) {
7316            !!!cp ('t401');
7317            !!!parse-error (type => 'in body',
7318                            text => $token->{tag_name}, token => $token);
7319            ## Ignore the token
7320            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7321            !!!next-token;
7322            next B;
7323            
7324            ## ISSUE: An issue on HTML5 new elements in the spec.
7325        } else {        } else {
7326          die "$0: $token->{type}: Unknown token";          if ($token->{tag_name} eq 'image') {
7327              !!!cp ('t384');
7328              !!!parse-error (type => 'image', token => $token);
7329              $token->{tag_name} = 'img';
7330            } else {
7331              !!!cp ('t385');
7332            }
7333    
7334            ## NOTE: There is an "as if <br>" code clone.
7335            $reconstruct_active_formatting_elements->($insert_to_current);
7336            
7337            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7338    
7339            if ({
7340                 applet => 1, marquee => 1, object => 1,
7341                }->{$token->{tag_name}}) {
7342              !!!cp ('t380');
7343              push @$active_formatting_elements, ['#marker', ''];
7344              !!!nack ('t380.1');
7345            } elsif ({
7346                      b => 1, big => 1, em => 1, font => 1, i => 1,
7347                      s => 1, small => 1, strile => 1,
7348                      strong => 1, tt => 1, u => 1,
7349                     }->{$token->{tag_name}}) {
7350              !!!cp ('t375');
7351              push @$active_formatting_elements, $self->{open_elements}->[-1];
7352              !!!nack ('t375.1');
7353            } elsif ($token->{tag_name} eq 'input') {
7354              !!!cp ('t388');
7355              ## TODO: associate with $self->{form_element} if defined
7356              pop @{$self->{open_elements}};
7357              !!!ack ('t388.2');
7358            } elsif ({
7359                      area => 1, basefont => 1, bgsound => 1, br => 1,
7360                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7361                      #image => 1,
7362                     }->{$token->{tag_name}}) {
7363              !!!cp ('t388.1');
7364              pop @{$self->{open_elements}};
7365              !!!ack ('t388.3');
7366            } elsif ($token->{tag_name} eq 'select') {
7367              ## TODO: associate with $self->{form_element} if defined
7368            
7369              if ($self->{insertion_mode} & TABLE_IMS or
7370                  $self->{insertion_mode} & BODY_TABLE_IMS or
7371                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7372                !!!cp ('t400.1');
7373                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7374              } else {
7375                !!!cp ('t400.2');
7376                $self->{insertion_mode} = IN_SELECT_IM;
7377              }
7378              !!!nack ('t400.3');
7379            } else {
7380              !!!nack ('t402');
7381            }
7382            
7383            !!!next-token;
7384            next B;
7385        }        }
7386      } else {      } elsif ($token->{type} == END_TAG_TOKEN) {
7387        die "$0: $self->{insertion_mode}: Unknown insertion mode";        if ($token->{tag_name} eq 'body') {
7388            ## has a |body| element in scope
7389            my $i;
7390            INSCOPE: {
7391              for (reverse @{$self->{open_elements}}) {
7392                if ($_->[1] & BODY_EL) {
7393                  !!!cp ('t405');
7394                  $i = $_;
7395                  last INSCOPE;
7396                } elsif ($_->[1] & SCOPING_EL) {
7397                  !!!cp ('t405.1');
7398                  last;
7399                }
7400              }
7401    
7402              !!!parse-error (type => 'start tag not allowed',
7403                              text => $token->{tag_name}, token => $token);
7404              ## NOTE: Ignore the token.
7405              !!!next-token;
7406              next B;
7407            } # INSCOPE
7408    
7409            for (@{$self->{open_elements}}) {
7410              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7411                !!!cp ('t403');
7412                !!!parse-error (type => 'not closed',
7413                                text => $_->[0]->manakai_local_name,
7414                                token => $token);
7415                last;
7416              } else {
7417                !!!cp ('t404');
7418              }
7419            }
7420    
7421            $self->{insertion_mode} = AFTER_BODY_IM;
7422            !!!next-token;
7423            next B;
7424          } elsif ($token->{tag_name} eq 'html') {
7425            ## TODO: Update this code.  It seems that the code below is not
7426            ## up-to-date, though it has same effect as speced.
7427            if (@{$self->{open_elements}} > 1 and
7428                $self->{open_elements}->[1]->[1] & BODY_EL) {
7429              ## ISSUE: There is an issue in the spec.
7430              unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7431                !!!cp ('t406');
7432                !!!parse-error (type => 'not closed',
7433                                text => $self->{open_elements}->[1]->[0]
7434                                    ->manakai_local_name,
7435                                token => $token);
7436              } else {
7437                !!!cp ('t407');
7438              }
7439              $self->{insertion_mode} = AFTER_BODY_IM;
7440              ## reprocess
7441              next B;
7442            } else {
7443              !!!cp ('t408');
7444              !!!parse-error (type => 'unmatched end tag',
7445                              text => $token->{tag_name}, token => $token);
7446              ## Ignore the token
7447              !!!next-token;
7448              next B;
7449            }
7450          } elsif ({
7451                    address => 1, blockquote => 1, center => 1, dir => 1,
7452                    div => 1, dl => 1, fieldset => 1, listing => 1,
7453                    menu => 1, ol => 1, pre => 1, ul => 1,
7454                    dd => 1, dt => 1, li => 1,
7455                    applet => 1, button => 1, marquee => 1, object => 1,
7456                   }->{$token->{tag_name}}) {
7457            ## has an element in scope
7458            my $i;
7459            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7460              my $node = $self->{open_elements}->[$_];
7461              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7462                !!!cp ('t410');
7463                $i = $_;
7464                last INSCOPE;
7465              } elsif ($node->[1] & SCOPING_EL) {
7466                !!!cp ('t411');
7467                last INSCOPE;
7468              }
7469            } # INSCOPE
7470    
7471            unless (defined $i) { # has an element in scope
7472              !!!cp ('t413');
7473              !!!parse-error (type => 'unmatched end tag',
7474                              text => $token->{tag_name}, token => $token);
7475              ## NOTE: Ignore the token.
7476            } else {
7477              ## Step 1. generate implied end tags
7478              while ({
7479                      ## END_TAG_OPTIONAL_EL
7480                      dd => ($token->{tag_name} ne 'dd'),
7481                      dt => ($token->{tag_name} ne 'dt'),
7482                      li => ($token->{tag_name} ne 'li'),
7483                      p => 1,
7484                      rt => 1,
7485                      rp => 1,
7486                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7487                !!!cp ('t409');
7488                pop @{$self->{open_elements}};
7489              }
7490    
7491              ## Step 2.
7492              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7493                      ne $token->{tag_name}) {
7494                !!!cp ('t412');
7495                !!!parse-error (type => 'not closed',
7496                                text => $self->{open_elements}->[-1]->[0]
7497                                    ->manakai_local_name,
7498                                token => $token);
7499              } else {
7500                !!!cp ('t414');
7501              }
7502    
7503              ## Step 3.
7504              splice @{$self->{open_elements}}, $i;
7505    
7506              ## Step 4.
7507              $clear_up_to_marker->()
7508                  if {
7509                    applet => 1, button => 1, marquee => 1, object => 1,
7510                  }->{$token->{tag_name}};
7511            }
7512            !!!next-token;
7513            next B;
7514          } elsif ($token->{tag_name} eq 'form') {
7515            undef $self->{form_element};
7516    
7517            ## has an element in scope
7518            my $i;
7519            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7520              my $node = $self->{open_elements}->[$_];
7521              if ($node->[1] & FORM_EL) {
7522                !!!cp ('t418');
7523                $i = $_;
7524                last INSCOPE;
7525              } elsif ($node->[1] & SCOPING_EL) {
7526                !!!cp ('t419');
7527                last INSCOPE;
7528              }
7529            } # INSCOPE
7530    
7531            unless (defined $i) { # has an element in scope
7532              !!!cp ('t421');
7533              !!!parse-error (type => 'unmatched end tag',
7534                              text => $token->{tag_name}, token => $token);
7535              ## NOTE: Ignore the token.
7536            } else {
7537              ## Step 1. generate implied end tags
7538              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7539                !!!cp ('t417');
7540                pop @{$self->{open_elements}};
7541              }
7542              
7543              ## Step 2.
7544              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7545                      ne $token->{tag_name}) {
7546                !!!cp ('t417.1');
7547                !!!parse-error (type => 'not closed',
7548                                text => $self->{open_elements}->[-1]->[0]
7549                                    ->manakai_local_name,
7550                                token => $token);
7551              } else {
7552                !!!cp ('t420');
7553              }  
7554              
7555              ## Step 3.
7556              splice @{$self->{open_elements}}, $i;
7557            }
7558    
7559            !!!next-token;
7560            next B;
7561          } elsif ({
7562                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7563                   }->{$token->{tag_name}}) {
7564            ## has an element in scope
7565            my $i;
7566            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7567              my $node = $self->{open_elements}->[$_];
7568              if ($node->[1] & HEADING_EL) {
7569                !!!cp ('t423');
7570                $i = $_;
7571                last INSCOPE;
7572              } elsif ($node->[1] & SCOPING_EL) {
7573                !!!cp ('t424');
7574                last INSCOPE;
7575              }
7576            } # INSCOPE
7577    
7578            unless (defined $i) { # has an element in scope
7579              !!!cp ('t425.1');
7580              !!!parse-error (type => 'unmatched end tag',
7581                              text => $token->{tag_name}, token => $token);
7582              ## NOTE: Ignore the token.
7583            } else {
7584              ## Step 1. generate implied end tags
7585              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7586                !!!cp ('t422');
7587                pop @{$self->{open_elements}};
7588              }
7589              
7590              ## Step 2.
7591              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7592                      ne $token->{tag_name}) {
7593                !!!cp ('t425');
7594                !!!parse-error (type => 'unmatched end tag',
7595                                text => $token->{tag_name}, token => $token);
7596              } else {
7597                !!!cp ('t426');
7598              }
7599    
7600              ## Step 3.
7601              splice @{$self->{open_elements}}, $i;
7602            }
7603            
7604            !!!next-token;
7605            next B;
7606          } elsif ($token->{tag_name} eq 'p') {
7607            ## has an element in scope
7608            my $i;
7609            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7610              my $node = $self->{open_elements}->[$_];
7611              if ($node->[1] & P_EL) {
7612                !!!cp ('t410.1');
7613                $i = $_;
7614                last INSCOPE;
7615              } elsif ($node->[1] & SCOPING_EL) {
7616                !!!cp ('t411.1');
7617                last INSCOPE;
7618              }
7619            } # INSCOPE
7620    
7621            if (defined $i) {
7622              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7623                      ne $token->{tag_name}) {
7624                !!!cp ('t412.1');
7625                !!!parse-error (type => 'not closed',
7626                                text => $self->{open_elements}->[-1]->[0]
7627                                    ->manakai_local_name,
7628                                token => $token);
7629              } else {
7630                !!!cp ('t414.1');
7631              }
7632    
7633              splice @{$self->{open_elements}}, $i;
7634            } else {
7635              !!!cp ('t413.1');
7636              !!!parse-error (type => 'unmatched end tag',
7637                              text => $token->{tag_name}, token => $token);
7638    
7639              !!!cp ('t415.1');
7640              ## As if <p>, then reprocess the current token
7641              my $el;
7642              !!!create-element ($el, $HTML_NS, 'p',, $token);
7643              $insert->($el);
7644              ## NOTE: Not inserted into |$self->{open_elements}|.
7645            }
7646    
7647            !!!next-token;
7648            next B;
7649          } elsif ({
7650                    a => 1,
7651                    b => 1, big => 1, em => 1, font => 1, i => 1,
7652                    nobr => 1, s => 1, small => 1, strile => 1,
7653                    strong => 1, tt => 1, u => 1,
7654                   }->{$token->{tag_name}}) {
7655            !!!cp ('t427');
7656            $formatting_end_tag->($token);
7657            next B;
7658          } elsif ($token->{tag_name} eq 'br') {
7659            !!!cp ('t428');
7660            !!!parse-error (type => 'unmatched end tag',
7661                            text => 'br', token => $token);
7662    
7663            ## As if <br>
7664            $reconstruct_active_formatting_elements->($insert_to_current);
7665            
7666            my $el;
7667            !!!create-element ($el, $HTML_NS, 'br',, $token);
7668            $insert->($el);
7669            
7670            ## Ignore the token.
7671            !!!next-token;
7672            next B;
7673          } elsif ({
7674                    caption => 1, col => 1, colgroup => 1, frame => 1,
7675                    frameset => 1, head => 1, option => 1, optgroup => 1,
7676                    tbody => 1, td => 1, tfoot => 1, th => 1,
7677                    thead => 1, tr => 1,
7678                    area => 1, basefont => 1, bgsound => 1,
7679                    embed => 1, hr => 1, iframe => 1, image => 1,
7680                    img => 1, input => 1, isindex => 1, noembed => 1,
7681                    noframes => 1, param => 1, select => 1, spacer => 1,
7682                    table => 1, textarea => 1, wbr => 1,
7683                    noscript => 0, ## TODO: if scripting is enabled
7684                   }->{$token->{tag_name}}) {
7685            !!!cp ('t429');
7686            !!!parse-error (type => 'unmatched end tag',
7687                            text => $token->{tag_name}, token => $token);
7688            ## Ignore the token
7689            !!!next-token;
7690            next B;
7691            
7692            ## ISSUE: Issue on HTML5 new elements in spec
7693            
7694          } else {
7695            ## Step 1
7696            my $node_i = -1;
7697            my $node = $self->{open_elements}->[$node_i];
7698    
7699            ## Step 2
7700            S2: {
7701              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7702                ## Step 1
7703                ## generate implied end tags
7704                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7705                  !!!cp ('t430');
7706                  ## NOTE: |<ruby><rt></ruby>|.
7707                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7708                  ## which seems wrong.
7709                  pop @{$self->{open_elements}};
7710                  $node_i++;
7711                }
7712            
7713                ## Step 2
7714                if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7715                        ne $token->{tag_name}) {
7716                  !!!cp ('t431');
7717                  ## NOTE: <x><y></x>
7718                  !!!parse-error (type => 'not closed',
7719                                  text => $self->{open_elements}->[-1]->[0]
7720                                      ->manakai_local_name,
7721                                  token => $token);
7722                } else {
7723                  !!!cp ('t432');
7724                }
7725                
7726                ## Step 3
7727                splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7728    
7729                !!!next-token;
7730                last S2;
7731              } else {
7732                ## Step 3
7733                if (not ($node->[1] & FORMATTING_EL) and
7734                    #not $phrasing_category->{$node->[1]} and
7735                    ($node->[1] & SPECIAL_EL or
7736                     $node->[1] & SCOPING_EL)) {
7737                  !!!cp ('t433');
7738                  !!!parse-error (type => 'unmatched end tag',
7739                                  text => $token->{tag_name}, token => $token);
7740                  ## Ignore the token
7741                  !!!next-token;
7742                  last S2;
7743                }
7744    
7745                !!!cp ('t434');
7746              }
7747              
7748              ## Step 4
7749              $node_i--;
7750              $node = $self->{open_elements}->[$node_i];
7751              
7752              ## Step 5;
7753              redo S2;
7754            } # S2
7755            next B;
7756          }
7757        }
7758        next B;
7759      } continue { # B
7760        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7761          ## NOTE: The code below is executed in cases where it does not have
7762          ## to be, but it it is harmless even in those cases.
7763          ## has an element in scope
7764          INSCOPE: {
7765            for (reverse 0..$#{$self->{open_elements}}) {
7766              my $node = $self->{open_elements}->[$_];
7767              if ($node->[1] & FOREIGN_EL) {
7768                last INSCOPE;
7769              } elsif ($node->[1] & SCOPING_EL) {
7770                last;
7771              }
7772            }
7773            
7774            ## NOTE: No foreign element in scope.
7775            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7776          } # INSCOPE
7777      }      }
7778    } # B    } # B
7779    
# Line 4970  sub _tree_construction_main ($) { Line 7782  sub _tree_construction_main ($) {
7782    ## TODO: script stuffs    ## TODO: script stuffs
7783  } # _tree_construct_main  } # _tree_construct_main
7784    
7785  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7786    my $class = shift;    my $class = shift;
7787    my $node = shift;    my $node = shift;
7788    my $s = \$_[0];    #my $s = \$_[0];
7789    my $onerror = $_[1];    my $onerror = $_[1];
7790      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7791    
7792      ## ISSUE: Should {confident} be true?
7793    
7794    my $nt = $node->node_type;    my $nt = $node->node_type;
7795    if ($nt == 9) {    if ($nt == 9) {
# Line 4991  sub set_inner_html ($$$) { Line 7806  sub set_inner_html ($$$) {
7806      }      }
7807    
7808      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7809      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7810    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7811      ## TODO: If non-html element      ## TODO: If non-html element
7812    
7813      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7814    
7815    ## TODO: Support for $get_wrapper
7816    
7817      ## Step 1 # MUST      ## Step 1 # MUST
7818      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7819      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5004  sub set_inner_html ($$$) { Line 7821  sub set_inner_html ($$$) {
7821      my $p = $class->new;      my $p = $class->new;
7822      $p->{document} = $doc;      $p->{document} = $doc;
7823    
7824      ## Step 9 # MUST      ## Step 8 # MUST
7825      my $i = 0;      my $i = 0;
7826      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7827      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7828      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
7829        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7830        $input = $get_wrapper->($input);
7831        $p->{set_nc} = sub {
7832        my $self = shift;        my $self = shift;
7833    
7834        pop @{$self->{prev_input_character}};        my $char = '';
7835        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        if (defined $self->{next_nc}) {
7836            $char = $self->{next_nc};
7837            delete $self->{next_nc};
7838            $self->{nc} = ord $char;
7839          } else {
7840            $self->{char_buffer} = '';
7841            $self->{char_buffer_pos} = 0;
7842            
7843            my $count = $input->manakai_read_until
7844                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7845                 $self->{char_buffer_pos});
7846            if ($count) {
7847              $self->{line_prev} = $self->{line};
7848              $self->{column_prev} = $self->{column};
7849              $self->{column}++;
7850              $self->{nc}
7851                  = ord substr ($self->{char_buffer},
7852                                $self->{char_buffer_pos}++, 1);
7853              return;
7854            }
7855            
7856            if ($input->read ($char, 1)) {
7857              $self->{nc} = ord $char;
7858            } else {
7859              $self->{nc} = -1;
7860              return;
7861            }
7862          }
7863    
7864          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7865          $p->{column}++;
7866    
7867        $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($self->{nc} == 0x000A) { # LF
7868        $self->{next_input_character} = ord substr $$s, $i++, 1;          $p->{line}++;
7869        $column++;          $p->{column} = 0;
7870            !!!cp ('i1');
7871        if ($self->{next_input_character} == 0x000A) { # LF        } elsif ($self->{nc} == 0x000D) { # CR
7872          $line++;  ## TODO: support for abort/streaming
7873          $column = 0;          my $next = '';
7874        } elsif ($self->{next_input_character} == 0x000D) { # CR          if ($input->read ($next, 1) and $next ne "\x0A") {
7875          $i++ if substr ($$s, $i, 1) eq "\x0A";            $self->{next_nc} = $next;
7876          $self->{next_input_character} = 0x000A; # LF # MUST          }
7877          $line++;          $self->{nc} = 0x000A; # LF # MUST
7878          $column = 0;          $p->{line}++;
7879        } elsif ($self->{next_input_character} > 0x10FFFF) {          $p->{column} = 0;
7880          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          !!!cp ('i2');
7881        } elsif ($self->{next_input_character} == 0x0000) { # NULL        } elsif ($self->{nc} == 0x0000) { # NULL
7882            !!!cp ('i4');
7883          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7884          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7885        }        }
7886      };      };
7887      $p->{prev_input_character} = [-1, -1, -1];  
7888      $p->{next_input_character} = -1;      $p->{read_until} = sub {
7889              #my ($scalar, $specials_range, $offset) = @_;
7890          return 0 if defined $p->{next_nc};
7891    
7892          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7893          my $offset = $_[2] || 0;
7894          
7895          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7896            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7897            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7898              substr ($_[0], $offset)
7899                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7900              my $count = $+[0] - $-[0];
7901              if ($count) {
7902                $p->{column} += $count;
7903                $p->{char_buffer_pos} += $count;
7904                $p->{line_prev} = $p->{line};
7905                $p->{column_prev} = $p->{column} - 1;
7906                $p->{nc} = -1;
7907              }
7908              return $count;
7909            } else {
7910              return 0;
7911            }
7912          } else {
7913            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7914            if ($count) {
7915              $p->{column} += $count;
7916              $p->{column_prev} += $count;
7917              $p->{nc} = -1;
7918            }
7919            return $count;
7920          }
7921        }; # $p->{read_until}
7922    
7923      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7924        my (%opt) = @_;        my (%opt) = @_;
7925        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7926          my $column = $opt{column};
7927          if (defined $opt{token} and defined $opt{token}->{line}) {
7928            $line = $opt{token}->{line};
7929            $column = $opt{token}->{column};
7930          }
7931          warn "Parse error ($opt{type}) at line $line column $column\n";
7932      };      };
7933      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7934        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7935      };      };
7936            
7937        my $char_onerror = sub {
7938          my (undef, $type, %opt) = @_;
7939          $ponerror->(layer => 'encode',
7940                      line => $p->{line}, column => $p->{column} + 1,
7941                      %opt, type => $type);
7942        }; # $char_onerror
7943        $input->onerror ($char_onerror);
7944    
7945      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7946      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7947    
7948      ## Step 2      ## Step 2
7949      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7950      $p->{content_model} = {      $p->{content_model} = {
7951        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7952        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5065  sub set_inner_html ($$$) { Line 7963  sub set_inner_html ($$$) {
7963          unless defined $p->{content_model};          unless defined $p->{content_model};
7964          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7965    
7966      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7967          ## TODO: Foreign element OK?
7968    
7969      ## Step 4      ## Step 3
7970      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7971        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7972    
7973      ## Step 5 # MUST      ## Step 4 # MUST
7974      $doc->append_child ($root);      $doc->append_child ($root);
7975    
7976      ## Step 6 # MUST      ## Step 5 # MUST
7977      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7978    
7979      undef $p->{head_element};      undef $p->{head_element};
7980    
7981      ## Step 7 # MUST      ## Step 6 # MUST
7982      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7983    
7984      ## Step 8 # MUST      ## Step 7 # MUST
7985      my $anode = $node;      my $anode = $node;
7986      AN: while (defined $anode) {      AN: while (defined $anode) {
7987        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7988          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7989          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7990            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7991                !!!cp ('i5');
7992              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7993              last AN;              last AN;
7994            }            }
# Line 5097  sub set_inner_html ($$$) { Line 7997  sub set_inner_html ($$$) {
7997        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7998      } # AN      } # AN
7999            
8000      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
8001      {      {
8002        my $self = $p;        my $self = $p;
8003        !!!next-token;        !!!next-token;
8004      }      }
8005      $p->_tree_construction_main;      $p->_tree_construction_main;
8006    
8007      ## Step 11 # MUST      ## Step 10 # MUST
8008      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
8009      for (@cn) {      for (@cn) {
8010        $node->remove_child ($_);        $node->remove_child ($_);
8011      }      }
8012      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
8013    
8014      ## Step 12 # MUST      ## Step 11 # MUST
8015      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
8016      for (@cn) {      for (@cn) {
8017        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5121  sub set_inner_html ($$$) { Line 8020  sub set_inner_html ($$$) {
8020      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8021    
8022      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8023    
8024        delete $p->{parse_error}; # delete loop
8025    } else {    } else {
8026      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";
8027    }    }
# Line 5128  sub set_inner_html ($$$) { Line 8029  sub set_inner_html ($$$) {
8029    
8030  } # tree construction stage  } # tree construction stage
8031    
8032  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
8033    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  
8034    
8035  1;  1;
8036  # $Date$  # $Date$

Legend:
Removed from v.1.48  
changed lines
  Added in v.1.187

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24