/[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.60 by wakaba, Sun Oct 14 09:21:46 2007 UTC revision 1.182 by wakaba, Mon Sep 15 07:19:03 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  sub parse_string ($$$;$) {      ## Step 1
399    my $self = shift->new;      if (defined $charset_name) {
400    my $s = \$_[0];        $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_char_string ($$$;$$) {
630      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
631      my $self = shift;
632      my $s = ref $_[0] ? $_[0] : \($_[0]);
633      require Whatpm::Charset::DecodeHandle;
634      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
635      return $self->parse_char_stream ($input, @_[1..$#_]);
636    } # parse_char_string
637    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
638    
639    sub parse_char_stream ($$$;$$) {
640      my $self = ref $_[0] ? shift : shift->new;
641      my $input = $_[0];
642    $self->{document} = $_[1];    $self->{document} = $_[1];
643      @{$self->{document}->child_nodes} = ();
644    
645    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
646    
647    my $i = 0;    $self->{confident} = 1 unless exists $self->{confident};
648    my $line = 1;    $self->{document}->input_encoding ($self->{input_encoding})
649    my $column = 0;        if defined $self->{input_encoding};
650    $self->{set_next_input_character} = sub {  ## TODO: |{input_encoding}| is needless?
651    
652      $self->{line_prev} = $self->{line} = 1;
653      $self->{column_prev} = -1;
654      $self->{column} = 0;
655      $self->{set_next_char} = 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_next_char}) {
660          $char = $self->{next_next_char};
661          delete $self->{next_next_char};
662          $self->{next_char} = ord $char;
663        } else {
664          $self->{char_buffer} = '';
665          $self->{char_buffer_pos} = 0;
666    
667          my $count = $input->manakai_read_until
668             ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
669          if ($count) {
670            $self->{line_prev} = $self->{line};
671            $self->{column_prev} = $self->{column};
672            $self->{column}++;
673            $self->{next_char}
674                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
675            return;
676          }
677    
678          if ($input->read ($char, 1)) {
679            $self->{next_char} = ord $char;
680          } else {
681            $self->{next_char} = -1;
682            return;
683          }
684        }
685    
686      $self->{next_input_character} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
687      $self->{next_input_character} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
688      $column++;      $self->{column}++;
689            
690      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 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->{next_char} == 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_next_char} = $next;
700      } elsif ($self->{next_input_character} == 0x0000) { # NULL        }
701          $self->{next_char} = 0x000A; # LF # MUST
702          $self->{line}++;
703          $self->{column} = 0;
704        } elsif ($self->{next_char} == 0x0000) { # NULL
705          !!!cp ('j4');
706        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
707        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 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_next_char};
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->{prev_char} = [-1, -1, -1];
730              $self->{next_char} = -1;
731            }
732            return $count;
733          } else {
734            return 0;
735          }
736        } else {
737          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
738          if ($count) {
739            $self->{column} += $count;
740            $self->{line_prev} = $self->{line};
741            $self->{column_prev} = $self->{column} - 1;
742            $self->{prev_char} = [-1, -1, -1];
743            $self->{next_char} = -1;
744          }
745          return $count;
746        }
747      }; # $self->{read_until}
748    
749    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
750      my (%opt) = @_;      my (%opt) = @_;
751      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
752        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
753        warn "Parse error ($opt{type}) at line $line column $column\n";
754    };    };
755    $self->{parse_error} = sub {    $self->{parse_error} = sub {
756      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
757    };    };
758    
759      my $char_onerror = sub {
760        my (undef, $type, %opt) = @_;
761        !!!parse-error (layer => 'encode',
762                        line => $self->{line}, column => $self->{column} + 1,
763                        %opt, type => $type);
764      }; # $char_onerror
765    
766      if ($_[3]) {
767        $input = $_[3]->($input);
768        $input->onerror ($char_onerror);
769      } else {
770        $input->onerror ($char_onerror) unless defined $input->onerror;
771      }
772    
773    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
774    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
775    $self->_construct_tree;    $self->_construct_tree;
776    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
777    
778      delete $self->{parse_error}; # remove loop
779    
780    return $self->{document};    return $self->{document};
781  } # parse_string  } # parse_char_stream
782    
783  sub new ($) {  sub new ($) {
784    my $class = shift;    my $class = shift;
785    my $self = bless {}, $class;    my $self = bless {
786    $self->{set_next_input_character} = sub {      level => {must => 'm',
787      $self->{next_input_character} = -1;                should => 's',
788                  warn => 'w',
789                  info => 'i',
790                  uncertain => 'u'},
791      }, $class;
792      $self->{set_next_char} = sub {
793        $self->{next_char} = -1;
794    };    };
795    $self->{parse_error} = sub {    $self->{parse_error} = sub {
796      #      #
797    };    };
798      $self->{change_encoding} = sub {
799        # if ($_[0] is a supported encoding) {
800        #   run "change the encoding" algorithm;
801        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
802        # }
803      };
804      $self->{application_cache_selection} = sub {
805        #
806      };
807    return $self;    return $self;
808  } # new  } # new
809    
# Line 160  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 817  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
817  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
818    
819  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
820  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
821  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
822  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
823  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 171  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 828  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
828  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
829  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
830  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
831  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
832  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
833  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
834  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 192  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 849  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
849  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
850  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
851  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
852    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
853    sub SELF_CLOSING_START_TAG_STATE () { 34 }
854    sub CDATA_SECTION_STATE () { 35 }
855    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
856    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
857    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
858    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
859    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
860    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
861    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
862    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
863    ## NOTE: "Entity data state", "entity in attribute value state", and
864    ## "consume a character reference" algorithm are jointly implemented
865    ## using the following six states:
866    sub ENTITY_STATE () { 44 }
867    sub ENTITY_HASH_STATE () { 45 }
868    sub NCR_NUM_STATE () { 46 }
869    sub HEXREF_X_STATE () { 47 }
870    sub HEXREF_HEX_STATE () { 48 }
871    sub ENTITY_NAME_STATE () { 49 }
872    
873  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
874  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 208  sub TABLE_IMS ()      { 0b1000000 } Line 885  sub TABLE_IMS ()      { 0b1000000 }
885  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
886  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
887  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
888    sub SELECT_IMS ()     { 0b10000000000 }
889    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
890        ## NOTE: "in foreign content" insertion mode is special; it is combined
891        ## with the secondary insertion mode.  In this parser, they are stored
892        ## together in the bit-or'ed form.
893    
894    ## NOTE: "initial" and "before html" insertion modes have no constants.
895    
896    ## NOTE: "after after body" insertion mode.
897  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
898    
899    ## NOTE: "after after frameset" insertion mode.
900  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
901    
902  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
903  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
904  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 224  sub IN_TABLE_IM () { TABLE_IMS } Line 912  sub IN_TABLE_IM () { TABLE_IMS }
912  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
913  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
914  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
915  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
916    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
917  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
918    
919  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 232  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 921  sub IN_COLUMN_GROUP_IM () { 0b10 }
921  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
922    my $self = shift;    my $self = shift;
923    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
924      #$self->{state_keyword}; # initialized when used
925      #$self->{entity__value}; # initialized when used
926      #$self->{entity__match}; # initialized when used
927    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
928    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
929    undef $self->{current_attribute};    undef $self->{current_attribute};
930    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
931    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
932    $self->{char} = [];    delete $self->{self_closing};
933    # $self->{next_input_character}    $self->{char_buffer} = '';
934      $self->{char_buffer_pos} = 0;
935      $self->{prev_char} = [-1, -1, -1];
936      $self->{next_char} = -1;
937    !!!next-input-character;    !!!next-input-character;
938    $self->{token} = [];    $self->{token} = [];
939    # $self->{escape}    # $self->{escape}
# Line 251  sub _initialize_tokenizer ($) { Line 946  sub _initialize_tokenizer ($) {
946  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
947  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
948  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
949  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
950  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
951    ##        ->{name}
952    ##        ->{value}
953    ##        ->{has_reference} == 1 or 0
954  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
955    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
956    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
957    ##     while the token is pushed back to the stack.
958    
959  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
960    
# Line 263  sub _initialize_tokenizer ($) { Line 964  sub _initialize_tokenizer ($) {
964  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
965  ## and removed from the list.  ## and removed from the list.
966    
967  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
968  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
 ## contains some requirements that are not detected by the  
 ## parsing algorithm:  
 ## - Some requirements on character encoding declarations. ## TODO  
 ## - "Elements MUST NOT contain content that their content model disallows."  
 ##   ... Some are parse error, some are not (will be reported by c.c.).  
 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO  
 ## - Text (in elements, attributes, and comments) SHOULD NOT contain  
 ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)  
   
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
969    
970  sub _get_next_token ($) {  sub _get_next_token ($) {
971    my $self = shift;    my $self = shift;
972    
973      if ($self->{self_closing}) {
974        !!!parse-error (type => 'nestc', token => $self->{current_token});
975        ## NOTE: The |self_closing| flag is only set by start tag token.
976        ## In addition, when a start tag token is emitted, it is always set to
977        ## |current_token|.
978        delete $self->{self_closing};
979      }
980    
981    if (@{$self->{token}}) {    if (@{$self->{token}}) {
982        $self->{self_closing} = $self->{token}->[0]->{self_closing};
983      return shift @{$self->{token}};      return shift @{$self->{token}};
984    }    }
985    
986    A: {    A: {
987      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
988        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
989          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
990            $self->{state} = ENTITY_DATA_STATE;              not $self->{escape}) {
991              !!!cp (1);
992              ## NOTE: In the spec, the tokenizer is switched to the
993              ## "entity data state".  In this implementation, the tokenizer
994              ## is switched to the |ENTITY_STATE|, which is an implementation
995              ## of the "consume a character reference" algorithm.
996              $self->{entity_additional} = -1;
997              $self->{prev_state} = DATA_STATE;
998              $self->{state} = ENTITY_STATE;
999            !!!next-input-character;            !!!next-input-character;
1000            redo A;            redo A;
1001          } else {          } else {
1002              !!!cp (2);
1003            #            #
1004          }          }
1005        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
1006          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1007            unless ($self->{escape}) {            unless ($self->{escape}) {
1008              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
1009                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
1010                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
1011                  !!!cp (3);
1012                $self->{escape} = 1;                $self->{escape} = 1;
1013                } else {
1014                  !!!cp (4);
1015              }              }
1016              } else {
1017                !!!cp (5);
1018            }            }
1019          }          }
1020                    
1021          #          #
1022        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
1023          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1024              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1025               not $self->{escape})) {               not $self->{escape})) {
1026              !!!cp (6);
1027            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
1028            !!!next-input-character;            !!!next-input-character;
1029            redo A;            redo A;
1030          } else {          } else {
1031              !!!cp (7);
1032            #            #
1033          }          }
1034        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1035          if ($self->{escape} and          if ($self->{escape} and
1036              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1037            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
1038                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
1039                !!!cp (8);
1040              delete $self->{escape};              delete $self->{escape};
1041              } else {
1042                !!!cp (9);
1043            }            }
1044            } else {
1045              !!!cp (10);
1046          }          }
1047                    
1048          #          #
1049        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1050          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
1051            !!!emit ({type => END_OF_FILE_TOKEN,
1052                      line => $self->{line}, column => $self->{column}});
1053          last A; ## TODO: ok?          last A; ## TODO: ok?
1054          } else {
1055            !!!cp (12);
1056        }        }
1057        # Anything else        # Anything else
1058        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1059                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
1060                       line => $self->{line}, column => $self->{column},
1061                      };
1062          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1063    
1064        ## Stay in the data state        ## Stay in the data state
1065        !!!next-input-character;        !!!next-input-character;
1066    
1067        !!!emit ($token);        !!!emit ($token);
1068    
1069        redo A;        redo A;
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!emit ({type => CHARACTER_TOKEN, data => '&'});  
       } else {  
         !!!emit ($token);  
       }  
   
       redo A;  
1070      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1071        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1072          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
1073              !!!cp (15);
1074            !!!next-input-character;            !!!next-input-character;
1075            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1076            redo A;            redo A;
1077          } else {          } else {
1078              !!!cp (16);
1079            ## reconsume            ## reconsume
1080            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1081    
1082            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1083                        line => $self->{line_prev},
1084                        column => $self->{column_prev},
1085                       });
1086    
1087            redo A;            redo A;
1088          }          }
1089        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1090          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1091              !!!cp (17);
1092            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1093            !!!next-input-character;            !!!next-input-character;
1094            redo A;            redo A;
1095          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1096              !!!cp (18);
1097            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1098            !!!next-input-character;            !!!next-input-character;
1099            redo A;            redo A;
1100          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1101                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1102              !!!cp (19);
1103            $self->{current_token}            $self->{current_token}
1104              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1105                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1106                   line => $self->{line_prev},
1107                   column => $self->{column_prev}};
1108            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1109            !!!next-input-character;            !!!next-input-character;
1110            redo A;            redo A;
1111          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1112                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1113              !!!cp (20);
1114            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1115                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
1116                                        line => $self->{line_prev},
1117                                        column => $self->{column_prev}};
1118            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1119            !!!next-input-character;            !!!next-input-character;
1120            redo A;            redo A;
1121          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1122            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1123              !!!parse-error (type => 'empty start tag',
1124                              line => $self->{line_prev},
1125                              column => $self->{column_prev});
1126            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1127            !!!next-input-character;            !!!next-input-character;
1128    
1129            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1130                        line => $self->{line_prev},
1131                        column => $self->{column_prev},
1132                       });
1133    
1134            redo A;            redo A;
1135          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1136            !!!parse-error (type => 'pio');            !!!cp (22);
1137              !!!parse-error (type => 'pio',
1138                              line => $self->{line_prev},
1139                              column => $self->{column_prev});
1140            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1141            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1142                                        line => $self->{line_prev},
1143                                        column => $self->{column_prev},
1144                                       };
1145              ## $self->{next_char} is intentionally left as is
1146            redo A;            redo A;
1147          } else {          } else {
1148            !!!parse-error (type => 'bare stago');            !!!cp (23);
1149              !!!parse-error (type => 'bare stago',
1150                              line => $self->{line_prev},
1151                              column => $self->{column_prev});
1152            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1153            ## reconsume            ## reconsume
1154    
1155            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1156                        line => $self->{line_prev},
1157                        column => $self->{column_prev},
1158                       });
1159    
1160            redo A;            redo A;
1161          }          }
# Line 418  sub _get_next_token ($) { Line 1163  sub _get_next_token ($) {
1163          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1164        }        }
1165      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1166          ## NOTE: The "close tag open state" in the spec is implemented as
1167          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1168    
1169          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1170        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1171          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1172            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1173            my @next_char;            $self->{state_keyword} = '';
1174            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            ## Reconsume.
1175              push @next_char, $self->{next_input_character};            redo A;
             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_STATE;  
   
               !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_input_character};  
         
           unless ($self->{next_input_character} == 0x0009 or # HT  
                   $self->{next_input_character} == 0x000A or # LF  
                   $self->{next_input_character} == 0x000B or # VT  
                   $self->{next_input_character} == 0x000C or # FF  
                   $self->{next_input_character} == 0x0020 or # SP  
                   $self->{next_input_character} == 0x003E or # >  
                   $self->{next_input_character} == 0x002F or # /  
                   $self->{next_input_character} == -1) {  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
             redo A;  
           } else {  
             $self->{next_input_character} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1176          } else {          } else {
1177            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1178            # next-input-character is already done            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1179              !!!cp (28);
1180            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1181            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1182              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1183                        line => $l, column => $c,
1184                       });
1185            redo A;            redo A;
1186          }          }
1187        }        }
1188          
1189        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1190            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1191          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1192                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1193                = {type => END_TAG_TOKEN,
1194                   tag_name => chr ($self->{next_char} + 0x0020),
1195                   line => $l, column => $c};
1196          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1197          !!!next-input-character;          !!!next-input-character;
1198          redo A;          redo A;
1199        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
1200                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1201            !!!cp (30);
1202          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1203                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
1204                                      line => $l, column => $c};
1205          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1206          !!!next-input-character;          !!!next-input-character;
1207          redo A;          redo A;
1208        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1209          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1210            !!!parse-error (type => 'empty end tag',
1211                            line => $self->{line_prev}, ## "<" in "</>"
1212                            column => $self->{column_prev} - 1);
1213          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1214          !!!next-input-character;          !!!next-input-character;
1215          redo A;          redo A;
1216        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1217            !!!cp (32);
1218          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1219          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1220          # reconsume          # reconsume
1221    
1222          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1223                      line => $l, column => $c,
1224                     });
1225    
1226          redo A;          redo A;
1227        } else {        } else {
1228            !!!cp (33);
1229          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1230          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1231          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1232          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1233                                      column => $self->{column_prev} - 1,
1234                                     };
1235            ## NOTE: $self->{next_char} is intentionally left as is.
1236            ## Although the "anything else" case of the spec not explicitly
1237            ## states that the next input character is to be reconsumed,
1238            ## it will be included to the |data| of the comment token
1239            ## generated from the bogus end tag, as defined in the
1240            ## "bogus comment state" entry.
1241            redo A;
1242          }
1243        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1244          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1245          if (length $ch) {
1246            my $CH = $ch;
1247            $ch =~ tr/a-z/A-Z/;
1248            my $nch = chr $self->{next_char};
1249            if ($nch eq $ch or $nch eq $CH) {
1250              !!!cp (24);
1251              ## Stay in the state.
1252              $self->{state_keyword} .= $nch;
1253              !!!next-input-character;
1254              redo A;
1255            } else {
1256              !!!cp (25);
1257              $self->{state} = DATA_STATE;
1258              ## Reconsume.
1259              !!!emit ({type => CHARACTER_TOKEN,
1260                        data => '</' . $self->{state_keyword},
1261                        line => $self->{line_prev},
1262                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1263                       });
1264              redo A;
1265            }
1266          } else { # after "<{tag-name}"
1267            unless ({
1268                     0x0009 => 1, # HT
1269                     0x000A => 1, # LF
1270                     0x000B => 1, # VT
1271                     0x000C => 1, # FF
1272                     0x0020 => 1, # SP
1273                     0x003E => 1, # >
1274                     0x002F => 1, # /
1275                     -1 => 1, # EOF
1276                    }->{$self->{next_char}}) {
1277              !!!cp (26);
1278              ## Reconsume.
1279              $self->{state} = DATA_STATE;
1280              !!!emit ({type => CHARACTER_TOKEN,
1281                        data => '</' . $self->{state_keyword},
1282                        line => $self->{line_prev},
1283                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1284                       });
1285              redo A;
1286            } else {
1287              !!!cp (27);
1288              $self->{current_token}
1289                  = {type => END_TAG_TOKEN,
1290                     tag_name => $self->{last_emitted_start_tag_name},
1291                     line => $self->{line_prev},
1292                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1293              $self->{state} = TAG_NAME_STATE;
1294              ## Reconsume.
1295              redo A;
1296            }
1297        }        }
1298      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1299        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1300            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1301            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1302            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1303            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1304            !!!cp (34);
1305          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1309          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1310            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
1311            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1312          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1313            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1314            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1315              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1316            }            #  !!! cp (36);
1317              #  !!! parse-error (type => 'end tag attribute');
1318              #} else {
1319                !!!cp (37);
1320              #}
1321          } else {          } else {
1322            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1323          }          }
# Line 529  sub _get_next_token ($) { Line 1327  sub _get_next_token ($) {
1327          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1328    
1329          redo A;          redo A;
1330        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1331                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1332          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1333            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1334            # start tag or end tag            # start tag or end tag
1335          ## Stay in this state          ## Stay in this state
1336          !!!next-input-character;          !!!next-input-character;
1337          redo A;          redo A;
1338        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1339          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1340          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1341            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1342            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1343          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1344            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1345            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1346              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1347            }            #  !!! cp (40);
1348              #  !!! parse-error (type => 'end tag attribute');
1349              #} else {
1350                !!!cp (41);
1351              #}
1352          } else {          } else {
1353            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1354          }          }
# Line 556  sub _get_next_token ($) { Line 1358  sub _get_next_token ($) {
1358          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1359    
1360          redo A;          redo A;
1361        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1362            !!!cp (42);
1363            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1364          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1365          redo A;          redo A;
1366        } else {        } else {
1367          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1368            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1369            # start tag or end tag            # start tag or end tag
1370          ## Stay in the state          ## Stay in the state
1371          !!!next-input-character;          !!!next-input-character;
1372          redo A;          redo A;
1373        }        }
1374      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1375        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1376            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1377            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1378            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1379            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1380            !!!cp (45);
1381          ## Stay in the state          ## Stay in the state
1382          !!!next-input-character;          !!!next-input-character;
1383          redo A;          redo A;
1384        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1385          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1386            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1387            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1388          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1389            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1390            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1391                !!!cp (47);
1392              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1393              } else {
1394                !!!cp (48);
1395            }            }
1396          } else {          } else {
1397            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 604  sub _get_next_token ($) { Line 1402  sub _get_next_token ($) {
1402          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1403    
1404          redo A;          redo A;
1405        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1406                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1407          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1408                                value => ''};          $self->{current_attribute}
1409                = {name => chr ($self->{next_char} + 0x0020),
1410                   value => '',
1411                   line => $self->{line}, column => $self->{column}};
1412          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1413          !!!next-input-character;          !!!next-input-character;
1414          redo A;          redo A;
1415        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1416            !!!cp (50);
1417            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1418          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN 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  
1419          redo A;          redo A;
1420        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1421          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1422          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1423            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1424            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1425          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1426            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1427            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1428                !!!cp (53);
1429              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1430              } else {
1431                !!!cp (54);
1432            }            }
1433          } else {          } else {
1434            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 645  sub _get_next_token ($) { Line 1440  sub _get_next_token ($) {
1440    
1441          redo A;          redo A;
1442        } else {        } else {
1443          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1444                                value => ''};               0x0022 => 1, # "
1445                 0x0027 => 1, # '
1446                 0x003D => 1, # =
1447                }->{$self->{next_char}}) {
1448              !!!cp (55);
1449              !!!parse-error (type => 'bad attribute name');
1450            } else {
1451              !!!cp (56);
1452            }
1453            $self->{current_attribute}
1454                = {name => chr ($self->{next_char}),
1455                   value => '',
1456                   line => $self->{line}, column => $self->{column}};
1457          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1458          !!!next-input-character;          !!!next-input-character;
1459          redo A;          redo A;
# Line 655  sub _get_next_token ($) { Line 1462  sub _get_next_token ($) {
1462        my $before_leave = sub {        my $before_leave = sub {
1463          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1464              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1465            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1466              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1467            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1468          } else {          } else {
1469              !!!cp (58);
1470            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1471              = $self->{current_attribute};              = $self->{current_attribute};
1472          }          }
1473        }; # $before_leave        }; # $before_leave
1474    
1475        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1476            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1477            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1478            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1479            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1480            !!!cp (59);
1481          $before_leave->();          $before_leave->();
1482          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1483          !!!next-input-character;          !!!next-input-character;
1484          redo A;          redo A;
1485        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1486            !!!cp (60);
1487          $before_leave->();          $before_leave->();
1488          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1489          !!!next-input-character;          !!!next-input-character;
1490          redo A;          redo A;
1491        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1492          $before_leave->();          $before_leave->();
1493          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1494            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1495            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1496          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1497              !!!cp (62);
1498            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1499            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1500              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 697  sub _get_next_token ($) { Line 1508  sub _get_next_token ($) {
1508          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1509    
1510          redo A;          redo A;
1511        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1512                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1513          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1514            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1515          ## Stay in the state          ## Stay in the state
1516          !!!next-input-character;          !!!next-input-character;
1517          redo A;          redo A;
1518        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1519            !!!cp (64);
1520          $before_leave->();          $before_leave->();
1521            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1522          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           #  
         } else {  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1523          redo A;          redo A;
1524        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1525          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1526          $before_leave->();          $before_leave->();
1527          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1528            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1529            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1530          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1531            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1532            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1533                !!!cp (67);
1534              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1535              } else {
1536                ## NOTE: This state should never be reached.
1537                !!!cp (68);
1538            }            }
1539          } else {          } else {
1540            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 739  sub _get_next_token ($) { Line 1546  sub _get_next_token ($) {
1546    
1547          redo A;          redo A;
1548        } else {        } else {
1549          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1550                $self->{next_char} == 0x0027) { # '
1551              !!!cp (69);
1552              !!!parse-error (type => 'bad attribute name');
1553            } else {
1554              !!!cp (70);
1555            }
1556            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1557          ## Stay in the state          ## Stay in the state
1558          !!!next-input-character;          !!!next-input-character;
1559          redo A;          redo A;
1560        }        }
1561      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1562        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1563            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1564            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1565            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1566            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1567            !!!cp (71);
1568          ## Stay in the state          ## Stay in the state
1569          !!!next-input-character;          !!!next-input-character;
1570          redo A;          redo A;
1571        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1572            !!!cp (72);
1573          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1574          !!!next-input-character;          !!!next-input-character;
1575          redo A;          redo A;
1576        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1577          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1578            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1579            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1580          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1581            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1582            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1583                !!!cp (74);
1584              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1585              } else {
1586                ## NOTE: This state should never be reached.
1587                !!!cp (75);
1588            }            }
1589          } else {          } else {
1590            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 776  sub _get_next_token ($) { Line 1595  sub _get_next_token ($) {
1595          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1596    
1597          redo A;          redo A;
1598        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1599                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1600          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1601                                value => ''};          $self->{current_attribute}
1602                = {name => chr ($self->{next_char} + 0x0020),
1603                   value => '',
1604                   line => $self->{line}, column => $self->{column}};
1605          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1606          !!!next-input-character;          !!!next-input-character;
1607          redo A;          redo A;
1608        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1609            !!!cp (77);
1610            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1611          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_input_character} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN 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_STATE;  
         # next-input-character is already done  
1612          redo A;          redo A;
1613        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1614          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1615          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1616            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1617            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1618          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1619            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1620            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1621                !!!cp (80);
1622              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1623              } else {
1624                ## NOTE: This state should never be reached.
1625                !!!cp (81);
1626            }            }
1627          } else {          } else {
1628            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 818  sub _get_next_token ($) { Line 1634  sub _get_next_token ($) {
1634    
1635          redo A;          redo A;
1636        } else {        } else {
1637          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1638                                value => ''};              $self->{next_char} == 0x0027) { # '
1639              !!!cp (78);
1640              !!!parse-error (type => 'bad attribute name');
1641            } else {
1642              !!!cp (82);
1643            }
1644            $self->{current_attribute}
1645                = {name => chr ($self->{next_char}),
1646                   value => '',
1647                   line => $self->{line}, column => $self->{column}};
1648          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1649          !!!next-input-character;          !!!next-input-character;
1650          redo A;                  redo A;        
1651        }        }
1652      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1653        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1654            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1655            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1656            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1657            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1658            !!!cp (83);
1659          ## Stay in the state          ## Stay in the state
1660          !!!next-input-character;          !!!next-input-character;
1661          redo A;          redo A;
1662        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1663            !!!cp (84);
1664          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1665          !!!next-input-character;          !!!next-input-character;
1666          redo A;          redo A;
1667        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1668            !!!cp (85);
1669          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1670          ## reconsume          ## reconsume
1671          redo A;          redo A;
1672        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1673            !!!cp (86);
1674          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1675          !!!next-input-character;          !!!next-input-character;
1676          redo A;          redo A;
1677        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1678            !!!parse-error (type => 'empty unquoted attribute value');
1679          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1680            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1681            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1682          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1683            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1684            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1685                !!!cp (88);
1686              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1687              } else {
1688                ## NOTE: This state should never be reached.
1689                !!!cp (89);
1690            }            }
1691          } else {          } else {
1692            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 864  sub _get_next_token ($) { Line 1697  sub _get_next_token ($) {
1697          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1698    
1699          redo A;          redo A;
1700        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1701          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1702          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1703            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1704            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1705          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1706            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1707            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1708                !!!cp (91);
1709              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1710              } else {
1711                ## NOTE: This state should never be reached.
1712                !!!cp (92);
1713            }            }
1714          } else {          } else {
1715            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 885  sub _get_next_token ($) { Line 1721  sub _get_next_token ($) {
1721    
1722          redo A;          redo A;
1723        } else {        } else {
1724          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1725              !!!cp (93);
1726              !!!parse-error (type => 'bad attribute value');
1727            } else {
1728              !!!cp (94);
1729            }
1730            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1731          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1732          !!!next-input-character;          !!!next-input-character;
1733          redo A;          redo A;
1734        }        }
1735      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1736        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1737          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1738            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1739            !!!next-input-character;
1740            redo A;
1741          } elsif ($self->{next_char} == 0x0026) { # &
1742            !!!cp (96);
1743            ## NOTE: In the spec, the tokenizer is switched to the
1744            ## "entity in attribute value state".  In this implementation, the
1745            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1746            ## implementation of the "consume a character reference" algorithm.
1747            $self->{prev_state} = $self->{state};
1748            $self->{entity_additional} = 0x0022; # "
1749            $self->{state} = ENTITY_STATE;
1750          !!!next-input-character;          !!!next-input-character;
1751          redo A;          redo A;
1752        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == -1) {
         $self->{last_attribute_value_state} = $self->{state};  
         $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1753          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1754          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1755            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1756            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1757          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1758            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1759            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1760                !!!cp (98);
1761              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1762              } else {
1763                ## NOTE: This state should never be reached.
1764                !!!cp (99);
1765            }            }
1766          } else {          } else {
1767            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 921  sub _get_next_token ($) { Line 1773  sub _get_next_token ($) {
1773    
1774          redo A;          redo A;
1775        } else {        } else {
1776          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1777            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1778            $self->{read_until}->($self->{current_attribute}->{value},
1779                                  q["&],
1780                                  length $self->{current_attribute}->{value});
1781    
1782          ## Stay in the state          ## Stay in the state
1783          !!!next-input-character;          !!!next-input-character;
1784          redo A;          redo A;
1785        }        }
1786      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1787        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1788          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1789            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1790            !!!next-input-character;
1791            redo A;
1792          } elsif ($self->{next_char} == 0x0026) { # &
1793            !!!cp (102);
1794            ## NOTE: In the spec, the tokenizer is switched to the
1795            ## "entity in attribute value state".  In this implementation, the
1796            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1797            ## implementation of the "consume a character reference" algorithm.
1798            $self->{entity_additional} = 0x0027; # '
1799            $self->{prev_state} = $self->{state};
1800            $self->{state} = ENTITY_STATE;
1801          !!!next-input-character;          !!!next-input-character;
1802          redo A;          redo A;
1803        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == -1) {
         $self->{last_attribute_value_state} = $self->{state};  
         $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1804          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1805          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1806            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1807            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1808          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1809            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1810            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1811                !!!cp (104);
1812              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1813              } else {
1814                ## NOTE: This state should never be reached.
1815                !!!cp (105);
1816            }            }
1817          } else {          } else {
1818            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 957  sub _get_next_token ($) { Line 1824  sub _get_next_token ($) {
1824    
1825          redo A;          redo A;
1826        } else {        } else {
1827          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1828            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1829            $self->{read_until}->($self->{current_attribute}->{value},
1830                                  q['&],
1831                                  length $self->{current_attribute}->{value});
1832    
1833          ## Stay in the state          ## Stay in the state
1834          !!!next-input-character;          !!!next-input-character;
1835          redo A;          redo A;
1836        }        }
1837      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1838        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1839            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1840            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1841            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1842            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1843            !!!cp (107);
1844          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1845          !!!next-input-character;          !!!next-input-character;
1846          redo A;          redo A;
1847        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1848          $self->{last_attribute_value_state} = $self->{state};          !!!cp (108);
1849          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## NOTE: In the spec, the tokenizer is switched to the
1850            ## "entity in attribute value state".  In this implementation, the
1851            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1852            ## implementation of the "consume a character reference" algorithm.
1853            $self->{entity_additional} = -1;
1854            $self->{prev_state} = $self->{state};
1855            $self->{state} = ENTITY_STATE;
1856          !!!next-input-character;          !!!next-input-character;
1857          redo A;          redo A;
1858        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1859          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1860            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1861            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1862          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1863            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1864            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1865                !!!cp (110);
1866              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1867              } else {
1868                ## NOTE: This state should never be reached.
1869                !!!cp (111);
1870            }            }
1871          } else {          } else {
1872            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 995  sub _get_next_token ($) { Line 1877  sub _get_next_token ($) {
1877          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1878    
1879          redo A;          redo A;
1880        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1881          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1882          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1883            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1884            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1885          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1886            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1887            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1888                !!!cp (113);
1889              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1890              } else {
1891                ## NOTE: This state should never be reached.
1892                !!!cp (114);
1893            }            }
1894          } else {          } else {
1895            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1016  sub _get_next_token ($) { Line 1901  sub _get_next_token ($) {
1901    
1902          redo A;          redo A;
1903        } else {        } else {
1904          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1905                 0x0022 => 1, # "
1906                 0x0027 => 1, # '
1907                 0x003D => 1, # =
1908                }->{$self->{next_char}}) {
1909              !!!cp (115);
1910              !!!parse-error (type => 'bad attribute value');
1911            } else {
1912              !!!cp (116);
1913            }
1914            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1915            $self->{read_until}->($self->{current_attribute}->{value},
1916                                  q["'=& >],
1917                                  length $self->{current_attribute}->{value});
1918    
1919          ## Stay in the state          ## Stay in the state
1920          !!!next-input-character;          !!!next-input-character;
1921          redo A;          redo A;
1922        }        }
1923      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1924        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        if ($self->{next_char} == 0x0009 or # HT
1925              $self->{next_char} == 0x000A or # LF
1926              $self->{next_char} == 0x000B or # VT
1927              $self->{next_char} == 0x000C or # FF
1928              $self->{next_char} == 0x0020) { # SP
1929            !!!cp (118);
1930            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1931            !!!next-input-character;
1932            redo A;
1933          } elsif ($self->{next_char} == 0x003E) { # >
1934            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1935              !!!cp (119);
1936              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1937            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1938              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1939              if ($self->{current_token}->{attributes}) {
1940                !!!cp (120);
1941                !!!parse-error (type => 'end tag attribute');
1942              } else {
1943                ## NOTE: This state should never be reached.
1944                !!!cp (121);
1945              }
1946            } else {
1947              die "$0: $self->{current_token}->{type}: Unknown token type";
1948            }
1949            $self->{state} = DATA_STATE;
1950            !!!next-input-character;
1951    
1952        unless (defined $token) {          !!!emit ($self->{current_token}); # start tag or end tag
1953          $self->{current_attribute}->{value} .= '&';  
1954            redo A;
1955          } elsif ($self->{next_char} == 0x002F) { # /
1956            !!!cp (122);
1957            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1958            !!!next-input-character;
1959            redo A;
1960          } elsif ($self->{next_char} == -1) {
1961            !!!parse-error (type => 'unclosed tag');
1962            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1963              !!!cp (122.3);
1964              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1965            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1966              if ($self->{current_token}->{attributes}) {
1967                !!!cp (122.1);
1968                !!!parse-error (type => 'end tag attribute');
1969              } else {
1970                ## NOTE: This state should never be reached.
1971                !!!cp (122.2);
1972              }
1973            } else {
1974              die "$0: $self->{current_token}->{type}: Unknown token type";
1975            }
1976            $self->{state} = DATA_STATE;
1977            ## Reconsume.
1978            !!!emit ($self->{current_token}); # start tag or end tag
1979            redo A;
1980        } else {        } else {
1981          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
1982          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
1983            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1984            ## reconsume
1985            redo A;
1986        }        }
1987        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1988          if ($self->{next_char} == 0x003E) { # >
1989            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1990              !!!cp ('124.2');
1991              !!!parse-error (type => 'nestc', token => $self->{current_token});
1992              ## TODO: Different type than slash in start tag
1993              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1994              if ($self->{current_token}->{attributes}) {
1995                !!!cp ('124.4');
1996                !!!parse-error (type => 'end tag attribute');
1997              } else {
1998                !!!cp ('124.5');
1999              }
2000              ## TODO: Test |<title></title/>|
2001            } else {
2002              !!!cp ('124.3');
2003              $self->{self_closing} = 1;
2004            }
2005    
2006        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
2007        # next-input-character is already done          !!!next-input-character;
2008        redo A;  
2009            !!!emit ($self->{current_token}); # start tag or end tag
2010    
2011            redo A;
2012          } elsif ($self->{next_char} == -1) {
2013            !!!parse-error (type => 'unclosed tag');
2014            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
2015              !!!cp (124.7);
2016              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
2017            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
2018              if ($self->{current_token}->{attributes}) {
2019                !!!cp (124.5);
2020                !!!parse-error (type => 'end tag attribute');
2021              } else {
2022                ## NOTE: This state should never be reached.
2023                !!!cp (124.6);
2024              }
2025            } else {
2026              die "$0: $self->{current_token}->{type}: Unknown token type";
2027            }
2028            $self->{state} = DATA_STATE;
2029            ## Reconsume.
2030            !!!emit ($self->{current_token}); # start tag or end tag
2031            redo A;
2032          } else {
2033            !!!cp ('124.4');
2034            !!!parse-error (type => 'nestc');
2035            ## TODO: This error type is wrong.
2036            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2037            ## Reconsume.
2038            redo A;
2039          }
2040      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2041        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
         
       my $token = {type => COMMENT_TOKEN, data => ''};  
   
       BC: {  
         if ($self->{next_input_character} == 0x003E) { # >  
           $self->{state} = DATA_STATE;  
           !!!next-input-character;  
2042    
2043            !!!emit ($token);        ## NOTE: Unlike spec's "bogus comment state", this implementation
2044          ## consumes characters one-by-one basis.
2045          
2046          if ($self->{next_char} == 0x003E) { # >
2047            !!!cp (124);
2048            $self->{state} = DATA_STATE;
2049            !!!next-input-character;
2050    
2051            redo A;          !!!emit ($self->{current_token}); # comment
2052          } elsif ($self->{next_input_character} == -1) {          redo A;
2053            $self->{state} = DATA_STATE;        } elsif ($self->{next_char} == -1) {
2054            ## reconsume          !!!cp (125);
2055            $self->{state} = DATA_STATE;
2056            ## reconsume
2057    
2058            !!!emit ($token);          !!!emit ($self->{current_token}); # comment
2059            redo A;
2060          } else {
2061            !!!cp (126);
2062            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2063            $self->{read_until}->($self->{current_token}->{data},
2064                                  q[>],
2065                                  length $self->{current_token}->{data});
2066    
2067            redo A;          ## Stay in the state.
2068          } else {          !!!next-input-character;
2069            $token->{data} .= chr ($self->{next_input_character});          redo A;
2070            !!!next-input-character;        }
           redo BC;  
         }  
       } # BC  
2071      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2072        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my @next_char;  
       push @next_char, $self->{next_input_character};  
2073                
2074        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2075            !!!cp (133);
2076            $self->{state} = MD_HYPHEN_STATE;
2077          !!!next-input-character;          !!!next-input-character;
2078          push @next_char, $self->{next_input_character};          redo A;
2079          if ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x0044 or # D
2080            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};                 $self->{next_char} == 0x0064) { # d
2081            $self->{state} = COMMENT_START_STATE;          ## ASCII case-insensitive.
2082            !!!next-input-character;          !!!cp (130);
2083            redo A;          $self->{state} = MD_DOCTYPE_STATE;
2084          }          $self->{state_keyword} = chr $self->{next_char};
       } elsif ($self->{next_input_character} == 0x0044 or # D  
                $self->{next_input_character} == 0x0064) { # d  
2085          !!!next-input-character;          !!!next-input-character;
2086          push @next_char, $self->{next_input_character};          redo A;
2087          if ($self->{next_input_character} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2088              $self->{next_input_character} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2089            !!!next-input-character;                 $self->{next_char} == 0x005B) { # [
2090            push @next_char, $self->{next_input_character};          !!!cp (135.4);                
2091            if ($self->{next_input_character} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2092                $self->{next_input_character} == 0x0063) { # c          $self->{state_keyword} = '[';
2093              !!!next-input-character;          !!!next-input-character;
2094              push @next_char, $self->{next_input_character};          redo A;
2095              if ($self->{next_input_character} == 0x0054 or # T        } else {
2096                  $self->{next_input_character} == 0x0074) { # t          !!!cp (136);
               !!!next-input-character;  
               push @next_char, $self->{next_input_character};  
               if ($self->{next_input_character} == 0x0059 or # Y  
                   $self->{next_input_character} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_input_character};  
                 if ($self->{next_input_character} == 0x0050 or # P  
                     $self->{next_input_character} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_input_character};  
                   if ($self->{next_input_character} == 0x0045 or # E  
                       $self->{next_input_character} == 0x0065) { # e  
                     ## ISSUE: What a stupid code this is!  
                     $self->{state} = DOCTYPE_STATE;  
                     !!!next-input-character;  
                     redo A;  
                   }  
                 }  
               }  
             }  
           }  
         }  
2097        }        }
2098    
2099        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2100        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2101        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2102          ## Reconsume.
2103        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2104          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2105                                    line => $self->{line_prev},
2106                                    column => $self->{column_prev} - 1,
2107                                   };
2108        redo A;        redo A;
2109              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2110        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2111        ## 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);
2112            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2113                                      line => $self->{line_prev},
2114                                      column => $self->{column_prev} - 2,
2115                                     };
2116            $self->{state} = COMMENT_START_STATE;
2117            !!!next-input-character;
2118            redo A;
2119          } else {
2120            !!!cp (128);
2121            !!!parse-error (type => 'bogus comment',
2122                            line => $self->{line_prev},
2123                            column => $self->{column_prev} - 2);
2124            $self->{state} = BOGUS_COMMENT_STATE;
2125            ## Reconsume.
2126            $self->{current_token} = {type => COMMENT_TOKEN,
2127                                      data => '-',
2128                                      line => $self->{line_prev},
2129                                      column => $self->{column_prev} - 2,
2130                                     };
2131            redo A;
2132          }
2133        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2134          ## ASCII case-insensitive.
2135          if ($self->{next_char} == [
2136                undef,
2137                0x004F, # O
2138                0x0043, # C
2139                0x0054, # T
2140                0x0059, # Y
2141                0x0050, # P
2142              ]->[length $self->{state_keyword}] or
2143              $self->{next_char} == [
2144                undef,
2145                0x006F, # o
2146                0x0063, # c
2147                0x0074, # t
2148                0x0079, # y
2149                0x0070, # p
2150              ]->[length $self->{state_keyword}]) {
2151            !!!cp (131);
2152            ## Stay in the state.
2153            $self->{state_keyword} .= chr $self->{next_char};
2154            !!!next-input-character;
2155            redo A;
2156          } elsif ((length $self->{state_keyword}) == 6 and
2157                   ($self->{next_char} == 0x0045 or # E
2158                    $self->{next_char} == 0x0065)) { # e
2159            !!!cp (129);
2160            $self->{state} = DOCTYPE_STATE;
2161            $self->{current_token} = {type => DOCTYPE_TOKEN,
2162                                      quirks => 1,
2163                                      line => $self->{line_prev},
2164                                      column => $self->{column_prev} - 7,
2165                                     };
2166            !!!next-input-character;
2167            redo A;
2168          } else {
2169            !!!cp (132);        
2170            !!!parse-error (type => 'bogus comment',
2171                            line => $self->{line_prev},
2172                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2173            $self->{state} = BOGUS_COMMENT_STATE;
2174            ## Reconsume.
2175            $self->{current_token} = {type => COMMENT_TOKEN,
2176                                      data => $self->{state_keyword},
2177                                      line => $self->{line_prev},
2178                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2179                                     };
2180            redo A;
2181          }
2182        } elsif ($self->{state} == MD_CDATA_STATE) {
2183          if ($self->{next_char} == {
2184                '[' => 0x0043, # C
2185                '[C' => 0x0044, # D
2186                '[CD' => 0x0041, # A
2187                '[CDA' => 0x0054, # T
2188                '[CDAT' => 0x0041, # A
2189              }->{$self->{state_keyword}}) {
2190            !!!cp (135.1);
2191            ## Stay in the state.
2192            $self->{state_keyword} .= chr $self->{next_char};
2193            !!!next-input-character;
2194            redo A;
2195          } elsif ($self->{state_keyword} eq '[CDATA' and
2196                   $self->{next_char} == 0x005B) { # [
2197            !!!cp (135.2);
2198            $self->{current_token} = {type => CHARACTER_TOKEN,
2199                                      data => '',
2200                                      line => $self->{line_prev},
2201                                      column => $self->{column_prev} - 7};
2202            $self->{state} = CDATA_SECTION_STATE;
2203            !!!next-input-character;
2204            redo A;
2205          } else {
2206            !!!cp (135.3);
2207            !!!parse-error (type => 'bogus comment',
2208                            line => $self->{line_prev},
2209                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2210            $self->{state} = BOGUS_COMMENT_STATE;
2211            ## Reconsume.
2212            $self->{current_token} = {type => COMMENT_TOKEN,
2213                                      data => $self->{state_keyword},
2214                                      line => $self->{line_prev},
2215                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2216                                     };
2217            redo A;
2218          }
2219      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2220        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2221            !!!cp (137);
2222          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2223          !!!next-input-character;          !!!next-input-character;
2224          redo A;          redo A;
2225        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2226            !!!cp (138);
2227          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2228          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2229          !!!next-input-character;          !!!next-input-character;
# Line 1134  sub _get_next_token ($) { Line 2231  sub _get_next_token ($) {
2231          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2232    
2233          redo A;          redo A;
2234        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2235            !!!cp (139);
2236          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2237          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2238          ## reconsume          ## reconsume
# Line 1143  sub _get_next_token ($) { Line 2241  sub _get_next_token ($) {
2241    
2242          redo A;          redo A;
2243        } else {        } else {
2244            !!!cp (140);
2245          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2246              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2247          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2248          !!!next-input-character;          !!!next-input-character;
2249          redo A;          redo A;
2250        }        }
2251      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2252        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2253            !!!cp (141);
2254          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2255          !!!next-input-character;          !!!next-input-character;
2256          redo A;          redo A;
2257        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2258            !!!cp (142);
2259          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2260          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2261          !!!next-input-character;          !!!next-input-character;
# Line 1162  sub _get_next_token ($) { Line 2263  sub _get_next_token ($) {
2263          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2264    
2265          redo A;          redo A;
2266        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2267            !!!cp (143);
2268          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2269          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2270          ## reconsume          ## reconsume
# Line 1171  sub _get_next_token ($) { Line 2273  sub _get_next_token ($) {
2273    
2274          redo A;          redo A;
2275        } else {        } else {
2276            !!!cp (144);
2277          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2278              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2279          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2280          !!!next-input-character;          !!!next-input-character;
2281          redo A;          redo A;
2282        }        }
2283      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2284        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2285            !!!cp (145);
2286          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2287          !!!next-input-character;          !!!next-input-character;
2288          redo A;          redo A;
2289        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2290            !!!cp (146);
2291          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2292          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2293          ## reconsume          ## reconsume
# Line 1191  sub _get_next_token ($) { Line 2296  sub _get_next_token ($) {
2296    
2297          redo A;          redo A;
2298        } else {        } else {
2299          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2300            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2301            $self->{read_until}->($self->{current_token}->{data},
2302                                  q[-],
2303                                  length $self->{current_token}->{data});
2304    
2305          ## Stay in the state          ## Stay in the state
2306          !!!next-input-character;          !!!next-input-character;
2307          redo A;          redo A;
2308        }        }
2309      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2310        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2311            !!!cp (148);
2312          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2313          !!!next-input-character;          !!!next-input-character;
2314          redo A;          redo A;
2315        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2316            !!!cp (149);
2317          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2318          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2319          ## reconsume          ## reconsume
# Line 1210  sub _get_next_token ($) { Line 2322  sub _get_next_token ($) {
2322    
2323          redo A;          redo A;
2324        } else {        } else {
2325          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2326            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2327          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2328          !!!next-input-character;          !!!next-input-character;
2329          redo A;          redo A;
2330        }        }
2331      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2332        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2333            !!!cp (151);
2334          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2335          !!!next-input-character;          !!!next-input-character;
2336    
2337          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2338    
2339          redo A;          redo A;
2340        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2341          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2342            !!!parse-error (type => 'dash in comment',
2343                            line => $self->{line_prev},
2344                            column => $self->{column_prev});
2345          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2346          ## Stay in the state          ## Stay in the state
2347          !!!next-input-character;          !!!next-input-character;
2348          redo A;          redo A;
2349        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2350            !!!cp (153);
2351          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2352          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2353          ## reconsume          ## reconsume
# Line 1238  sub _get_next_token ($) { Line 2356  sub _get_next_token ($) {
2356    
2357          redo A;          redo A;
2358        } else {        } else {
2359          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2360          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2361                            line => $self->{line_prev},
2362                            column => $self->{column_prev});
2363            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2364          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2365          !!!next-input-character;          !!!next-input-character;
2366          redo A;          redo A;
2367        }        }
2368      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2369        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2370            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2371            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2372            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2373            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2374            !!!cp (155);
2375          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2376          !!!next-input-character;          !!!next-input-character;
2377          redo A;          redo A;
2378        } else {        } else {
2379            !!!cp (156);
2380          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2381          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2382          ## reconsume          ## reconsume
2383          redo A;          redo A;
2384        }        }
2385      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2386        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2387            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2388            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2389            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2390            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2391            !!!cp (157);
2392          ## Stay in the state          ## Stay in the state
2393          !!!next-input-character;          !!!next-input-character;
2394          redo A;          redo A;
2395        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2396            !!!cp (158);
2397          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2398          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2399          !!!next-input-character;          !!!next-input-character;
2400    
2401          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2402    
2403          redo A;          redo A;
2404        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2405            !!!cp (159);
2406          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2407          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2408          ## reconsume          ## reconsume
2409    
2410          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2411    
2412          redo A;          redo A;
2413        } else {        } else {
2414          $self->{current_token}          !!!cp (160);
2415              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
2416                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2417  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2418          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2419          !!!next-input-character;          !!!next-input-character;
# Line 1296  sub _get_next_token ($) { Line 2421  sub _get_next_token ($) {
2421        }        }
2422      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2423  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2424        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2425            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2426            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2427            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2428            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2429            !!!cp (161);
2430          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2431          !!!next-input-character;          !!!next-input-character;
2432          redo A;          redo A;
2433        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2434            !!!cp (162);
2435          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2436          !!!next-input-character;          !!!next-input-character;
2437    
2438          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2439    
2440          redo A;          redo A;
2441        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2442            !!!cp (163);
2443          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2444          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2445          ## reconsume          ## reconsume
2446    
2447          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2448          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2449    
2450          redo A;          redo A;
2451        } else {        } else {
2452            !!!cp (164);
2453          $self->{current_token}->{name}          $self->{current_token}->{name}
2454            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2455          ## Stay in the state          ## Stay in the state
2456          !!!next-input-character;          !!!next-input-character;
2457          redo A;          redo A;
2458        }        }
2459      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2460        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2461            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2462            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2463            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2464            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2465            !!!cp (165);
2466          ## Stay in the state          ## Stay in the state
2467          !!!next-input-character;          !!!next-input-character;
2468          redo A;          redo A;
2469        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2470            !!!cp (166);
2471          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2472          !!!next-input-character;          !!!next-input-character;
2473    
2474          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2475    
2476          redo A;          redo A;
2477        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2478            !!!cp (167);
2479          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2480          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2481          ## reconsume          ## reconsume
2482    
2483          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2484          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2485    
2486          redo A;          redo A;
2487        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2488                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2489            $self->{state} = PUBLIC_STATE;
2490            $self->{state_keyword} = chr $self->{next_char};
2491          !!!next-input-character;          !!!next-input-character;
2492          if ($self->{next_input_character} == 0x0055 or # U          redo A;
2493              $self->{next_input_character} == 0x0075) { # u        } elsif ($self->{next_char} == 0x0053 or # S
2494            !!!next-input-character;                 $self->{next_char} == 0x0073) { # s
2495            if ($self->{next_input_character} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2496                $self->{next_input_character} == 0x0062) { # b          $self->{state_keyword} = chr $self->{next_char};
             !!!next-input-character;  
             if ($self->{next_input_character} == 0x004C or # L  
                 $self->{next_input_character} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_input_character} == 0x0049 or # I  
                   $self->{next_input_character} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_input_character} == 0x0043 or # C  
                     $self->{next_input_character} == 0x0063) { # c  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
       } elsif ($self->{next_input_character} == 0x0053 or # S  
                $self->{next_input_character} == 0x0073) { # s  
2497          !!!next-input-character;          !!!next-input-character;
2498          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_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
2499        } else {        } else {
2500            !!!cp (180);
2501            !!!parse-error (type => 'string after DOCTYPE name');
2502            $self->{current_token}->{quirks} = 1;
2503    
2504            $self->{state} = BOGUS_DOCTYPE_STATE;
2505          !!!next-input-character;          !!!next-input-character;
2506          #          redo A;
2507        }        }
2508        } elsif ($self->{state} == PUBLIC_STATE) {
2509          ## ASCII case-insensitive
2510          if ($self->{next_char} == [
2511                undef,
2512                0x0055, # U
2513                0x0042, # B
2514                0x004C, # L
2515                0x0049, # I
2516              ]->[length $self->{state_keyword}] or
2517              $self->{next_char} == [
2518                undef,
2519                0x0075, # u
2520                0x0062, # b
2521                0x006C, # l
2522                0x0069, # i
2523              ]->[length $self->{state_keyword}]) {
2524            !!!cp (175);
2525            ## Stay in the state.
2526            $self->{state_keyword} .= chr $self->{next_char};
2527            !!!next-input-character;
2528            redo A;
2529          } elsif ((length $self->{state_keyword}) == 5 and
2530                   ($self->{next_char} == 0x0043 or # C
2531                    $self->{next_char} == 0x0063)) { # c
2532            !!!cp (168);
2533            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2534            !!!next-input-character;
2535            redo A;
2536          } else {
2537            !!!cp (169);
2538            !!!parse-error (type => 'string after DOCTYPE name',
2539                            line => $self->{line_prev},
2540                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2541            $self->{current_token}->{quirks} = 1;
2542    
2543        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2544        $self->{state} = BOGUS_DOCTYPE_STATE;          ## Reconsume.
2545        # next-input-character is already done          redo A;
2546        redo A;        }
2547        } elsif ($self->{state} == SYSTEM_STATE) {
2548          ## ASCII case-insensitive
2549          if ($self->{next_char} == [
2550                undef,
2551                0x0059, # Y
2552                0x0053, # S
2553                0x0054, # T
2554                0x0045, # E
2555              ]->[length $self->{state_keyword}] or
2556              $self->{next_char} == [
2557                undef,
2558                0x0079, # y
2559                0x0073, # s
2560                0x0074, # t
2561                0x0065, # e
2562              ]->[length $self->{state_keyword}]) {
2563            !!!cp (170);
2564            ## Stay in the state.
2565            $self->{state_keyword} .= chr $self->{next_char};
2566            !!!next-input-character;
2567            redo A;
2568          } elsif ((length $self->{state_keyword}) == 5 and
2569                   ($self->{next_char} == 0x004D or # M
2570                    $self->{next_char} == 0x006D)) { # m
2571            !!!cp (171);
2572            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2573            !!!next-input-character;
2574            redo A;
2575          } else {
2576            !!!cp (172);
2577            !!!parse-error (type => 'string after DOCTYPE name',
2578                            line => $self->{line_prev},
2579                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2580            $self->{current_token}->{quirks} = 1;
2581    
2582            $self->{state} = BOGUS_DOCTYPE_STATE;
2583            ## Reconsume.
2584            redo A;
2585          }
2586      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2587        if ({        if ({
2588              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2589              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2590            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2591            !!!cp (181);
2592          ## Stay in the state          ## Stay in the state
2593          !!!next-input-character;          !!!next-input-character;
2594          redo A;          redo A;
2595        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2596            !!!cp (182);
2597          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2598          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2599          !!!next-input-character;          !!!next-input-character;
2600          redo A;          redo A;
2601        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2602            !!!cp (183);
2603          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2604          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2605          !!!next-input-character;          !!!next-input-character;
2606          redo A;          redo A;
2607        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2608            !!!cp (184);
2609          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2610    
2611          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2612          !!!next-input-character;          !!!next-input-character;
2613    
2614          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2615          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2616    
2617          redo A;          redo A;
2618        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2619            !!!cp (185);
2620          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2621    
2622          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2623          ## reconsume          ## reconsume
2624    
2625          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2626          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2627    
2628          redo A;          redo A;
2629        } else {        } else {
2630            !!!cp (186);
2631          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2632            $self->{current_token}->{quirks} = 1;
2633    
2634          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2635          !!!next-input-character;          !!!next-input-character;
2636          redo A;          redo A;
2637        }        }
2638      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2639        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2640            !!!cp (187);
2641          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2642          !!!next-input-character;          !!!next-input-character;
2643          redo A;          redo A;
2644        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2645            !!!cp (188);
2646            !!!parse-error (type => 'unclosed PUBLIC literal');
2647    
2648            $self->{state} = DATA_STATE;
2649            !!!next-input-character;
2650    
2651            $self->{current_token}->{quirks} = 1;
2652            !!!emit ($self->{current_token}); # DOCTYPE
2653    
2654            redo A;
2655          } elsif ($self->{next_char} == -1) {
2656            !!!cp (189);
2657          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2658    
2659          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2660          ## reconsume          ## reconsume
2661    
2662          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2663          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2664    
2665          redo A;          redo A;
2666        } else {        } else {
2667            !!!cp (190);
2668          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2669              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2670            $self->{read_until}->($self->{current_token}->{public_identifier},
2671                                  q[">],
2672                                  length $self->{current_token}->{public_identifier});
2673    
2674          ## Stay in the state          ## Stay in the state
2675          !!!next-input-character;          !!!next-input-character;
2676          redo A;          redo A;
2677        }        }
2678      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2679        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2680            !!!cp (191);
2681          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2682          !!!next-input-character;          !!!next-input-character;
2683          redo A;          redo A;
2684        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2685            !!!cp (192);
2686            !!!parse-error (type => 'unclosed PUBLIC literal');
2687    
2688            $self->{state} = DATA_STATE;
2689            !!!next-input-character;
2690    
2691            $self->{current_token}->{quirks} = 1;
2692            !!!emit ($self->{current_token}); # DOCTYPE
2693    
2694            redo A;
2695          } elsif ($self->{next_char} == -1) {
2696            !!!cp (193);
2697          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2698    
2699          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2700          ## reconsume          ## reconsume
2701    
2702          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2703          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2704    
2705          redo A;          redo A;
2706        } else {        } else {
2707            !!!cp (194);
2708          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2709              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2710            $self->{read_until}->($self->{current_token}->{public_identifier},
2711                                  q['>],
2712                                  length $self->{current_token}->{public_identifier});
2713    
2714          ## Stay in the state          ## Stay in the state
2715          !!!next-input-character;          !!!next-input-character;
2716          redo A;          redo A;
# Line 1507  sub _get_next_token ($) { Line 2719  sub _get_next_token ($) {
2719        if ({        if ({
2720              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2721              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2722            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2723            !!!cp (195);
2724          ## Stay in the state          ## Stay in the state
2725          !!!next-input-character;          !!!next-input-character;
2726          redo A;          redo A;
2727        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2728            !!!cp (196);
2729          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2730          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2731          !!!next-input-character;          !!!next-input-character;
2732          redo A;          redo A;
2733        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2734            !!!cp (197);
2735          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2736          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2737          !!!next-input-character;          !!!next-input-character;
2738          redo A;          redo A;
2739        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2740            !!!cp (198);
2741          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2742          !!!next-input-character;          !!!next-input-character;
2743    
2744          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2745    
2746          redo A;          redo A;
2747        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2748            !!!cp (199);
2749          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2750    
2751          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2752          ## reconsume          ## reconsume
2753    
2754          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2755          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2756    
2757          redo A;          redo A;
2758        } else {        } else {
2759            !!!cp (200);
2760          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2761            $self->{current_token}->{quirks} = 1;
2762    
2763          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2764          !!!next-input-character;          !!!next-input-character;
2765          redo A;          redo A;
# Line 1548  sub _get_next_token ($) { Line 2768  sub _get_next_token ($) {
2768        if ({        if ({
2769              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2770              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2771            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2772            !!!cp (201);
2773          ## Stay in the state          ## Stay in the state
2774          !!!next-input-character;          !!!next-input-character;
2775          redo A;          redo A;
2776        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2777            !!!cp (202);
2778          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2779          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2780          !!!next-input-character;          !!!next-input-character;
2781          redo A;          redo A;
2782        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2783            !!!cp (203);
2784          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2785          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2786          !!!next-input-character;          !!!next-input-character;
2787          redo A;          redo A;
2788        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2789            !!!cp (204);
2790          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2791          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2792          !!!next-input-character;          !!!next-input-character;
2793    
2794          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2795          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2796    
2797          redo A;          redo A;
2798        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2799            !!!cp (205);
2800          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2801    
2802          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2803          ## reconsume          ## reconsume
2804    
2805          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2806          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2807    
2808          redo A;          redo A;
2809        } else {        } else {
2810            !!!cp (206);
2811          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2812            $self->{current_token}->{quirks} = 1;
2813    
2814          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2815          !!!next-input-character;          !!!next-input-character;
2816          redo A;          redo A;
2817        }        }
2818      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2819        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2820            !!!cp (207);
2821          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2822          !!!next-input-character;          !!!next-input-character;
2823          redo A;          redo A;
2824        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2825            !!!cp (208);
2826            !!!parse-error (type => 'unclosed SYSTEM literal');
2827    
2828            $self->{state} = DATA_STATE;
2829            !!!next-input-character;
2830    
2831            $self->{current_token}->{quirks} = 1;
2832            !!!emit ($self->{current_token}); # DOCTYPE
2833    
2834            redo A;
2835          } elsif ($self->{next_char} == -1) {
2836            !!!cp (209);
2837          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2838    
2839          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2840          ## reconsume          ## reconsume
2841    
2842          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2843          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2844    
2845          redo A;          redo A;
2846        } else {        } else {
2847            !!!cp (210);
2848          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2849              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2850            $self->{read_until}->($self->{current_token}->{system_identifier},
2851                                  q[">],
2852                                  length $self->{current_token}->{system_identifier});
2853    
2854          ## Stay in the state          ## Stay in the state
2855          !!!next-input-character;          !!!next-input-character;
2856          redo A;          redo A;
2857        }        }
2858      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2859        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2860            !!!cp (211);
2861          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2862          !!!next-input-character;          !!!next-input-character;
2863          redo A;          redo A;
2864        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2865            !!!cp (212);
2866            !!!parse-error (type => 'unclosed SYSTEM literal');
2867    
2868            $self->{state} = DATA_STATE;
2869            !!!next-input-character;
2870    
2871            $self->{current_token}->{quirks} = 1;
2872            !!!emit ($self->{current_token}); # DOCTYPE
2873    
2874            redo A;
2875          } elsif ($self->{next_char} == -1) {
2876            !!!cp (213);
2877          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2878    
2879          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2880          ## reconsume          ## reconsume
2881    
2882          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2883          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2884    
2885          redo A;          redo A;
2886        } else {        } else {
2887            !!!cp (214);
2888          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2889              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2890            $self->{read_until}->($self->{current_token}->{system_identifier},
2891                                  q['>],
2892                                  length $self->{current_token}->{system_identifier});
2893    
2894          ## Stay in the state          ## Stay in the state
2895          !!!next-input-character;          !!!next-input-character;
2896          redo A;          redo A;
# Line 1635  sub _get_next_token ($) { Line 2899  sub _get_next_token ($) {
2899        if ({        if ({
2900              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2901              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2902            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2903            !!!cp (215);
2904          ## Stay in the state          ## Stay in the state
2905          !!!next-input-character;          !!!next-input-character;
2906          redo A;          redo A;
2907        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2908            !!!cp (216);
2909          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2910          !!!next-input-character;          !!!next-input-character;
2911    
2912          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2913    
2914          redo A;          redo A;
2915        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2916            !!!cp (217);
2917          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2918          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2919          ## reconsume          ## reconsume
2920    
2921          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2922          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2923    
2924          redo A;          redo A;
2925        } else {        } else {
2926            !!!cp (218);
2927          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2928            #$self->{current_token}->{quirks} = 1;
2929    
2930          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2931          !!!next-input-character;          !!!next-input-character;
2932          redo A;          redo A;
2933        }        }
2934      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2935        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2936            !!!cp (219);
2937          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2938          !!!next-input-character;          !!!next-input-character;
2939    
         delete $self->{current_token}->{correct};  
2940          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2941    
2942          redo A;          redo A;
2943        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2944            !!!cp (220);
2945          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2946          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2947          ## reconsume          ## reconsume
2948    
         delete $self->{current_token}->{correct};  
2949          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2950    
2951          redo A;          redo A;
2952        } else {        } else {
2953            !!!cp (221);
2954            my $s = '';
2955            $self->{read_until}->($s, q[>], 0);
2956    
2957          ## Stay in the state          ## Stay in the state
2958          !!!next-input-character;          !!!next-input-character;
2959          redo A;          redo A;
2960        }        }
2961      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2962        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2963      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2964    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2965          
2966    die "$0: _get_next_token: unexpected case";        if ($self->{next_char} == 0x005D) { # ]
2967  } # _get_next_token          !!!cp (221.1);
2968            $self->{state} = CDATA_SECTION_MSE1_STATE;
2969            !!!next-input-character;
2970            redo A;
2971          } elsif ($self->{next_char} == -1) {
2972            $self->{state} = DATA_STATE;
2973            !!!next-input-character;
2974            if (length $self->{current_token}->{data}) { # character
2975              !!!cp (221.2);
2976              !!!emit ($self->{current_token}); # character
2977            } else {
2978              !!!cp (221.3);
2979              ## No token to emit. $self->{current_token} is discarded.
2980            }        
2981            redo A;
2982          } else {
2983            !!!cp (221.4);
2984            $self->{current_token}->{data} .= chr $self->{next_char};
2985            $self->{read_until}->($self->{current_token}->{data},
2986                                  q<]>,
2987                                  length $self->{current_token}->{data});
2988    
2989  sub _tokenize_attempt_to_consume_an_entity ($$) {          ## Stay in the state.
2990    my ($self, $in_attr) = @_;          !!!next-input-character;
2991            redo A;
2992          }
2993    
2994    if ({        ## ISSUE: "text tokens" in spec.
2995         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2996         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{next_char} == 0x005D) { # ]
2997        }->{$self->{next_input_character}}) {          !!!cp (221.5);
2998      ## Don't consume          $self->{state} = CDATA_SECTION_MSE2_STATE;
2999      ## No error          !!!next-input-character;
3000      return undef;          redo A;
3001    } elsif ($self->{next_input_character} == 0x0023) { # #        } else {
3002      !!!next-input-character;          !!!cp (221.6);
3003      if ($self->{next_input_character} == 0x0078 or # x          $self->{current_token}->{data} .= ']';
3004          $self->{next_input_character} == 0x0058) { # X          $self->{state} = CDATA_SECTION_STATE;
3005        my $code;          ## Reconsume.
3006        X: {          redo A;
3007          my $x_char = $self->{next_input_character};        }
3008          !!!next-input-character;      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3009          if (0x0030 <= $self->{next_input_character} and        if ($self->{next_char} == 0x003E) { # >
3010              $self->{next_input_character} <= 0x0039) { # 0..9          $self->{state} = DATA_STATE;
3011            $code ||= 0;          !!!next-input-character;
3012            $code *= 0x10;          if (length $self->{current_token}->{data}) { # character
3013            $code += $self->{next_input_character} - 0x0030;            !!!cp (221.7);
3014            redo X;            !!!emit ($self->{current_token}); # 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;  
3015          } else {          } else {
3016            !!!parse-error (type => 'no refc');            !!!cp (221.8);
3017              ## No token to emit. $self->{current_token} is discarded.
3018          }          }
3019            redo A;
3020          } elsif ($self->{next_char} == 0x005D) { # ]
3021            !!!cp (221.9); # character
3022            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
3023            ## Stay in the state.
3024            !!!next-input-character;
3025            redo A;
3026          } else {
3027            !!!cp (221.11);
3028            $self->{current_token}->{data} .= ']]'; # character
3029            $self->{state} = CDATA_SECTION_STATE;
3030            ## Reconsume.
3031            redo A;
3032          }
3033        } elsif ($self->{state} == ENTITY_STATE) {
3034          if ({
3035            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3036            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3037            $self->{entity_additional} => 1,
3038          }->{$self->{next_char}}) {
3039            !!!cp (1001);
3040            ## Don't consume
3041            ## No error
3042            ## Return nothing.
3043            #
3044          } elsif ($self->{next_char} == 0x0023) { # #
3045            !!!cp (999);
3046            $self->{state} = ENTITY_HASH_STATE;
3047            $self->{state_keyword} = '#';
3048            !!!next-input-character;
3049            redo A;
3050          } elsif ((0x0041 <= $self->{next_char} and
3051                    $self->{next_char} <= 0x005A) or # A..Z
3052                   (0x0061 <= $self->{next_char} and
3053                    $self->{next_char} <= 0x007A)) { # a..z
3054            !!!cp (998);
3055            require Whatpm::_NamedEntityList;
3056            $self->{state} = ENTITY_NAME_STATE;
3057            $self->{state_keyword} = chr $self->{next_char};
3058            $self->{entity__value} = $self->{state_keyword};
3059            $self->{entity__match} = 0;
3060            !!!next-input-character;
3061            redo A;
3062          } else {
3063            !!!cp (1027);
3064            !!!parse-error (type => 'bare ero');
3065            ## Return nothing.
3066            #
3067          }
3068    
3069          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3070            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## reference" algorithm.  In other word, there is an "&" character
3071            $code = 0xFFFD;        ## that does not introduce a character reference, which would be
3072          } elsif ($code > 0x10FFFF) {        ## appended to the parent element or the attribute value in later
3073            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        ## process of the tokenizer.
3074            $code = 0xFFFD;  
3075          } elsif ($code == 0x000D) {        if ($self->{prev_state} == DATA_STATE) {
3076            !!!parse-error (type => 'CR character reference');          !!!cp (997);
3077            $code = 0x000A;          $self->{state} = $self->{prev_state};
3078          } elsif (0x80 <= $code and $code <= 0x9F) {          ## Reconsume.
3079            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3080            $code = $c1_entity_char->{$code};                    line => $self->{line_prev},
3081          }                    column => $self->{column_prev},
3082                     });
3083          return {type => CHARACTER_TOKEN, data => chr $code};          redo A;
3084        } # X        } else {
3085      } elsif (0x0030 <= $self->{next_input_character} and          !!!cp (996);
3086               $self->{next_input_character} <= 0x0039) { # 0..9          $self->{current_attribute}->{value} .= '&';
3087        my $code = $self->{next_input_character} - 0x0030;          $self->{state} = $self->{prev_state};
3088        !!!next-input-character;          ## Reconsume.
3089                  redo A;
3090        while (0x0030 <= $self->{next_input_character} and        }
3091                  $self->{next_input_character} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3092          $code *= 10;        if ($self->{next_char} == 0x0078 or # x
3093          $code += $self->{next_input_character} - 0x0030;            $self->{next_char} == 0x0058) { # X
3094            !!!cp (995);
3095            $self->{state} = HEXREF_X_STATE;
3096            $self->{state_keyword} .= chr $self->{next_char};
3097            !!!next-input-character;
3098            redo A;
3099          } elsif (0x0030 <= $self->{next_char} and
3100                   $self->{next_char} <= 0x0039) { # 0..9
3101            !!!cp (994);
3102            $self->{state} = NCR_NUM_STATE;
3103            $self->{state_keyword} = $self->{next_char} - 0x0030;
3104            !!!next-input-character;
3105            redo A;
3106          } else {
3107            !!!parse-error (type => 'bare nero',
3108                            line => $self->{line_prev},
3109                            column => $self->{column_prev} - 1);
3110    
3111            ## NOTE: According to the spec algorithm, nothing is returned,
3112            ## and then "&#" is appended to the parent element or the attribute
3113            ## value in the later processing.
3114    
3115            if ($self->{prev_state} == DATA_STATE) {
3116              !!!cp (1019);
3117              $self->{state} = $self->{prev_state};
3118              ## Reconsume.
3119              !!!emit ({type => CHARACTER_TOKEN,
3120                        data => '&#',
3121                        line => $self->{line_prev},
3122                        column => $self->{column_prev} - 1,
3123                       });
3124              redo A;
3125            } else {
3126              !!!cp (993);
3127              $self->{current_attribute}->{value} .= '&#';
3128              $self->{state} = $self->{prev_state};
3129              ## Reconsume.
3130              redo A;
3131            }
3132          }
3133        } elsif ($self->{state} == NCR_NUM_STATE) {
3134          if (0x0030 <= $self->{next_char} and
3135              $self->{next_char} <= 0x0039) { # 0..9
3136            !!!cp (1012);
3137            $self->{state_keyword} *= 10;
3138            $self->{state_keyword} += $self->{next_char} - 0x0030;
3139                    
3140            ## Stay in the state.
3141            !!!next-input-character;
3142            redo A;
3143          } elsif ($self->{next_char} == 0x003B) { # ;
3144            !!!cp (1013);
3145          !!!next-input-character;          !!!next-input-character;
3146            #
3147          } else {
3148            !!!cp (1014);
3149            !!!parse-error (type => 'no refc');
3150            ## Reconsume.
3151            #
3152        }        }
3153    
3154        if ($self->{next_input_character} == 0x003B) { # ;        my $code = $self->{state_keyword};
3155          my $l = $self->{line_prev};
3156          my $c = $self->{column_prev};
3157          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3158            !!!cp (1015);
3159            !!!parse-error (type => 'invalid character reference',
3160                            text => (sprintf 'U+%04X', $code),
3161                            line => $l, column => $c);
3162            $code = 0xFFFD;
3163          } elsif ($code > 0x10FFFF) {
3164            !!!cp (1016);
3165            !!!parse-error (type => 'invalid character reference',
3166                            text => (sprintf 'U-%08X', $code),
3167                            line => $l, column => $c);
3168            $code = 0xFFFD;
3169          } elsif ($code == 0x000D) {
3170            !!!cp (1017);
3171            !!!parse-error (type => 'CR character reference',
3172                            line => $l, column => $c);
3173            $code = 0x000A;
3174          } elsif (0x80 <= $code and $code <= 0x9F) {
3175            !!!cp (1018);
3176            !!!parse-error (type => 'C1 character reference',
3177                            text => (sprintf 'U+%04X', $code),
3178                            line => $l, column => $c);
3179            $code = $c1_entity_char->{$code};
3180          }
3181    
3182          if ($self->{prev_state} == DATA_STATE) {
3183            !!!cp (992);
3184            $self->{state} = $self->{prev_state};
3185            ## Reconsume.
3186            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3187                      line => $l, column => $c,
3188                     });
3189            redo A;
3190          } else {
3191            !!!cp (991);
3192            $self->{current_attribute}->{value} .= chr $code;
3193            $self->{current_attribute}->{has_reference} = 1;
3194            $self->{state} = $self->{prev_state};
3195            ## Reconsume.
3196            redo A;
3197          }
3198        } elsif ($self->{state} == HEXREF_X_STATE) {
3199          if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3200              (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3201              (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3202            # 0..9, A..F, a..f
3203            !!!cp (990);
3204            $self->{state} = HEXREF_HEX_STATE;
3205            $self->{state_keyword} = 0;
3206            ## Reconsume.
3207            redo A;
3208          } else {
3209            !!!parse-error (type => 'bare hcro',
3210                            line => $self->{line_prev},
3211                            column => $self->{column_prev} - 2);
3212    
3213            ## NOTE: According to the spec algorithm, nothing is returned,
3214            ## and then "&#" followed by "X" or "x" is appended to the parent
3215            ## element or the attribute value in the later processing.
3216    
3217            if ($self->{prev_state} == DATA_STATE) {
3218              !!!cp (1005);
3219              $self->{state} = $self->{prev_state};
3220              ## Reconsume.
3221              !!!emit ({type => CHARACTER_TOKEN,
3222                        data => '&' . $self->{state_keyword},
3223                        line => $self->{line_prev},
3224                        column => $self->{column_prev} - length $self->{state_keyword},
3225                       });
3226              redo A;
3227            } else {
3228              !!!cp (989);
3229              $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3230              $self->{state} = $self->{prev_state};
3231              ## Reconsume.
3232              redo A;
3233            }
3234          }
3235        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3236          if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3237            # 0..9
3238            !!!cp (1002);
3239            $self->{state_keyword} *= 0x10;
3240            $self->{state_keyword} += $self->{next_char} - 0x0030;
3241            ## Stay in the state.
3242            !!!next-input-character;
3243            redo A;
3244          } elsif (0x0061 <= $self->{next_char} and
3245                   $self->{next_char} <= 0x0066) { # a..f
3246            !!!cp (1003);
3247            $self->{state_keyword} *= 0x10;
3248            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3249            ## Stay in the state.
3250            !!!next-input-character;
3251            redo A;
3252          } elsif (0x0041 <= $self->{next_char} and
3253                   $self->{next_char} <= 0x0046) { # A..F
3254            !!!cp (1004);
3255            $self->{state_keyword} *= 0x10;
3256            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3257            ## Stay in the state.
3258            !!!next-input-character;
3259            redo A;
3260          } elsif ($self->{next_char} == 0x003B) { # ;
3261            !!!cp (1006);
3262          !!!next-input-character;          !!!next-input-character;
3263            #
3264        } else {        } else {
3265          !!!parse-error (type => 'no refc');          !!!cp (1007);
3266            !!!parse-error (type => 'no refc',
3267                            line => $self->{line},
3268                            column => $self->{column});
3269            ## Reconsume.
3270            #
3271        }        }
3272    
3273          my $code = $self->{state_keyword};
3274          my $l = $self->{line_prev};
3275          my $c = $self->{column_prev};
3276        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3277          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1008);
3278            !!!parse-error (type => 'invalid character reference',
3279                            text => (sprintf 'U+%04X', $code),
3280                            line => $l, column => $c);
3281          $code = 0xFFFD;          $code = 0xFFFD;
3282        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3283          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1009);
3284            !!!parse-error (type => 'invalid character reference',
3285                            text => (sprintf 'U-%08X', $code),
3286                            line => $l, column => $c);
3287          $code = 0xFFFD;          $code = 0xFFFD;
3288        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3289          !!!parse-error (type => 'CR character reference');          !!!cp (1010);
3290            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3291          $code = 0x000A;          $code = 0x000A;
3292        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3293          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1011);
3294            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3295          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3296        }        }
3297          
3298        return {type => CHARACTER_TOKEN, data => chr $code};        if ($self->{prev_state} == DATA_STATE) {
3299      } else {          !!!cp (988);
3300        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3301        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3302        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3303        return undef;                    line => $l, column => $c,
3304      }                   });
3305    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
3306              $self->{next_input_character} <= 0x005A) or        } else {
3307             (0x0061 <= $self->{next_input_character} and          !!!cp (987);
3308              $self->{next_input_character} <= 0x007A)) {          $self->{current_attribute}->{value} .= chr $code;
3309      my $entity_name = chr $self->{next_input_character};          $self->{current_attribute}->{has_reference} = 1;
3310      !!!next-input-character;          $self->{state} = $self->{prev_state};
3311            ## Reconsume.
3312      my $value = $entity_name;          redo A;
3313      my $match = 0;        }
3314      require Whatpm::_NamedEntityList;      } elsif ($self->{state} == ENTITY_NAME_STATE) {
3315      our $EntityChar;        if (length $self->{state_keyword} < 30 and
3316              ## NOTE: Some number greater than the maximum length of entity name
3317      while (length $entity_name < 10 and            ((0x0041 <= $self->{next_char} and # a
3318             ## NOTE: Some number greater than the maximum length of entity name              $self->{next_char} <= 0x005A) or # x
3319             ((0x0041 <= $self->{next_input_character} and # a             (0x0061 <= $self->{next_char} and # a
3320               $self->{next_input_character} <= 0x005A) or # x              $self->{next_char} <= 0x007A) or # z
3321              (0x0061 <= $self->{next_input_character} and # a             (0x0030 <= $self->{next_char} and # 0
3322               $self->{next_input_character} <= 0x007A) or # z              $self->{next_char} <= 0x0039) or # 9
3323              (0x0030 <= $self->{next_input_character} and # 0             $self->{next_char} == 0x003B)) { # ;
3324               $self->{next_input_character} <= 0x0039) or # 9          our $EntityChar;
3325              $self->{next_input_character} == 0x003B)) { # ;          $self->{state_keyword} .= chr $self->{next_char};
3326        $entity_name .= chr $self->{next_input_character};          if (defined $EntityChar->{$self->{state_keyword}}) {
3327        if (defined $EntityChar->{$entity_name}) {            if ($self->{next_char} == 0x003B) { # ;
3328          if ($self->{next_input_character} == 0x003B) { # ;              !!!cp (1020);
3329            $value = $EntityChar->{$entity_name};              $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3330            $match = 1;              $self->{entity__match} = 1;
3331            !!!next-input-character;              !!!next-input-character;
3332            last;              #
3333              } else {
3334                !!!cp (1021);
3335                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3336                $self->{entity__match} = -1;
3337                ## Stay in the state.
3338                !!!next-input-character;
3339                redo A;
3340              }
3341          } else {          } else {
3342            $value = $EntityChar->{$entity_name};            !!!cp (1022);
3343            $match = -1;            $self->{entity__value} .= chr $self->{next_char};
3344              $self->{entity__match} *= 2;
3345              ## Stay in the state.
3346            !!!next-input-character;            !!!next-input-character;
3347              redo A;
3348          }          }
       } else {  
         $value .= chr $self->{next_input_character};  
         $match *= 2;  
         !!!next-input-character;  
3349        }        }
3350      }  
3351              my $data;
3352      if ($match > 0) {        my $has_ref;
3353        return {type => CHARACTER_TOKEN, data => $value};        if ($self->{entity__match} > 0) {
3354      } elsif ($match < 0) {          !!!cp (1023);
3355        !!!parse-error (type => 'no refc');          $data = $self->{entity__value};
3356        if ($in_attr and $match < -1) {          $has_ref = 1;
3357          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          #
3358          } elsif ($self->{entity__match} < 0) {
3359            !!!parse-error (type => 'no refc');
3360            if ($self->{prev_state} != DATA_STATE and # in attribute
3361                $self->{entity__match} < -1) {
3362              !!!cp (1024);
3363              $data = '&' . $self->{state_keyword};
3364              #
3365            } else {
3366              !!!cp (1025);
3367              $data = $self->{entity__value};
3368              $has_ref = 1;
3369              #
3370            }
3371        } else {        } else {
3372          return {type => CHARACTER_TOKEN, data => $value};          !!!cp (1026);
3373            !!!parse-error (type => 'bare ero',
3374                            line => $self->{line_prev},
3375                            column => $self->{column_prev} - length $self->{state_keyword});
3376            $data = '&' . $self->{state_keyword};
3377            #
3378          }
3379      
3380          ## NOTE: In these cases, when a character reference is found,
3381          ## it is consumed and a character token is returned, or, otherwise,
3382          ## nothing is consumed and returned, according to the spec algorithm.
3383          ## In this implementation, anything that has been examined by the
3384          ## tokenizer is appended to the parent element or the attribute value
3385          ## as string, either literal string when no character reference or
3386          ## entity-replaced string otherwise, in this stage, since any characters
3387          ## that would not be consumed are appended in the data state or in an
3388          ## appropriate attribute value state anyway.
3389    
3390          if ($self->{prev_state} == DATA_STATE) {
3391            !!!cp (986);
3392            $self->{state} = $self->{prev_state};
3393            ## Reconsume.
3394            !!!emit ({type => CHARACTER_TOKEN,
3395                      data => $data,
3396                      line => $self->{line_prev},
3397                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3398                     });
3399            redo A;
3400          } else {
3401            !!!cp (985);
3402            $self->{current_attribute}->{value} .= $data;
3403            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3404            $self->{state} = $self->{prev_state};
3405            ## Reconsume.
3406            redo A;
3407        }        }
3408      } else {      } else {
3409        !!!parse-error (type => 'bare ero');        die "$0: $self->{state}: Unknown state";
       ## NOTE: No characters are consumed in the spec.  
       return {type => CHARACTER_TOKEN, data => '&'.$value};  
3410      }      }
3411    } else {    } # A  
3412      ## no characters are consumed  
3413      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3414      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3415    
3416  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3417    my $self = shift;    my $self = shift;
# Line 1864  sub _initialize_tree_constructor ($) { Line 3420  sub _initialize_tree_constructor ($) {
3420    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3421    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3422    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3423      $self->{document}->set_user_data (manakai_source_line => 1);
3424      $self->{document}->set_user_data (manakai_source_column => 1);
3425  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3426    
3427  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1890  sub _construct_tree ($) { Line 3448  sub _construct_tree ($) {
3448        
3449    !!!next-token;    !!!next-token;
3450    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3451    undef $self->{form_element};    undef $self->{form_element};
3452    undef $self->{head_element};    undef $self->{head_element};
3453    $self->{open_elements} = [];    $self->{open_elements} = [];
3454    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3455    
3456      ## NOTE: The "initial" insertion mode.
3457    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3458    
3459      ## NOTE: The "before html" insertion mode.
3460    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3461      $self->{insertion_mode} = BEFORE_HEAD_IM;
3462    
3463      ## NOTE: The "before head" insertion mode and so on.
3464    $self->_tree_construction_main;    $self->_tree_construction_main;
3465  } # _construct_tree  } # _construct_tree
3466    
3467  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3468    my $self = shift;    my $self = shift;
3469    
3470      ## NOTE: "initial" insertion mode
3471    
3472    INITIAL: {    INITIAL: {
3473      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3474        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 1910  sub _tree_construction_initial ($) { Line 3476  sub _tree_construction_initial ($) {
3476        ## language.        ## language.
3477        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3478        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3479        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3480        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3481            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3482          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3483            !!!parse-error (type => 'not HTML5', token => $token);
3484        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3485          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3486          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3487          } elsif (defined $token->{public_identifier}) {
3488            if ($token->{public_identifier} eq 'XSLT-compat') {
3489              !!!cp ('t1.2');
3490              !!!parse-error (type => 'XSLT-compat', token => $token,
3491                              level => $self->{level}->{should});
3492            } else {
3493              !!!parse-error (type => 'not HTML5', token => $token);
3494            }
3495          } else {
3496            !!!cp ('t3');
3497            #
3498        }        }
3499                
3500        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3501          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3502          ## NOTE: Default value for both |public_id| and |system_id| attributes
3503          ## are empty strings, so that we don't set any value in missing cases.
3504        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3505            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3506        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1930  sub _tree_construction_initial ($) { Line 3509  sub _tree_construction_initial ($) {
3509        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3510        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3511                
3512        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3513            !!!cp ('t4');
3514          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3515        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3516          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3517          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3518          if ({          my $prefix = [
3519            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3520            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3521            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3522            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3523            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3524            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3525            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3526            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3527            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3528            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3529            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3530            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3531            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3532            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3533            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3534            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3535            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3536            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3537            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3538            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3539            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3540            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3541            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3542            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3543            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3544            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3545            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3546            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3547            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3548            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3549            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3550            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3551            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3552            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3553            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3554            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3555            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3556            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3557            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3558            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3559            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3560            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3561            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3562            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3563            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3564            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3565            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3566            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3567            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3568            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3569            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3570            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3571            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3572            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3573            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3574            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3575            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3576            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3577            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3578            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3579            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3580            "-//W3C//DTD W3 HTML//EN" => 1,            }
3581            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3582            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3583            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3584            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3585            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3586            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3587            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3588          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3589                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3590            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3591                !!!cp ('t6');
3592              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3593            } else {            } else {
3594                !!!cp ('t7');
3595              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3596            }            }
3597          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3598                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3599              !!!cp ('t8');
3600            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3601            } else {
3602              !!!cp ('t9');
3603          }          }
3604          } else {
3605            !!!cp ('t10');
3606        }        }
3607        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3608          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3609          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3610          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") {
3611              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3612              ## marked as quirks.
3613            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3614              !!!cp ('t11');
3615            } else {
3616              !!!cp ('t12');
3617          }          }
3618          } else {
3619            !!!cp ('t13');
3620        }        }
3621                
3622        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3623        !!!next-token;        !!!next-token;
3624        return;        return;
3625      } elsif ({      } elsif ({
# Line 2035  sub _tree_construction_initial ($) { Line 3627  sub _tree_construction_initial ($) {
3627                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3628                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3629               }->{$token->{type}}) {               }->{$token->{type}}) {
3630        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3631          !!!parse-error (type => 'no DOCTYPE', token => $token);
3632        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3633        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3634        ## reprocess        ## reprocess
3635          !!!ack-later;
3636        return;        return;
3637      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3638        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3639          ## Ignore the token          ## Ignore the token
3640    
3641          unless (length $token->{data}) {          unless (length $token->{data}) {
3642            ## Stay in the phase            !!!cp ('t15');
3643              ## Stay in the insertion mode.
3644            !!!next-token;            !!!next-token;
3645            redo INITIAL;            redo INITIAL;
3646            } else {
3647              !!!cp ('t16');
3648          }          }
3649          } else {
3650            !!!cp ('t17');
3651        }        }
3652    
3653        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3654        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3655        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3656        ## reprocess        ## reprocess
3657        return;        return;
3658      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3659          !!!cp ('t18');
3660        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3661        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3662                
3663        ## Stay in the phase.        ## Stay in the insertion mode.
3664        !!!next-token;        !!!next-token;
3665        redo INITIAL;        redo INITIAL;
3666      } else {      } else {
3667        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3668      }      }
3669    } # INITIAL    } # INITIAL
3670    
3671      die "$0: _tree_construction_initial: This should be never reached";
3672  } # _tree_construction_initial  } # _tree_construction_initial
3673    
3674  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3675    my $self = shift;    my $self = shift;
3676    
3677      ## NOTE: "before html" insertion mode.
3678        
3679    B: {    B: {
3680        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3681          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3682            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3683          ## Ignore the token          ## Ignore the token
3684          ## Stay in the phase          ## Stay in the insertion mode.
3685          !!!next-token;          !!!next-token;
3686          redo B;          redo B;
3687        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3688            !!!cp ('t20');
3689          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3690          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3691          ## Stay in the phase          ## Stay in the insertion mode.
3692          !!!next-token;          !!!next-token;
3693          redo B;          redo B;
3694        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2090  sub _tree_construction_root_element ($) Line 3696  sub _tree_construction_root_element ($)
3696            ## Ignore the token.            ## Ignore the token.
3697    
3698            unless (length $token->{data}) {            unless (length $token->{data}) {
3699              ## Stay in the phase              !!!cp ('t21');
3700                ## Stay in the insertion mode.
3701              !!!next-token;              !!!next-token;
3702              redo B;              redo B;
3703              } else {
3704                !!!cp ('t22');
3705            }            }
3706            } else {
3707              !!!cp ('t23');
3708          }          }
3709    
3710            $self->{application_cache_selection}->(undef);
3711    
3712          #          #
3713          } elsif ($token->{type} == START_TAG_TOKEN) {
3714            if ($token->{tag_name} eq 'html') {
3715              my $root_element;
3716              !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3717              $self->{document}->append_child ($root_element);
3718              push @{$self->{open_elements}},
3719                  [$root_element, $el_category->{html}];
3720    
3721              if ($token->{attributes}->{manifest}) {
3722                !!!cp ('t24');
3723                $self->{application_cache_selection}
3724                    ->($token->{attributes}->{manifest}->{value});
3725                ## ISSUE: Spec is unclear on relative references.
3726                ## According to Hixie (#whatwg 2008-03-19), it should be
3727                ## resolved against the base URI of the document in HTML
3728                ## or xml:base of the element in XHTML.
3729              } else {
3730                !!!cp ('t25');
3731                $self->{application_cache_selection}->(undef);
3732              }
3733    
3734              !!!nack ('t25c');
3735    
3736              !!!next-token;
3737              return; ## Go to the "before head" insertion mode.
3738            } else {
3739              !!!cp ('t25.1');
3740              #
3741            }
3742        } elsif ({        } elsif ({
                 START_TAG_TOKEN, 1,  
3743                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3744                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3745                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3746          ## ISSUE: There is an issue in the spec          !!!cp ('t26');
3747          #          #
3748        } else {        } else {
3749          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3750        }        }
3751        ## TODO: application cache selection algorithm  
3752        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3753        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3754        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3755        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3756        #redo B;  
3757        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3758    
3759        ## NOTE: Reprocess the token.
3760        !!!ack-later;
3761        return; ## Go to the "before head" insertion mode.
3762    
3763        ## ISSUE: There is an issue in the spec
3764    } # B    } # B
3765    
3766      die "$0: _tree_construction_root_element: This should never be reached";
3767  } # _tree_construction_root_element  } # _tree_construction_root_element
3768    
3769  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2128  sub _reset_insertion_mode ($) { Line 3778  sub _reset_insertion_mode ($) {
3778            
3779      ## Step 3      ## Step 3
3780      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"!?  
3781        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3782          $last = 1;          $last = 1;
3783          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3784            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3785                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3786              #          } else {
3787            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3788          }          }
3789        }        }
3790              
3791        ## Step 4..13        ## Step 4..14
3792        my $new_mode = {        my $new_mode;
3793          if ($node->[1] & FOREIGN_EL) {
3794            !!!cp ('t28.1');
3795            ## NOTE: Strictly spaking, the line below only applies to MathML and
3796            ## SVG elements.  Currently the HTML syntax supports only MathML and
3797            ## SVG elements as foreigners.
3798            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3799          } elsif ($node->[1] & TABLE_CELL_EL) {
3800            if ($last) {
3801              !!!cp ('t28.2');
3802              #
3803            } else {
3804              !!!cp ('t28.3');
3805              $new_mode = IN_CELL_IM;
3806            }
3807          } else {
3808            !!!cp ('t28.4');
3809            $new_mode = {
3810                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3811                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3812                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3813                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3814                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3815                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2160  sub _reset_insertion_mode ($) { Line 3820  sub _reset_insertion_mode ($) {
3820                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3821                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3822                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3823                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3824          }
3825        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3826                
3827        ## Step 14        ## Step 15
3828        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3829          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3830              !!!cp ('t29');
3831            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3832          } else {          } else {
3833              ## ISSUE: Can this state be reached?
3834              !!!cp ('t30');
3835            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3836          }          }
3837          return;          return;
3838          } else {
3839            !!!cp ('t31');
3840        }        }
3841                
3842        ## Step 15        ## Step 16
3843        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3844                
3845        ## Step 16        ## Step 17
3846        $i--;        $i--;
3847        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3848                
3849        ## Step 17        ## Step 18
3850        redo S3;        redo S3;
3851      } # S3      } # S3
3852    
3853      die "$0: _reset_insertion_mode: This line should never be reached";
3854  } # _reset_insertion_mode  } # _reset_insertion_mode
3855    
3856  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2204  sub _tree_construction_main ($) { Line 3872  sub _tree_construction_main ($) {
3872      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3873      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3874        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3875            !!!cp ('t32');
3876          return;          return;
3877        }        }
3878      }      }
# Line 2218  sub _tree_construction_main ($) { Line 3887  sub _tree_construction_main ($) {
3887    
3888        ## Step 6        ## Step 6
3889        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3890            !!!cp ('t33_1');
3891          #          #
3892        } else {        } else {
3893          my $in_open_elements;          my $in_open_elements;
3894          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3895            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3896                !!!cp ('t33');
3897              $in_open_elements = 1;              $in_open_elements = 1;
3898              last OE;              last OE;
3899            }            }
3900          }          }
3901          if ($in_open_elements) {          if ($in_open_elements) {
3902              !!!cp ('t34');
3903            #            #
3904          } else {          } else {
3905              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3906              !!!cp ('t35');
3907            redo S4;            redo S4;
3908          }          }
3909        }        }
# Line 2252  sub _tree_construction_main ($) { Line 3926  sub _tree_construction_main ($) {
3926    
3927        ## Step 11        ## Step 11
3928        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3929            !!!cp ('t36');
3930          ## Step 7'          ## Step 7'
3931          $i++;          $i++;
3932          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3933                    
3934          redo S7;          redo S7;
3935        }        }
3936    
3937          !!!cp ('t37');
3938      } # S7      } # S7
3939    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3940    
3941    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3942      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3943        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3944            !!!cp ('t38');
3945          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3946          return;          return;
3947        }        }
3948      }      }
3949    
3950        !!!cp ('t39');
3951    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3952    
3953    my $parse_rcdata = sub ($$) {    my $insert;
3954      my ($content_model_flag, $insert) = @_;  
3955      my $parse_rcdata = sub ($) {
3956        my ($content_model_flag) = @_;
3957    
3958      ## Step 1      ## Step 1
3959      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3960      my $el;      my $el;
3961      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3962    
3963      ## Step 2      ## Step 2
3964      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3965    
3966      ## Step 3      ## Step 3
3967      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2287  sub _tree_construction_main ($) { Line 3969  sub _tree_construction_main ($) {
3969    
3970      ## Step 4      ## Step 4
3971      my $text = '';      my $text = '';
3972        !!!nack ('t40.1');
3973      !!!next-token;      !!!next-token;
3974      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3975          !!!cp ('t40');
3976        $text .= $token->{data};        $text .= $token->{data};
3977        !!!next-token;        !!!next-token;
3978      }      }
3979    
3980      ## Step 5      ## Step 5
3981      if (length $text) {      if (length $text) {
3982          !!!cp ('t41');
3983        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3984        $el->append_child ($text);        $el->append_child ($text);
3985      }      }
# Line 2303  sub _tree_construction_main ($) { Line 3988  sub _tree_construction_main ($) {
3988      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3989    
3990      ## Step 7      ## Step 7
3991      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3992            $token->{tag_name} eq $start_tag_name) {
3993          !!!cp ('t42');
3994        ## 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});  
3995      } else {      } else {
3996        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3997          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3998            !!!cp ('t43');
3999            !!!parse-error (type => 'in CDATA:#eof', token => $token);
4000          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4001            !!!cp ('t44');
4002            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4003          } else {
4004            die "$0: $content_model_flag in parse_rcdata";
4005          }
4006      }      }
4007      !!!next-token;      !!!next-token;
4008    }; # $parse_rcdata    }; # $parse_rcdata
4009    
4010    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
4011      my $script_el;      my $script_el;
4012      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4013      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4014    
4015      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
4016      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4017            
4018      my $text = '';      my $text = '';
4019        !!!nack ('t45.1');
4020      !!!next-token;      !!!next-token;
4021      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
4022          !!!cp ('t45');
4023        $text .= $token->{data};        $text .= $token->{data};
4024        !!!next-token;        !!!next-token;
4025      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
4026      if (length $text) {      if (length $text) {
4027          !!!cp ('t46');
4028        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
4029      }      }
4030                                
# Line 2338  sub _tree_construction_main ($) { Line 4032  sub _tree_construction_main ($) {
4032    
4033      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
4034          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4035          !!!cp ('t47');
4036        ## Ignore the token        ## Ignore the token
4037      } else {      } else {
4038        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4039          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4040        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4041        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4042      }      }
4043            
4044      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4045          !!!cp ('t49');
4046        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4047      } else {      } else {
4048          !!!cp ('t50');
4049        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4050        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4051    
# Line 2361  sub _tree_construction_main ($) { Line 4059  sub _tree_construction_main ($) {
4059      !!!next-token;      !!!next-token;
4060    }; # $script_start_tag    }; # $script_start_tag
4061    
4062      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4063      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4064      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4065    
4066    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4067      my $tag_name = shift;      my $end_tag_token = shift;
4068        my $tag_name = $end_tag_token->{tag_name};
4069    
4070        ## NOTE: The adoption agency algorithm (AAA).
4071    
4072      FET: {      FET: {
4073        ## Step 1        ## Step 1
4074        my $formatting_element;        my $formatting_element;
4075        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4076        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4077          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4078              !!!cp ('t52');
4079              last AFE;
4080            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4081                         eq $tag_name) {
4082              !!!cp ('t51');
4083            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4084            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4085            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4086          }          }
4087        } # AFE        } # AFE
4088        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4089          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4090            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4091          ## Ignore the token          ## Ignore the token
4092          !!!next-token;          !!!next-token;
4093          return;          return;
# Line 2390  sub _tree_construction_main ($) { Line 4099  sub _tree_construction_main ($) {
4099          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4100          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4101            if ($in_scope) {            if ($in_scope) {
4102                !!!cp ('t54');
4103              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4104              last INSCOPE;              last INSCOPE;
4105            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4106              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4107                !!!parse-error (type => 'unmatched end tag',
4108                                text => $token->{tag_name},
4109                                token => $end_tag_token);
4110              ## Ignore the token              ## Ignore the token
4111              !!!next-token;              !!!next-token;
4112              return;              return;
4113            }            }
4114          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4115                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4116            $in_scope = 0;            $in_scope = 0;
4117          }          }
4118        } # INSCOPE        } # INSCOPE
4119        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4120          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4121            !!!parse-error (type => 'unmatched end tag',
4122                            text => $token->{tag_name},
4123                            token => $end_tag_token);
4124          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4125          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4126          return;          return;
4127        }        }
4128        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4129          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4130            !!!parse-error (type => 'not closed',
4131                            text => $self->{open_elements}->[-1]->[0]
4132                                ->manakai_local_name,
4133                            token => $end_tag_token);
4134        }        }
4135                
4136        ## Step 2        ## Step 2
# Line 2420  sub _tree_construction_main ($) { Line 4138  sub _tree_construction_main ($) {
4138        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4139        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4140          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4141          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4142              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4143              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4144               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4145              !!!cp ('t59');
4146            $furthest_block = $node;            $furthest_block = $node;
4147            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4148          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4149              !!!cp ('t60');
4150            last OE;            last OE;
4151          }          }
4152        } # OE        } # OE
4153                
4154        ## Step 3        ## Step 3
4155        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4156            !!!cp ('t61');
4157          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4158          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4159          !!!next-token;          !!!next-token;
# Line 2445  sub _tree_construction_main ($) { Line 4166  sub _tree_construction_main ($) {
4166        ## Step 5        ## Step 5
4167        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4168        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4169            !!!cp ('t62');
4170          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4171        }        }
4172                
# Line 2467  sub _tree_construction_main ($) { Line 4189  sub _tree_construction_main ($) {
4189          S7S2: {          S7S2: {
4190            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4191              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4192                  !!!cp ('t63');
4193                $node_i_in_active = $_;                $node_i_in_active = $_;
4194                last S7S2;                last S7S2;
4195              }              }
# Line 2480  sub _tree_construction_main ($) { Line 4203  sub _tree_construction_main ($) {
4203                    
4204          ## Step 4          ## Step 4
4205          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4206              !!!cp ('t64');
4207            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4208          }          }
4209                    
4210          ## Step 5          ## Step 5
4211          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4212              !!!cp ('t65');
4213            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4214            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4215            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2502  sub _tree_construction_main ($) { Line 4227  sub _tree_construction_main ($) {
4227        } # S7          } # S7  
4228                
4229        ## Step 8        ## Step 8
4230        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4231            my $foster_parent_element;
4232            my $next_sibling;
4233            OE: for (reverse 0..$#{$self->{open_elements}}) {
4234              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4235                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4236                                 if (defined $parent and $parent->node_type == 1) {
4237                                   !!!cp ('t65.1');
4238                                   $foster_parent_element = $parent;
4239                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4240                                 } else {
4241                                   !!!cp ('t65.2');
4242                                   $foster_parent_element
4243                                     = $self->{open_elements}->[$_ - 1]->[0];
4244                                 }
4245                                 last OE;
4246                               }
4247                             } # OE
4248                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4249                               unless defined $foster_parent_element;
4250            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4251            $open_tables->[-1]->[1] = 1; # tainted
4252          } else {
4253            !!!cp ('t65.3');
4254            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4255          }
4256                
4257        ## Step 9        ## Step 9
4258        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2519  sub _tree_construction_main ($) { Line 4269  sub _tree_construction_main ($) {
4269        my $i;        my $i;
4270        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4271          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4272              !!!cp ('t66');
4273            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4274            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4275          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4276              !!!cp ('t67');
4277            $i = $_;            $i = $_;
4278          }          }
4279        } # AFE        } # AFE
# Line 2531  sub _tree_construction_main ($) { Line 4283  sub _tree_construction_main ($) {
4283        undef $i;        undef $i;
4284        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4285          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4286              !!!cp ('t68');
4287            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4288            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4289          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4290              !!!cp ('t69');
4291            $i = $_;            $i = $_;
4292          }          }
4293        } # OE        } # OE
# Line 2544  sub _tree_construction_main ($) { Line 4298  sub _tree_construction_main ($) {
4298      } # FET      } # FET
4299    }; # $formatting_end_tag    }; # $formatting_end_tag
4300    
4301    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4302      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4303    }; # $insert_to_current    }; # $insert_to_current
4304    
4305    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4306                         my $child = shift;      my $child = shift;
4307                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4308                              table => 1, tbody => 1, tfoot => 1,        # MUST
4309                              thead => 1, tr => 1,        my $foster_parent_element;
4310                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4311                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4312                           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') {  
4313                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4314                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4315                                   !!!cp ('t70');
4316                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4317                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4318                               } else {                               } else {
4319                                   !!!cp ('t71');
4320                                 $foster_parent_element                                 $foster_parent_element
4321                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4322                               }                               }
# Line 2574  sub _tree_construction_main ($) { Line 4327  sub _tree_construction_main ($) {
4327                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4328                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4329                             ($child, $next_sibling);                             ($child, $next_sibling);
4330                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4331                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4332                         }        !!!cp ('t72');
4333          $self->{open_elements}->[-1]->[0]->append_child ($child);
4334        }
4335    }; # $insert_to_foster    }; # $insert_to_foster
4336    
4337    my $insert;    B: while (1) {
   
   B: {  
4338      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4339        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
4340          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4341        ## Ignore the token        ## Ignore the token
4342        ## Stay in the phase        ## Stay in the phase
4343        !!!next-token;        !!!next-token;
4344        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         #  
       } else {  
         ## 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_TOKEN, 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.  
       }  
   
       ## Stop parsing  
       last B;  
4345      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4346               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4347        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4348          ## Turn into the main phase          !!!cp ('t79');
4349          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4350          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4351        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4352          ## Turn into the main phase          !!!cp ('t80');
4353          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4354          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4355          } else {
4356            !!!cp ('t81');
4357        }        }
4358    
4359  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4360  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!parse-error (type => 'not first start tag');  
       }  
4361        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4362        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4363          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4364              !!!cp ('t84');
4365            $top_el->set_attribute_ns            $top_el->set_attribute_ns
4366              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
4367               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4368          }          }
4369        }        }
4370          !!!nack ('t84.1');
4371        !!!next-token;        !!!next-token;
4372        redo B;        next B;
4373      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4374        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4375        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4376            !!!cp ('t85');
4377          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
4378        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4379            !!!cp ('t86');
4380          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
4381        } else {        } else {
4382            !!!cp ('t87');
4383          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4384        }        }
4385        !!!next-token;        !!!next-token;
4386        redo B;        next B;
4387      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4388          if ($token->{type} == CHARACTER_TOKEN) {
4389            !!!cp ('t87.1');
4390            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4391            !!!next-token;
4392            next B;
4393          } elsif ($token->{type} == START_TAG_TOKEN) {
4394            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4395                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4396                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4397                ($token->{tag_name} eq 'svg' and
4398                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4399              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4400              !!!cp ('t87.2');
4401              #
4402            } elsif ({
4403                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4404                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4405                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4406                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4407                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4408                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4409                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4410                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4411                     }->{$token->{tag_name}}) {
4412              !!!cp ('t87.2');
4413              !!!parse-error (type => 'not closed',
4414                              text => $self->{open_elements}->[-1]->[0]
4415                                  ->manakai_local_name,
4416                              token => $token);
4417    
4418              pop @{$self->{open_elements}}
4419                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4420    
4421              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4422              ## Reprocess.
4423              next B;
4424            } else {
4425              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4426              my $tag_name = $token->{tag_name};
4427              if ($nsuri eq $SVG_NS) {
4428                $tag_name = {
4429                   altglyph => 'altGlyph',
4430                   altglyphdef => 'altGlyphDef',
4431                   altglyphitem => 'altGlyphItem',
4432                   animatecolor => 'animateColor',
4433                   animatemotion => 'animateMotion',
4434                   animatetransform => 'animateTransform',
4435                   clippath => 'clipPath',
4436                   feblend => 'feBlend',
4437                   fecolormatrix => 'feColorMatrix',
4438                   fecomponenttransfer => 'feComponentTransfer',
4439                   fecomposite => 'feComposite',
4440                   feconvolvematrix => 'feConvolveMatrix',
4441                   fediffuselighting => 'feDiffuseLighting',
4442                   fedisplacementmap => 'feDisplacementMap',
4443                   fedistantlight => 'feDistantLight',
4444                   feflood => 'feFlood',
4445                   fefunca => 'feFuncA',
4446                   fefuncb => 'feFuncB',
4447                   fefuncg => 'feFuncG',
4448                   fefuncr => 'feFuncR',
4449                   fegaussianblur => 'feGaussianBlur',
4450                   feimage => 'feImage',
4451                   femerge => 'feMerge',
4452                   femergenode => 'feMergeNode',
4453                   femorphology => 'feMorphology',
4454                   feoffset => 'feOffset',
4455                   fepointlight => 'fePointLight',
4456                   fespecularlighting => 'feSpecularLighting',
4457                   fespotlight => 'feSpotLight',
4458                   fetile => 'feTile',
4459                   feturbulence => 'feTurbulence',
4460                   foreignobject => 'foreignObject',
4461                   glyphref => 'glyphRef',
4462                   lineargradient => 'linearGradient',
4463                   radialgradient => 'radialGradient',
4464                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4465                   textpath => 'textPath',  
4466                }->{$tag_name} || $tag_name;
4467              }
4468    
4469              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4470    
4471              ## "adjust foreign attributes" - done in insert-element-f
4472    
4473              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4474    
4475              if ($self->{self_closing}) {
4476                pop @{$self->{open_elements}};
4477                !!!ack ('t87.3');
4478              } else {
4479                !!!cp ('t87.4');
4480              }
4481    
4482              !!!next-token;
4483              next B;
4484            }
4485          } elsif ($token->{type} == END_TAG_TOKEN) {
4486            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4487            !!!cp ('t87.5');
4488            #
4489          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4490            !!!cp ('t87.6');
4491            !!!parse-error (type => 'not closed',
4492                            text => $self->{open_elements}->[-1]->[0]
4493                                ->manakai_local_name,
4494                            token => $token);
4495    
4496            pop @{$self->{open_elements}}
4497                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4498    
4499            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4500            ## Reprocess.
4501            next B;
4502          } else {
4503            die "$0: $token->{type}: Unknown token type";        
4504          }
4505        }
4506    
4507        if ($self->{insertion_mode} & HEAD_IMS) {
4508        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4509          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4510            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4511                !!!cp ('t88.2');
4512                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4513                #
4514              } else {
4515                !!!cp ('t88.1');
4516                ## Ignore the token.
4517                #
4518              }
4519            unless (length $token->{data}) {            unless (length $token->{data}) {
4520                !!!cp ('t88');
4521              !!!next-token;              !!!next-token;
4522              redo B;              next B;
4523            }            }
4524    ## TODO: set $token->{column} appropriately
4525          }          }
4526    
4527          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4528              !!!cp ('t89');
4529            ## As if <head>            ## As if <head>
4530            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4531            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4532            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4533                  [$self->{head_element}, $el_category->{head}];
4534    
4535            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4536            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4537    
4538            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4539          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4540              !!!cp ('t90');
4541            ## As if </noscript>            ## As if </noscript>
4542            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4543            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4544                        
4545            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4546            ## As if </head>            ## As if </head>
# Line 2685  sub _tree_construction_main ($) { Line 4548  sub _tree_construction_main ($) {
4548    
4549            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4550          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4551              !!!cp ('t91');
4552            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4553    
4554            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4555            } else {
4556              !!!cp ('t92');
4557          }          }
4558    
4559              ## "after head" insertion mode          ## "after head" insertion mode
4560              ## As if <body>          ## As if <body>
4561              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4562              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4563              ## reprocess          ## reprocess
4564              redo B;          next B;
4565            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4566              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4567                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4568                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4569                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4570                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4571                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4572                  !!!next-token;              push @{$self->{open_elements}},
4573                  redo B;                  [$self->{head_element}, $el_category->{head}];
4574                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4575                  #              !!!nack ('t93.1');
4576                } else {              !!!next-token;
4577                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4578                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4579                  !!!next-token;              !!!cp ('t93.2');
4580                  redo B;              !!!parse-error (type => 'after head', text => 'head',
4581                }                              token => $token);
4582              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              ## Ignore the token
4583                ## As if <head>              !!!nack ('t93.3');
4584                !!!create-element ($self->{head_element}, 'head');              !!!next-token;
4585                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              next B;
4586                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
4587                !!!cp ('t95');
4588                !!!parse-error (type => 'in head:head',
4589                                token => $token); # or in head noscript
4590                ## Ignore the token
4591                !!!nack ('t95.1');
4592                !!!next-token;
4593                next B;
4594              }
4595            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4596              !!!cp ('t96');
4597              ## As if <head>
4598              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4599              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4600              push @{$self->{open_elements}},
4601                  [$self->{head_element}, $el_category->{head}];
4602    
4603                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4604                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4605              }          } else {
4606              !!!cp ('t97');
4607            }
4608    
4609              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4610                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4611                    !!!cp ('t98');
4612                  ## As if </noscript>                  ## As if </noscript>
4613                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4614                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4615                                    token => $token);
4616                                
4617                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4618                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4619                  } else {
4620                    !!!cp ('t99');
4621                }                }
4622    
4623                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4624                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4625                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4626                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4627                                    text => $token->{tag_name}, token => $token);
4628                    push @{$self->{open_elements}},
4629                        [$self->{head_element}, $el_category->{head}];
4630                  } else {
4631                    !!!cp ('t101');
4632                }                }
4633                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4634                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4635                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4636                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4637                  !!!nack ('t101.1');
4638                !!!next-token;                !!!next-token;
4639                redo B;                next B;
4640              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4641                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4642                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4643                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4644                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4645                                    text => $token->{tag_name}, token => $token);
4646                    push @{$self->{open_elements}},
4647                        [$self->{head_element}, $el_category->{head}];
4648                  } else {
4649                    !!!cp ('t103');
4650                }                }
4651                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4652                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4653                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4654                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4655                  !!!ack ('t103.1');
4656                !!!next-token;                !!!next-token;
4657                redo B;                next B;
4658              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4659                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4660                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4661                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4662                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4663                                    text => $token->{tag_name}, token => $token);
4664                    push @{$self->{open_elements}},
4665                        [$self->{head_element}, $el_category->{head}];
4666                  } else {
4667                    !!!cp ('t105');
4668                }                }
4669                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4670                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4671    
4672                unless ($self->{confident}) {                unless ($self->{confident}) {
4673                  my $charset;                  if ($token->{attributes}->{charset}) {
4674                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4675                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4676                  }                    ## in the {change_encoding} callback.
4677                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4678                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4679                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4680                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4681                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4682                          ->set_user_data (manakai_has_reference =>
4683                                               $token->{attributes}->{charset}
4684                                                   ->{has_reference});
4685                    } elsif ($token->{attributes}->{content}) {
4686                      if ($token->{attributes}->{content}->{value}
4687                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4688                              [\x09-\x0D\x20]*=
4689                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4690                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4691                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4692                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4693                        ## in the {change_encoding} callback.
4694                        $self->{change_encoding}
4695                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4696                               $token);
4697                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4698                            ->set_user_data (manakai_has_reference =>
4699                                                 $token->{attributes}->{content}
4700                                                       ->{has_reference});
4701                      } else {
4702                        !!!cp ('t108');
4703                      }
4704                    }
4705                  } else {
4706                    if ($token->{attributes}->{charset}) {
4707                      !!!cp ('t109');
4708                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4709                          ->set_user_data (manakai_has_reference =>
4710                                               $token->{attributes}->{charset}
4711                                                   ->{has_reference});
4712                    }
4713                    if ($token->{attributes}->{content}) {
4714                      !!!cp ('t110');
4715                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4716                          ->set_user_data (manakai_has_reference =>
4717                                               $token->{attributes}->{content}
4718                                                   ->{has_reference});
4719                  }                  }
                 ## TODO: Change the encoding  
4720                }                }
4721    
4722                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4723                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4724                  !!!ack ('t110.1');
4725                !!!next-token;                !!!next-token;
4726                redo B;                next B;
4727              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4728                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4729                    !!!cp ('t111');
4730                  ## As if </noscript>                  ## As if </noscript>
4731                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4732                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4733                                    token => $token);
4734                                
4735                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4736                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4737                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4738                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4739                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4740                                    text => $token->{tag_name}, token => $token);
4741                    push @{$self->{open_elements}},
4742                        [$self->{head_element}, $el_category->{head}];
4743                  } else {
4744                    !!!cp ('t113');
4745                }                }
4746    
4747                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4748                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4749                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4750                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4751                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4752                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4753                redo B;                next B;
4754              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4755                         $token->{tag_name} eq 'noframes') {
4756                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4757                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4758                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4759                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4760                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4761                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4762                                    text => $token->{tag_name}, token => $token);
4763                    push @{$self->{open_elements}},
4764                        [$self->{head_element}, $el_category->{head}];
4765                  } else {
4766                    !!!cp ('t115');
4767                }                }
4768                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4769                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4770                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4771                redo B;                next B;
4772              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4773                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4774                    !!!cp ('t116');
4775                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4776                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4777                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4778                    !!!nack ('t116.1');
4779                  !!!next-token;                  !!!next-token;
4780                  redo B;                  next B;
4781                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4782                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4783                    !!!parse-error (type => 'in noscript', text => 'noscript',
4784                                    token => $token);
4785                  ## Ignore the token                  ## Ignore the token
4786                    !!!nack ('t117.1');
4787                  !!!next-token;                  !!!next-token;
4788                  redo B;                  next B;
4789                } else {                } else {
4790                    !!!cp ('t118');
4791                  #                  #
4792                }                }
4793              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4794                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4795                    !!!cp ('t119');
4796                  ## As if </noscript>                  ## As if </noscript>
4797                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4798                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4799                                    token => $token);
4800                                
4801                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4802                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4803                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4804                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4805                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4806                                    text => $token->{tag_name}, token => $token);
4807                    push @{$self->{open_elements}},
4808                        [$self->{head_element}, $el_category->{head}];
4809                  } else {
4810                    !!!cp ('t121');
4811                }                }
4812    
4813                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4814                $script_start_tag->($insert_to_current);                $script_start_tag->();
4815                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4816                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4817                redo B;                next B;
4818              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4819                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4820                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4821                    !!!cp ('t122');
4822                  ## As if </noscript>                  ## As if </noscript>
4823                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4824                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4825                                    text => $token->{tag_name}, token => $token);
4826                                    
4827                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4828                  ## As if </head>                  ## As if </head>
# Line 2866  sub _tree_construction_main ($) { Line 4830  sub _tree_construction_main ($) {
4830                                    
4831                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4832                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4833                    !!!cp ('t124');
4834                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4835                                    
4836                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4837                  } else {
4838                    !!!cp ('t125');
4839                }                }
4840    
4841                ## "after head" insertion mode                ## "after head" insertion mode
4842                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4843                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4844                    !!!cp ('t126');
4845                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4846                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4847                    !!!cp ('t127');
4848                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4849                } else {                } else {
4850                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4851                }                }
4852                  !!!nack ('t127.1');
4853                !!!next-token;                !!!next-token;
4854                redo B;                next B;
4855              } else {              } else {
4856                  !!!cp ('t128');
4857                #                #
4858              }              }
4859    
4860              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4861                  !!!cp ('t129');
4862                ## As if </noscript>                ## As if </noscript>
4863                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4864                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4865                                  text => $token->{tag_name}, token => $token);
4866                                
4867                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4868                ## As if </head>                ## As if </head>
# Line 2897  sub _tree_construction_main ($) { Line 4870  sub _tree_construction_main ($) {
4870    
4871                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4872              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4873                  !!!cp ('t130');
4874                ## As if </head>                ## As if </head>
4875                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4876    
4877                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4878                } else {
4879                  !!!cp ('t131');
4880              }              }
4881    
4882              ## "after head" insertion mode              ## "after head" insertion mode
4883              ## As if <body>              ## As if <body>
4884              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4885              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4886              ## reprocess              ## reprocess
4887              redo B;              !!!ack-later;
4888                next B;
4889            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4890              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4891                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4892                    !!!cp ('t132');
4893                  ## As if <head>                  ## As if <head>
4894                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4895                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4896                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4897                        [$self->{head_element}, $el_category->{head}];
4898    
4899                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4900                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4901                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4902                  !!!next-token;                  !!!next-token;
4903                  redo B;                  next B;
4904                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4905                    !!!cp ('t133');
4906                  ## As if </noscript>                  ## As if </noscript>
4907                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4908                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
4909                                    text => 'head', token => $token);
4910                                    
4911                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4912                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4913                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4914                  !!!next-token;                  !!!next-token;
4915                  redo B;                  next B;
4916                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4917                    !!!cp ('t134');
4918                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4919                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4920                  !!!next-token;                  !!!next-token;
4921                  redo B;                  next B;
4922                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4923                    !!!cp ('t134.1');
4924                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4925                                    token => $token);
4926                    ## Ignore the token
4927                    !!!next-token;
4928                    next B;
4929                } else {                } else {
4930                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4931                }                }
4932              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4933                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4934                    !!!cp ('t136');
4935                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4936                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4937                  !!!next-token;                  !!!next-token;
4938                  redo B;                  next B;
4939                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4940                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4941                    !!!cp ('t137');
4942                    !!!parse-error (type => 'unmatched end tag',
4943                                    text => 'noscript', token => $token);
4944                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4945                  !!!next-token;                  !!!next-token;
4946                  redo B;                  next B;
4947                } else {                } else {
4948                    !!!cp ('t138');
4949                  #                  #
4950                }                }
4951              } elsif ({              } elsif ({
4952                        body => 1, html => 1,                        body => 1, html => 1,
4953                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4954                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4955                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4956                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4957                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4958                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag',
4959                                    text => $token->{tag_name}, token => $token);
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4960                  ## Ignore the token                  ## Ignore the token
4961                  !!!next-token;                  !!!next-token;
4962                  redo B;                  next B;
4963                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4964                    !!!cp ('t140.1');
4965                    !!!parse-error (type => 'unmatched end tag',
4966                                    text => $token->{tag_name}, token => $token);
4967                    ## Ignore the token
4968                    !!!next-token;
4969                    next B;
4970                  } else {
4971                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4972                }                }
4973                              } elsif ($token->{tag_name} eq 'p') {
4974                #                !!!cp ('t142');
4975              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4976                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4977                       }->{$token->{tag_name}}) {                ## Ignore the token
4978                  !!!next-token;
4979                  next B;
4980                } elsif ($token->{tag_name} eq 'br') {
4981                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4982                  ## As if <head>                  !!!cp ('t142.2');
4983                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
4984                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4985                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4986                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4987      
4988                    ## Reprocess in the "after head" insertion mode...
4989                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4990                    !!!cp ('t143.2');
4991                    ## As if </head>
4992                    pop @{$self->{open_elements}};
4993                    $self->{insertion_mode} = AFTER_HEAD_IM;
4994      
4995                    ## Reprocess in the "after head" insertion mode...
4996                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4997                    !!!cp ('t143.3');
4998                    ## ISSUE: Two parse errors for <head><noscript></br>
4999                    !!!parse-error (type => 'unmatched end tag',
5000                                    text => 'br', token => $token);
5001                    ## As if </noscript>
5002                    pop @{$self->{open_elements}};
5003                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
5004    
5005                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
5006                }                  ## As if </head>
5007                    pop @{$self->{open_elements}};
5008                    $self->{insertion_mode} = AFTER_HEAD_IM;
5009    
5010                #                  ## Reprocess in the "after head" insertion mode...
5011              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5012                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
5013                  #                  #
5014                } else {                } else {
5015                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5016                }                }
5017    
5018                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5019                  !!!parse-error (type => 'unmatched end tag',
5020                                  text => 'br', token => $token);
5021                  ## Ignore the token
5022                  !!!next-token;
5023                  next B;
5024                } else {
5025                  !!!cp ('t145');
5026                  !!!parse-error (type => 'unmatched end tag',
5027                                  text => $token->{tag_name}, token => $token);
5028                  ## Ignore the token
5029                  !!!next-token;
5030                  next B;
5031              }              }
5032    
5033              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5034                  !!!cp ('t146');
5035                ## As if </noscript>                ## As if </noscript>
5036                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5037                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
5038                                  text => $token->{tag_name}, token => $token);
5039                                
5040                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5041                ## As if </head>                ## As if </head>
# Line 3009  sub _tree_construction_main ($) { Line 5043  sub _tree_construction_main ($) {
5043    
5044                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
5045              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5046                  !!!cp ('t147');
5047                ## As if </head>                ## As if </head>
5048                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5049    
5050                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
5051              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5052                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
5053                  !!!cp ('t148');
5054                  !!!parse-error (type => 'unmatched end tag',
5055                                  text => $token->{tag_name}, token => $token);
5056                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5057                !!!next-token;                !!!next-token;
5058                redo B;                next B;
5059                } else {
5060                  !!!cp ('t149');
5061              }              }
5062    
5063              ## "after head" insertion mode              ## "after head" insertion mode
5064              ## As if <body>              ## As if <body>
5065              !!!insert-element ('body');              !!!insert-element ('body',, $token);
5066              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
5067              ## reprocess              ## reprocess
5068              redo B;              next B;
5069            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5070              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5071            }            !!!cp ('t149.1');
5072    
5073              ## NOTE: As if <head>
5074              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5075              $self->{open_elements}->[-1]->[0]->append_child
5076                  ($self->{head_element});
5077              #push @{$self->{open_elements}},
5078              #    [$self->{head_element}, $el_category->{head}];
5079              #$self->{insertion_mode} = IN_HEAD_IM;
5080              ## NOTE: Reprocess.
5081    
5082              ## NOTE: As if </head>
5083              #pop @{$self->{open_elements}};
5084              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5085              ## NOTE: Reprocess.
5086              
5087              #
5088            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5089              !!!cp ('t149.2');
5090    
5091              ## NOTE: As if </head>
5092              pop @{$self->{open_elements}};
5093              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5094              ## NOTE: Reprocess.
5095    
5096              #
5097            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5098              !!!cp ('t149.3');
5099    
5100              !!!parse-error (type => 'in noscript:#eof', token => $token);
5101    
5102              ## As if </noscript>
5103              pop @{$self->{open_elements}};
5104              #$self->{insertion_mode} = IN_HEAD_IM;
5105              ## NOTE: Reprocess.
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            } else {
5114              !!!cp ('t149.4');
5115              #
5116            }
5117    
5118            ## NOTE: As if <body>
5119            !!!insert-element ('body',, $token);
5120            $self->{insertion_mode} = IN_BODY_IM;
5121            ## NOTE: Reprocess.
5122            next B;
5123          } else {
5124            die "$0: $token->{type}: Unknown token type";
5125          }
5126    
5127            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5128      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
5129            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5130                !!!cp ('t150');
5131              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
5132              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5133                            
5134              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5135    
5136              !!!next-token;              !!!next-token;
5137              redo B;              next B;
5138            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5139              if ({              if ({
5140                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3047  sub _tree_construction_main ($) { Line 5142  sub _tree_construction_main ($) {
5142                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5143                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5144                  ## have an element in table scope                  ## have an element in table scope
5145                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5146                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5147                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5148                      $tn = $node->[1];                      !!!cp ('t151');
5149                      last INSCOPE;  
5150                    } elsif ({                      ## Close the cell
5151                              table => 1, html => 1,                      !!!back-token; # <x>
5152                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
5153                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
5154                    }                                line => $token->{line},
5155                  } # INSCOPE                                column => $token->{column}};
5156                    unless (defined $tn) {                      next B;
5157                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5158                      ## Ignore the token                      !!!cp ('t152');
5159                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
5160                      redo B;                      last;
5161                    }                    }
5162                                    }
5163                  ## Close the cell  
5164                  !!!back-token; # <?>                  !!!cp ('t153');
5165                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
5166                  redo B;                      text => $token->{tag_name}, token => $token);
5167                    ## Ignore the token
5168                    !!!nack ('t153.1');
5169                    !!!next-token;
5170                    next B;
5171                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5172                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5173                                    token => $token);
5174                                    
5175                  ## As if </caption>                  ## NOTE: As if </caption>.
5176                  ## have a table element in table scope                  ## have a table element in table scope
5177                  my $i;                  my $i;
5178                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5179                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5180                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5181                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5182                      last INSCOPE;                        !!!cp ('t155');
5183                    } elsif ({                        $i = $_;
5184                              table => 1, html => 1,                        last INSCOPE;
5185                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5186                      last INSCOPE;                        !!!cp ('t156');
5187                          last;
5188                        }
5189                    }                    }
5190    
5191                      !!!cp ('t157');
5192                      !!!parse-error (type => 'start tag not allowed',
5193                                      text => $token->{tag_name}, token => $token);
5194                      ## Ignore the token
5195                      !!!nack ('t157.1');
5196                      !!!next-token;
5197                      next B;
5198                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5199                                    
5200                  ## generate implied end tags                  ## generate implied end tags
5201                  if ({                  while ($self->{open_elements}->[-1]->[1]
5202                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5203                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
5204                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token; # <?>  
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5205                  }                  }
5206    
5207                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5208                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
5209                      !!!parse-error (type => 'not closed',
5210                                      text => $self->{open_elements}->[-1]->[0]
5211                                          ->manakai_local_name,
5212                                      token => $token);
5213                    } else {
5214                      !!!cp ('t160');
5215                  }                  }
5216                                    
5217                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3119  sub _tree_construction_main ($) { Line 5221  sub _tree_construction_main ($) {
5221                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5222                                    
5223                  ## reprocess                  ## reprocess
5224                  redo B;                  !!!ack-later;
5225                    next B;
5226                } else {                } else {
5227                    !!!cp ('t161');
5228                  #                  #
5229                }                }
5230              } else {              } else {
5231                  !!!cp ('t162');
5232                #                #
5233              }              }
5234            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3133  sub _tree_construction_main ($) { Line 5238  sub _tree_construction_main ($) {
5238                  my $i;                  my $i;
5239                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5240                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5241                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5242                        !!!cp ('t163');
5243                      $i = $_;                      $i = $_;
5244                      last INSCOPE;                      last INSCOPE;
5245                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5246                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
5247                      last INSCOPE;                      last INSCOPE;
5248                    }                    }
5249                  } # INSCOPE                  } # INSCOPE
5250                    unless (defined $i) {                    unless (defined $i) {
5251                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
5252                        !!!parse-error (type => 'unmatched end tag',
5253                                        text => $token->{tag_name},
5254                                        token => $token);
5255                      ## Ignore the token                      ## Ignore the token
5256                      !!!next-token;                      !!!next-token;
5257                      redo B;                      next B;
5258                    }                    }
5259                                    
5260                  ## generate implied end tags                  ## generate implied end tags
5261                  if ({                  while ($self->{open_elements}->[-1]->[1]
5262                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5263                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
5264                       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_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5265                  }                  }
5266                    
5267                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5268                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
5269                      !!!cp ('t167');
5270                      !!!parse-error (type => 'not closed',
5271                                      text => $self->{open_elements}->[-1]->[0]
5272                                          ->manakai_local_name,
5273                                      token => $token);
5274                    } else {
5275                      !!!cp ('t168');
5276                  }                  }
5277                                    
5278                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3174  sub _tree_construction_main ($) { Line 5282  sub _tree_construction_main ($) {
5282                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5283                                    
5284                  !!!next-token;                  !!!next-token;
5285                  redo B;                  next B;
5286                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5287                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
5288                    !!!parse-error (type => 'unmatched end tag',
5289                                    text => $token->{tag_name}, token => $token);
5290                  ## Ignore the token                  ## Ignore the token
5291                  !!!next-token;                  !!!next-token;
5292                  redo B;                  next B;
5293                } else {                } else {
5294                    !!!cp ('t170');
5295                  #                  #
5296                }                }
5297              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
5298                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5299                  ## have a table element in table scope                  ## have a table element in table scope
5300                  my $i;                  my $i;
5301                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5302                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5303                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5304                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5305                      last INSCOPE;                        !!!cp ('t171');
5306                    } elsif ({                        $i = $_;
5307                              table => 1, html => 1,                        last INSCOPE;
5308                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5309                      last INSCOPE;                        !!!cp ('t172');
5310                          last;
5311                        }
5312                    }                    }
5313    
5314                      !!!cp ('t173');
5315                      !!!parse-error (type => 'unmatched end tag',
5316                                      text => $token->{tag_name}, token => $token);
5317                      ## Ignore the token
5318                      !!!next-token;
5319                      next B;
5320                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5321                                    
5322                  ## generate implied end tags                  ## generate implied end tags
5323                  if ({                  while ($self->{open_elements}->[-1]->[1]
5324                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5325                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
5326                       tbody => 1, tfoot=> 1, thead => 1,                    pop @{$self->{open_elements}};
                     }->{$self->{open_elements}->[-1]->[1]}) {  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5327                  }                  }
5328                                    
5329                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5330                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
5331                      !!!parse-error (type => 'not closed',
5332                                      text => $self->{open_elements}->[-1]->[0]
5333                                          ->manakai_local_name,
5334                                      token => $token);
5335                    } else {
5336                      !!!cp ('t176');
5337                  }                  }
5338                                    
5339                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3228  sub _tree_construction_main ($) { Line 5343  sub _tree_construction_main ($) {
5343                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5344                                    
5345                  !!!next-token;                  !!!next-token;
5346                  redo B;                  next B;
5347                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5348                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5349                    !!!parse-error (type => 'unmatched end tag',
5350                                    text => $token->{tag_name}, token => $token);
5351                  ## Ignore the token                  ## Ignore the token
5352                  !!!next-token;                  !!!next-token;
5353                  redo B;                  next B;
5354                } else {                } else {
5355                    !!!cp ('t178');
5356                  #                  #
5357                }                }
5358              } elsif ({              } elsif ({
# Line 3245  sub _tree_construction_main ($) { Line 5363  sub _tree_construction_main ($) {
5363                ## have an element in table scope                ## have an element in table scope
5364                my $i;                my $i;
5365                my $tn;                my $tn;
5366                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5367                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5368                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5369                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5370                    last INSCOPE;                      !!!cp ('t179');
5371                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5372                    $tn = $node->[1];  
5373                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5374                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5375                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5376                            table => 1, html => 1,                                line => $token->{line},
5377                           }->{$node->[1]}) {                                column => $token->{column}};
5378                    last INSCOPE;                      next B;
5379                      } elsif ($node->[1] & TABLE_CELL_EL) {
5380                        !!!cp ('t180');
5381                        $tn = $node->[0]->manakai_local_name;
5382                        ## NOTE: There is exactly one |td| or |th| element
5383                        ## in scope in the stack of open elements by definition.
5384                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5385                        ## ISSUE: Can this be reached?
5386                        !!!cp ('t181');
5387                        last;
5388                      }
5389                  }                  }
5390                } # INSCOPE  
5391                unless (defined $i) {                  !!!cp ('t182');
5392                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5393                        text => $token->{tag_name}, token => $token);
5394                  ## Ignore the token                  ## Ignore the token
5395                  !!!next-token;                  !!!next-token;
5396                  redo B;                  next B;
5397                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5398              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5399                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5400                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5401                                  token => $token);
5402    
5403                ## As if </caption>                ## As if </caption>
5404                ## have a table element in table scope                ## have a table element in table scope
5405                my $i;                my $i;
5406                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5407                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5408                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5409                      !!!cp ('t184');
5410                    $i = $_;                    $i = $_;
5411                    last INSCOPE;                    last INSCOPE;
5412                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5413                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5414                    last INSCOPE;                    last INSCOPE;
5415                  }                  }
5416                } # INSCOPE                } # INSCOPE
5417                unless (defined $i) {                unless (defined $i) {
5418                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5419                    !!!parse-error (type => 'unmatched end tag',
5420                                    text => 'caption', token => $token);
5421                  ## Ignore the token                  ## Ignore the token
5422                  !!!next-token;                  !!!next-token;
5423                  redo B;                  next B;
5424                }                }
5425                                
5426                ## generate implied end tags                ## generate implied end tags
5427                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5428                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5429                     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_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5430                }                }
5431    
5432                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5433                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5434                    !!!parse-error (type => 'not closed',
5435                                    text => $self->{open_elements}->[-1]->[0]
5436                                        ->manakai_local_name,
5437                                    token => $token);
5438                  } else {
5439                    !!!cp ('t189');
5440                }                }
5441    
5442                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3321  sub _tree_construction_main ($) { Line 5446  sub _tree_construction_main ($) {
5446                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5447    
5448                ## reprocess                ## reprocess
5449                redo B;                next B;
5450              } elsif ({              } elsif ({
5451                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5452                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5453                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5454                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
5455                    !!!parse-error (type => 'unmatched end tag',
5456                                    text => $token->{tag_name}, token => $token);
5457                  ## Ignore the token                  ## Ignore the token
5458                  !!!next-token;                  !!!next-token;
5459                  redo B;                  next B;
5460                } else {                } else {
5461                    !!!cp ('t191');
5462                  #                  #
5463                }                }
5464              } elsif ({              } elsif ({
# Line 3338  sub _tree_construction_main ($) { Line 5466  sub _tree_construction_main ($) {
5466                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5467                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5468                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5469                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5470                  !!!parse-error (type => 'unmatched end tag',
5471                                  text => $token->{tag_name}, token => $token);
5472                ## Ignore the token                ## Ignore the token
5473                !!!next-token;                !!!next-token;
5474                redo B;                next B;
5475              } else {              } else {
5476                  !!!cp ('t193');
5477                #                #
5478              }              }
5479          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5480            for my $entry (@{$self->{open_elements}}) {
5481              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5482                !!!cp ('t75');
5483                !!!parse-error (type => 'in body:#eof', token => $token);
5484                last;
5485              }
5486            }
5487    
5488            ## Stop parsing.
5489            last B;
5490        } else {        } else {
5491          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5492        }        }
# Line 3353  sub _tree_construction_main ($) { Line 5495  sub _tree_construction_main ($) {
5495        #        #
5496      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5497        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5498              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5499                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5500              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5501                                
5502                unless (length $token->{data}) {            unless (length $token->{data}) {
5503                  !!!next-token;              !!!cp ('t194');
5504                  redo B;              !!!next-token;
5505                }              next B;
5506              }            } else {
5507                !!!cp ('t195');
5508              }
5509            }
5510    
5511              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5512    
5513              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5514              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3370  sub _tree_construction_main ($) { Line 5516  sub _tree_construction_main ($) {
5516              ## result in a new Text node.              ## result in a new Text node.
5517              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5518                            
5519              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]}) {  
5520                # MUST                # MUST
5521                my $foster_parent_element;                my $foster_parent_element;
5522                my $next_sibling;                my $next_sibling;
5523                my $prev_sibling;                my $prev_sibling;
5524                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5525                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5526                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5527                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5528                        !!!cp ('t196');
5529                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5530                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5531                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5532                    } else {                    } else {
5533                        !!!cp ('t197');
5534                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5535                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5536                    }                    }
# Line 3397  sub _tree_construction_main ($) { Line 5542  sub _tree_construction_main ($) {
5542                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5543                if (defined $prev_sibling and                if (defined $prev_sibling and
5544                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5545                    !!!cp ('t198');
5546                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5547                } else {                } else {
5548                    !!!cp ('t199');
5549                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5550                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5551                     $next_sibling);                     $next_sibling);
5552                }                }
5553              } else {            $open_tables->[-1]->[1] = 1; # tainted
5554                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5555              }            !!!cp ('t200');
5556              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5557            }
5558                            
5559              !!!next-token;          !!!next-token;
5560              redo B;          next B;
5561        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5562              if ({          if ({
5563                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5564                   th => 1, td => 1,               th => 1, td => 1,
5565                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5566                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5567                  ## Clear back to table context              ## Clear back to table context
5568                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5569                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5570                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!cp ('t201');
5571                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5572                  }              }
5573                                
5574                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5575                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5576                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5577                }            }
5578              
5579                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5580                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5581                    !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t202');
5582                  }                !!!parse-error (type => 'missing start tag:tr', token => $token);
5583                }
5584                                    
5585                  ## Clear back to table body context              ## Clear back to table body context
5586                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5587                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5588                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5589                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## ISSUE: Can this case be reached?
5590                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5591                  }              }
5592                                    
5593                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5594                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5595                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5596                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5597                      !!!nack ('t204');
5598                    !!!next-token;                    !!!next-token;
5599                    redo B;                    next B;
5600                  } else {                  } else {
5601                    !!!insert-element ('tr');                    !!!cp ('t205');
5602                      !!!insert-element ('tr',, $token);
5603                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5604                  }                  }
5605                  } else {
5606                    !!!cp ('t206');
5607                }                }
5608    
5609                ## Clear back to table row context                ## Clear back to table row context
5610                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5611                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5612                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5613                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5614                }                }
5615                                
5616                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5617                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5618    
5619                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5620                                
5621                  !!!nack ('t207.1');
5622                !!!next-token;                !!!next-token;
5623                redo B;                next B;
5624              } elsif ({              } elsif ({
5625                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5626                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3477  sub _tree_construction_main ($) { Line 5632  sub _tree_construction_main ($) {
5632                  my $i;                  my $i;
5633                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5634                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5635                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5636                        !!!cp ('t208');
5637                      $i = $_;                      $i = $_;
5638                      last INSCOPE;                      last INSCOPE;
5639                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5640                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5641                      last INSCOPE;                      last INSCOPE;
5642                    }                    }
5643                  } # INSCOPE                  } # INSCOPE
5644                  unless (defined $i) {                  unless (defined $i) {
5645                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5646    ## TODO: This type is wrong.
5647                      !!!parse-error (type => 'unmacthed end tag',
5648                                      text => $token->{tag_name}, token => $token);
5649                    ## Ignore the token                    ## Ignore the token
5650                      !!!nack ('t210.1');
5651                    !!!next-token;                    !!!next-token;
5652                    redo B;                    next B;
5653                  }                  }
5654                                    
5655                  ## Clear back to table row context                  ## Clear back to table row context
5656                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5657                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5658                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5659                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5660                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5661                  }                  }
5662                                    
5663                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5664                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5665                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5666                      !!!cp ('t212');
5667                    ## reprocess                    ## reprocess
5668                    redo B;                    !!!ack-later;
5669                      next B;
5670                  } else {                  } else {
5671                      !!!cp ('t213');
5672                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5673                  }                  }
5674                }                }
# Line 3516  sub _tree_construction_main ($) { Line 5678  sub _tree_construction_main ($) {
5678                  my $i;                  my $i;
5679                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5680                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5681                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5682                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5683                      $i = $_;                      $i = $_;
5684                      last INSCOPE;                      last INSCOPE;
5685                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5686                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5687                      last INSCOPE;                      last INSCOPE;
5688                    }                    }
5689                  } # INSCOPE                  } # INSCOPE
5690                  unless (defined $i) {                  unless (defined $i) {
5691                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5692    ## TODO: This erorr type is wrong.
5693                      !!!parse-error (type => 'unmatched end tag',
5694                                      text => $token->{tag_name}, token => $token);
5695                    ## Ignore the token                    ## Ignore the token
5696                      !!!nack ('t216.1');
5697                    !!!next-token;                    !!!next-token;
5698                    redo B;                    next B;
5699                  }                  }
5700    
5701                  ## Clear back to table body context                  ## Clear back to table body context
5702                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5703                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5704                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5705                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5706                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5707                  }                  }
5708                                    
# Line 3552  sub _tree_construction_main ($) { Line 5716  sub _tree_construction_main ($) {
5716                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5717                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5718                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5719                  } else {
5720                    !!!cp ('t218');
5721                }                }
5722    
5723                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5724                  ## Clear back to table context                  ## Clear back to table context
5725                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5726                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5727                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5728                      ## ISSUE: Can this state be reached?
5729                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5730                  }                  }
5731                                    
5732                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5733                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5734                  ## reprocess                  ## reprocess
5735                  redo B;                  !!!ack-later;
5736                    next B;
5737                } elsif ({                } elsif ({
5738                          caption => 1,                          caption => 1,
5739                          colgroup => 1,                          colgroup => 1,
5740                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5741                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5742                  ## Clear back to table context                  ## Clear back to table context
5743                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5744                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5745                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5746                      ## ISSUE: Can this state be reached?
5747                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5748                  }                  }
5749                                    
5750                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5751                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5752                                    
5753                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5754                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5755                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5756                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3590  sub _tree_construction_main ($) { Line 5759  sub _tree_construction_main ($) {
5759                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5760                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5761                  !!!next-token;                  !!!next-token;
5762                  redo B;                  !!!nack ('t220.1');
5763                    next B;
5764                } else {                } else {
5765                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5766                }                }
5767              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5768                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5769                                  text => $self->{open_elements}->[-1]->[0]
5770                                      ->manakai_local_name,
5771                                  token => $token);
5772    
5773                ## As if </table>                ## As if </table>
5774                ## have a table element in table scope                ## have a table element in table scope
5775                my $i;                my $i;
5776                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5777                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5778                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5779                      !!!cp ('t221');
5780                    $i = $_;                    $i = $_;
5781                    last INSCOPE;                    last INSCOPE;
5782                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5783                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5784                    last INSCOPE;                    last INSCOPE;
5785                  }                  }
5786                } # INSCOPE                } # INSCOPE
5787                unless (defined $i) {                unless (defined $i) {
5788                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5789    ## TODO: The following is wrong, maybe.
5790                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5791                                    token => $token);
5792                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5793                    !!!nack ('t223.1');
5794                  !!!next-token;                  !!!next-token;
5795                  redo B;                  next B;
5796                }                }
5797                                
5798    ## TODO: Followings are removed from the latest spec.
5799                ## generate implied end tags                ## generate implied end tags
5800                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5801                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5802                     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_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5803                }                }
5804    
5805                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5806                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5807                    ## NOTE: |<table><tr><table>|
5808                    !!!parse-error (type => 'not closed',
5809                                    text => $self->{open_elements}->[-1]->[0]
5810                                        ->manakai_local_name,
5811                                    token => $token);
5812                  } else {
5813                    !!!cp ('t226');
5814                }                }
5815    
5816                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5817                  pop @{$open_tables};
5818    
5819                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5820    
5821                ## reprocess            ## reprocess
5822                redo B;            !!!ack-later;
5823          } else {            next B;
5824            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5825              if (not $open_tables->[-1]->[1]) { # tainted
5826                !!!cp ('t227.8');
5827                ## NOTE: This is a "as if in head" code clone.
5828                $parse_rcdata->(CDATA_CONTENT_MODEL);
5829                next B;
5830              } else {
5831                !!!cp ('t227.7');
5832                #
5833              }
5834            } elsif ($token->{tag_name} eq 'script') {
5835              if (not $open_tables->[-1]->[1]) { # tainted
5836                !!!cp ('t227.6');
5837                ## NOTE: This is a "as if in head" code clone.
5838                $script_start_tag->();
5839                next B;
5840              } else {
5841                !!!cp ('t227.5');
5842                #
5843              }
5844            } elsif ($token->{tag_name} eq 'input') {
5845              if (not $open_tables->[-1]->[1]) { # tainted
5846                if ($token->{attributes}->{type}) { ## TODO: case
5847                  my $type = lc $token->{attributes}->{type}->{value};
5848                  if ($type eq 'hidden') {
5849                    !!!cp ('t227.3');
5850                    !!!parse-error (type => 'in table',
5851                                    text => $token->{tag_name}, token => $token);
5852    
5853            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5854    
5855                    ## TODO: form element pointer
5856    
5857                    pop @{$self->{open_elements}};
5858    
5859                    !!!next-token;
5860                    !!!ack ('t227.2.1');
5861                    next B;
5862                  } else {
5863                    !!!cp ('t227.2');
5864                    #
5865                  }
5866                } else {
5867                  !!!cp ('t227.1');
5868                  #
5869                }
5870              } else {
5871                !!!cp ('t227.4');
5872                #
5873              }
5874            } else {
5875              !!!cp ('t227');
5876            #            #
5877          }          }
5878    
5879            !!!parse-error (type => 'in table', text => $token->{tag_name},
5880                            token => $token);
5881    
5882            $insert = $insert_to_foster;
5883            #
5884        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5885              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5886                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3655  sub _tree_construction_main ($) { Line 5888  sub _tree_construction_main ($) {
5888                my $i;                my $i;
5889                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5890                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5891                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5892                      !!!cp ('t228');
5893                    $i = $_;                    $i = $_;
5894                    last INSCOPE;                    last INSCOPE;
5895                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5896                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5897                    last INSCOPE;                    last INSCOPE;
5898                  }                  }
5899                } # INSCOPE                } # INSCOPE
5900                unless (defined $i) {                unless (defined $i) {
5901                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5902                    !!!parse-error (type => 'unmatched end tag',
5903                                    text => $token->{tag_name}, token => $token);
5904                  ## Ignore the token                  ## Ignore the token
5905                    !!!nack ('t230.1');
5906                  !!!next-token;                  !!!next-token;
5907                  redo B;                  next B;
5908                  } else {
5909                    !!!cp ('t232');
5910                }                }
5911    
5912                ## Clear back to table row context                ## Clear back to table row context
5913                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5914                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5915                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5916                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5917                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5918                }                }
5919    
5920                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5921                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5922                !!!next-token;                !!!next-token;
5923                redo B;                !!!nack ('t231.1');
5924                  next B;
5925              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5926                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5927                  ## As if </tr>                  ## As if </tr>
# Line 3690  sub _tree_construction_main ($) { Line 5929  sub _tree_construction_main ($) {
5929                  my $i;                  my $i;
5930                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5931                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5932                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5933                        !!!cp ('t233');
5934                      $i = $_;                      $i = $_;
5935                      last INSCOPE;                      last INSCOPE;
5936                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5937                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5938                      last INSCOPE;                      last INSCOPE;
5939                    }                    }
5940                  } # INSCOPE                  } # INSCOPE
5941                  unless (defined $i) {                  unless (defined $i) {
5942                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5943    ## TODO: The following is wrong.
5944                      !!!parse-error (type => 'unmatched end tag',
5945                                      text => $token->{type}, token => $token);
5946                    ## Ignore the token                    ## Ignore the token
5947                      !!!nack ('t236.1');
5948                    !!!next-token;                    !!!next-token;
5949                    redo B;                    next B;
5950                  }                  }
5951                                    
5952                  ## Clear back to table row context                  ## Clear back to table row context
5953                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5954                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5955                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5956                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5957                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5958                  }                  }
5959                                    
# Line 3724  sub _tree_construction_main ($) { Line 5967  sub _tree_construction_main ($) {
5967                  my $i;                  my $i;
5968                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5969                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5970                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5971                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5972                      $i = $_;                      $i = $_;
5973                      last INSCOPE;                      last INSCOPE;
5974                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5975                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5976                      last INSCOPE;                      last INSCOPE;
5977                    }                    }
5978                  } # INSCOPE                  } # INSCOPE
5979                  unless (defined $i) {                  unless (defined $i) {
5980                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5981                      !!!parse-error (type => 'unmatched end tag',
5982                                      text => $token->{tag_name}, token => $token);
5983                    ## Ignore the token                    ## Ignore the token
5984                      !!!nack ('t239.1');
5985                    !!!next-token;                    !!!next-token;
5986                    redo B;                    next B;
5987                  }                  }
5988                                    
5989                  ## Clear back to table body context                  ## Clear back to table body context
5990                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5991                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5992                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5993                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5994                  }                  }
5995                                    
# Line 3762  sub _tree_construction_main ($) { Line 6005  sub _tree_construction_main ($) {
6005                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
6006                }                }
6007    
6008                  ## NOTE: </table> in the "in table" insertion mode.
6009                  ## When you edit the code fragment below, please ensure that
6010                  ## the code for <table> in the "in table" insertion mode
6011                  ## is synced with it.
6012    
6013                ## have a table element in table scope                ## have a table element in table scope
6014                my $i;                my $i;
6015                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6016                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6017                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
6018                      !!!cp ('t241');
6019                    $i = $_;                    $i = $_;
6020                    last INSCOPE;                    last INSCOPE;
6021                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6022                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
6023                    last INSCOPE;                    last INSCOPE;
6024                  }                  }
6025                } # INSCOPE                } # INSCOPE
6026                unless (defined $i) {                unless (defined $i) {
6027                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
6028                    !!!parse-error (type => 'unmatched end tag',
6029                                    text => $token->{tag_name}, token => $token);
6030                  ## Ignore the token                  ## Ignore the token
6031                    !!!nack ('t243.1');
6032                  !!!next-token;                  !!!next-token;
6033                  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_TOKEN,  
                           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]);  
6034                }                }
6035                                    
6036                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
6037                  pop @{$open_tables};
6038                                
6039                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6040                                
6041                !!!next-token;                !!!next-token;
6042                redo B;                next B;
6043              } elsif ({              } elsif ({
6044                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6045                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3813  sub _tree_construction_main ($) { Line 6049  sub _tree_construction_main ($) {
6049                  my $i;                  my $i;
6050                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6051                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6052                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6053                        !!!cp ('t247');
6054                      $i = $_;                      $i = $_;
6055                      last INSCOPE;                      last INSCOPE;
6056                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6057                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
6058                      last INSCOPE;                      last INSCOPE;
6059                    }                    }
6060                  } # INSCOPE                  } # INSCOPE
6061                    unless (defined $i) {                    unless (defined $i) {
6062                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
6063                        !!!parse-error (type => 'unmatched end tag',
6064                                        text => $token->{tag_name}, token => $token);
6065                      ## Ignore the token                      ## Ignore the token
6066                        !!!nack ('t249.1');
6067                      !!!next-token;                      !!!next-token;
6068                      redo B;                      next B;
6069                    }                    }
6070                                    
6071                  ## As if </tr>                  ## As if </tr>
# Line 3834  sub _tree_construction_main ($) { Line 6073  sub _tree_construction_main ($) {
6073                  my $i;                  my $i;
6074                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6075                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6076                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6077                        !!!cp ('t250');
6078                      $i = $_;                      $i = $_;
6079                      last INSCOPE;                      last INSCOPE;
6080                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6081                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
6082                      last INSCOPE;                      last INSCOPE;
6083                    }                    }
6084                  } # INSCOPE                  } # INSCOPE
6085                    unless (defined $i) {                    unless (defined $i) {
6086                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
6087                        !!!parse-error (type => 'unmatched end tag',
6088                                        text => 'tr', token => $token);
6089                      ## Ignore the token                      ## Ignore the token
6090                        !!!nack ('t252.1');
6091                      !!!next-token;                      !!!next-token;
6092                      redo B;                      next B;
6093                    }                    }
6094                                    
6095                  ## Clear back to table row context                  ## Clear back to table row context
6096                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6097                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
6098                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
6099                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6100                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6101                  }                  }
6102                                    
# Line 3867  sub _tree_construction_main ($) { Line 6109  sub _tree_construction_main ($) {
6109                my $i;                my $i;
6110                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6111                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6112                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6113                      !!!cp ('t254');
6114                    $i = $_;                    $i = $_;
6115                    last INSCOPE;                    last INSCOPE;
6116                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6117                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6118                    last INSCOPE;                    last INSCOPE;
6119                  }                  }
6120                } # INSCOPE                } # INSCOPE
6121                unless (defined $i) {                unless (defined $i) {
6122                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
6123                    !!!parse-error (type => 'unmatched end tag',
6124                                    text => $token->{tag_name}, token => $token);
6125                  ## Ignore the token                  ## Ignore the token
6126                    !!!nack ('t256.1');
6127                  !!!next-token;                  !!!next-token;
6128                  redo B;                  next B;
6129                }                }
6130    
6131                ## Clear back to table body context                ## Clear back to table body context
6132                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6133                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6134                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6135                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6136                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6137                }                }
6138    
6139                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6140                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6141                  !!!nack ('t257.1');
6142                !!!next-token;                !!!next-token;
6143                redo B;                next B;
6144              } elsif ({              } elsif ({
6145                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6146                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6147                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6148                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6149                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6150                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6151                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6152                !!!next-token;                            text => $token->{tag_name}, token => $token);
6153                redo B;            ## Ignore the token
6154          } else {            !!!nack ('t258.1');
6155            !!!parse-error (type => 'in table:/'.$token->{tag_name});             !!!next-token;
6156              next B;
6157            } else {
6158              !!!cp ('t259');
6159              !!!parse-error (type => 'in table:/',
6160                              text => $token->{tag_name}, token => $token);
6161    
6162            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6163            #            #
6164          }          }
6165          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6166            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6167                    @{$self->{open_elements}} == 1) { # redundant, maybe
6168              !!!parse-error (type => 'in body:#eof', token => $token);
6169              !!!cp ('t259.1');
6170              #
6171            } else {
6172              !!!cp ('t259.2');
6173              #
6174            }
6175    
6176            ## Stop parsing
6177            last B;
6178        } else {        } else {
6179          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6180        }        }
# Line 3919  sub _tree_construction_main ($) { Line 6183  sub _tree_construction_main ($) {
6183              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6184                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6185                unless (length $token->{data}) {                unless (length $token->{data}) {
6186                    !!!cp ('t260');
6187                  !!!next-token;                  !!!next-token;
6188                  redo B;                  next B;
6189                }                }
6190              }              }
6191                            
6192                !!!cp ('t261');
6193              #              #
6194            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6195              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6196                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
6197                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6198                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6199                  !!!ack ('t262.1');
6200                !!!next-token;                !!!next-token;
6201                redo B;                next B;
6202              } else {              } else {
6203                  !!!cp ('t263');
6204                #                #
6205              }              }
6206            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6207              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6208                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6209                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
6210                    !!!parse-error (type => 'unmatched end tag',
6211                                    text => 'colgroup', token => $token);
6212                  ## Ignore the token                  ## Ignore the token
6213                  !!!next-token;                  !!!next-token;
6214                  redo B;                  next B;
6215                } else {                } else {
6216                    !!!cp ('t265');
6217                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6218                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6219                  !!!next-token;                  !!!next-token;
6220                  redo B;                              next B;            
6221                }                }
6222              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6223                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
6224                  !!!parse-error (type => 'unmatched end tag',
6225                                  text => 'col', token => $token);
6226                ## Ignore the token                ## Ignore the token
6227                !!!next-token;                !!!next-token;
6228                redo B;                next B;
6229              } else {              } else {
6230                  !!!cp ('t267');
6231                #                #
6232              }              }
6233            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6234              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6235            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6236              !!!cp ('t270.2');
6237              ## Stop parsing.
6238              last B;
6239            } else {
6240              ## NOTE: As if </colgroup>.
6241              !!!cp ('t270.1');
6242              pop @{$self->{open_elements}}; # colgroup
6243              $self->{insertion_mode} = IN_TABLE_IM;
6244              ## Reprocess.
6245              next B;
6246            }
6247          } else {
6248            die "$0: $token->{type}: Unknown token type";
6249          }
6250    
6251            ## As if </colgroup>            ## As if </colgroup>
6252            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6253              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
6254    ## TODO: Wrong error type?
6255                !!!parse-error (type => 'unmatched end tag',
6256                                text => 'colgroup', token => $token);
6257              ## Ignore the token              ## Ignore the token
6258                !!!nack ('t269.1');
6259              !!!next-token;              !!!next-token;
6260              redo B;              next B;
6261            } else {            } else {
6262                !!!cp ('t270');
6263              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6264              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6265                !!!ack-later;
6266              ## reprocess              ## reprocess
6267              redo B;              next B;
6268            }            }
6269      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6270        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6271            !!!cp ('t271');
6272          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6273          !!!next-token;          !!!next-token;
6274          redo B;          next B;
6275        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6276              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6277                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6278                  ## As if </option>              !!!cp ('t272');
6279                  pop @{$self->{open_elements}};              ## As if </option>
6280                }              pop @{$self->{open_elements}};
6281              } else {
6282                !!!cp ('t273');
6283              }
6284    
6285                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6286                !!!next-token;            !!!nack ('t273.1');
6287                redo B;            !!!next-token;
6288              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6289                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6290                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6291                  pop @{$self->{open_elements}};              !!!cp ('t274');
6292                }              ## As if </option>
6293                pop @{$self->{open_elements}};
6294              } else {
6295                !!!cp ('t275');
6296              }
6297    
6298                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6299                  ## As if </optgroup>              !!!cp ('t276');
6300                  pop @{$self->{open_elements}};              ## As if </optgroup>
6301                }              pop @{$self->{open_elements}};
6302              } else {
6303                !!!cp ('t277');
6304              }
6305    
6306                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6307                !!!next-token;            !!!nack ('t277.1');
6308                redo B;            !!!next-token;
6309              } elsif ($token->{tag_name} eq 'select') {            next B;
6310                !!!parse-error (type => 'not closed:select');          } elsif ({
6311                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6312                ## have an element in table scope                   }->{$token->{tag_name}} or
6313                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6314                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6315                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6316                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6317                    $i = $_;                     tr => 1, td => 1, th => 1,
6318                    last INSCOPE;                    }->{$token->{tag_name}})) {
6319                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6320                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6321                           }->{$node->[1]}) {                            token => $token);
6322                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6323                  }            ## as if there were </select> (otherwise).
6324                } # INSCOPE            ## have an element in table scope
6325                unless (defined $i) {            my $i;
6326                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6327                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6328                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6329                  redo B;                !!!cp ('t278');
6330                }                $i = $_;
6331                  last INSCOPE;
6332                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6333                  !!!cp ('t279');
6334                  last INSCOPE;
6335                }
6336              } # INSCOPE
6337              unless (defined $i) {
6338                !!!cp ('t280');
6339                !!!parse-error (type => 'unmatched end tag',
6340                                text => 'select', token => $token);
6341                ## Ignore the token
6342                !!!nack ('t280.1');
6343                !!!next-token;
6344                next B;
6345              }
6346                                
6347                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6348              splice @{$self->{open_elements}}, $i;
6349    
6350                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6351    
6352                !!!next-token;            if ($token->{tag_name} eq 'select') {
6353                redo B;              !!!nack ('t281.2');
6354                !!!next-token;
6355                next B;
6356              } else {
6357                !!!cp ('t281.1');
6358                !!!ack-later;
6359                ## Reprocess the token.
6360                next B;
6361              }
6362          } else {          } else {
6363            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
6364              !!!parse-error (type => 'in select',
6365                              text => $token->{tag_name}, token => $token);
6366            ## Ignore the token            ## Ignore the token
6367              !!!nack ('t282.1');
6368            !!!next-token;            !!!next-token;
6369            redo B;            next B;
6370          }          }
6371        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6372              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6373                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6374                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6375                  ## As if </option>              !!!cp ('t283');
6376                  splice @{$self->{open_elements}}, -2;              ## As if </option>
6377                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
6378                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6379                } else {              !!!cp ('t284');
6380                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
6381                  ## Ignore the token            } else {
6382                }              !!!cp ('t285');
6383                !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6384                redo B;                              text => $token->{tag_name}, token => $token);
6385              } elsif ($token->{tag_name} eq 'option') {              ## Ignore the token
6386                if ($self->{open_elements}->[-1]->[1] eq 'option') {            }
6387                  pop @{$self->{open_elements}};            !!!nack ('t285.1');
6388                } else {            !!!next-token;
6389                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            next B;
6390                  ## Ignore the token          } elsif ($token->{tag_name} eq 'option') {
6391                }            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6392                !!!next-token;              !!!cp ('t286');
6393                redo B;              pop @{$self->{open_elements}};
6394              } elsif ($token->{tag_name} eq 'select') {            } else {
6395                ## have an element in table scope              !!!cp ('t287');
6396                my $i;              !!!parse-error (type => 'unmatched end tag',
6397                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                              text => $token->{tag_name}, token => $token);
6398                  my $node = $self->{open_elements}->[$_];              ## Ignore the token
6399                  if ($node->[1] eq $token->{tag_name}) {            }
6400                    $i = $_;            !!!nack ('t287.1');
6401                    last INSCOPE;            !!!next-token;
6402                  } elsif ({            next B;
6403                            table => 1, html => 1,          } elsif ($token->{tag_name} eq 'select') {
6404                           }->{$node->[1]}) {            ## have an element in table scope
6405                    last INSCOPE;            my $i;
6406                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6407                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6408                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6409                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t288');
6410                  ## Ignore the token                $i = $_;
6411                  !!!next-token;                last INSCOPE;
6412                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6413                }                !!!cp ('t289');
6414                  last INSCOPE;
6415                }
6416              } # INSCOPE
6417              unless (defined $i) {
6418                !!!cp ('t290');
6419                !!!parse-error (type => 'unmatched end tag',
6420                                text => $token->{tag_name}, token => $token);
6421                ## Ignore the token
6422                !!!nack ('t290.1');
6423                !!!next-token;
6424                next B;
6425              }
6426                                
6427                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6428              splice @{$self->{open_elements}}, $i;
6429    
6430                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6431    
6432                !!!next-token;            !!!nack ('t291.1');
6433                redo B;            !!!next-token;
6434              } elsif ({            next B;
6435                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6436                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6437                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6438                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6439                     }->{$token->{tag_name}}) {
6440    ## TODO: The following is wrong?
6441              !!!parse-error (type => 'unmatched end tag',
6442                              text => $token->{tag_name}, token => $token);
6443                                
6444                ## have an element in table scope            ## have an element in table scope
6445                my $i;            my $i;
6446                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6447                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6448                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6449                    $i = $_;                !!!cp ('t292');
6450                    last INSCOPE;                $i = $_;
6451                  } elsif ({                last INSCOPE;
6452                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6453                           }->{$node->[1]}) {                !!!cp ('t293');
6454                    last INSCOPE;                last INSCOPE;
6455                  }              }
6456                } # INSCOPE            } # INSCOPE
6457                unless (defined $i) {            unless (defined $i) {
6458                  ## Ignore the token              !!!cp ('t294');
6459                  !!!next-token;              ## Ignore the token
6460                  redo B;              !!!nack ('t294.1');
6461                }              !!!next-token;
6462                next B;
6463              }
6464                                
6465                ## As if </select>            ## As if </select>
6466                ## have an element in table scope            ## have an element in table scope
6467                undef $i;            undef $i;
6468                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6469                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6470                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6471                    $i = $_;                !!!cp ('t295');
6472                    last INSCOPE;                $i = $_;
6473                  } elsif ({                last INSCOPE;
6474                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6475                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
6476                    last INSCOPE;                !!!cp ('t296');
6477                  }                last INSCOPE;
6478                } # INSCOPE              }
6479                unless (defined $i) {            } # INSCOPE
6480                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
6481                  ## Ignore the </select> token              !!!cp ('t297');
6482                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
6483                  redo B;              !!!parse-error (type => 'unmatched end tag',
6484                }                              text => 'select', token => $token);
6485                ## Ignore the </select> token
6486                !!!nack ('t297.1');
6487                !!!next-token; ## TODO: ok?
6488                next B;
6489              }
6490                                
6491                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
6492              splice @{$self->{open_elements}}, $i;
6493    
6494                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6495    
6496                ## reprocess            !!!ack-later;
6497                redo B;            ## reprocess
6498              next B;
6499          } else {          } else {
6500            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
6501              !!!parse-error (type => 'in select:/',
6502                              text => $token->{tag_name}, token => $token);
6503            ## Ignore the token            ## Ignore the token
6504              !!!nack ('t299.3');
6505            !!!next-token;            !!!next-token;
6506            redo B;            next B;
6507            }
6508          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6509            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6510                    @{$self->{open_elements}} == 1) { # redundant, maybe
6511              !!!cp ('t299.1');
6512              !!!parse-error (type => 'in body:#eof', token => $token);
6513            } else {
6514              !!!cp ('t299.2');
6515          }          }
6516    
6517            ## Stop parsing.
6518            last B;
6519        } else {        } else {
6520          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6521        }        }
# Line 4156  sub _tree_construction_main ($) { Line 6529  sub _tree_construction_main ($) {
6529            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6530                        
6531            unless (length $token->{data}) {            unless (length $token->{data}) {
6532                !!!cp ('t300');
6533              !!!next-token;              !!!next-token;
6534              redo B;              next B;
6535            }            }
6536          }          }
6537                    
6538          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6539            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6540              !!!parse-error (type => 'after html:#text', token => $token);
6541    
6542            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6543            } else {
6544              !!!cp ('t302');
6545          }          }
6546                    
6547          ## "after body" insertion mode          ## "after body" insertion mode
6548          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6549    
6550          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6551          ## reprocess          ## reprocess
6552          redo B;          next B;
6553        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6554          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6555            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6556              !!!parse-error (type => 'after html',
6557                              text => $token->{tag_name}, token => $token);
6558                        
6559            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6560            } else {
6561              !!!cp ('t304');
6562          }          }
6563    
6564          ## "after body" insertion mode          ## "after body" insertion mode
6565          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6566                            text => $token->{tag_name}, token => $token);
6567    
6568          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6569            !!!ack-later;
6570          ## reprocess          ## reprocess
6571          redo B;          next B;
6572        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6573          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6574            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6575              !!!parse-error (type => 'after html:/',
6576                              text => $token->{tag_name}, token => $token);
6577                        
6578            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6579            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6580            } else {
6581              !!!cp ('t306');
6582          }          }
6583    
6584          ## "after body" insertion mode          ## "after body" insertion mode
6585          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6586            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6587              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6588                !!!parse-error (type => 'unmatched end tag',
6589                                text => 'html', token => $token);
6590              ## Ignore the token              ## Ignore the token
6591              !!!next-token;              !!!next-token;
6592              redo B;              next B;
6593            } else {            } else {
6594                !!!cp ('t308');
6595              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6596              !!!next-token;              !!!next-token;
6597              redo B;              next B;
6598            }            }
6599          } else {          } else {
6600            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6601              !!!parse-error (type => 'after body:/',
6602                              text => $token->{tag_name}, token => $token);
6603    
6604            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6605            ## reprocess            ## reprocess
6606            redo B;            next B;
6607          }          }
6608          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6609            !!!cp ('t309.2');
6610            ## Stop parsing
6611            last B;
6612        } else {        } else {
6613          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6614        }        }
# Line 4222  sub _tree_construction_main ($) { Line 6618  sub _tree_construction_main ($) {
6618            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6619                        
6620            unless (length $token->{data}) {            unless (length $token->{data}) {
6621                !!!cp ('t310');
6622              !!!next-token;              !!!next-token;
6623              redo B;              next B;
6624            }            }
6625          }          }
6626                    
6627          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6628            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6629              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6630                !!!parse-error (type => 'in frameset:#text', token => $token);
6631            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6632              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6633            } else { # "after html frameset"              !!!parse-error (type => 'after frameset:#text', token => $token);
6634              !!!parse-error (type => 'after html:#character');            } else { # "after after frameset"
6635                !!!cp ('t313');
6636              $self->{insertion_mode} = AFTER_FRAMESET_IM;              !!!parse-error (type => 'after html:#text', token => $token);
             ## Reprocess in the "main" phase, "after frameset"...  
             !!!parse-error (type => 'after frameset:#character');  
6637            }            }
6638                        
6639            ## Ignore the token.            ## Ignore the token.
6640            if (length $token->{data}) {            if (length $token->{data}) {
6641                !!!cp ('t314');
6642              ## reprocess the rest of characters              ## reprocess the rest of characters
6643            } else {            } else {
6644                !!!cp ('t315');
6645              !!!next-token;              !!!next-token;
6646            }            }
6647            redo B;            next B;
6648          }          }
6649                    
6650          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6651        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!parse-error (type => 'after html:'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "main" phase, "after frameset" insertion mode...  
         }  
   
6652          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6653              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6654            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6655              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6656              !!!nack ('t318.1');
6657            !!!next-token;            !!!next-token;
6658            redo B;            next B;
6659          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6660                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6661            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6662              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6663            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6664              !!!ack ('t319.1');
6665            !!!next-token;            !!!next-token;
6666            redo B;            next B;
6667          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6668            ## NOTE: As if in body.            !!!cp ('t320');
6669            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6670            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6671              next B;
6672    
6673              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6674              ## has no parse error.
6675          } else {          } else {
6676            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6677              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6678            } else {              !!!parse-error (type => 'in frameset',
6679              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6680              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6681                !!!cp ('t322');
6682                !!!parse-error (type => 'after frameset',
6683                                text => $token->{tag_name}, token => $token);
6684              } else { # "after after frameset"
6685                !!!cp ('t322.2');
6686                !!!parse-error (type => 'after after frameset',
6687                                text => $token->{tag_name}, token => $token);
6688            }            }
6689            ## Ignore the token            ## Ignore the token
6690              !!!nack ('t322.1');
6691            !!!next-token;            !!!next-token;
6692            redo B;            next B;
6693          }          }
6694        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!parse-error (type => 'after html:/'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "main" phase, "after frameset" insertion mode...  
         }  
   
6695          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6696              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6697            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6698                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6699              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6700                !!!parse-error (type => 'unmatched end tag',
6701                                text => $token->{tag_name}, token => $token);
6702              ## Ignore the token              ## Ignore the token
6703              !!!next-token;              !!!next-token;
6704            } else {            } else {
6705                !!!cp ('t326');
6706              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6707              !!!next-token;              !!!next-token;
6708            }            }
6709    
6710            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6711                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6712                !!!cp ('t327');
6713              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6714              } else {
6715                !!!cp ('t328');
6716            }            }
6717            redo B;            next B;
6718          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6719                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6720              !!!cp ('t329');
6721            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6722            !!!next-token;            !!!next-token;
6723            redo B;            next B;
6724          } else {          } else {
6725            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6726              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6727            } else {              !!!parse-error (type => 'in frameset:/',
6728              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6729              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6730                !!!cp ('t330.1');
6731                !!!parse-error (type => 'after frameset:/',
6732                                text => $token->{tag_name}, token => $token);
6733              } else { # "after after html"
6734                !!!cp ('t331');
6735                !!!parse-error (type => 'after after frameset:/',
6736                                text => $token->{tag_name}, token => $token);
6737            }            }
6738            ## Ignore the token            ## Ignore the token
6739            !!!next-token;            !!!next-token;
6740            redo B;            next B;
6741            }
6742          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6743            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6744                    @{$self->{open_elements}} == 1) { # redundant, maybe
6745              !!!cp ('t331.1');
6746              !!!parse-error (type => 'in body:#eof', token => $token);
6747            } else {
6748              !!!cp ('t331.2');
6749          }          }
6750            
6751            ## Stop parsing
6752            last B;
6753        } else {        } else {
6754          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6755        }        }
# Line 4335  sub _tree_construction_main ($) { Line 6762  sub _tree_construction_main ($) {
6762      ## "in body" insertion mode      ## "in body" insertion mode
6763      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6764        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6765            !!!cp ('t332');
6766          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6767          $script_start_tag->($insert);          $script_start_tag->();
6768          redo B;          next B;
6769        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6770            !!!cp ('t333');
6771          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6772          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6773          redo B;          next B;
6774        } elsif ({        } elsif ({
6775                  base => 1, link => 1,                  base => 1, link => 1,
6776                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6777            !!!cp ('t334');
6778          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6779          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6780          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6781            !!!ack ('t334.1');
6782          !!!next-token;          !!!next-token;
6783          redo B;          next B;
6784        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6785          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6786          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6787          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6788    
6789          unless ($self->{confident}) {          unless ($self->{confident}) {
6790            my $charset;            if ($token->{attributes}->{charset}) {
6791            if ($token->{attributes}->{charset}) { ## TODO: And if supported              !!!cp ('t335');
6792              $charset = $token->{attributes}->{charset}->{value};              ## NOTE: Whether the encoding is supported or not is handled
6793            }              ## in the {change_encoding} callback.
6794            if ($token->{attributes}->{'http-equiv'}) {              $self->{change_encoding}
6795              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6796              if ($token->{attributes}->{'http-equiv'}->{value}              
6797                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6798                    ->set_user_data (manakai_has_reference =>
6799                                         $token->{attributes}->{charset}
6800                                             ->{has_reference});
6801              } elsif ($token->{attributes}->{content}) {
6802                if ($token->{attributes}->{content}->{value}
6803                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6804                        [\x09-\x0D\x20]*=
6805                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6806                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6807                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
6808              } ## TODO: And if supported                ## NOTE: Whether the encoding is supported or not is handled
6809                  ## in the {change_encoding} callback.
6810                  $self->{change_encoding}
6811                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6812                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6813                      ->set_user_data (manakai_has_reference =>
6814                                           $token->{attributes}->{content}
6815                                                 ->{has_reference});
6816                }
6817              }
6818            } else {
6819              if ($token->{attributes}->{charset}) {
6820                !!!cp ('t337');
6821                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6822                    ->set_user_data (manakai_has_reference =>
6823                                         $token->{attributes}->{charset}
6824                                             ->{has_reference});
6825              }
6826              if ($token->{attributes}->{content}) {
6827                !!!cp ('t338');
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            }            }
           ## TODO: Change the encoding  
6833          }          }
6834    
6835            !!!ack ('t338.1');
6836          !!!next-token;          !!!next-token;
6837          redo B;          next B;
6838        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6839          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6840          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6841          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6842            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6843        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6844          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6845                                
6846          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6847              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6848              !!!cp ('t342');
6849            ## Ignore the token            ## Ignore the token
6850          } else {          } else {
6851            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6852            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6853              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6854                  !!!cp ('t343');
6855                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6856                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6857                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6858              }              }
6859            }            }
6860          }          }
6861            !!!nack ('t343.1');
6862          !!!next-token;          !!!next-token;
6863          redo B;          next B;
6864        } elsif ({        } elsif ({
6865                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6866                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6867                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6868                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6869                  pre => 1,                  pre => 1, listing => 1,
6870                    form => 1,
6871                    table => 1,
6872                    hr => 1,
6873                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6874            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6875              !!!cp ('t350');
6876              !!!parse-error (type => 'in form:form', token => $token);
6877              ## Ignore the token
6878              !!!nack ('t350.1');
6879              !!!next-token;
6880              next B;
6881            }
6882    
6883          ## has a p element in scope          ## has a p element in scope
6884          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6885            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6886              !!!back-token;              !!!cp ('t344');
6887              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6888              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6889            } elsif ({                        line => $token->{line}, column => $token->{column}};
6890                      table => 1, caption => 1, td => 1, th => 1,              next B;
6891                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6892                     }->{$_->[1]}) {              !!!cp ('t345');
6893              last INSCOPE;              last INSCOPE;
6894            }            }
6895          } # INSCOPE          } # INSCOPE
6896                        
6897          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6898          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6899              !!!nack ('t346.1');
6900            !!!next-token;            !!!next-token;
6901            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6902              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6903              unless (length $token->{data}) {              unless (length $token->{data}) {
6904                  !!!cp ('t346');
6905                !!!next-token;                !!!next-token;
6906                } else {
6907                  !!!cp ('t349');
6908              }              }
6909              } else {
6910                !!!cp ('t348');
6911            }            }
6912          } else {          } elsif ($token->{tag_name} eq 'form') {
6913              !!!cp ('t347.1');
6914              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6915    
6916              !!!nack ('t347.2');
6917            !!!next-token;            !!!next-token;
6918          }          } elsif ($token->{tag_name} eq 'table') {
6919          redo B;            !!!cp ('t382');
6920        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6921          if (defined $self->{form_element}) {            
6922            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6923            ## Ignore the token  
6924              !!!nack ('t382.1');
6925              !!!next-token;
6926            } elsif ($token->{tag_name} eq 'hr') {
6927              !!!cp ('t386');
6928              pop @{$self->{open_elements}};
6929            
6930              !!!nack ('t386.1');
6931            !!!next-token;            !!!next-token;
           redo B;  
6932          } else {          } else {
6933            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
               redo B;  
             } 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];  
6934            !!!next-token;            !!!next-token;
           redo B;  
6935          }          }
6936        } elsif ($token->{tag_name} eq 'li') {          next B;
6937          ## has a p element in scope        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             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;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
6938          ## has a p element in scope          ## has a p element in scope
6939          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6940            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6941              !!!back-token;              !!!cp ('t353');
6942              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6943              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6944            } elsif ({                        line => $token->{line}, column => $token->{column}};
6945                      table => 1, caption => 1, td => 1, th => 1,              next B;
6946                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6947                     }->{$_->[1]}) {              !!!cp ('t354');
6948              last INSCOPE;              last INSCOPE;
6949            }            }
6950          } # INSCOPE          } # INSCOPE
# Line 4527  sub _tree_construction_main ($) { Line 6952  sub _tree_construction_main ($) {
6952          ## Step 1          ## Step 1
6953          my $i = -1;          my $i = -1;
6954          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6955            my $li_or_dtdd = {li => {li => 1},
6956                              dt => {dt => 1, dd => 1},
6957                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6958          LI: {          LI: {
6959            ## Step 2            ## Step 2
6960            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6961              if ($i != -1) {              if ($i != -1) {
6962                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6963                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6964                                  text => $self->{open_elements}->[-1]->[0]
6965                                      ->manakai_local_name,
6966                                  token => $token);
6967                } else {
6968                  !!!cp ('t356');
6969              }              }
6970              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6971              last LI;              last LI;
6972              } else {
6973                !!!cp ('t357');
6974            }            }
6975                        
6976            ## Step 3            ## Step 3
6977            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6978                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6979                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6980                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6981                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6982                  not ($node->[1] & DIV_EL)) {
6983                !!!cp ('t358');
6984              last LI;              last LI;
6985            }            }
6986                        
6987              !!!cp ('t359');
6988            ## Step 4            ## Step 4
6989            $i--;            $i--;
6990            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6991            redo LI;            redo LI;
6992          } # LI          } # LI
6993                        
6994          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6995            !!!nack ('t359.1');
6996          !!!next-token;          !!!next-token;
6997          redo B;          next B;
6998        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6999          ## has a p element in scope          ## has a p element in scope
7000          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
7001            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
7002              !!!back-token;              !!!cp ('t367');
7003              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
7004              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
7005            } elsif ({                        line => $token->{line}, column => $token->{column}};
7006                      table => 1, caption => 1, td => 1, th => 1,              next B;
7007                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
7008                     }->{$_->[1]}) {              !!!cp ('t368');
7009              last INSCOPE;              last INSCOPE;
7010            }            }
7011          } # INSCOPE          } # INSCOPE
7012                        
7013          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7014                        
7015          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7016                        
7017            !!!nack ('t368.1');
7018          !!!next-token;          !!!next-token;
7019          redo B;          next B;
       } 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_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
7020        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
7021          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7022            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
7023            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
7024              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
7025                !!!parse-error (type => 'in a:a', token => $token);
7026                            
7027              !!!back-token;              !!!back-token; # <a>
7028              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
7029              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
7030                $formatting_end_tag->($token);
7031                            
7032              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
7033                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7034                    !!!cp ('t372');
7035                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
7036                  last AFE2;                  last AFE2;
7037                }                }
7038              } # AFE2              } # AFE2
7039              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
7040                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7041                    !!!cp ('t373');
7042                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
7043                  last OE;                  last OE;
7044                }                }
7045              } # OE              } # OE
7046              last AFE;              last AFE;
7047            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
7048                !!!cp ('t374');
7049              last AFE;              last AFE;
7050            }            }
7051          } # AFE          } # AFE
7052                        
7053          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7054    
7055          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7056          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7057    
7058            !!!nack ('t374.1');
7059          !!!next-token;          !!!next-token;
7060          redo B;          next B;
       } 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;  
         redo B;  
7061        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
7062          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7063    
7064          ## has a |nobr| element in scope          ## has a |nobr| element in scope
7065          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7066            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7067            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
7068              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
7069              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
7070              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
7071              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7072            } elsif ({                        line => $token->{line}, column => $token->{column}};
7073                      table => 1, caption => 1, td => 1, th => 1,              next B;
7074                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
7075                     }->{$node->[1]}) {              !!!cp ('t377');
7076              last INSCOPE;              last INSCOPE;
7077            }            }
7078          } # INSCOPE          } # INSCOPE
7079                    
7080          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7081          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7082                    
7083            !!!nack ('t377.1');
7084          !!!next-token;          !!!next-token;
7085          redo B;          next B;
7086        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7087          ## has a button element in scope          ## has a button element in scope
7088          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7089            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7090            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7091              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
7092              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
7093              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
7094              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7095            } elsif ({                        line => $token->{line}, column => $token->{column}};
7096                      table => 1, caption => 1, td => 1, th => 1,              next B;
7097                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
7098                     }->{$node->[1]}) {              !!!cp ('t379');
7099              last INSCOPE;              last INSCOPE;
7100            }            }
7101          } # INSCOPE          } # INSCOPE
7102                        
7103          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7104                        
7105          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7106          push @$active_formatting_elements, ['#marker', ''];  
7107            ## TODO: associate with $self->{form_element} if defined
7108    
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
7109          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7110            
7111          !!!next-token;          !!!nack ('t379.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } 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_TOKEN, tag_name => 'p'};  
             redo B;  
           } 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_IM;  
             
7112          !!!next-token;          !!!next-token;
7113          redo B;          next B;
7114        } elsif ({        } elsif ({
7115                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7116                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7117                  image => 1,                  noembed => 1,
7118                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7119                    noscript => 0, ## TODO: 1 if scripting is enabled
7120                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7121          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7122            !!!parse-error (type => 'image');            !!!cp ('t381');
7123            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
7124            } else {
7125              !!!cp ('t399');
7126          }          }
7127            ## NOTE: There is an "as if in body" code clone.
7128          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7129          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } 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_TOKEN, tag_name => 'p'};  
             redo B;  
           } 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;  
         redo B;  
       } 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;  
         redo B;  
7130        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7131          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7132                    
7133          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7134              !!!cp ('t389');
7135            ## Ignore the token            ## Ignore the token
7136              !!!nack ('t389'); ## NOTE: Not acknowledged.
7137            !!!next-token;            !!!next-token;
7138            redo B;            next B;
7139          } else {          } else {
7140              !!!ack ('t391.1');
7141    
7142            my $at = $token->{attributes};            my $at = $token->{attributes};
7143            my $form_attrs;            my $form_attrs;
7144            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 4815  sub _tree_construction_main ($) { Line 7148  sub _tree_construction_main ($) {
7148            delete $at->{prompt};            delete $at->{prompt};
7149            my @tokens = (            my @tokens = (
7150                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7151                           attributes => $form_attrs},                           attributes => $form_attrs,
7152                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7153                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7154                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7155                            {type => START_TAG_TOKEN, tag_name => 'p',
7156                             line => $token->{line}, column => $token->{column}},
7157                            {type => START_TAG_TOKEN, tag_name => 'label',
7158                             line => $token->{line}, column => $token->{column}},
7159                         );                         );
7160            if ($prompt_attr) {            if ($prompt_attr) {
7161              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
7162                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7163                               #line => $token->{line}, column => $token->{column},
7164                              };
7165            } else {            } else {
7166                !!!cp ('t391');
7167              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7168                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7169                               #line => $token->{line}, column => $token->{column},
7170                              }; # SHOULD
7171              ## TODO: make this configurable              ## TODO: make this configurable
7172            }            }
7173            push @tokens,            push @tokens,
7174                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7175                             line => $token->{line}, column => $token->{column}},
7176                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7177                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7178                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7179                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7180                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7181            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7182                             line => $token->{line}, column => $token->{column}},
7183                            {type => END_TAG_TOKEN, tag_name => 'form',
7184                             line => $token->{line}, column => $token->{column}};
7185            !!!back-token (@tokens);            !!!back-token (@tokens);
7186            redo B;            !!!next-token;
7187              next B;
7188          }          }
7189        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7190          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7191          my $el;          my $el;
7192          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7193                    
7194          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7195          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4850  sub _tree_construction_main ($) { Line 7198  sub _tree_construction_main ($) {
7198          $insert->($el);          $insert->($el);
7199                    
7200          my $text = '';          my $text = '';
7201            !!!nack ('t392.1');
7202          !!!next-token;          !!!next-token;
7203          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7204            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
7205            unless (length $token->{data}) {            unless (length $token->{data}) {
7206                !!!cp ('t392');
7207              !!!next-token;              !!!next-token;
7208              } else {
7209                !!!cp ('t393');
7210            }            }
7211            } else {
7212              !!!cp ('t394');
7213          }          }
7214          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
7215              !!!cp ('t395');
7216            $text .= $token->{data};            $text .= $token->{data};
7217            !!!next-token;            !!!next-token;
7218          }          }
7219          if (length $text) {          if (length $text) {
7220              !!!cp ('t396');
7221            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
7222          }          }
7223                    
# Line 4869  sub _tree_construction_main ($) { Line 7225  sub _tree_construction_main ($) {
7225                    
7226          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
7227              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
7228              !!!cp ('t397');
7229            ## Ignore the token            ## Ignore the token
7230          } else {          } else {
7231            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
7232              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7233          }          }
7234          !!!next-token;          !!!next-token;
7235            next B;
7236          } elsif ($token->{tag_name} eq 'rt' or
7237                   $token->{tag_name} eq 'rp') {
7238            ## has a |ruby| element in scope
7239            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7240              my $node = $self->{open_elements}->[$_];
7241              if ($node->[1] & RUBY_EL) {
7242                !!!cp ('t398.1');
7243                ## generate implied end tags
7244                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7245                  !!!cp ('t398.2');
7246                  pop @{$self->{open_elements}};
7247                }
7248                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7249                  !!!cp ('t398.3');
7250                  !!!parse-error (type => 'not closed',
7251                                  text => $self->{open_elements}->[-1]->[0]
7252                                      ->manakai_local_name,
7253                                  token => $token);
7254                  pop @{$self->{open_elements}}
7255                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7256                }
7257                last INSCOPE;
7258              } elsif ($node->[1] & SCOPING_EL) {
7259                !!!cp ('t398.4');
7260                last INSCOPE;
7261              }
7262            } # INSCOPE
7263    
7264            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7265    
7266            !!!nack ('t398.5');
7267            !!!next-token;
7268          redo B;          redo B;
7269        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7270                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
7271          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7272    
7273            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7274    
7275            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7276    
7277            ## "adjust foreign attributes" - done in insert-element-f
7278                    
7279          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7280                    
7281          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
7282              pop @{$self->{open_elements}};
7283              !!!ack ('t398.1');
7284            } else {
7285              !!!cp ('t398.2');
7286              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7287              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7288              ## mode, "in body" (not "in foreign content") secondary insertion
7289              ## mode, maybe.
7290            }
7291    
7292          !!!next-token;          !!!next-token;
7293          redo B;          next B;
7294        } elsif ({        } elsif ({
7295                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7296                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
7297                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
7298                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7299                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7300          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
7301            !!!parse-error (type => 'in body',
7302                            text => $token->{tag_name}, token => $token);
7303          ## Ignore the token          ## Ignore the token
7304            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7305          !!!next-token;          !!!next-token;
7306          redo B;          next B;
7307                    
7308          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7309        } else {        } else {
7310            if ($token->{tag_name} eq 'image') {
7311              !!!cp ('t384');
7312              !!!parse-error (type => 'image', token => $token);
7313              $token->{tag_name} = 'img';
7314            } else {
7315              !!!cp ('t385');
7316            }
7317    
7318            ## NOTE: There is an "as if <br>" code clone.
7319          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7320                    
7321          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7322    
7323            if ({
7324                 applet => 1, marquee => 1, object => 1,
7325                }->{$token->{tag_name}}) {
7326              !!!cp ('t380');
7327              push @$active_formatting_elements, ['#marker', ''];
7328              !!!nack ('t380.1');
7329            } elsif ({
7330                      b => 1, big => 1, em => 1, font => 1, i => 1,
7331                      s => 1, small => 1, strile => 1,
7332                      strong => 1, tt => 1, u => 1,
7333                     }->{$token->{tag_name}}) {
7334              !!!cp ('t375');
7335              push @$active_formatting_elements, $self->{open_elements}->[-1];
7336              !!!nack ('t375.1');
7337            } elsif ($token->{tag_name} eq 'input') {
7338              !!!cp ('t388');
7339              ## TODO: associate with $self->{form_element} if defined
7340              pop @{$self->{open_elements}};
7341              !!!ack ('t388.2');
7342            } elsif ({
7343                      area => 1, basefont => 1, bgsound => 1, br => 1,
7344                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7345                      #image => 1,
7346                     }->{$token->{tag_name}}) {
7347              !!!cp ('t388.1');
7348              pop @{$self->{open_elements}};
7349              !!!ack ('t388.3');
7350            } elsif ($token->{tag_name} eq 'select') {
7351              ## TODO: associate with $self->{form_element} if defined
7352            
7353              if ($self->{insertion_mode} & TABLE_IMS or
7354                  $self->{insertion_mode} & BODY_TABLE_IMS or
7355                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7356                !!!cp ('t400.1');
7357                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7358              } else {
7359                !!!cp ('t400.2');
7360                $self->{insertion_mode} = IN_SELECT_IM;
7361              }
7362              !!!nack ('t400.3');
7363            } else {
7364              !!!nack ('t402');
7365            }
7366                    
7367          !!!next-token;          !!!next-token;
7368          redo B;          next B;
7369        }        }
7370      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7371        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7372          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7373              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7374            for (@{$self->{open_elements}}) {          INSCOPE: {
7375              unless ({            for (reverse @{$self->{open_elements}}) {
7376                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7377                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7378                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7379                      }->{$_->[1]}) {                last INSCOPE;
7380                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
7381                  !!!cp ('t405.1');
7382                  last;
7383              }              }
7384            }            }
7385    
7386            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7387                              text => $token->{tag_name}, token => $token);
7388              ## NOTE: Ignore the token.
7389            !!!next-token;            !!!next-token;
7390            redo B;            next B;
7391          } else {          } # INSCOPE
7392            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
7393            ## Ignore the token          for (@{$self->{open_elements}}) {
7394            !!!next-token;            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7395            redo B;              !!!cp ('t403');
7396                !!!parse-error (type => 'not closed',
7397                                text => $_->[0]->manakai_local_name,
7398                                token => $token);
7399                last;
7400              } else {
7401                !!!cp ('t404');
7402              }
7403          }          }
7404    
7405            $self->{insertion_mode} = AFTER_BODY_IM;
7406            !!!next-token;
7407            next B;
7408        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7409          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          ## TODO: Update this code.  It seems that the code below is not
7410            ## up-to-date, though it has same effect as speced.
7411            if (@{$self->{open_elements}} > 1 and
7412                $self->{open_elements}->[1]->[1] & BODY_EL) {
7413            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7414            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7415              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
7416                !!!parse-error (type => 'not closed',
7417                                text => $self->{open_elements}->[1]->[0]
7418                                    ->manakai_local_name,
7419                                token => $token);
7420              } else {
7421                !!!cp ('t407');
7422            }            }
7423            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7424            ## reprocess            ## reprocess
7425            redo B;            next B;
7426          } else {          } else {
7427            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
7428              !!!parse-error (type => 'unmatched end tag',
7429                              text => $token->{tag_name}, token => $token);
7430            ## Ignore the token            ## Ignore the token
7431            !!!next-token;            !!!next-token;
7432            redo B;            next B;
7433          }          }
7434        } elsif ({        } elsif ({
7435                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7436                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7437                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7438                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7439                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7440                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7441          ## has an element in scope          ## has an element in scope
7442          my $i;          my $i;
7443          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7444            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7445            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7446              ## generate implied end tags              !!!cp ('t410');
             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_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
7447              $i = $_;              $i = $_;
7448              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7449            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7450                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7451              last INSCOPE;              last INSCOPE;
7452            }            }
7453          } # INSCOPE          } # INSCOPE
7454            
7455          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7456            if (defined $i) {            !!!cp ('t413');
7457              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag',
7458                              text => $token->{tag_name}, token => $token);
7459              ## NOTE: Ignore the token.
7460            } else {
7461              ## Step 1. generate implied end tags
7462              while ({
7463                      ## END_TAG_OPTIONAL_EL
7464                      dd => ($token->{tag_name} ne 'dd'),
7465                      dt => ($token->{tag_name} ne 'dt'),
7466                      li => ($token->{tag_name} ne 'li'),
7467                      p => 1,
7468                      rt => 1,
7469                      rp => 1,
7470                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7471                !!!cp ('t409');
7472                pop @{$self->{open_elements}};
7473              }
7474    
7475              ## Step 2.
7476              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7477                      ne $token->{tag_name}) {
7478                !!!cp ('t412');
7479                !!!parse-error (type => 'not closed',
7480                                text => $self->{open_elements}->[-1]->[0]
7481                                    ->manakai_local_name,
7482                                token => $token);
7483            } else {            } else {
7484              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
7485            }            }
7486          }  
7487                      ## Step 3.
         if (defined $i) {  
7488            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7489          } elsif ($token->{tag_name} eq 'p') {  
7490            ## As if <p>, then reprocess the current token            ## Step 4.
7491            my $el;            $clear_up_to_marker->()
7492            !!!create-element ($el, 'p');                if {
7493            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
7494                  }->{$token->{tag_name}};
7495          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7496          !!!next-token;          !!!next-token;
7497          redo B;          next B;
7498        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7499            undef $self->{form_element};
7500    
7501          ## has an element in scope          ## has an element in scope
7502            my $i;
7503          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7504            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7505            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7506              ## generate implied end tags              !!!cp ('t418');
7507              if ({              $i = $_;
                  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_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
7508              last INSCOPE;              last INSCOPE;
7509            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7510                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7511              last INSCOPE;              last INSCOPE;
7512            }            }
7513          } # INSCOPE          } # INSCOPE
7514            
7515          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7516            pop @{$self->{open_elements}};            !!!cp ('t421');
7517          } else {            !!!parse-error (type => 'unmatched end tag',
7518            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                            text => $token->{tag_name}, token => $token);
7519              ## NOTE: Ignore the token.
7520            } else {
7521              ## Step 1. generate implied end tags
7522              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7523                !!!cp ('t417');
7524                pop @{$self->{open_elements}};
7525              }
7526              
7527              ## Step 2.
7528              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7529                      ne $token->{tag_name}) {
7530                !!!cp ('t417.1');
7531                !!!parse-error (type => 'not closed',
7532                                text => $self->{open_elements}->[-1]->[0]
7533                                    ->manakai_local_name,
7534                                token => $token);
7535              } else {
7536                !!!cp ('t420');
7537              }  
7538              
7539              ## Step 3.
7540              splice @{$self->{open_elements}}, $i;
7541          }          }
7542    
         undef $self->{form_element};  
7543          !!!next-token;          !!!next-token;
7544          redo B;          next B;
7545        } elsif ({        } elsif ({
7546                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7547                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5050  sub _tree_construction_main ($) { Line 7549  sub _tree_construction_main ($) {
7549          my $i;          my $i;
7550          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7551            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7552            if ({            if ($node->[1] & HEADING_EL) {
7553                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,              !!!cp ('t423');
               }->{$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_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
7554              $i = $_;              $i = $_;
7555              last INSCOPE;              last INSCOPE;
7556            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7557                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7558              last INSCOPE;              last INSCOPE;
7559            }            }
7560          } # INSCOPE          } # INSCOPE
7561            
7562          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7563            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
7564              !!!parse-error (type => 'unmatched end tag',
7565                              text => $token->{tag_name}, token => $token);
7566              ## NOTE: Ignore the token.
7567            } else {
7568              ## Step 1. generate implied end tags
7569              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7570                !!!cp ('t422');
7571                pop @{$self->{open_elements}};
7572              }
7573              
7574              ## Step 2.
7575              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7576                      ne $token->{tag_name}) {
7577                !!!cp ('t425');
7578                !!!parse-error (type => 'unmatched end tag',
7579                                text => $token->{tag_name}, token => $token);
7580              } else {
7581                !!!cp ('t426');
7582              }
7583    
7584              ## Step 3.
7585              splice @{$self->{open_elements}}, $i;
7586          }          }
7587                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7588          !!!next-token;          !!!next-token;
7589          redo B;          next B;
7590          } elsif ($token->{tag_name} eq 'p') {
7591            ## has an element in scope
7592            my $i;
7593            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7594              my $node = $self->{open_elements}->[$_];
7595              if ($node->[1] & P_EL) {
7596                !!!cp ('t410.1');
7597                $i = $_;
7598                last INSCOPE;
7599              } elsif ($node->[1] & SCOPING_EL) {
7600                !!!cp ('t411.1');
7601                last INSCOPE;
7602              }
7603            } # INSCOPE
7604    
7605            if (defined $i) {
7606              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7607                      ne $token->{tag_name}) {
7608                !!!cp ('t412.1');
7609                !!!parse-error (type => 'not closed',
7610                                text => $self->{open_elements}->[-1]->[0]
7611                                    ->manakai_local_name,
7612                                token => $token);
7613              } else {
7614                !!!cp ('t414.1');
7615              }
7616    
7617              splice @{$self->{open_elements}}, $i;
7618            } else {
7619              !!!cp ('t413.1');
7620              !!!parse-error (type => 'unmatched end tag',
7621                              text => $token->{tag_name}, token => $token);
7622    
7623              !!!cp ('t415.1');
7624              ## As if <p>, then reprocess the current token
7625              my $el;
7626              !!!create-element ($el, $HTML_NS, 'p',, $token);
7627              $insert->($el);
7628              ## NOTE: Not inserted into |$self->{open_elements}|.
7629            }
7630    
7631            !!!next-token;
7632            next B;
7633        } elsif ({        } elsif ({
7634                  a => 1,                  a => 1,
7635                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7636                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7637                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7638                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7639          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7640          redo B;          $formatting_end_tag->($token);
7641            next B;
7642        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7643          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7644            !!!parse-error (type => 'unmatched end tag',
7645                            text => 'br', token => $token);
7646    
7647          ## As if <br>          ## As if <br>
7648          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7649                    
7650          my $el;          my $el;
7651          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7652          $insert->($el);          $insert->($el);
7653                    
7654          ## Ignore the token.          ## Ignore the token.
7655          !!!next-token;          !!!next-token;
7656          redo B;          next B;
7657        } elsif ({        } elsif ({
7658                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7659                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5114  sub _tree_construction_main ($) { Line 7666  sub _tree_construction_main ($) {
7666                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7667                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7668                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7669          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7670            !!!parse-error (type => 'unmatched end tag',
7671                            text => $token->{tag_name}, token => $token);
7672          ## Ignore the token          ## Ignore the token
7673          !!!next-token;          !!!next-token;
7674          redo B;          next B;
7675                    
7676          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7677                    
# Line 5128  sub _tree_construction_main ($) { Line 7682  sub _tree_construction_main ($) {
7682    
7683          ## Step 2          ## Step 2
7684          S2: {          S2: {
7685            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7686              ## Step 1              ## Step 1
7687              ## generate implied end tags              ## generate implied end tags
7688              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7689                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7690                   td => 1, th => 1, tr => 1,                ## NOTE: |<ruby><rt></ruby>|.
7691                   tbody => 1, tfoot => 1, thead => 1,                ## ISSUE: <ruby><rt></rt> will also take this code path,
7692                  }->{$self->{open_elements}->[-1]->[1]}) {                ## which seems wrong.
7693                !!!back-token;                pop @{$self->{open_elements}};
7694                $token = {type => END_TAG_TOKEN,                $node_i++;
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7695              }              }
7696                    
7697              ## Step 2              ## Step 2
7698              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7699                        ne $token->{tag_name}) {
7700                  !!!cp ('t431');
7701                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7702                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7703                                  text => $self->{open_elements}->[-1]->[0]
7704                                      ->manakai_local_name,
7705                                  token => $token);
7706                } else {
7707                  !!!cp ('t432');
7708              }              }
7709                            
7710              ## Step 3              ## Step 3
7711              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7712    
7713              !!!next-token;              !!!next-token;
7714              last S2;              last S2;
7715            } else {            } else {
7716              ## Step 3              ## Step 3
7717              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7718                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7719                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7720                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7721                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7722                  !!!parse-error (type => 'unmatched end tag',
7723                                  text => $token->{tag_name}, token => $token);
7724                ## Ignore the token                ## Ignore the token
7725                !!!next-token;                !!!next-token;
7726                last S2;                last S2;
7727              }              }
7728    
7729                !!!cp ('t434');
7730            }            }
7731                        
7732            ## Step 4            ## Step 4
# Line 5173  sub _tree_construction_main ($) { Line 7736  sub _tree_construction_main ($) {
7736            ## Step 5;            ## Step 5;
7737            redo S2;            redo S2;
7738          } # S2          } # S2
7739          redo B;          next B;
7740        }        }
7741      }      }
7742      redo B;      next B;
7743      } continue { # B
7744        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7745          ## NOTE: The code below is executed in cases where it does not have
7746          ## to be, but it it is harmless even in those cases.
7747          ## has an element in scope
7748          INSCOPE: {
7749            for (reverse 0..$#{$self->{open_elements}}) {
7750              my $node = $self->{open_elements}->[$_];
7751              if ($node->[1] & FOREIGN_EL) {
7752                last INSCOPE;
7753              } elsif ($node->[1] & SCOPING_EL) {
7754                last;
7755              }
7756            }
7757            
7758            ## NOTE: No foreign element in scope.
7759            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7760          } # INSCOPE
7761        }
7762    } # B    } # B
7763    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
7764    ## Stop parsing # MUST    ## Stop parsing # MUST
7765        
7766    ## TODO: script stuffs    ## TODO: script stuffs
7767  } # _tree_construct_main  } # _tree_construct_main
7768    
7769  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7770    my $class = shift;    my $class = shift;
7771    my $node = shift;    my $node = shift;
7772    my $s = \$_[0];    #my $s = \$_[0];
7773    my $onerror = $_[1];    my $onerror = $_[1];
7774      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7775    
7776      ## ISSUE: Should {confident} be true?
7777    
7778    my $nt = $node->node_type;    my $nt = $node->node_type;
7779    if ($nt == 9) {    if ($nt == 9) {
# Line 5210  sub set_inner_html ($$$) { Line 7790  sub set_inner_html ($$$) {
7790      }      }
7791    
7792      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7793      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7794    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7795      ## TODO: If non-html element      ## TODO: If non-html element
7796    
7797      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7798    
7799    ## TODO: Support for $get_wrapper
7800    
7801      ## Step 1 # MUST      ## Step 1 # MUST
7802      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7803      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5223  sub set_inner_html ($$$) { Line 7805  sub set_inner_html ($$$) {
7805      my $p = $class->new;      my $p = $class->new;
7806      $p->{document} = $doc;      $p->{document} = $doc;
7807    
7808      ## Step 9 # MUST      ## Step 8 # MUST
7809      my $i = 0;      my $i = 0;
7810      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7811      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7812      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
7813        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7814        $input = $get_wrapper->($input);
7815        $p->{set_next_char} = sub {
7816        my $self = shift;        my $self = shift;
7817    
7818        pop @{$self->{prev_input_character}};        my $char = '';
7819        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        if (defined $self->{next_next_char}) {
7820            $char = $self->{next_next_char};
7821            delete $self->{next_next_char};
7822            $self->{next_char} = ord $char;
7823          } else {
7824            $self->{char_buffer} = '';
7825            $self->{char_buffer_pos} = 0;
7826            
7827            my $count = $input->manakai_read_until
7828                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7829                 $self->{char_buffer_pos});
7830            if ($count) {
7831              $self->{line_prev} = $self->{line};
7832              $self->{column_prev} = $self->{column};
7833              $self->{column}++;
7834              $self->{next_char}
7835                  = ord substr ($self->{char_buffer},
7836                                $self->{char_buffer_pos}++, 1);
7837              return;
7838            }
7839            
7840            if ($input->read ($char, 1)) {
7841              $self->{next_char} = ord $char;
7842            } else {
7843              $self->{next_char} = -1;
7844              return;
7845            }
7846          }
7847    
7848        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7849        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7850        $column++;  
7851          if ($self->{next_char} == 0x000A) { # LF
7852        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7853          $line++;          $p->{column} = 0;
7854          $column = 0;          !!!cp ('i1');
7855        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7856          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7857          $self->{next_input_character} = 0x000A; # LF # MUST          my $next = '';
7858          $line++;          if ($input->read ($next, 1) and $next ne "\x0A") {
7859          $column = 0;            $self->{next_next_char} = $next;
7860        } elsif ($self->{next_input_character} > 0x10FFFF) {          }
7861          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0x000A; # LF # MUST
7862        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $p->{line}++;
7863            $p->{column} = 0;
7864            !!!cp ('i2');
7865          } elsif ($self->{next_char} == 0x0000) { # NULL
7866            !!!cp ('i4');
7867          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7868          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7869        }        }
7870      };      };
7871      $p->{prev_input_character} = [-1, -1, -1];  
7872      $p->{next_input_character} = -1;      $p->{read_until} = sub {
7873              #my ($scalar, $specials_range, $offset) = @_;
7874          return 0 if defined $p->{next_next_char};
7875    
7876          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7877          my $offset = $_[2] || 0;
7878          
7879          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7880            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7881            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7882              substr ($_[0], $offset)
7883                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7884              my $count = $+[0] - $-[0];
7885              if ($count) {
7886                $p->{column} += $count;
7887                $p->{char_buffer_pos} += $count;
7888                $p->{line_prev} = $p->{line};
7889                $p->{column_prev} = $p->{column} - 1;
7890                $p->{prev_char} = [-1, -1, -1];
7891                $p->{next_char} = -1;
7892              }
7893              return $count;
7894            } else {
7895              return 0;
7896            }
7897          } else {
7898            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7899            if ($count) {
7900              $p->{column} += $count;
7901              $p->{column_prev} += $count;
7902              $p->{prev_char} = [-1, -1, -1];
7903              $p->{next_char} = -1;
7904            }
7905            return $count;
7906          }
7907        }; # $p->{read_until}
7908    
7909      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7910        my (%opt) = @_;        my (%opt) = @_;
7911        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7912          my $column = $opt{column};
7913          if (defined $opt{token} and defined $opt{token}->{line}) {
7914            $line = $opt{token}->{line};
7915            $column = $opt{token}->{column};
7916          }
7917          warn "Parse error ($opt{type}) at line $line column $column\n";
7918      };      };
7919      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7920        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7921      };      };
7922            
7923        my $char_onerror = sub {
7924          my (undef, $type, %opt) = @_;
7925          $ponerror->(layer => 'encode',
7926                      line => $p->{line}, column => $p->{column} + 1,
7927                      %opt, type => $type);
7928        }; # $char_onerror
7929        $input->onerror ($char_onerror);
7930    
7931      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7932      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7933    
7934      ## Step 2      ## Step 2
7935      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7936      $p->{content_model} = {      $p->{content_model} = {
7937        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7938        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5284  sub set_inner_html ($$$) { Line 7949  sub set_inner_html ($$$) {
7949          unless defined $p->{content_model};          unless defined $p->{content_model};
7950          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7951    
7952      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7953          ## TODO: Foreign element OK?
7954    
7955      ## Step 4      ## Step 3
7956      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7957        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7958    
7959      ## Step 5 # MUST      ## Step 4 # MUST
7960      $doc->append_child ($root);      $doc->append_child ($root);
7961    
7962      ## Step 6 # MUST      ## Step 5 # MUST
7963      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7964    
7965      undef $p->{head_element};      undef $p->{head_element};
7966    
7967      ## Step 7 # MUST      ## Step 6 # MUST
7968      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7969    
7970      ## Step 8 # MUST      ## Step 7 # MUST
7971      my $anode = $node;      my $anode = $node;
7972      AN: while (defined $anode) {      AN: while (defined $anode) {
7973        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7974          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7975          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7976            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7977                !!!cp ('i5');
7978              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7979              last AN;              last AN;
7980            }            }
# Line 5316  sub set_inner_html ($$$) { Line 7983  sub set_inner_html ($$$) {
7983        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7984      } # AN      } # AN
7985            
7986      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7987      {      {
7988        my $self = $p;        my $self = $p;
7989        !!!next-token;        !!!next-token;
7990      }      }
7991      $p->_tree_construction_main;      $p->_tree_construction_main;
7992    
7993      ## Step 11 # MUST      ## Step 10 # MUST
7994      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7995      for (@cn) {      for (@cn) {
7996        $node->remove_child ($_);        $node->remove_child ($_);
7997      }      }
7998      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7999    
8000      ## Step 12 # MUST      ## Step 11 # MUST
8001      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
8002      for (@cn) {      for (@cn) {
8003        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5340  sub set_inner_html ($$$) { Line 8006  sub set_inner_html ($$$) {
8006      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8007    
8008      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8009    
8010        delete $p->{parse_error}; # delete loop
8011    } else {    } else {
8012      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";
8013    }    }
# Line 5347  sub set_inner_html ($$$) { Line 8015  sub set_inner_html ($$$) {
8015    
8016  } # tree construction stage  } # tree construction stage
8017    
8018  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
8019    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  
8020    
8021  1;  1;
8022  # $Date$  # $Date$

Legend:
Removed from v.1.60  
changed lines
  Added in v.1.182

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24