/[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.61 by wakaba, Sun Nov 4 04:15:06 2007 UTC revision 1.183 by wakaba, Mon Sep 15 08:09:39 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6    ## NOTE: This module don't check all HTML5 parse errors; character
7    ## encoding related parse errors are expected to be handled by relevant
8    ## modules.
9    ## Parse errors for control characters that are not allowed in HTML5
10    ## documents, for surrogate code points, and for noncharacter code
11    ## points, as well as U+FFFD substitions for characters whose code points
12    ## is higher than U+10FFFF may be detected by combining the parser with
13    ## the checker implemented by Whatpm::Charset::UnicodeChecker (for its
14    ## usage example, see |t/HTML-tree.t| in the Whatpm package or the
15    ## WebHACC::Language::HTML module in the WebHACC package).
16    
17  ## ISSUE:  ## ISSUE:
18  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
19  ## doc.write ('');  ## doc.write ('');
20  ## alert (doc.compatMode);  ## alert (doc.compatMode);
21    
22  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  require IO::Handle;
23  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
24  ## is not yet clear.  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
25  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
26  ## "{U+FEFF}..." in GB18030?  my $SVG_NS = q<http://www.w3.org/2000/svg>;
27    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
28  my $permitted_slash_tag_name = {  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
29    base => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
30    link => 1,  
31    meta => 1,  sub A_EL () { 0b1 }
32    hr => 1,  sub ADDRESS_EL () { 0b10 }
33    br => 1,  sub BODY_EL () { 0b100 }
34    img=> 1,  sub BUTTON_EL () { 0b1000 }
35    embed => 1,  sub CAPTION_EL () { 0b10000 }
36    param => 1,  sub DD_EL () { 0b100000 }
37    area => 1,  sub DIV_EL () { 0b1000000 }
38    col => 1,  sub DT_EL () { 0b10000000 }
39    input => 1,  sub FORM_EL () { 0b100000000 }
40    sub FORMATTING_EL () { 0b1000000000 }
41    sub FRAMESET_EL () { 0b10000000000 }
42    sub HEADING_EL () { 0b100000000000 }
43    sub HTML_EL () { 0b1000000000000 }
44    sub LI_EL () { 0b10000000000000 }
45    sub NOBR_EL () { 0b100000000000000 }
46    sub OPTION_EL () { 0b1000000000000000 }
47    sub OPTGROUP_EL () { 0b10000000000000000 }
48    sub P_EL () { 0b100000000000000000 }
49    sub SELECT_EL () { 0b1000000000000000000 }
50    sub TABLE_EL () { 0b10000000000000000000 }
51    sub TABLE_CELL_EL () { 0b100000000000000000000 }
52    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
53    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
54    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
55    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
56    sub FOREIGN_EL () { 0b10000000000000000000000000 }
57    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
58    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
59    sub RUBY_EL () { 0b10000000000000000000000000000 }
60    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
61    
62    sub TABLE_ROWS_EL () {
63      TABLE_EL |
64      TABLE_ROW_EL |
65      TABLE_ROW_GROUP_EL
66    }
67    
68    ## NOTE: Used in "generate implied end tags" algorithm.
69    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
70    ## is used in "generate implied end tags" implementation (search for the
71    ## function mae).
72    sub END_TAG_OPTIONAL_EL () {
73      DD_EL |
74      DT_EL |
75      LI_EL |
76      P_EL |
77      RUBY_COMPONENT_EL
78    }
79    
80    ## NOTE: Used in </body> and EOF algorithms.
81    sub ALL_END_TAG_OPTIONAL_EL () {
82      DD_EL |
83      DT_EL |
84      LI_EL |
85      P_EL |
86    
87      BODY_EL |
88      HTML_EL |
89      TABLE_CELL_EL |
90      TABLE_ROW_EL |
91      TABLE_ROW_GROUP_EL
92    }
93    
94    sub SCOPING_EL () {
95      BUTTON_EL |
96      CAPTION_EL |
97      HTML_EL |
98      TABLE_EL |
99      TABLE_CELL_EL |
100      MISC_SCOPING_EL
101    }
102    
103    sub TABLE_SCOPING_EL () {
104      HTML_EL |
105      TABLE_EL
106    }
107    
108    sub TABLE_ROWS_SCOPING_EL () {
109      HTML_EL |
110      TABLE_ROW_GROUP_EL
111    }
112    
113    sub TABLE_ROW_SCOPING_EL () {
114      HTML_EL |
115      TABLE_ROW_EL
116    }
117    
118    sub SPECIAL_EL () {
119      ADDRESS_EL |
120      BODY_EL |
121      DIV_EL |
122    
123      DD_EL |
124      DT_EL |
125      LI_EL |
126      P_EL |
127    
128      FORM_EL |
129      FRAMESET_EL |
130      HEADING_EL |
131      OPTION_EL |
132      OPTGROUP_EL |
133      SELECT_EL |
134      TABLE_ROW_EL |
135      TABLE_ROW_GROUP_EL |
136      MISC_SPECIAL_EL
137    }
138    
139    my $el_category = {
140      a => A_EL | FORMATTING_EL,
141      address => ADDRESS_EL,
142      applet => MISC_SCOPING_EL,
143      area => MISC_SPECIAL_EL,
144      b => FORMATTING_EL,
145      base => MISC_SPECIAL_EL,
146      basefont => MISC_SPECIAL_EL,
147      bgsound => MISC_SPECIAL_EL,
148      big => FORMATTING_EL,
149      blockquote => MISC_SPECIAL_EL,
150      body => BODY_EL,
151      br => MISC_SPECIAL_EL,
152      button => BUTTON_EL,
153      caption => CAPTION_EL,
154      center => MISC_SPECIAL_EL,
155      col => MISC_SPECIAL_EL,
156      colgroup => MISC_SPECIAL_EL,
157      dd => DD_EL,
158      dir => MISC_SPECIAL_EL,
159      div => DIV_EL,
160      dl => MISC_SPECIAL_EL,
161      dt => DT_EL,
162      em => FORMATTING_EL,
163      embed => MISC_SPECIAL_EL,
164      fieldset => MISC_SPECIAL_EL,
165      font => FORMATTING_EL,
166      form => FORM_EL,
167      frame => MISC_SPECIAL_EL,
168      frameset => FRAMESET_EL,
169      h1 => HEADING_EL,
170      h2 => HEADING_EL,
171      h3 => HEADING_EL,
172      h4 => HEADING_EL,
173      h5 => HEADING_EL,
174      h6 => HEADING_EL,
175      head => MISC_SPECIAL_EL,
176      hr => MISC_SPECIAL_EL,
177      html => HTML_EL,
178      i => FORMATTING_EL,
179      iframe => MISC_SPECIAL_EL,
180      img => MISC_SPECIAL_EL,
181      input => MISC_SPECIAL_EL,
182      isindex => MISC_SPECIAL_EL,
183      li => LI_EL,
184      link => MISC_SPECIAL_EL,
185      listing => MISC_SPECIAL_EL,
186      marquee => MISC_SCOPING_EL,
187      menu => MISC_SPECIAL_EL,
188      meta => MISC_SPECIAL_EL,
189      nobr => NOBR_EL | FORMATTING_EL,
190      noembed => MISC_SPECIAL_EL,
191      noframes => MISC_SPECIAL_EL,
192      noscript => MISC_SPECIAL_EL,
193      object => MISC_SCOPING_EL,
194      ol => MISC_SPECIAL_EL,
195      optgroup => OPTGROUP_EL,
196      option => OPTION_EL,
197      p => P_EL,
198      param => MISC_SPECIAL_EL,
199      plaintext => MISC_SPECIAL_EL,
200      pre => MISC_SPECIAL_EL,
201      rp => RUBY_COMPONENT_EL,
202      rt => RUBY_COMPONENT_EL,
203      ruby => RUBY_EL,
204      s => FORMATTING_EL,
205      script => MISC_SPECIAL_EL,
206      select => SELECT_EL,
207      small => FORMATTING_EL,
208      spacer => MISC_SPECIAL_EL,
209      strike => FORMATTING_EL,
210      strong => FORMATTING_EL,
211      style => MISC_SPECIAL_EL,
212      table => TABLE_EL,
213      tbody => TABLE_ROW_GROUP_EL,
214      td => TABLE_CELL_EL,
215      textarea => MISC_SPECIAL_EL,
216      tfoot => TABLE_ROW_GROUP_EL,
217      th => TABLE_CELL_EL,
218      thead => TABLE_ROW_GROUP_EL,
219      title => MISC_SPECIAL_EL,
220      tr => TABLE_ROW_EL,
221      tt => FORMATTING_EL,
222      u => FORMATTING_EL,
223      ul => MISC_SPECIAL_EL,
224      wbr => MISC_SPECIAL_EL,
225    };
226    
227    my $el_category_f = {
228      $MML_NS => {
229        'annotation-xml' => MML_AXML_EL,
230        mi => FOREIGN_FLOW_CONTENT_EL,
231        mo => FOREIGN_FLOW_CONTENT_EL,
232        mn => FOREIGN_FLOW_CONTENT_EL,
233        ms => FOREIGN_FLOW_CONTENT_EL,
234        mtext => FOREIGN_FLOW_CONTENT_EL,
235      },
236      $SVG_NS => {
237        foreignObject => FOREIGN_FLOW_CONTENT_EL,
238        desc => FOREIGN_FLOW_CONTENT_EL,
239        title => FOREIGN_FLOW_CONTENT_EL,
240      },
241      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
242    };
243    
244    my $svg_attr_name = {
245      attributename => 'attributeName',
246      attributetype => 'attributeType',
247      basefrequency => 'baseFrequency',
248      baseprofile => 'baseProfile',
249      calcmode => 'calcMode',
250      clippathunits => 'clipPathUnits',
251      contentscripttype => 'contentScriptType',
252      contentstyletype => 'contentStyleType',
253      diffuseconstant => 'diffuseConstant',
254      edgemode => 'edgeMode',
255      externalresourcesrequired => 'externalResourcesRequired',
256      filterres => 'filterRes',
257      filterunits => 'filterUnits',
258      glyphref => 'glyphRef',
259      gradienttransform => 'gradientTransform',
260      gradientunits => 'gradientUnits',
261      kernelmatrix => 'kernelMatrix',
262      kernelunitlength => 'kernelUnitLength',
263      keypoints => 'keyPoints',
264      keysplines => 'keySplines',
265      keytimes => 'keyTimes',
266      lengthadjust => 'lengthAdjust',
267      limitingconeangle => 'limitingConeAngle',
268      markerheight => 'markerHeight',
269      markerunits => 'markerUnits',
270      markerwidth => 'markerWidth',
271      maskcontentunits => 'maskContentUnits',
272      maskunits => 'maskUnits',
273      numoctaves => 'numOctaves',
274      pathlength => 'pathLength',
275      patterncontentunits => 'patternContentUnits',
276      patterntransform => 'patternTransform',
277      patternunits => 'patternUnits',
278      pointsatx => 'pointsAtX',
279      pointsaty => 'pointsAtY',
280      pointsatz => 'pointsAtZ',
281      preservealpha => 'preserveAlpha',
282      preserveaspectratio => 'preserveAspectRatio',
283      primitiveunits => 'primitiveUnits',
284      refx => 'refX',
285      refy => 'refY',
286      repeatcount => 'repeatCount',
287      repeatdur => 'repeatDur',
288      requiredextensions => 'requiredExtensions',
289      requiredfeatures => 'requiredFeatures',
290      specularconstant => 'specularConstant',
291      specularexponent => 'specularExponent',
292      spreadmethod => 'spreadMethod',
293      startoffset => 'startOffset',
294      stddeviation => 'stdDeviation',
295      stitchtiles => 'stitchTiles',
296      surfacescale => 'surfaceScale',
297      systemlanguage => 'systemLanguage',
298      tablevalues => 'tableValues',
299      targetx => 'targetX',
300      targety => 'targetY',
301      textlength => 'textLength',
302      viewbox => 'viewBox',
303      viewtarget => 'viewTarget',
304      xchannelselector => 'xChannelSelector',
305      ychannelselector => 'yChannelSelector',
306      zoomandpan => 'zoomAndPan',
307  };  };
308    
309    my $foreign_attr_xname = {
310      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
311      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
312      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
313      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
314      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
315      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
316      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
317      'xml:base' => [$XML_NS, ['xml', 'base']],
318      'xml:lang' => [$XML_NS, ['xml', 'lang']],
319      'xml:space' => [$XML_NS, ['xml', 'space']],
320      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
321      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
322    };
323    
324    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
325    
326  my $c1_entity_char = {  my $c1_entity_char = {
327    0x80 => 0x20AC,    0x80 => 0x20AC,
328    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 358  my $c1_entity_char = {
358    0x9F => 0x0178,    0x9F => 0x0178,
359  }; # $c1_entity_char  }; # $c1_entity_char
360    
361  my $special_category = {  sub parse_byte_string ($$$$;$) {
362    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
363    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
364    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
365    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
366    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
367    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
368    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$$) {
369    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
370    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $self = ref $_[0] ? shift : shift->new;
371    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $charset_name = shift;
372  };    my $byte_stream = $_[0];
373  my $scoping_category = {  
374    button => 1, caption => 1, html => 1, marquee => 1, object => 1,    my $onerror = $_[2] || sub {
375    table => 1, td => 1, th => 1,      my (%opt) = @_;
376  };      warn "Parse error ($opt{type})\n";
377  my $formatting_category = {    };
378    a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,    $self->{parse_error} = $onerror; # updated later by parse_char_string
379    s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
380  };    my $get_wrapper = $_[3] || sub ($) {
381  # $phrasing_category: all other elements      return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
382      };
383    
384      ## HTML5 encoding sniffing algorithm
385      require Message::Charset::Info;
386      my $charset;
387      my $buffer;
388      my ($char_stream, $e_status);
389    
390      SNIFFING: {
391        ## NOTE: By setting |allow_fallback| option true when the
392        ## |get_decode_handle| method is invoked, we ignore what the HTML5
393        ## spec requires, i.e. unsupported encoding should be ignored.
394          ## TODO: We should not do this unless the parser is invoked
395          ## in the conformance checking mode, in which this behavior
396          ## would be useful.
397    
398        ## Step 1
399        if (defined $charset_name) {
400          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
401              ## TODO: Is this ok?  Transfer protocol's parameter should be
402              ## interpreted in its semantics?
403    
404          ## ISSUE: Unsupported encoding is not ignored according to the spec.
405          ($char_stream, $e_status) = $charset->get_decode_handle
406              ($byte_stream, allow_error_reporting => 1,
407               allow_fallback => 1);
408          if ($char_stream) {
409            $self->{confident} = 1;
410            last SNIFFING;
411          } else {
412            ## TODO: unsupported error
413          }
414        }
415    
416        ## Step 2
417        my $byte_buffer = '';
418        for (1..1024) {
419          my $char = $byte_stream->getc;
420          last unless defined $char;
421          $byte_buffer .= $char;
422        } ## TODO: timeout
423    
424        ## Step 3
425        if ($byte_buffer =~ /^\xFE\xFF/) {
426          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
427          ($char_stream, $e_status) = $charset->get_decode_handle
428              ($byte_stream, allow_error_reporting => 1,
429               allow_fallback => 1, byte_buffer => \$byte_buffer);
430          $self->{confident} = 1;
431          last SNIFFING;
432        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
433          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
434          ($char_stream, $e_status) = $charset->get_decode_handle
435              ($byte_stream, allow_error_reporting => 1,
436               allow_fallback => 1, byte_buffer => \$byte_buffer);
437          $self->{confident} = 1;
438          last SNIFFING;
439        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
440          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($byte_stream, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          $self->{confident} = 1;
445          last SNIFFING;
446        }
447    
448        ## Step 4
449        ## TODO: <meta charset>
450    
451        ## Step 5
452        ## TODO: from history
453    
454        ## Step 6
455        require Whatpm::Charset::UniversalCharDet;
456        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
457            ($byte_buffer);
458        if (defined $charset_name) {
459          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
460    
461          ## ISSUE: Unsupported encoding is not ignored according to the spec.
462          require Whatpm::Charset::DecodeHandle;
463          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
464              ($byte_stream);
465          ($char_stream, $e_status) = $charset->get_decode_handle
466              ($buffer, allow_error_reporting => 1,
467               allow_fallback => 1, byte_buffer => \$byte_buffer);
468          if ($char_stream) {
469            $buffer->{buffer} = $byte_buffer;
470            !!!parse-error (type => 'sniffing:chardet',
471                            text => $charset_name,
472                            level => $self->{level}->{info},
473                            layer => 'encode',
474                            line => 1, column => 1);
475            $self->{confident} = 0;
476            last SNIFFING;
477          }
478        }
479    
480        ## Step 7: default
481        ## TODO: Make this configurable.
482        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
483            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
484            ## detectable in the step 6.
485        require Whatpm::Charset::DecodeHandle;
486        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
487            ($byte_stream);
488        ($char_stream, $e_status)
489            = $charset->get_decode_handle ($buffer,
490                                           allow_error_reporting => 1,
491                                           allow_fallback => 1,
492                                           byte_buffer => \$byte_buffer);
493        $buffer->{buffer} = $byte_buffer;
494        !!!parse-error (type => 'sniffing:default',
495                        text => 'windows-1252',
496                        level => $self->{level}->{info},
497                        line => 1, column => 1,
498                        layer => 'encode');
499        $self->{confident} = 0;
500      } # SNIFFING
501    
502      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
503        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
504        !!!parse-error (type => 'chardecode:fallback',
505                        #text => $self->{input_encoding},
506                        level => $self->{level}->{uncertain},
507                        line => 1, column => 1,
508                        layer => 'encode');
509      } elsif (not ($e_status &
510                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
511        $self->{input_encoding} = $charset->get_iana_name;
512        !!!parse-error (type => 'chardecode:no error',
513                        text => $self->{input_encoding},
514                        level => $self->{level}->{uncertain},
515                        line => 1, column => 1,
516                        layer => 'encode');
517      } else {
518        $self->{input_encoding} = $charset->get_iana_name;
519      }
520    
521      $self->{change_encoding} = sub {
522        my $self = shift;
523        $charset_name = shift;
524        my $token = shift;
525    
526        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
527        ($char_stream, $e_status) = $charset->get_decode_handle
528            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
529             byte_buffer => \ $buffer->{buffer});
530        
531        if ($char_stream) { # if supported
532          ## "Change the encoding" algorithm:
533    
534  sub parse_string ($$$;$) {        ## Step 1    
535    my $self = shift->new;        if ($charset->{category} &
536    my $s = \$_[0];            Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
537            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
538            ($char_stream, $e_status) = $charset->get_decode_handle
539                ($byte_stream,
540                 byte_buffer => \ $buffer->{buffer});
541          }
542          $charset_name = $charset->get_iana_name;
543          
544          ## Step 2
545          if (defined $self->{input_encoding} and
546              $self->{input_encoding} eq $charset_name) {
547            !!!parse-error (type => 'charset label:matching',
548                            text => $charset_name,
549                            level => $self->{level}->{info});
550            $self->{confident} = 1;
551            return;
552          }
553    
554          !!!parse-error (type => 'charset label detected',
555                          text => $self->{input_encoding},
556                          value => $charset_name,
557                          level => $self->{level}->{warn},
558                          token => $token);
559          
560          ## Step 3
561          # if (can) {
562            ## change the encoding on the fly.
563            #$self->{confident} = 1;
564            #return;
565          # }
566          
567          ## Step 4
568          throw Whatpm::HTML::RestartParser ();
569        }
570      }; # $self->{change_encoding}
571    
572      my $char_onerror = sub {
573        my (undef, $type, %opt) = @_;
574        !!!parse-error (layer => 'encode',
575                        line => $self->{line}, column => $self->{column} + 1,
576                        %opt, type => $type);
577        if ($opt{octets}) {
578          ${$opt{octets}} = "\x{FFFD}"; # relacement character
579        }
580      };
581    
582      my $wrapped_char_stream = $get_wrapper->($char_stream);
583      $wrapped_char_stream->onerror ($char_onerror);
584    
585      my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
586      my $return;
587      try {
588        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
589      } catch Whatpm::HTML::RestartParser with {
590        ## NOTE: Invoked after {change_encoding}.
591    
592        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
593          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
594          !!!parse-error (type => 'chardecode:fallback',
595                          level => $self->{level}->{uncertain},
596                          #text => $self->{input_encoding},
597                          line => 1, column => 1,
598                          layer => 'encode');
599        } elsif (not ($e_status &
600                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
601          $self->{input_encoding} = $charset->get_iana_name;
602          !!!parse-error (type => 'chardecode:no error',
603                          text => $self->{input_encoding},
604                          level => $self->{level}->{uncertain},
605                          line => 1, column => 1,
606                          layer => 'encode');
607        } else {
608          $self->{input_encoding} = $charset->get_iana_name;
609        }
610        $self->{confident} = 1;
611    
612        $wrapped_char_stream = $get_wrapper->($char_stream);
613        $wrapped_char_stream->onerror ($char_onerror);
614    
615        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
616      };
617      return $return;
618    } # parse_byte_stream
619    
620    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
621    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
622    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
623    ## because the core part of our HTML parser expects a string of character,
624    ## not a string of bytes or code units or anything which might contain a BOM.
625    ## Therefore, any parser interface that accepts a string of bytes,
626    ## such as |parse_byte_string| in this module, must ensure that it does
627    ## strip the BOM and never strip any ZWNBSP.
628    
629    sub parse_char_string ($$$;$$) {
630      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
631      my $self = shift;
632      my $s = ref $_[0] ? $_[0] : \($_[0]);
633      require Whatpm::Charset::DecodeHandle;
634      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
635      return $self->parse_char_stream ($input, @_[1..$#_]);
636    } # parse_char_string
637    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
638    
639    sub parse_char_stream ($$$;$$) {
640      my $self = ref $_[0] ? shift : shift->new;
641      my $input = $_[0];
642    $self->{document} = $_[1];    $self->{document} = $_[1];
643      @{$self->{document}->child_nodes} = ();
644    
645    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
646    
647    my $i = 0;    $self->{confident} = 1 unless exists $self->{confident};
648    my $line = 1;    $self->{document}->input_encoding ($self->{input_encoding})
649    my $column = 0;        if defined $self->{input_encoding};
650    $self->{set_next_input_character} = sub {  ## TODO: |{input_encoding}| is needless?
651    
652      $self->{line_prev} = $self->{line} = 1;
653      $self->{column_prev} = -1;
654      $self->{column} = 0;
655      $self->{set_nc} = sub {
656      my $self = shift;      my $self = shift;
657    
658      pop @{$self->{prev_input_character}};      my $char = '';
659      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      if (defined $self->{next_nc}) {
660          $char = $self->{next_nc};
661          delete $self->{next_nc};
662          $self->{nc} = ord $char;
663        } else {
664          $self->{char_buffer} = '';
665          $self->{char_buffer_pos} = 0;
666    
667          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->{nc}
674                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
675            return;
676          }
677    
678      $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($input->read ($char, 1)) {
679      $self->{next_input_character} = ord substr $$s, $i++, 1;          $self->{nc} = ord $char;
680      $column++;        } else {
681            $self->{nc} = -1;
682            return;
683          }
684        }
685    
686        ($self->{line_prev}, $self->{column_prev})
687            = ($self->{line}, $self->{column});
688        $self->{column}++;
689            
690      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{nc} == 0x000A) { # LF
691        $line++;        !!!cp ('j1');
692        $column = 0;        $self->{line}++;
693      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
694        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{nc} == 0x000D) { # CR
695        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
696        $line++;  ## TODO: support for abort/streaming
697        $column = 0;        my $next = '';
698      } elsif ($self->{next_input_character} > 0x10FFFF) {        if ($input->read ($next, 1) and $next ne "\x0A") {
699        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_nc} = $next;
700      } elsif ($self->{next_input_character} == 0x0000) { # NULL        }
701          $self->{nc} = 0x000A; # LF # MUST
702          $self->{line}++;
703          $self->{column} = 0;
704        } elsif ($self->{nc} == 0x0000) { # NULL
705          !!!cp ('j4');
706        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
707        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
708      }      }
709    };    };
710    $self->{prev_input_character} = [-1, -1, -1];  
711    $self->{next_input_character} = -1;    $self->{read_until} = sub {
712        #my ($scalar, $specials_range, $offset) = @_;
713        return 0 if defined $self->{next_nc};
714    
715        my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
716        my $offset = $_[2] || 0;
717    
718        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
719          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
720          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
721            substr ($_[0], $offset)
722                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
723            my $count = $+[0] - $-[0];
724            if ($count) {
725              $self->{column} += $count;
726              $self->{char_buffer_pos} += $count;
727              $self->{line_prev} = $self->{line};
728              $self->{column_prev} = $self->{column} - 1;
729              $self->{prev_char} = [-1, -1, -1];
730              $self->{nc} = -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->{nc} = -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_nc} = sub {
793        $self->{nc} = -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 {    $self->{application_cache_selection} = sub {
805      #      #
806    };    };
# Line 163  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 174  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 195  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 211  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 227  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 235  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->{s_kwd}; # 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->{ct}; # current token
929    undef $self->{current_attribute};    undef $self->{ca}; # current attribute
930    undef $self->{last_emitted_start_tag_name};    undef $self->{last_stag_name}; # 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->{nc} = -1; # next input character
937      #$self->{next_nc}
938    !!!next-input-character;    !!!next-input-character;
939    $self->{token} = [];    $self->{token} = [];
940    # $self->{escape}    # $self->{escape}
# Line 252  sub _initialize_tokenizer ($) { Line 945  sub _initialize_tokenizer ($) {
945  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
946  ##   ->{name} (DOCTYPE_TOKEN)  ##   ->{name} (DOCTYPE_TOKEN)
947  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
948  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{pubid} (DOCTYPE_TOKEN)
949  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{sysid} (DOCTYPE_TOKEN)
950  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
951  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
952    ##        ->{name}
953    ##        ->{value}
954    ##        ->{has_reference} == 1 or 0
955  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
956    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
957    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
958    ##     while the token is pushed back to the stack.
959    
960  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
961    
# Line 266  sub _initialize_tokenizer ($) { Line 965  sub _initialize_tokenizer ($) {
965  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
966  ## and removed from the list.  ## and removed from the list.
967    
968  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
969  ## 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,  
970    
971  sub _get_next_token ($) {  sub _get_next_token ($) {
972    my $self = shift;    my $self = shift;
973    
974      if ($self->{self_closing}) {
975        !!!parse-error (type => 'nestc', token => $self->{ct});
976        ## NOTE: The |self_closing| flag is only set by start tag token.
977        ## In addition, when a start tag token is emitted, it is always set to
978        ## |ct|.
979        delete $self->{self_closing};
980      }
981    
982    if (@{$self->{token}}) {    if (@{$self->{token}}) {
983        $self->{self_closing} = $self->{token}->[0]->{self_closing};
984      return shift @{$self->{token}};      return shift @{$self->{token}};
985    }    }
986    
987    A: {    A: {
988      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
989        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{nc} == 0x0026) { # &
990          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
991            $self->{state} = ENTITY_DATA_STATE;              not $self->{escape}) {
992              !!!cp (1);
993              ## NOTE: In the spec, the tokenizer is switched to the
994              ## "entity data state".  In this implementation, the tokenizer
995              ## is switched to the |ENTITY_STATE|, which is an implementation
996              ## of the "consume a character reference" algorithm.
997              $self->{entity_add} = -1;
998              $self->{prev_state} = DATA_STATE;
999              $self->{state} = ENTITY_STATE;
1000            !!!next-input-character;            !!!next-input-character;
1001            redo A;            redo A;
1002          } else {          } else {
1003              !!!cp (2);
1004            #            #
1005          }          }
1006        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
1007          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1008            unless ($self->{escape}) {            unless ($self->{escape}) {
1009              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
1010                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
1011                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
1012                  !!!cp (3);
1013                $self->{escape} = 1;                $self->{escape} = 1;
1014                } else {
1015                  !!!cp (4);
1016              }              }
1017              } else {
1018                !!!cp (5);
1019            }            }
1020          }          }
1021                    
1022          #          #
1023        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{nc} == 0x003C) { # <
1024          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1025              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1026               not $self->{escape})) {               not $self->{escape})) {
1027              !!!cp (6);
1028            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
1029            !!!next-input-character;            !!!next-input-character;
1030            redo A;            redo A;
1031          } else {          } else {
1032              !!!cp (7);
1033            #            #
1034          }          }
1035        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1036          if ($self->{escape} and          if ($self->{escape} and
1037              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1038            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
1039                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
1040                !!!cp (8);
1041              delete $self->{escape};              delete $self->{escape};
1042              } else {
1043                !!!cp (9);
1044            }            }
1045            } else {
1046              !!!cp (10);
1047          }          }
1048                    
1049          #          #
1050        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1051          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
1052            !!!emit ({type => END_OF_FILE_TOKEN,
1053                      line => $self->{line}, column => $self->{column}});
1054          last A; ## TODO: ok?          last A; ## TODO: ok?
1055          } else {
1056            !!!cp (12);
1057        }        }
1058        # Anything else        # Anything else
1059        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1060                     data => chr $self->{next_input_character}};                     data => chr $self->{nc},
1061                       line => $self->{line}, column => $self->{column},
1062                      };
1063          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1064    
1065        ## Stay in the data state        ## Stay in the data state
1066        !!!next-input-character;        !!!next-input-character;
1067    
1068        !!!emit ($token);        !!!emit ($token);
1069    
1070        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;  
1071      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1072        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1073          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{nc} == 0x002F) { # /
1074              !!!cp (15);
1075            !!!next-input-character;            !!!next-input-character;
1076            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1077            redo A;            redo A;
1078          } else {          } else {
1079              !!!cp (16);
1080            ## reconsume            ## reconsume
1081            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1082    
1083            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1084                        line => $self->{line_prev},
1085                        column => $self->{column_prev},
1086                       });
1087    
1088            redo A;            redo A;
1089          }          }
1090        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1091          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{nc} == 0x0021) { # !
1092              !!!cp (17);
1093            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1094            !!!next-input-character;            !!!next-input-character;
1095            redo A;            redo A;
1096          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1097              !!!cp (18);
1098            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1099            !!!next-input-character;            !!!next-input-character;
1100            redo A;            redo A;
1101          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{nc} and
1102                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1103            $self->{current_token}            !!!cp (19);
1104              $self->{ct}
1105              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1106                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{nc} + 0x0020),
1107                   line => $self->{line_prev},
1108                   column => $self->{column_prev}};
1109            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1110            !!!next-input-character;            !!!next-input-character;
1111            redo A;            redo A;
1112          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{nc} and
1113                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1114            $self->{current_token} = {type => START_TAG_TOKEN,            !!!cp (20);
1115                              tag_name => chr ($self->{next_input_character})};            $self->{ct} = {type => START_TAG_TOKEN,
1116                                        tag_name => chr ($self->{nc}),
1117                                        line => $self->{line_prev},
1118                                        column => $self->{column_prev}};
1119            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1120            !!!next-input-character;            !!!next-input-character;
1121            redo A;            redo A;
1122          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1123            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1124              !!!parse-error (type => 'empty start tag',
1125                              line => $self->{line_prev},
1126                              column => $self->{column_prev});
1127            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1128            !!!next-input-character;            !!!next-input-character;
1129    
1130            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1131                        line => $self->{line_prev},
1132                        column => $self->{column_prev},
1133                       });
1134    
1135            redo A;            redo A;
1136          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1137            !!!parse-error (type => 'pio');            !!!cp (22);
1138              !!!parse-error (type => 'pio',
1139                              line => $self->{line_prev},
1140                              column => $self->{column_prev});
1141            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1142            ## $self->{next_input_character} is intentionally left as is            $self->{ct} = {type => COMMENT_TOKEN, data => '',
1143                                        line => $self->{line_prev},
1144                                        column => $self->{column_prev},
1145                                       };
1146              ## $self->{nc} is intentionally left as is
1147            redo A;            redo A;
1148          } else {          } else {
1149            !!!parse-error (type => 'bare stago');            !!!cp (23);
1150              !!!parse-error (type => 'bare stago',
1151                              line => $self->{line_prev},
1152                              column => $self->{column_prev});
1153            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1154            ## reconsume            ## reconsume
1155    
1156            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1157                        line => $self->{line_prev},
1158                        column => $self->{column_prev},
1159                       });
1160    
1161            redo A;            redo A;
1162          }          }
# Line 421  sub _get_next_token ($) { Line 1164  sub _get_next_token ($) {
1164          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1165        }        }
1166      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1167        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        ## NOTE: The "close tag open state" in the spec is implemented as
1168          if (defined $self->{last_emitted_start_tag_name}) {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
           ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>  
           my @next_char;  
           TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
             push @next_char, $self->{next_input_character};  
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               $self->{next_input_character} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
1169    
1170                !!!emit ({type => CHARACTER_TOKEN, data => '</'});        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1171            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1172                redo A;          if (defined $self->{last_stag_name}) {
1173              }            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1174            }            $self->{s_kwd} = '';
1175            push @next_char, $self->{next_input_character};            ## Reconsume.
1176                    redo A;
           unless ($self->{next_input_character} == 0x0009 or # HT  
                   $self->{next_input_character} == 0x000A or # LF  
                   $self->{next_input_character} == 0x000B or # VT  
                   $self->{next_input_character} == 0x000C or # FF  
                   $self->{next_input_character} == 0x0020 or # SP  
                   $self->{next_input_character} == 0x003E or # >  
                   $self->{next_input_character} == 0x002F or # /  
                   $self->{next_input_character} == -1) {  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
             redo A;  
           } else {  
             $self->{next_input_character} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1177          } else {          } else {
1178            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1179            # next-input-character is already done            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1180              !!!cp (28);
1181            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1182            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1183              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1184                        line => $l, column => $c,
1185                       });
1186            redo A;            redo A;
1187          }          }
1188        }        }
1189          
1190        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{nc} and
1191            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1192          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1193                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{ct}
1194                = {type => END_TAG_TOKEN,
1195                   tag_name => chr ($self->{nc} + 0x0020),
1196                   line => $l, column => $c};
1197          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1198          !!!next-input-character;          !!!next-input-character;
1199          redo A;          redo A;
1200        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{nc} and
1201                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{nc} <= 0x007A) { # a..z
1202          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (30);
1203                            tag_name => chr ($self->{next_input_character})};          $self->{ct} = {type => END_TAG_TOKEN,
1204                                      tag_name => chr ($self->{nc}),
1205                                      line => $l, column => $c};
1206          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1207          !!!next-input-character;          !!!next-input-character;
1208          redo A;          redo A;
1209        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1210          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1211            !!!parse-error (type => 'empty end tag',
1212                            line => $self->{line_prev}, ## "<" in "</>"
1213                            column => $self->{column_prev} - 1);
1214          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1218            !!!cp (32);
1219          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1220          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1221          # reconsume          # reconsume
1222    
1223          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1224                      line => $l, column => $c,
1225                     });
1226    
1227          redo A;          redo A;
1228        } else {        } else {
1229            !!!cp (33);
1230          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1231          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1232          ## $self->{next_input_character} is intentionally left as is          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1233          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1234                                      column => $self->{column_prev} - 1,
1235                                     };
1236            ## NOTE: $self->{nc} is intentionally left as is.
1237            ## Although the "anything else" case of the spec not explicitly
1238            ## states that the next input character is to be reconsumed,
1239            ## it will be included to the |data| of the comment token
1240            ## generated from the bogus end tag, as defined in the
1241            ## "bogus comment state" entry.
1242            redo A;
1243          }
1244        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1245          my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1246          if (length $ch) {
1247            my $CH = $ch;
1248            $ch =~ tr/a-z/A-Z/;
1249            my $nch = chr $self->{nc};
1250            if ($nch eq $ch or $nch eq $CH) {
1251              !!!cp (24);
1252              ## Stay in the state.
1253              $self->{s_kwd} .= $nch;
1254              !!!next-input-character;
1255              redo A;
1256            } else {
1257              !!!cp (25);
1258              $self->{state} = DATA_STATE;
1259              ## Reconsume.
1260              !!!emit ({type => CHARACTER_TOKEN,
1261                        data => '</' . $self->{s_kwd},
1262                        line => $self->{line_prev},
1263                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1264                       });
1265              redo A;
1266            }
1267          } else { # after "<{tag-name}"
1268            unless ({
1269                     0x0009 => 1, # HT
1270                     0x000A => 1, # LF
1271                     0x000B => 1, # VT
1272                     0x000C => 1, # FF
1273                     0x0020 => 1, # SP
1274                     0x003E => 1, # >
1275                     0x002F => 1, # /
1276                     -1 => 1, # EOF
1277                    }->{$self->{nc}}) {
1278              !!!cp (26);
1279              ## Reconsume.
1280              $self->{state} = DATA_STATE;
1281              !!!emit ({type => CHARACTER_TOKEN,
1282                        data => '</' . $self->{s_kwd},
1283                        line => $self->{line_prev},
1284                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1285                       });
1286              redo A;
1287            } else {
1288              !!!cp (27);
1289              $self->{ct}
1290                  = {type => END_TAG_TOKEN,
1291                     tag_name => $self->{last_stag_name},
1292                     line => $self->{line_prev},
1293                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1294              $self->{state} = TAG_NAME_STATE;
1295              ## Reconsume.
1296              redo A;
1297            }
1298        }        }
1299      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1300        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1301            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1302            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1303            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1304            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1305            !!!cp (34);
1306          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1307          !!!next-input-character;          !!!next-input-character;
1308          redo A;          redo A;
1309        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1310          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1311            $self->{current_token}->{first_start_tag}            !!!cp (35);
1312                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1313            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1314            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1315            if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1316              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1317            }            #  !!! cp (36);
1318              #  !!! parse-error (type => 'end tag attribute');
1319              #} else {
1320                !!!cp (37);
1321              #}
1322          } else {          } else {
1323            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1324          }          }
1325          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1326          !!!next-input-character;          !!!next-input-character;
1327    
1328          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1329    
1330          redo A;          redo A;
1331        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1332                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1333          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1334            $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1335            # start tag or end tag            # start tag or end tag
1336          ## Stay in this state          ## Stay in this state
1337          !!!next-input-character;          !!!next-input-character;
1338          redo A;          redo A;
1339        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1340          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1341          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1342            $self->{current_token}->{first_start_tag}            !!!cp (39);
1343                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1344            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1345            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1346            if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1347              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1348            }            #  !!! cp (40);
1349              #  !!! parse-error (type => 'end tag attribute');
1350              #} else {
1351                !!!cp (41);
1352              #}
1353          } else {          } else {
1354            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1355          }          }
1356          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1357          # reconsume          # reconsume
1358    
1359          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1360    
1361          redo A;          redo A;
1362        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1363            !!!cp (42);
1364            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1365          !!!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  
1366          redo A;          redo A;
1367        } else {        } else {
1368          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1369            $self->{ct}->{tag_name} .= chr $self->{nc};
1370            # start tag or end tag            # start tag or end tag
1371          ## Stay in the state          ## Stay in the state
1372          !!!next-input-character;          !!!next-input-character;
1373          redo A;          redo A;
1374        }        }
1375      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1376        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1377            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1378            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1379            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1380            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1381            !!!cp (45);
1382          ## Stay in the state          ## Stay in the state
1383          !!!next-input-character;          !!!next-input-character;
1384          redo A;          redo A;
1385        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1386          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1387            $self->{current_token}->{first_start_tag}            !!!cp (46);
1388                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1389            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1390            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1391            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1392                !!!cp (47);
1393              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1394              } else {
1395                !!!cp (48);
1396            }            }
1397          } else {          } else {
1398            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1399          }          }
1400          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1401          !!!next-input-character;          !!!next-input-character;
1402    
1403          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1404    
1405          redo A;          redo A;
1406        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1407                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1408          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1409                                value => ''};          $self->{ca}
1410                = {name => chr ($self->{nc} + 0x0020),
1411                   value => '',
1412                   line => $self->{line}, column => $self->{column}};
1413          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1414          !!!next-input-character;          !!!next-input-character;
1415          redo A;          redo A;
1416        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1417            !!!cp (50);
1418            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1419          !!!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  
1420          redo A;          redo A;
1421        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1422          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1423          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1424            $self->{current_token}->{first_start_tag}            !!!cp (52);
1425                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1426            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1427            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1428            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1429                !!!cp (53);
1430              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1431              } else {
1432                !!!cp (54);
1433            }            }
1434          } else {          } else {
1435            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1436          }          }
1437          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1438          # reconsume          # reconsume
1439    
1440          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1441    
1442          redo A;          redo A;
1443        } else {        } else {
1444          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1445                                value => ''};               0x0022 => 1, # "
1446                 0x0027 => 1, # '
1447                 0x003D => 1, # =
1448                }->{$self->{nc}}) {
1449              !!!cp (55);
1450              !!!parse-error (type => 'bad attribute name');
1451            } else {
1452              !!!cp (56);
1453            }
1454            $self->{ca}
1455                = {name => chr ($self->{nc}),
1456                   value => '',
1457                   line => $self->{line}, column => $self->{column}};
1458          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1459          !!!next-input-character;          !!!next-input-character;
1460          redo A;          redo A;
1461        }        }
1462      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1463        my $before_leave = sub {        my $before_leave = sub {
1464          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1465              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1466            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1467            ## Discard $self->{current_attribute} # MUST            !!!parse-error (type => 'duplicate attribute', text => $self->{ca}->{name}, line => $self->{ca}->{line}, column => $self->{ca}->{column});
1468          } else {            ## Discard $self->{ca} # MUST
1469            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}          } else {
1470              = $self->{current_attribute};            !!!cp (58);
1471              $self->{ct}->{attributes}->{$self->{ca}->{name}}
1472                = $self->{ca};
1473          }          }
1474        }; # $before_leave        }; # $before_leave
1475    
1476        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1477            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1478            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1479            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1480            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1481            !!!cp (59);
1482          $before_leave->();          $before_leave->();
1483          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1484          !!!next-input-character;          !!!next-input-character;
1485          redo A;          redo A;
1486        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1487            !!!cp (60);
1488          $before_leave->();          $before_leave->();
1489          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1490          !!!next-input-character;          !!!next-input-character;
1491          redo A;          redo A;
1492        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1493          $before_leave->();          $before_leave->();
1494          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1495            $self->{current_token}->{first_start_tag}            !!!cp (61);
1496                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1497            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1498          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {            !!!cp (62);
1499            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1500            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1501              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1502            }            }
1503          } else {          } else {
1504            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1505          }          }
1506          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1507          !!!next-input-character;          !!!next-input-character;
1508    
1509          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1510    
1511          redo A;          redo A;
1512        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1513                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1514          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1515            $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1516          ## Stay in the state          ## Stay in the state
1517          !!!next-input-character;          !!!next-input-character;
1518          redo A;          redo A;
1519        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1520            !!!cp (64);
1521          $before_leave->();          $before_leave->();
1522            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1523          !!!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  
1524          redo A;          redo A;
1525        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1526          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1527          $before_leave->();          $before_leave->();
1528          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1529            $self->{current_token}->{first_start_tag}            !!!cp (66);
1530                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1531            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1532            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1533            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1534                !!!cp (67);
1535              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1536              } else {
1537                ## NOTE: This state should never be reached.
1538                !!!cp (68);
1539            }            }
1540          } else {          } else {
1541            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1542          }          }
1543          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1544          # reconsume          # reconsume
1545    
1546          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1547    
1548          redo A;          redo A;
1549        } else {        } else {
1550          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x0022 or # "
1551                $self->{nc} == 0x0027) { # '
1552              !!!cp (69);
1553              !!!parse-error (type => 'bad attribute name');
1554            } else {
1555              !!!cp (70);
1556            }
1557            $self->{ca}->{name} .= chr ($self->{nc});
1558          ## Stay in the state          ## Stay in the state
1559          !!!next-input-character;          !!!next-input-character;
1560          redo A;          redo A;
1561        }        }
1562      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1563        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1564            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1565            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1566            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1567            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1568            !!!cp (71);
1569          ## Stay in the state          ## Stay in the state
1570          !!!next-input-character;          !!!next-input-character;
1571          redo A;          redo A;
1572        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1573            !!!cp (72);
1574          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1575          !!!next-input-character;          !!!next-input-character;
1576          redo A;          redo A;
1577        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1578          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1579            $self->{current_token}->{first_start_tag}            !!!cp (73);
1580                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1581            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1582            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1583            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1584                !!!cp (74);
1585              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1586              } else {
1587                ## NOTE: This state should never be reached.
1588                !!!cp (75);
1589            }            }
1590          } else {          } else {
1591            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1592          }          }
1593          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1594          !!!next-input-character;          !!!next-input-character;
1595    
1596          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1597    
1598          redo A;          redo A;
1599        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1600                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1601          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1602                                value => ''};          $self->{ca}
1603                = {name => chr ($self->{nc} + 0x0020),
1604                   value => '',
1605                   line => $self->{line}, column => $self->{column}};
1606          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1607          !!!next-input-character;          !!!next-input-character;
1608          redo A;          redo A;
1609        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1610            !!!cp (77);
1611            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1612          !!!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  
1613          redo A;          redo A;
1614        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1615          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1616          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1617            $self->{current_token}->{first_start_tag}            !!!cp (79);
1618                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1619            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1620            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1621            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1622                !!!cp (80);
1623              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1624              } else {
1625                ## NOTE: This state should never be reached.
1626                !!!cp (81);
1627            }            }
1628          } else {          } else {
1629            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1630          }          }
1631          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1632          # reconsume          # reconsume
1633    
1634          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1635    
1636          redo A;          redo A;
1637        } else {        } else {
1638          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{nc} == 0x0022 or # "
1639                                value => ''};              $self->{nc} == 0x0027) { # '
1640              !!!cp (78);
1641              !!!parse-error (type => 'bad attribute name');
1642            } else {
1643              !!!cp (82);
1644            }
1645            $self->{ca}
1646                = {name => chr ($self->{nc}),
1647                   value => '',
1648                   line => $self->{line}, column => $self->{column}};
1649          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1650          !!!next-input-character;          !!!next-input-character;
1651          redo A;                  redo A;        
1652        }        }
1653      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1654        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1655            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1656            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1657            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1658            $self->{next_input_character} == 0x0020) { # SP                  $self->{nc} == 0x0020) { # SP      
1659            !!!cp (83);
1660          ## Stay in the state          ## Stay in the state
1661          !!!next-input-character;          !!!next-input-character;
1662          redo A;          redo A;
1663        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
1664            !!!cp (84);
1665          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1666          !!!next-input-character;          !!!next-input-character;
1667          redo A;          redo A;
1668        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1669            !!!cp (85);
1670          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1671          ## reconsume          ## reconsume
1672          redo A;          redo A;
1673        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
1674            !!!cp (86);
1675          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1679          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          !!!parse-error (type => 'empty unquoted attribute value');
1680            $self->{current_token}->{first_start_tag}          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1681                = not defined $self->{last_emitted_start_tag_name};            !!!cp (87);
1682            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1683          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1684            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1685            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1686                !!!cp (88);
1687              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1688              } else {
1689                ## NOTE: This state should never be reached.
1690                !!!cp (89);
1691            }            }
1692          } else {          } else {
1693            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1694          }          }
1695          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1696          !!!next-input-character;          !!!next-input-character;
1697    
1698          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1699    
1700          redo A;          redo A;
1701        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1702          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1703          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1704            $self->{current_token}->{first_start_tag}            !!!cp (90);
1705                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1706            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1707            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1708            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1709                !!!cp (91);
1710              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1711              } else {
1712                ## NOTE: This state should never be reached.
1713                !!!cp (92);
1714            }            }
1715          } else {          } else {
1716            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1717          }          }
1718          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1719          ## reconsume          ## reconsume
1720    
1721          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1722    
1723          redo A;          redo A;
1724        } else {        } else {
1725          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x003D) { # =
1726              !!!cp (93);
1727              !!!parse-error (type => 'bad attribute value');
1728            } else {
1729              !!!cp (94);
1730            }
1731            $self->{ca}->{value} .= chr ($self->{nc});
1732          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1733          !!!next-input-character;          !!!next-input-character;
1734          redo A;          redo A;
1735        }        }
1736      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1737        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
1738          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1739          !!!next-input-character;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1740          redo A;          !!!next-input-character;
1741        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1742          $self->{last_attribute_value_state} = $self->{state};        } elsif ($self->{nc} == 0x0026) { # &
1743          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          !!!cp (96);
1744            ## NOTE: In the spec, the tokenizer is switched to the
1745            ## "entity in attribute value state".  In this implementation, the
1746            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1747            ## implementation of the "consume a character reference" algorithm.
1748            $self->{prev_state} = $self->{state};
1749            $self->{entity_add} = 0x0022; # "
1750            $self->{state} = ENTITY_STATE;
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
1753        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1754          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1755          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1756            $self->{current_token}->{first_start_tag}            !!!cp (97);
1757                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1758            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1759            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1760            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1761                !!!cp (98);
1762              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1763              } else {
1764                ## NOTE: This state should never be reached.
1765                !!!cp (99);
1766            }            }
1767          } else {          } else {
1768            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1769          }          }
1770          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1771          ## reconsume          ## reconsume
1772    
1773          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1774    
1775          redo A;          redo A;
1776        } else {        } else {
1777          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1778            $self->{ca}->{value} .= chr ($self->{nc});
1779            $self->{read_until}->($self->{ca}->{value},
1780                                  q["&],
1781                                  length $self->{ca}->{value});
1782    
1783          ## Stay in the state          ## Stay in the state
1784          !!!next-input-character;          !!!next-input-character;
1785          redo A;          redo A;
1786        }        }
1787      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1788        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
1789          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1790          !!!next-input-character;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1791          redo A;          !!!next-input-character;
1792        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1793          $self->{last_attribute_value_state} = $self->{state};        } elsif ($self->{nc} == 0x0026) { # &
1794          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          !!!cp (102);
1795            ## NOTE: In the spec, the tokenizer is switched to the
1796            ## "entity in attribute value state".  In this implementation, the
1797            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1798            ## implementation of the "consume a character reference" algorithm.
1799            $self->{entity_add} = 0x0027; # '
1800            $self->{prev_state} = $self->{state};
1801            $self->{state} = ENTITY_STATE;
1802          !!!next-input-character;          !!!next-input-character;
1803          redo A;          redo A;
1804        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1805          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1806          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1807            $self->{current_token}->{first_start_tag}            !!!cp (103);
1808                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1809            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1810            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1811            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1812                !!!cp (104);
1813              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1814              } else {
1815                ## NOTE: This state should never be reached.
1816                !!!cp (105);
1817            }            }
1818          } else {          } else {
1819            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1820          }          }
1821          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1822          ## reconsume          ## reconsume
1823    
1824          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1825    
1826          redo A;          redo A;
1827        } else {        } else {
1828          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1829            $self->{ca}->{value} .= chr ($self->{nc});
1830            $self->{read_until}->($self->{ca}->{value},
1831                                  q['&],
1832                                  length $self->{ca}->{value});
1833    
1834          ## Stay in the state          ## Stay in the state
1835          !!!next-input-character;          !!!next-input-character;
1836          redo A;          redo A;
1837        }        }
1838      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1839        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1840            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1841            $self->{next_input_character} == 0x000B or # HT            $self->{nc} == 0x000B or # HT
1842            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1843            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1844            !!!cp (107);
1845          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1846          !!!next-input-character;          !!!next-input-character;
1847          redo A;          redo A;
1848        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1849          $self->{last_attribute_value_state} = $self->{state};          !!!cp (108);
1850          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## NOTE: In the spec, the tokenizer is switched to the
1851          !!!next-input-character;          ## "entity in attribute value state".  In this implementation, the
1852          redo A;          ## tokenizer is switched to the |ENTITY_STATE|, which is an
1853        } elsif ($self->{next_input_character} == 0x003E) { # >          ## implementation of the "consume a character reference" algorithm.
1854          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          $self->{entity_add} = -1;
1855            $self->{current_token}->{first_start_tag}          $self->{prev_state} = $self->{state};
1856                = not defined $self->{last_emitted_start_tag_name};          $self->{state} = ENTITY_STATE;
1857            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          !!!next-input-character;
1858          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          redo A;
1859          } elsif ($self->{nc} == 0x003E) { # >
1860            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1861              !!!cp (109);
1862              $self->{last_stag_name} = $self->{ct}->{tag_name};
1863            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1864            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1865            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1866                !!!cp (110);
1867              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1868              } else {
1869                ## NOTE: This state should never be reached.
1870                !!!cp (111);
1871            }            }
1872          } else {          } else {
1873            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1874          }          }
1875          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1876          !!!next-input-character;          !!!next-input-character;
1877    
1878          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1879    
1880          redo A;          redo A;
1881        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1882          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1883          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1884            $self->{current_token}->{first_start_tag}            !!!cp (112);
1885                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1886            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1887            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1888            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1889                !!!cp (113);
1890              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1891              } else {
1892                ## NOTE: This state should never be reached.
1893                !!!cp (114);
1894            }            }
1895          } else {          } else {
1896            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1897          }          }
1898          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1899          ## reconsume          ## reconsume
1900    
1901          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1902    
1903          redo A;          redo A;
1904        } else {        } else {
1905          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1906                 0x0022 => 1, # "
1907                 0x0027 => 1, # '
1908                 0x003D => 1, # =
1909                }->{$self->{nc}}) {
1910              !!!cp (115);
1911              !!!parse-error (type => 'bad attribute value');
1912            } else {
1913              !!!cp (116);
1914            }
1915            $self->{ca}->{value} .= chr ($self->{nc});
1916            $self->{read_until}->($self->{ca}->{value},
1917                                  q["'=& >],
1918                                  length $self->{ca}->{value});
1919    
1920          ## Stay in the state          ## Stay in the state
1921          !!!next-input-character;          !!!next-input-character;
1922          redo A;          redo A;
1923        }        }
1924      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1925        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        if ($self->{nc} == 0x0009 or # HT
1926              $self->{nc} == 0x000A or # LF
1927              $self->{nc} == 0x000B or # VT
1928              $self->{nc} == 0x000C or # FF
1929              $self->{nc} == 0x0020) { # SP
1930            !!!cp (118);
1931            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1932            !!!next-input-character;
1933            redo A;
1934          } elsif ($self->{nc} == 0x003E) { # >
1935            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1936              !!!cp (119);
1937              $self->{last_stag_name} = $self->{ct}->{tag_name};
1938            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1939              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1940              if ($self->{ct}->{attributes}) {
1941                !!!cp (120);
1942                !!!parse-error (type => 'end tag attribute');
1943              } else {
1944                ## NOTE: This state should never be reached.
1945                !!!cp (121);
1946              }
1947            } else {
1948              die "$0: $self->{ct}->{type}: Unknown token type";
1949            }
1950            $self->{state} = DATA_STATE;
1951            !!!next-input-character;
1952    
1953            !!!emit ($self->{ct}); # start tag or end tag
1954    
1955        unless (defined $token) {          redo A;
1956          $self->{current_attribute}->{value} .= '&';        } elsif ($self->{nc} == 0x002F) { # /
1957            !!!cp (122);
1958            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1959            !!!next-input-character;
1960            redo A;
1961          } elsif ($self->{nc} == -1) {
1962            !!!parse-error (type => 'unclosed tag');
1963            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1964              !!!cp (122.3);
1965              $self->{last_stag_name} = $self->{ct}->{tag_name};
1966            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1967              if ($self->{ct}->{attributes}) {
1968                !!!cp (122.1);
1969                !!!parse-error (type => 'end tag attribute');
1970              } else {
1971                ## NOTE: This state should never be reached.
1972                !!!cp (122.2);
1973              }
1974            } else {
1975              die "$0: $self->{ct}->{type}: Unknown token type";
1976            }
1977            $self->{state} = DATA_STATE;
1978            ## Reconsume.
1979            !!!emit ($self->{ct}); # start tag or end tag
1980            redo A;
1981        } else {        } else {
1982          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
1983          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
1984            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1985            ## reconsume
1986            redo A;
1987        }        }
1988        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1989          if ($self->{nc} == 0x003E) { # >
1990            if ($self->{ct}->{type} == END_TAG_TOKEN) {
1991              !!!cp ('124.2');
1992              !!!parse-error (type => 'nestc', token => $self->{ct});
1993              ## TODO: Different type than slash in start tag
1994              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1995              if ($self->{ct}->{attributes}) {
1996                !!!cp ('124.4');
1997                !!!parse-error (type => 'end tag attribute');
1998              } else {
1999                !!!cp ('124.5');
2000              }
2001              ## TODO: Test |<title></title/>|
2002            } else {
2003              !!!cp ('124.3');
2004              $self->{self_closing} = 1;
2005            }
2006    
2007        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
2008        # next-input-character is already done          !!!next-input-character;
2009        redo A;  
2010            !!!emit ($self->{ct}); # start tag or end tag
2011    
2012            redo A;
2013          } elsif ($self->{nc} == -1) {
2014            !!!parse-error (type => 'unclosed tag');
2015            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2016              !!!cp (124.7);
2017              $self->{last_stag_name} = $self->{ct}->{tag_name};
2018            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2019              if ($self->{ct}->{attributes}) {
2020                !!!cp (124.5);
2021                !!!parse-error (type => 'end tag attribute');
2022              } else {
2023                ## NOTE: This state should never be reached.
2024                !!!cp (124.6);
2025              }
2026            } else {
2027              die "$0: $self->{ct}->{type}: Unknown token type";
2028            }
2029            $self->{state} = DATA_STATE;
2030            ## Reconsume.
2031            !!!emit ($self->{ct}); # start tag or end tag
2032            redo A;
2033          } else {
2034            !!!cp ('124.4');
2035            !!!parse-error (type => 'nestc');
2036            ## TODO: This error type is wrong.
2037            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2038            ## Reconsume.
2039            redo A;
2040          }
2041      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2042        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
         
       my $token = {type => COMMENT_TOKEN, data => ''};  
2043    
2044        BC: {        ## NOTE: Unlike spec's "bogus comment state", this implementation
2045          if ($self->{next_input_character} == 0x003E) { # >        ## consumes characters one-by-one basis.
2046            $self->{state} = DATA_STATE;        
2047            !!!next-input-character;        if ($self->{nc} == 0x003E) { # >
2048            !!!cp (124);
2049            !!!emit ($token);          $self->{state} = DATA_STATE;
2050            !!!next-input-character;
2051    
2052            redo A;          !!!emit ($self->{ct}); # comment
2053          } elsif ($self->{next_input_character} == -1) {          redo A;
2054            $self->{state} = DATA_STATE;        } elsif ($self->{nc} == -1) {
2055            ## reconsume          !!!cp (125);
2056            $self->{state} = DATA_STATE;
2057            ## reconsume
2058    
2059            !!!emit ($token);          !!!emit ($self->{ct}); # comment
2060            redo A;
2061          } else {
2062            !!!cp (126);
2063            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2064            $self->{read_until}->($self->{ct}->{data},
2065                                  q[>],
2066                                  length $self->{ct}->{data});
2067    
2068            redo A;          ## Stay in the state.
2069          } else {          !!!next-input-character;
2070            $token->{data} .= chr ($self->{next_input_character});          redo A;
2071            !!!next-input-character;        }
           redo BC;  
         }  
       } # BC  
2072      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2073        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my @next_char;  
       push @next_char, $self->{next_input_character};  
2074                
2075        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2076            !!!cp (133);
2077            $self->{state} = MD_HYPHEN_STATE;
2078          !!!next-input-character;          !!!next-input-character;
2079          push @next_char, $self->{next_input_character};          redo A;
2080          if ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x0044 or # D
2081            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};                 $self->{nc} == 0x0064) { # d
2082            $self->{state} = COMMENT_START_STATE;          ## ASCII case-insensitive.
2083            !!!next-input-character;          !!!cp (130);
2084            redo A;          $self->{state} = MD_DOCTYPE_STATE;
2085          }          $self->{s_kwd} = chr $self->{nc};
       } elsif ($self->{next_input_character} == 0x0044 or # D  
                $self->{next_input_character} == 0x0064) { # d  
2086          !!!next-input-character;          !!!next-input-character;
2087          push @next_char, $self->{next_input_character};          redo A;
2088          if ($self->{next_input_character} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2089              $self->{next_input_character} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2090            !!!next-input-character;                 $self->{nc} == 0x005B) { # [
2091            push @next_char, $self->{next_input_character};          !!!cp (135.4);                
2092            if ($self->{next_input_character} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2093                $self->{next_input_character} == 0x0063) { # c          $self->{s_kwd} = '[';
2094              !!!next-input-character;          !!!next-input-character;
2095              push @next_char, $self->{next_input_character};          redo A;
2096              if ($self->{next_input_character} == 0x0054 or # T        } else {
2097                  $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;  
                   }  
                 }  
               }  
             }  
           }  
         }  
2098        }        }
2099    
2100        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2101        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2102        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2103          ## Reconsume.
2104        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2105          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2106                                    line => $self->{line_prev},
2107                                    column => $self->{column_prev} - 1,
2108                                   };
2109        redo A;        redo A;
2110              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2111        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2112        ## 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);
2113            $self->{ct} = {type => COMMENT_TOKEN, data => '',
2114                                      line => $self->{line_prev},
2115                                      column => $self->{column_prev} - 2,
2116                                     };
2117            $self->{state} = COMMENT_START_STATE;
2118            !!!next-input-character;
2119            redo A;
2120          } else {
2121            !!!cp (128);
2122            !!!parse-error (type => 'bogus comment',
2123                            line => $self->{line_prev},
2124                            column => $self->{column_prev} - 2);
2125            $self->{state} = BOGUS_COMMENT_STATE;
2126            ## Reconsume.
2127            $self->{ct} = {type => COMMENT_TOKEN,
2128                                      data => '-',
2129                                      line => $self->{line_prev},
2130                                      column => $self->{column_prev} - 2,
2131                                     };
2132            redo A;
2133          }
2134        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2135          ## ASCII case-insensitive.
2136          if ($self->{nc} == [
2137                undef,
2138                0x004F, # O
2139                0x0043, # C
2140                0x0054, # T
2141                0x0059, # Y
2142                0x0050, # P
2143              ]->[length $self->{s_kwd}] or
2144              $self->{nc} == [
2145                undef,
2146                0x006F, # o
2147                0x0063, # c
2148                0x0074, # t
2149                0x0079, # y
2150                0x0070, # p
2151              ]->[length $self->{s_kwd}]) {
2152            !!!cp (131);
2153            ## Stay in the state.
2154            $self->{s_kwd} .= chr $self->{nc};
2155            !!!next-input-character;
2156            redo A;
2157          } elsif ((length $self->{s_kwd}) == 6 and
2158                   ($self->{nc} == 0x0045 or # E
2159                    $self->{nc} == 0x0065)) { # e
2160            !!!cp (129);
2161            $self->{state} = DOCTYPE_STATE;
2162            $self->{ct} = {type => DOCTYPE_TOKEN,
2163                                      quirks => 1,
2164                                      line => $self->{line_prev},
2165                                      column => $self->{column_prev} - 7,
2166                                     };
2167            !!!next-input-character;
2168            redo A;
2169          } else {
2170            !!!cp (132);        
2171            !!!parse-error (type => 'bogus comment',
2172                            line => $self->{line_prev},
2173                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2174            $self->{state} = BOGUS_COMMENT_STATE;
2175            ## Reconsume.
2176            $self->{ct} = {type => COMMENT_TOKEN,
2177                                      data => $self->{s_kwd},
2178                                      line => $self->{line_prev},
2179                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2180                                     };
2181            redo A;
2182          }
2183        } elsif ($self->{state} == MD_CDATA_STATE) {
2184          if ($self->{nc} == {
2185                '[' => 0x0043, # C
2186                '[C' => 0x0044, # D
2187                '[CD' => 0x0041, # A
2188                '[CDA' => 0x0054, # T
2189                '[CDAT' => 0x0041, # A
2190              }->{$self->{s_kwd}}) {
2191            !!!cp (135.1);
2192            ## Stay in the state.
2193            $self->{s_kwd} .= chr $self->{nc};
2194            !!!next-input-character;
2195            redo A;
2196          } elsif ($self->{s_kwd} eq '[CDATA' and
2197                   $self->{nc} == 0x005B) { # [
2198            !!!cp (135.2);
2199            $self->{ct} = {type => CHARACTER_TOKEN,
2200                                      data => '',
2201                                      line => $self->{line_prev},
2202                                      column => $self->{column_prev} - 7};
2203            $self->{state} = CDATA_SECTION_STATE;
2204            !!!next-input-character;
2205            redo A;
2206          } else {
2207            !!!cp (135.3);
2208            !!!parse-error (type => 'bogus comment',
2209                            line => $self->{line_prev},
2210                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2211            $self->{state} = BOGUS_COMMENT_STATE;
2212            ## Reconsume.
2213            $self->{ct} = {type => COMMENT_TOKEN,
2214                                      data => $self->{s_kwd},
2215                                      line => $self->{line_prev},
2216                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2217                                     };
2218            redo A;
2219          }
2220      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2221        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2222            !!!cp (137);
2223          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2224          !!!next-input-character;          !!!next-input-character;
2225          redo A;          redo A;
2226        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2227            !!!cp (138);
2228          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2229          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2230          !!!next-input-character;          !!!next-input-character;
2231    
2232          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2233    
2234          redo A;          redo A;
2235        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2236            !!!cp (139);
2237          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2238          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2239          ## reconsume          ## reconsume
2240    
2241          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2242    
2243          redo A;          redo A;
2244        } else {        } else {
2245          $self->{current_token}->{data} # comment          !!!cp (140);
2246              .= chr ($self->{next_input_character});          $self->{ct}->{data} # comment
2247                .= chr ($self->{nc});
2248          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2249          !!!next-input-character;          !!!next-input-character;
2250          redo A;          redo A;
2251        }        }
2252      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2253        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2254            !!!cp (141);
2255          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2256          !!!next-input-character;          !!!next-input-character;
2257          redo A;          redo A;
2258        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2259            !!!cp (142);
2260          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2261          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2262          !!!next-input-character;          !!!next-input-character;
2263    
2264          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2265    
2266          redo A;          redo A;
2267        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2268            !!!cp (143);
2269          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2270          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2271          ## reconsume          ## reconsume
2272    
2273          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2274    
2275          redo A;          redo A;
2276        } else {        } else {
2277          $self->{current_token}->{data} # comment          !!!cp (144);
2278              .= '-' . chr ($self->{next_input_character});          $self->{ct}->{data} # comment
2279                .= '-' . chr ($self->{nc});
2280          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2281          !!!next-input-character;          !!!next-input-character;
2282          redo A;          redo A;
2283        }        }
2284      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2285        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2286            !!!cp (145);
2287          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2288          !!!next-input-character;          !!!next-input-character;
2289          redo A;          redo A;
2290        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2291            !!!cp (146);
2292          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2293          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2294          ## reconsume          ## reconsume
2295    
2296          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2297    
2298          redo A;          redo A;
2299        } else {        } else {
2300          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2301            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2302            $self->{read_until}->($self->{ct}->{data},
2303                                  q[-],
2304                                  length $self->{ct}->{data});
2305    
2306          ## Stay in the state          ## Stay in the state
2307          !!!next-input-character;          !!!next-input-character;
2308          redo A;          redo A;
2309        }        }
2310      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2311        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2312            !!!cp (148);
2313          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2314          !!!next-input-character;          !!!next-input-character;
2315          redo A;          redo A;
2316        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2317            !!!cp (149);
2318          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2319          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2320          ## reconsume          ## reconsume
2321    
2322          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2323    
2324          redo A;          redo A;
2325        } else {        } else {
2326          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2327            $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2328          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2329          !!!next-input-character;          !!!next-input-character;
2330          redo A;          redo A;
2331        }        }
2332      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2333        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2334            !!!cp (151);
2335          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2336          !!!next-input-character;          !!!next-input-character;
2337    
2338          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2339    
2340          redo A;          redo A;
2341        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2342          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2343          $self->{current_token}->{data} .= '-'; # comment          !!!parse-error (type => 'dash in comment',
2344                            line => $self->{line_prev},
2345                            column => $self->{column_prev});
2346            $self->{ct}->{data} .= '-'; # comment
2347          ## Stay in the state          ## Stay in the state
2348          !!!next-input-character;          !!!next-input-character;
2349          redo A;          redo A;
2350        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2351            !!!cp (153);
2352          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2353          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2354          ## reconsume          ## reconsume
2355    
2356          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2357    
2358          redo A;          redo A;
2359        } else {        } else {
2360          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2361          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2362                            line => $self->{line_prev},
2363                            column => $self->{column_prev});
2364            $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2365          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2366          !!!next-input-character;          !!!next-input-character;
2367          redo A;          redo A;
2368        }        }
2369      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2370        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2371            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2372            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2373            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2374            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2375            !!!cp (155);
2376          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2377          !!!next-input-character;          !!!next-input-character;
2378          redo A;          redo A;
2379        } else {        } else {
2380            !!!cp (156);
2381          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2382          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2383          ## reconsume          ## reconsume
2384          redo A;          redo A;
2385        }        }
2386      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2387        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2388            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2389            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2390            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2391            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2392            !!!cp (157);
2393          ## Stay in the state          ## Stay in the state
2394          !!!next-input-character;          !!!next-input-character;
2395          redo A;          redo A;
2396        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2397            !!!cp (158);
2398          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2399          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2400          !!!next-input-character;          !!!next-input-character;
2401    
2402          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2403    
2404          redo A;          redo A;
2405        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2406            !!!cp (159);
2407          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2408          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2409          ## reconsume          ## reconsume
2410    
2411          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2412    
2413          redo A;          redo A;
2414        } else {        } else {
2415          $self->{current_token}          !!!cp (160);
2416              = {type => DOCTYPE_TOKEN,          $self->{ct}->{name} = chr $self->{nc};
2417                 name => chr ($self->{next_input_character}),          delete $self->{ct}->{quirks};
                correct => 1};  
2418  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2419          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2420          !!!next-input-character;          !!!next-input-character;
# Line 1299  sub _get_next_token ($) { Line 2422  sub _get_next_token ($) {
2422        }        }
2423      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2424  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2425        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2426            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2427            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2428            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2429            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2430            !!!cp (161);
2431          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2432          !!!next-input-character;          !!!next-input-character;
2433          redo A;          redo A;
2434        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2435            !!!cp (162);
2436          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2437          !!!next-input-character;          !!!next-input-character;
2438    
2439          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2440    
2441          redo A;          redo A;
2442        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2443            !!!cp (163);
2444          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2445          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2446          ## reconsume          ## reconsume
2447    
2448          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2449          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2450    
2451          redo A;          redo A;
2452        } else {        } else {
2453          $self->{current_token}->{name}          !!!cp (164);
2454            .= chr ($self->{next_input_character}); # DOCTYPE          $self->{ct}->{name}
2455              .= chr ($self->{nc}); # DOCTYPE
2456          ## Stay in the state          ## Stay in the state
2457          !!!next-input-character;          !!!next-input-character;
2458          redo A;          redo A;
2459        }        }
2460      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2461        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2462            $self->{next_input_character} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2463            $self->{next_input_character} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2464            $self->{next_input_character} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2465            $self->{next_input_character} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2466            !!!cp (165);
2467          ## Stay in the state          ## Stay in the state
2468          !!!next-input-character;          !!!next-input-character;
2469          redo A;          redo A;
2470        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2471            !!!cp (166);
2472          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2473          !!!next-input-character;          !!!next-input-character;
2474    
2475          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2476    
2477          redo A;          redo A;
2478        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2479            !!!cp (167);
2480          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2481          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2482          ## reconsume          ## reconsume
2483    
2484          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2485          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2486    
2487          redo A;          redo A;
2488        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{nc} == 0x0050 or # P
2489                 $self->{next_input_character} == 0x0070) { # p                 $self->{nc} == 0x0070) { # p
2490            $self->{state} = PUBLIC_STATE;
2491            $self->{s_kwd} = chr $self->{nc};
2492          !!!next-input-character;          !!!next-input-character;
2493          if ($self->{next_input_character} == 0x0055 or # U          redo A;
2494              $self->{next_input_character} == 0x0075) { # u        } elsif ($self->{nc} == 0x0053 or # S
2495            !!!next-input-character;                 $self->{nc} == 0x0073) { # s
2496            if ($self->{next_input_character} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2497                $self->{next_input_character} == 0x0062) { # b          $self->{s_kwd} = chr $self->{nc};
             !!!next-input-character;  
             if ($self->{next_input_character} == 0x004C or # L  
                 $self->{next_input_character} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_input_character} == 0x0049 or # I  
                   $self->{next_input_character} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_input_character} == 0x0043 or # C  
                     $self->{next_input_character} == 0x0063) { # c  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
       } elsif ($self->{next_input_character} == 0x0053 or # S  
                $self->{next_input_character} == 0x0073) { # s  
2498          !!!next-input-character;          !!!next-input-character;
2499          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;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
2500        } else {        } else {
2501            !!!cp (180);
2502            !!!parse-error (type => 'string after DOCTYPE name');
2503            $self->{ct}->{quirks} = 1;
2504    
2505            $self->{state} = BOGUS_DOCTYPE_STATE;
2506          !!!next-input-character;          !!!next-input-character;
2507          #          redo A;
2508          }
2509        } elsif ($self->{state} == PUBLIC_STATE) {
2510          ## ASCII case-insensitive
2511          if ($self->{nc} == [
2512                undef,
2513                0x0055, # U
2514                0x0042, # B
2515                0x004C, # L
2516                0x0049, # I
2517              ]->[length $self->{s_kwd}] or
2518              $self->{nc} == [
2519                undef,
2520                0x0075, # u
2521                0x0062, # b
2522                0x006C, # l
2523                0x0069, # i
2524              ]->[length $self->{s_kwd}]) {
2525            !!!cp (175);
2526            ## Stay in the state.
2527            $self->{s_kwd} .= chr $self->{nc};
2528            !!!next-input-character;
2529            redo A;
2530          } elsif ((length $self->{s_kwd}) == 5 and
2531                   ($self->{nc} == 0x0043 or # C
2532                    $self->{nc} == 0x0063)) { # c
2533            !!!cp (168);
2534            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2535            !!!next-input-character;
2536            redo A;
2537          } else {
2538            !!!cp (169);
2539            !!!parse-error (type => 'string after DOCTYPE name',
2540                            line => $self->{line_prev},
2541                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2542            $self->{ct}->{quirks} = 1;
2543    
2544            $self->{state} = BOGUS_DOCTYPE_STATE;
2545            ## Reconsume.
2546            redo A;
2547        }        }
2548        } elsif ($self->{state} == SYSTEM_STATE) {
2549          ## ASCII case-insensitive
2550          if ($self->{nc} == [
2551                undef,
2552                0x0059, # Y
2553                0x0053, # S
2554                0x0054, # T
2555                0x0045, # E
2556              ]->[length $self->{s_kwd}] or
2557              $self->{nc} == [
2558                undef,
2559                0x0079, # y
2560                0x0073, # s
2561                0x0074, # t
2562                0x0065, # e
2563              ]->[length $self->{s_kwd}]) {
2564            !!!cp (170);
2565            ## Stay in the state.
2566            $self->{s_kwd} .= chr $self->{nc};
2567            !!!next-input-character;
2568            redo A;
2569          } elsif ((length $self->{s_kwd}) == 5 and
2570                   ($self->{nc} == 0x004D or # M
2571                    $self->{nc} == 0x006D)) { # m
2572            !!!cp (171);
2573            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2574            !!!next-input-character;
2575            redo A;
2576          } else {
2577            !!!cp (172);
2578            !!!parse-error (type => 'string after DOCTYPE name',
2579                            line => $self->{line_prev},
2580                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2581            $self->{ct}->{quirks} = 1;
2582    
2583        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2584        $self->{state} = BOGUS_DOCTYPE_STATE;          ## Reconsume.
2585        # next-input-character is already done          redo A;
2586        redo A;        }
2587      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2588        if ({        if ({
2589              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2590              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2591            }->{$self->{next_input_character}}) {            }->{$self->{nc}}) {
2592            !!!cp (181);
2593          ## Stay in the state          ## Stay in the state
2594          !!!next-input-character;          !!!next-input-character;
2595          redo A;          redo A;
2596        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{nc} eq 0x0022) { # "
2597          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          !!!cp (182);
2598            $self->{ct}->{pubid} = ''; # DOCTYPE
2599          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2600          !!!next-input-character;          !!!next-input-character;
2601          redo A;          redo A;
2602        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{nc} eq 0x0027) { # '
2603          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          !!!cp (183);
2604            $self->{ct}->{pubid} = ''; # DOCTYPE
2605          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2606          !!!next-input-character;          !!!next-input-character;
2607          redo A;          redo A;
2608        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{nc} eq 0x003E) { # >
2609            !!!cp (184);
2610          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2611    
2612          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2613          !!!next-input-character;          !!!next-input-character;
2614    
2615          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2616          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2617    
2618          redo A;          redo A;
2619        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2620            !!!cp (185);
2621          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2622    
2623          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2624          ## reconsume          ## reconsume
2625    
2626          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2627          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2628    
2629          redo A;          redo A;
2630        } else {        } else {
2631            !!!cp (186);
2632          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2633            $self->{ct}->{quirks} = 1;
2634    
2635          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2636          !!!next-input-character;          !!!next-input-character;
2637          redo A;          redo A;
2638        }        }
2639      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2640        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2641            !!!cp (187);
2642          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2643          !!!next-input-character;          !!!next-input-character;
2644          redo A;          redo A;
2645        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2646            !!!cp (188);
2647            !!!parse-error (type => 'unclosed PUBLIC literal');
2648    
2649            $self->{state} = DATA_STATE;
2650            !!!next-input-character;
2651    
2652            $self->{ct}->{quirks} = 1;
2653            !!!emit ($self->{ct}); # DOCTYPE
2654    
2655            redo A;
2656          } elsif ($self->{nc} == -1) {
2657            !!!cp (189);
2658          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2659    
2660          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2661          ## reconsume          ## reconsume
2662    
2663          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2664          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2665    
2666          redo A;          redo A;
2667        } else {        } else {
2668          $self->{current_token}->{public_identifier} # DOCTYPE          !!!cp (190);
2669              .= chr $self->{next_input_character};          $self->{ct}->{pubid} # DOCTYPE
2670                .= chr $self->{nc};
2671            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2672                                  length $self->{ct}->{pubid});
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->{nc} == 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->{nc} == 0x003E) { # >
2685            !!!cp (192);
2686            !!!parse-error (type => 'unclosed PUBLIC literal');
2687    
2688            $self->{state} = DATA_STATE;
2689            !!!next-input-character;
2690    
2691            $self->{ct}->{quirks} = 1;
2692            !!!emit ($self->{ct}); # DOCTYPE
2693    
2694            redo A;
2695          } elsif ($self->{nc} == -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->{ct}->{quirks} = 1;
2703          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2704    
2705          redo A;          redo A;
2706        } else {        } else {
2707          $self->{current_token}->{public_identifier} # DOCTYPE          !!!cp (194);
2708              .= chr $self->{next_input_character};          $self->{ct}->{pubid} # DOCTYPE
2709                .= chr $self->{nc};
2710            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2711                                  length $self->{ct}->{pubid});
2712    
2713          ## Stay in the state          ## Stay in the state
2714          !!!next-input-character;          !!!next-input-character;
2715          redo A;          redo A;
# Line 1510  sub _get_next_token ($) { Line 2718  sub _get_next_token ($) {
2718        if ({        if ({
2719              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2720              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2721            }->{$self->{next_input_character}}) {            }->{$self->{nc}}) {
2722            !!!cp (195);
2723          ## Stay in the state          ## Stay in the state
2724          !!!next-input-character;          !!!next-input-character;
2725          redo A;          redo A;
2726        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2727          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (196);
2728            $self->{ct}->{sysid} = ''; # DOCTYPE
2729          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2730          !!!next-input-character;          !!!next-input-character;
2731          redo A;          redo A;
2732        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2733          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (197);
2734            $self->{ct}->{sysid} = ''; # DOCTYPE
2735          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2736          !!!next-input-character;          !!!next-input-character;
2737          redo A;          redo A;
2738        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2739            !!!cp (198);
2740          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2741          !!!next-input-character;          !!!next-input-character;
2742    
2743          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2744    
2745          redo A;          redo A;
2746        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2747            !!!cp (199);
2748          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2749    
2750          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2751          ## reconsume          ## reconsume
2752    
2753          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2754          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2755    
2756          redo A;          redo A;
2757        } else {        } else {
2758            !!!cp (200);
2759          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2760            $self->{ct}->{quirks} = 1;
2761    
2762          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2763          !!!next-input-character;          !!!next-input-character;
2764          redo A;          redo A;
# Line 1551  sub _get_next_token ($) { Line 2767  sub _get_next_token ($) {
2767        if ({        if ({
2768              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2769              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2770            }->{$self->{next_input_character}}) {            }->{$self->{nc}}) {
2771            !!!cp (201);
2772          ## Stay in the state          ## Stay in the state
2773          !!!next-input-character;          !!!next-input-character;
2774          redo A;          redo A;
2775        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2776          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (202);
2777            $self->{ct}->{sysid} = ''; # DOCTYPE
2778          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2779          !!!next-input-character;          !!!next-input-character;
2780          redo A;          redo A;
2781        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2782          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (203);
2783            $self->{ct}->{sysid} = ''; # DOCTYPE
2784          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2785          !!!next-input-character;          !!!next-input-character;
2786          redo A;          redo A;
2787        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2788            !!!cp (204);
2789          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2790          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2791          !!!next-input-character;          !!!next-input-character;
2792    
2793          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2794          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2795    
2796          redo A;          redo A;
2797        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2798            !!!cp (205);
2799          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2800    
2801          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2802          ## reconsume          ## reconsume
2803    
2804          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2805          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2806    
2807          redo A;          redo A;
2808        } else {        } else {
2809            !!!cp (206);
2810          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2811            $self->{ct}->{quirks} = 1;
2812    
2813          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2814          !!!next-input-character;          !!!next-input-character;
2815          redo A;          redo A;
2816        }        }
2817      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2818        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2819            !!!cp (207);
2820          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2821          !!!next-input-character;          !!!next-input-character;
2822          redo A;          redo A;
2823        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2824            !!!cp (208);
2825            !!!parse-error (type => 'unclosed SYSTEM literal');
2826    
2827            $self->{state} = DATA_STATE;
2828            !!!next-input-character;
2829    
2830            $self->{ct}->{quirks} = 1;
2831            !!!emit ($self->{ct}); # DOCTYPE
2832    
2833            redo A;
2834          } elsif ($self->{nc} == -1) {
2835            !!!cp (209);
2836          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2837    
2838          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2839          ## reconsume          ## reconsume
2840    
2841          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2842          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2843    
2844          redo A;          redo A;
2845        } else {        } else {
2846          $self->{current_token}->{system_identifier} # DOCTYPE          !!!cp (210);
2847              .= chr $self->{next_input_character};          $self->{ct}->{sysid} # DOCTYPE
2848                .= chr $self->{nc};
2849            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2850                                  length $self->{ct}->{sysid});
2851    
2852          ## Stay in the state          ## Stay in the state
2853          !!!next-input-character;          !!!next-input-character;
2854          redo A;          redo A;
2855        }        }
2856      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2857        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2858            !!!cp (211);
2859          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2860          !!!next-input-character;          !!!next-input-character;
2861          redo A;          redo A;
2862        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2863            !!!cp (212);
2864            !!!parse-error (type => 'unclosed SYSTEM literal');
2865    
2866            $self->{state} = DATA_STATE;
2867            !!!next-input-character;
2868    
2869            $self->{ct}->{quirks} = 1;
2870            !!!emit ($self->{ct}); # DOCTYPE
2871    
2872            redo A;
2873          } elsif ($self->{nc} == -1) {
2874            !!!cp (213);
2875          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2876    
2877          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2878          ## reconsume          ## reconsume
2879    
2880          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2881          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2882    
2883          redo A;          redo A;
2884        } else {        } else {
2885          $self->{current_token}->{system_identifier} # DOCTYPE          !!!cp (214);
2886              .= chr $self->{next_input_character};          $self->{ct}->{sysid} # DOCTYPE
2887                .= chr $self->{nc};
2888            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2889                                  length $self->{ct}->{sysid});
2890    
2891          ## Stay in the state          ## Stay in the state
2892          !!!next-input-character;          !!!next-input-character;
2893          redo A;          redo A;
# Line 1638  sub _get_next_token ($) { Line 2896  sub _get_next_token ($) {
2896        if ({        if ({
2897              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2898              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2899            }->{$self->{next_input_character}}) {            }->{$self->{nc}}) {
2900            !!!cp (215);
2901          ## Stay in the state          ## Stay in the state
2902          !!!next-input-character;          !!!next-input-character;
2903          redo A;          redo A;
2904        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2905            !!!cp (216);
2906          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2907          !!!next-input-character;          !!!next-input-character;
2908    
2909          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2910    
2911          redo A;          redo A;
2912        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2913            !!!cp (217);
2914          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2915          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2916          ## reconsume          ## reconsume
2917    
2918          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2919          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2920    
2921          redo A;          redo A;
2922        } else {        } else {
2923            !!!cp (218);
2924          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2925            #$self->{ct}->{quirks} = 1;
2926    
2927          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2928          !!!next-input-character;          !!!next-input-character;
2929          redo A;          redo A;
2930        }        }
2931      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2932        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2933            !!!cp (219);
2934          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2935          !!!next-input-character;          !!!next-input-character;
2936    
2937          delete $self->{current_token}->{correct};          !!!emit ($self->{ct}); # DOCTYPE
         !!!emit ($self->{current_token}); # DOCTYPE  
2938    
2939          redo A;          redo A;
2940        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2941            !!!cp (220);
2942          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2943          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2944          ## reconsume          ## reconsume
2945    
2946          delete $self->{current_token}->{correct};          !!!emit ($self->{ct}); # DOCTYPE
         !!!emit ($self->{current_token}); # DOCTYPE  
2947    
2948          redo A;          redo A;
2949        } else {        } else {
2950            !!!cp (221);
2951            my $s = '';
2952            $self->{read_until}->($s, q[>], 0);
2953    
2954          ## Stay in the state          ## Stay in the state
2955          !!!next-input-character;          !!!next-input-character;
2956          redo A;          redo A;
2957        }        }
2958      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2959        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2960      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2961    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2962          
2963    die "$0: _get_next_token: unexpected case";        if ($self->{nc} == 0x005D) { # ]
2964  } # _get_next_token          !!!cp (221.1);
2965            $self->{state} = CDATA_SECTION_MSE1_STATE;
2966            !!!next-input-character;
2967            redo A;
2968          } elsif ($self->{nc} == -1) {
2969            $self->{state} = DATA_STATE;
2970            !!!next-input-character;
2971            if (length $self->{ct}->{data}) { # character
2972              !!!cp (221.2);
2973              !!!emit ($self->{ct}); # character
2974            } else {
2975              !!!cp (221.3);
2976              ## No token to emit. $self->{ct} is discarded.
2977            }        
2978            redo A;
2979          } else {
2980            !!!cp (221.4);
2981            $self->{ct}->{data} .= chr $self->{nc};
2982            $self->{read_until}->($self->{ct}->{data},
2983                                  q<]>,
2984                                  length $self->{ct}->{data});
2985    
2986  sub _tokenize_attempt_to_consume_an_entity ($$) {          ## Stay in the state.
2987    my ($self, $in_attr) = @_;          !!!next-input-character;
2988            redo A;
2989          }
2990    
2991    if ({        ## ISSUE: "text tokens" in spec.
2992         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2993         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{nc} == 0x005D) { # ]
2994        }->{$self->{next_input_character}}) {          !!!cp (221.5);
2995      ## Don't consume          $self->{state} = CDATA_SECTION_MSE2_STATE;
2996      ## No error          !!!next-input-character;
2997      return undef;          redo A;
2998    } elsif ($self->{next_input_character} == 0x0023) { # #        } else {
2999      !!!next-input-character;          !!!cp (221.6);
3000      if ($self->{next_input_character} == 0x0078 or # x          $self->{ct}->{data} .= ']';
3001          $self->{next_input_character} == 0x0058) { # X          $self->{state} = CDATA_SECTION_STATE;
3002        my $code;          ## Reconsume.
3003        X: {          redo A;
3004          my $x_char = $self->{next_input_character};        }
3005          !!!next-input-character;      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3006          if (0x0030 <= $self->{next_input_character} and        if ($self->{nc} == 0x003E) { # >
3007              $self->{next_input_character} <= 0x0039) { # 0..9          $self->{state} = DATA_STATE;
3008            $code ||= 0;          !!!next-input-character;
3009            $code *= 0x10;          if (length $self->{ct}->{data}) { # character
3010            $code += $self->{next_input_character} - 0x0030;            !!!cp (221.7);
3011            redo X;            !!!emit ($self->{ct}); # character
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0066) { # a..f  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0046) { # A..F  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!parse-error (type => 'bare hcro');  
           !!!back-next-input-character ($x_char, $self->{next_input_character});  
           $self->{next_input_character} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_input_character} == 0x003B) { # ;  
           !!!next-input-character;  
3012          } else {          } else {
3013            !!!parse-error (type => 'no refc');            !!!cp (221.8);
3014              ## No token to emit. $self->{ct} is discarded.
3015          }          }
3016            redo A;
3017          } elsif ($self->{nc} == 0x005D) { # ]
3018            !!!cp (221.9); # character
3019            $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3020            ## Stay in the state.
3021            !!!next-input-character;
3022            redo A;
3023          } else {
3024            !!!cp (221.11);
3025            $self->{ct}->{data} .= ']]'; # character
3026            $self->{state} = CDATA_SECTION_STATE;
3027            ## Reconsume.
3028            redo A;
3029          }
3030        } elsif ($self->{state} == ENTITY_STATE) {
3031          if ({
3032            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3033            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3034            $self->{entity_add} => 1,
3035          }->{$self->{nc}}) {
3036            !!!cp (1001);
3037            ## Don't consume
3038            ## No error
3039            ## Return nothing.
3040            #
3041          } elsif ($self->{nc} == 0x0023) { # #
3042            !!!cp (999);
3043            $self->{state} = ENTITY_HASH_STATE;
3044            $self->{s_kwd} = '#';
3045            !!!next-input-character;
3046            redo A;
3047          } elsif ((0x0041 <= $self->{nc} and
3048                    $self->{nc} <= 0x005A) or # A..Z
3049                   (0x0061 <= $self->{nc} and
3050                    $self->{nc} <= 0x007A)) { # a..z
3051            !!!cp (998);
3052            require Whatpm::_NamedEntityList;
3053            $self->{state} = ENTITY_NAME_STATE;
3054            $self->{s_kwd} = chr $self->{nc};
3055            $self->{entity__value} = $self->{s_kwd};
3056            $self->{entity__match} = 0;
3057            !!!next-input-character;
3058            redo A;
3059          } else {
3060            !!!cp (1027);
3061            !!!parse-error (type => 'bare ero');
3062            ## Return nothing.
3063            #
3064          }
3065    
3066          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3067            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## reference" algorithm.  In other word, there is an "&" character
3068            $code = 0xFFFD;        ## that does not introduce a character reference, which would be
3069          } elsif ($code > 0x10FFFF) {        ## appended to the parent element or the attribute value in later
3070            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        ## process of the tokenizer.
3071            $code = 0xFFFD;  
3072          } elsif ($code == 0x000D) {        if ($self->{prev_state} == DATA_STATE) {
3073            !!!parse-error (type => 'CR character reference');          !!!cp (997);
3074            $code = 0x000A;          $self->{state} = $self->{prev_state};
3075          } elsif (0x80 <= $code and $code <= 0x9F) {          ## Reconsume.
3076            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3077            $code = $c1_entity_char->{$code};                    line => $self->{line_prev},
3078          }                    column => $self->{column_prev},
3079                     });
3080          return {type => CHARACTER_TOKEN, data => chr $code};          redo A;
3081        } # X        } else {
3082      } elsif (0x0030 <= $self->{next_input_character} and          !!!cp (996);
3083               $self->{next_input_character} <= 0x0039) { # 0..9          $self->{ca}->{value} .= '&';
3084        my $code = $self->{next_input_character} - 0x0030;          $self->{state} = $self->{prev_state};
3085        !!!next-input-character;          ## Reconsume.
3086                  redo A;
3087        while (0x0030 <= $self->{next_input_character} and        }
3088                  $self->{next_input_character} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3089          $code *= 10;        if ($self->{nc} == 0x0078 or # x
3090          $code += $self->{next_input_character} - 0x0030;            $self->{nc} == 0x0058) { # X
3091            !!!cp (995);
3092            $self->{state} = HEXREF_X_STATE;
3093            $self->{s_kwd} .= chr $self->{nc};
3094            !!!next-input-character;
3095            redo A;
3096          } elsif (0x0030 <= $self->{nc} and
3097                   $self->{nc} <= 0x0039) { # 0..9
3098            !!!cp (994);
3099            $self->{state} = NCR_NUM_STATE;
3100            $self->{s_kwd} = $self->{nc} - 0x0030;
3101            !!!next-input-character;
3102            redo A;
3103          } else {
3104            !!!parse-error (type => 'bare nero',
3105                            line => $self->{line_prev},
3106                            column => $self->{column_prev} - 1);
3107    
3108            ## NOTE: According to the spec algorithm, nothing is returned,
3109            ## and then "&#" is appended to the parent element or the attribute
3110            ## value in the later processing.
3111    
3112            if ($self->{prev_state} == DATA_STATE) {
3113              !!!cp (1019);
3114              $self->{state} = $self->{prev_state};
3115              ## Reconsume.
3116              !!!emit ({type => CHARACTER_TOKEN,
3117                        data => '&#',
3118                        line => $self->{line_prev},
3119                        column => $self->{column_prev} - 1,
3120                       });
3121              redo A;
3122            } else {
3123              !!!cp (993);
3124              $self->{ca}->{value} .= '&#';
3125              $self->{state} = $self->{prev_state};
3126              ## Reconsume.
3127              redo A;
3128            }
3129          }
3130        } elsif ($self->{state} == NCR_NUM_STATE) {
3131          if (0x0030 <= $self->{nc} and
3132              $self->{nc} <= 0x0039) { # 0..9
3133            !!!cp (1012);
3134            $self->{s_kwd} *= 10;
3135            $self->{s_kwd} += $self->{nc} - 0x0030;
3136                    
3137            ## Stay in the state.
3138            !!!next-input-character;
3139            redo A;
3140          } elsif ($self->{nc} == 0x003B) { # ;
3141            !!!cp (1013);
3142          !!!next-input-character;          !!!next-input-character;
3143            #
3144          } else {
3145            !!!cp (1014);
3146            !!!parse-error (type => 'no refc');
3147            ## Reconsume.
3148            #
3149        }        }
3150    
3151        if ($self->{next_input_character} == 0x003B) { # ;        my $code = $self->{s_kwd};
3152          my $l = $self->{line_prev};
3153          my $c = $self->{column_prev};
3154          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3155            !!!cp (1015);
3156            !!!parse-error (type => 'invalid character reference',
3157                            text => (sprintf 'U+%04X', $code),
3158                            line => $l, column => $c);
3159            $code = 0xFFFD;
3160          } elsif ($code > 0x10FFFF) {
3161            !!!cp (1016);
3162            !!!parse-error (type => 'invalid character reference',
3163                            text => (sprintf 'U-%08X', $code),
3164                            line => $l, column => $c);
3165            $code = 0xFFFD;
3166          } elsif ($code == 0x000D) {
3167            !!!cp (1017);
3168            !!!parse-error (type => 'CR character reference',
3169                            line => $l, column => $c);
3170            $code = 0x000A;
3171          } elsif (0x80 <= $code and $code <= 0x9F) {
3172            !!!cp (1018);
3173            !!!parse-error (type => 'C1 character reference',
3174                            text => (sprintf 'U+%04X', $code),
3175                            line => $l, column => $c);
3176            $code = $c1_entity_char->{$code};
3177          }
3178    
3179          if ($self->{prev_state} == DATA_STATE) {
3180            !!!cp (992);
3181            $self->{state} = $self->{prev_state};
3182            ## Reconsume.
3183            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3184                      line => $l, column => $c,
3185                     });
3186            redo A;
3187          } else {
3188            !!!cp (991);
3189            $self->{ca}->{value} .= chr $code;
3190            $self->{ca}->{has_reference} = 1;
3191            $self->{state} = $self->{prev_state};
3192            ## Reconsume.
3193            redo A;
3194          }
3195        } elsif ($self->{state} == HEXREF_X_STATE) {
3196          if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3197              (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3198              (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3199            # 0..9, A..F, a..f
3200            !!!cp (990);
3201            $self->{state} = HEXREF_HEX_STATE;
3202            $self->{s_kwd} = 0;
3203            ## Reconsume.
3204            redo A;
3205          } else {
3206            !!!parse-error (type => 'bare hcro',
3207                            line => $self->{line_prev},
3208                            column => $self->{column_prev} - 2);
3209    
3210            ## NOTE: According to the spec algorithm, nothing is returned,
3211            ## and then "&#" followed by "X" or "x" is appended to the parent
3212            ## element or the attribute value in the later processing.
3213    
3214            if ($self->{prev_state} == DATA_STATE) {
3215              !!!cp (1005);
3216              $self->{state} = $self->{prev_state};
3217              ## Reconsume.
3218              !!!emit ({type => CHARACTER_TOKEN,
3219                        data => '&' . $self->{s_kwd},
3220                        line => $self->{line_prev},
3221                        column => $self->{column_prev} - length $self->{s_kwd},
3222                       });
3223              redo A;
3224            } else {
3225              !!!cp (989);
3226              $self->{ca}->{value} .= '&' . $self->{s_kwd};
3227              $self->{state} = $self->{prev_state};
3228              ## Reconsume.
3229              redo A;
3230            }
3231          }
3232        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3233          if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3234            # 0..9
3235            !!!cp (1002);
3236            $self->{s_kwd} *= 0x10;
3237            $self->{s_kwd} += $self->{nc} - 0x0030;
3238            ## Stay in the state.
3239            !!!next-input-character;
3240            redo A;
3241          } elsif (0x0061 <= $self->{nc} and
3242                   $self->{nc} <= 0x0066) { # a..f
3243            !!!cp (1003);
3244            $self->{s_kwd} *= 0x10;
3245            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3246            ## Stay in the state.
3247            !!!next-input-character;
3248            redo A;
3249          } elsif (0x0041 <= $self->{nc} and
3250                   $self->{nc} <= 0x0046) { # A..F
3251            !!!cp (1004);
3252            $self->{s_kwd} *= 0x10;
3253            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3254            ## Stay in the state.
3255          !!!next-input-character;          !!!next-input-character;
3256            redo A;
3257          } elsif ($self->{nc} == 0x003B) { # ;
3258            !!!cp (1006);
3259            !!!next-input-character;
3260            #
3261        } else {        } else {
3262          !!!parse-error (type => 'no refc');          !!!cp (1007);
3263            !!!parse-error (type => 'no refc',
3264                            line => $self->{line},
3265                            column => $self->{column});
3266            ## Reconsume.
3267            #
3268        }        }
3269    
3270          my $code = $self->{s_kwd};
3271          my $l = $self->{line_prev};
3272          my $c = $self->{column_prev};
3273        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3274          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1008);
3275            !!!parse-error (type => 'invalid character reference',
3276                            text => (sprintf 'U+%04X', $code),
3277                            line => $l, column => $c);
3278          $code = 0xFFFD;          $code = 0xFFFD;
3279        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3280          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1009);
3281            !!!parse-error (type => 'invalid character reference',
3282                            text => (sprintf 'U-%08X', $code),
3283                            line => $l, column => $c);
3284          $code = 0xFFFD;          $code = 0xFFFD;
3285        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3286          !!!parse-error (type => 'CR character reference');          !!!cp (1010);
3287            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3288          $code = 0x000A;          $code = 0x000A;
3289        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3290          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1011);
3291            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3292          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3293        }        }
3294          
3295        return {type => CHARACTER_TOKEN, data => chr $code};        if ($self->{prev_state} == DATA_STATE) {
3296      } else {          !!!cp (988);
3297        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3298        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3299        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3300        return undef;                    line => $l, column => $c,
3301      }                   });
3302    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
3303              $self->{next_input_character} <= 0x005A) or        } else {
3304             (0x0061 <= $self->{next_input_character} and          !!!cp (987);
3305              $self->{next_input_character} <= 0x007A)) {          $self->{ca}->{value} .= chr $code;
3306      my $entity_name = chr $self->{next_input_character};          $self->{ca}->{has_reference} = 1;
3307      !!!next-input-character;          $self->{state} = $self->{prev_state};
3308            ## Reconsume.
3309      my $value = $entity_name;          redo A;
3310      my $match = 0;        }
3311      require Whatpm::_NamedEntityList;      } elsif ($self->{state} == ENTITY_NAME_STATE) {
3312      our $EntityChar;        if (length $self->{s_kwd} < 30 and
3313              ## NOTE: Some number greater than the maximum length of entity name
3314      while (length $entity_name < 10 and            ((0x0041 <= $self->{nc} and # a
3315             ## NOTE: Some number greater than the maximum length of entity name              $self->{nc} <= 0x005A) or # x
3316             ((0x0041 <= $self->{next_input_character} and # a             (0x0061 <= $self->{nc} and # a
3317               $self->{next_input_character} <= 0x005A) or # x              $self->{nc} <= 0x007A) or # z
3318              (0x0061 <= $self->{next_input_character} and # a             (0x0030 <= $self->{nc} and # 0
3319               $self->{next_input_character} <= 0x007A) or # z              $self->{nc} <= 0x0039) or # 9
3320              (0x0030 <= $self->{next_input_character} and # 0             $self->{nc} == 0x003B)) { # ;
3321               $self->{next_input_character} <= 0x0039) or # 9          our $EntityChar;
3322              $self->{next_input_character} == 0x003B)) { # ;          $self->{s_kwd} .= chr $self->{nc};
3323        $entity_name .= chr $self->{next_input_character};          if (defined $EntityChar->{$self->{s_kwd}}) {
3324        if (defined $EntityChar->{$entity_name}) {            if ($self->{nc} == 0x003B) { # ;
3325          if ($self->{next_input_character} == 0x003B) { # ;              !!!cp (1020);
3326            $value = $EntityChar->{$entity_name};              $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3327            $match = 1;              $self->{entity__match} = 1;
3328            !!!next-input-character;              !!!next-input-character;
3329            last;              #
3330              } else {
3331                !!!cp (1021);
3332                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3333                $self->{entity__match} = -1;
3334                ## Stay in the state.
3335                !!!next-input-character;
3336                redo A;
3337              }
3338          } else {          } else {
3339            $value = $EntityChar->{$entity_name};            !!!cp (1022);
3340            $match = -1;            $self->{entity__value} .= chr $self->{nc};
3341              $self->{entity__match} *= 2;
3342              ## Stay in the state.
3343            !!!next-input-character;            !!!next-input-character;
3344              redo A;
3345          }          }
       } else {  
         $value .= chr $self->{next_input_character};  
         $match *= 2;  
         !!!next-input-character;  
3346        }        }
3347      }  
3348              my $data;
3349      if ($match > 0) {        my $has_ref;
3350        return {type => CHARACTER_TOKEN, data => $value};        if ($self->{entity__match} > 0) {
3351      } elsif ($match < 0) {          !!!cp (1023);
3352        !!!parse-error (type => 'no refc');          $data = $self->{entity__value};
3353        if ($in_attr and $match < -1) {          $has_ref = 1;
3354          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          #
3355          } elsif ($self->{entity__match} < 0) {
3356            !!!parse-error (type => 'no refc');
3357            if ($self->{prev_state} != DATA_STATE and # in attribute
3358                $self->{entity__match} < -1) {
3359              !!!cp (1024);
3360              $data = '&' . $self->{s_kwd};
3361              #
3362            } else {
3363              !!!cp (1025);
3364              $data = $self->{entity__value};
3365              $has_ref = 1;
3366              #
3367            }
3368        } else {        } else {
3369          return {type => CHARACTER_TOKEN, data => $value};          !!!cp (1026);
3370            !!!parse-error (type => 'bare ero',
3371                            line => $self->{line_prev},
3372                            column => $self->{column_prev} - length $self->{s_kwd});
3373            $data = '&' . $self->{s_kwd};
3374            #
3375          }
3376      
3377          ## NOTE: In these cases, when a character reference is found,
3378          ## it is consumed and a character token is returned, or, otherwise,
3379          ## nothing is consumed and returned, according to the spec algorithm.
3380          ## In this implementation, anything that has been examined by the
3381          ## tokenizer is appended to the parent element or the attribute value
3382          ## as string, either literal string when no character reference or
3383          ## entity-replaced string otherwise, in this stage, since any characters
3384          ## that would not be consumed are appended in the data state or in an
3385          ## appropriate attribute value state anyway.
3386    
3387          if ($self->{prev_state} == DATA_STATE) {
3388            !!!cp (986);
3389            $self->{state} = $self->{prev_state};
3390            ## Reconsume.
3391            !!!emit ({type => CHARACTER_TOKEN,
3392                      data => $data,
3393                      line => $self->{line_prev},
3394                      column => $self->{column_prev} + 1 - length $self->{s_kwd},
3395                     });
3396            redo A;
3397          } else {
3398            !!!cp (985);
3399            $self->{ca}->{value} .= $data;
3400            $self->{ca}->{has_reference} = 1 if $has_ref;
3401            $self->{state} = $self->{prev_state};
3402            ## Reconsume.
3403            redo A;
3404        }        }
3405      } else {      } else {
3406        !!!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};  
3407      }      }
3408    } else {    } # A  
3409      ## no characters are consumed  
3410      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3411      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3412    
3413  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3414    my $self = shift;    my $self = shift;
# Line 1867  sub _initialize_tree_constructor ($) { Line 3417  sub _initialize_tree_constructor ($) {
3417    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3418    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3419    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3420      $self->{document}->set_user_data (manakai_source_line => 1);
3421      $self->{document}->set_user_data (manakai_source_column => 1);
3422  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3423    
3424  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1893  sub _construct_tree ($) { Line 3445  sub _construct_tree ($) {
3445        
3446    !!!next-token;    !!!next-token;
3447    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3448    undef $self->{form_element};    undef $self->{form_element};
3449    undef $self->{head_element};    undef $self->{head_element};
3450    $self->{open_elements} = [];    $self->{open_elements} = [];
3451    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3452    
3453      ## NOTE: The "initial" insertion mode.
3454    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3455    
3456      ## NOTE: The "before html" insertion mode.
3457    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3458      $self->{insertion_mode} = BEFORE_HEAD_IM;
3459    
3460      ## NOTE: The "before head" insertion mode and so on.
3461    $self->_tree_construction_main;    $self->_tree_construction_main;
3462  } # _construct_tree  } # _construct_tree
3463    
3464  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3465    my $self = shift;    my $self = shift;
3466    
3467      ## NOTE: "initial" insertion mode
3468    
3469    INITIAL: {    INITIAL: {
3470      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3471        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 1913  sub _tree_construction_initial ($) { Line 3473  sub _tree_construction_initial ($) {
3473        ## language.        ## language.
3474        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3475        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3476        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3477        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3478            defined $token->{public_identifier} or            defined $token->{sysid}) {
3479            defined $token->{system_identifier}) {          !!!cp ('t1');
3480          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3481        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3482          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3483          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3484          } elsif (defined $token->{pubid}) {
3485            if ($token->{pubid} eq 'XSLT-compat') {
3486              !!!cp ('t1.2');
3487              !!!parse-error (type => 'XSLT-compat', token => $token,
3488                              level => $self->{level}->{should});
3489            } else {
3490              !!!parse-error (type => 'not HTML5', token => $token);
3491            }
3492          } else {
3493            !!!cp ('t3');
3494            #
3495        }        }
3496                
3497        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3498          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3499        $doctype->public_id ($token->{public_identifier})        ## NOTE: Default value for both |public_id| and |system_id| attributes
3500            if defined $token->{public_identifier};        ## are empty strings, so that we don't set any value in missing cases.
3501        $doctype->system_id ($token->{system_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3502            if defined $token->{system_identifier};        $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
3503        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
3504        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3505        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3506                
3507        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3508            !!!cp ('t4');
3509          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3510        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
3511          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
3512          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3513          if ({          my $prefix = [
3514            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3515            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3516            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3517            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3518            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3519            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3520            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3521            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3522            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3523            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3524            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3525            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3526            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3527            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3528            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3529            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3530            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3531            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3532            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3533            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3534            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3535            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3536            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3537            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3538            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3539            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3540            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3541            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3542            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3543            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3544            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3545            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3546            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3547            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3548            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3549            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3550            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3551            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3552            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3553            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3554            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3555            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3556            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3557            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3558            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3559            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3560            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3561            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3562            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3563            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3564            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3565            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3566            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3567            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3568            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3569            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3570            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3571            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3572            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3573            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3574            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3575            "-//W3C//DTD W3 HTML//EN" => 1,            }
3576            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3577            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3578            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3579            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3580            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3581            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3582            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3583          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3584                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3585            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
3586                !!!cp ('t6');
3587              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3588            } else {            } else {
3589                !!!cp ('t7');
3590              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3591            }            }
3592          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3593                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3594              !!!cp ('t8');
3595            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3596            } else {
3597              !!!cp ('t9');
3598          }          }
3599          } else {
3600            !!!cp ('t10');
3601        }        }
3602        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
3603          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
3604          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3605          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") {
3606              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3607              ## marked as quirks.
3608            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3609              !!!cp ('t11');
3610            } else {
3611              !!!cp ('t12');
3612          }          }
3613          } else {
3614            !!!cp ('t13');
3615        }        }
3616                
3617        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3618        !!!next-token;        !!!next-token;
3619        return;        return;
3620      } elsif ({      } elsif ({
# Line 2038  sub _tree_construction_initial ($) { Line 3622  sub _tree_construction_initial ($) {
3622                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3623                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3624               }->{$token->{type}}) {               }->{$token->{type}}) {
3625        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3626          !!!parse-error (type => 'no DOCTYPE', token => $token);
3627        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3628        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3629        ## reprocess        ## reprocess
3630          !!!ack-later;
3631        return;        return;
3632      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3633        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3634          ## Ignore the token          ## Ignore the token
3635    
3636          unless (length $token->{data}) {          unless (length $token->{data}) {
3637            ## Stay in the phase            !!!cp ('t15');
3638              ## Stay in the insertion mode.
3639            !!!next-token;            !!!next-token;
3640            redo INITIAL;            redo INITIAL;
3641            } else {
3642              !!!cp ('t16');
3643          }          }
3644          } else {
3645            !!!cp ('t17');
3646        }        }
3647    
3648        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3649        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3650        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3651        ## reprocess        ## reprocess
3652        return;        return;
3653      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3654          !!!cp ('t18');
3655        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3656        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3657                
3658        ## Stay in the phase.        ## Stay in the insertion mode.
3659        !!!next-token;        !!!next-token;
3660        redo INITIAL;        redo INITIAL;
3661      } else {      } else {
3662        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3663      }      }
3664    } # INITIAL    } # INITIAL
3665    
3666      die "$0: _tree_construction_initial: This should be never reached";
3667  } # _tree_construction_initial  } # _tree_construction_initial
3668    
3669  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3670    my $self = shift;    my $self = shift;
3671    
3672      ## NOTE: "before html" insertion mode.
3673        
3674    B: {    B: {
3675        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3676          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3677            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3678          ## Ignore the token          ## Ignore the token
3679          ## Stay in the phase          ## Stay in the insertion mode.
3680          !!!next-token;          !!!next-token;
3681          redo B;          redo B;
3682        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3683            !!!cp ('t20');
3684          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3685          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3686          ## Stay in the phase          ## Stay in the insertion mode.
3687          !!!next-token;          !!!next-token;
3688          redo B;          redo B;
3689        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2093  sub _tree_construction_root_element ($) Line 3691  sub _tree_construction_root_element ($)
3691            ## Ignore the token.            ## Ignore the token.
3692    
3693            unless (length $token->{data}) {            unless (length $token->{data}) {
3694              ## Stay in the phase              !!!cp ('t21');
3695                ## Stay in the insertion mode.
3696              !!!next-token;              !!!next-token;
3697              redo B;              redo B;
3698              } else {
3699                !!!cp ('t22');
3700            }            }
3701            } else {
3702              !!!cp ('t23');
3703          }          }
3704    
3705          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3706    
3707          #          #
3708        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3709          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3710              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
3711            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3712                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3713            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3714                  [$root_element, $el_category->{html}];
3715    
3716              if ($token->{attributes}->{manifest}) {
3717                !!!cp ('t24');
3718                $self->{application_cache_selection}
3719                    ->($token->{attributes}->{manifest}->{value});
3720                ## ISSUE: Spec is unclear on relative references.
3721                ## According to Hixie (#whatwg 2008-03-19), it should be
3722                ## resolved against the base URI of the document in HTML
3723                ## or xml:base of the element in XHTML.
3724              } else {
3725                !!!cp ('t25');
3726                $self->{application_cache_selection}->(undef);
3727              }
3728    
3729              !!!nack ('t25c');
3730    
3731              !!!next-token;
3732              return; ## Go to the "before head" insertion mode.
3733          } else {          } else {
3734            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3735              #
3736          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3737        } elsif ({        } elsif ({
3738                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3739                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3740                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3741          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3742          #          #
3743        } else {        } else {
3744          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3745        }        }
3746    
3747        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3748        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3749        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3750        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3751        #redo B;  
3752        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3753    
3754        ## NOTE: Reprocess the token.
3755        !!!ack-later;
3756        return; ## Go to the "before head" insertion mode.
3757    
3758        ## ISSUE: There is an issue in the spec
3759    } # B    } # B
3760    
3761      die "$0: _tree_construction_root_element: This should never be reached";
3762  } # _tree_construction_root_element  } # _tree_construction_root_element
3763    
3764  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2147  sub _reset_insertion_mode ($) { Line 3773  sub _reset_insertion_mode ($) {
3773            
3774      ## Step 3      ## Step 3
3775      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"!?  
3776        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3777          $last = 1;          $last = 1;
3778          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3779            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3780                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3781              #          } else {
3782            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3783          }          }
3784        }        }
3785              
3786        ## Step 4..13        ## Step 4..14
3787        my $new_mode = {        my $new_mode;
3788          if ($node->[1] & FOREIGN_EL) {
3789            !!!cp ('t28.1');
3790            ## NOTE: Strictly spaking, the line below only applies to MathML and
3791            ## SVG elements.  Currently the HTML syntax supports only MathML and
3792            ## SVG elements as foreigners.
3793            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3794          } elsif ($node->[1] & TABLE_CELL_EL) {
3795            if ($last) {
3796              !!!cp ('t28.2');
3797              #
3798            } else {
3799              !!!cp ('t28.3');
3800              $new_mode = IN_CELL_IM;
3801            }
3802          } else {
3803            !!!cp ('t28.4');
3804            $new_mode = {
3805                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3806                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3807                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3808                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3809                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3810                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2179  sub _reset_insertion_mode ($) { Line 3815  sub _reset_insertion_mode ($) {
3815                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3816                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3817                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3818                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3819          }
3820        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3821                
3822        ## Step 14        ## Step 15
3823        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3824          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3825              !!!cp ('t29');
3826            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3827          } else {          } else {
3828              ## ISSUE: Can this state be reached?
3829              !!!cp ('t30');
3830            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3831          }          }
3832          return;          return;
3833          } else {
3834            !!!cp ('t31');
3835        }        }
3836                
3837        ## Step 15        ## Step 16
3838        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3839                
3840        ## Step 16        ## Step 17
3841        $i--;        $i--;
3842        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3843                
3844        ## Step 17        ## Step 18
3845        redo S3;        redo S3;
3846      } # S3      } # S3
3847    
3848      die "$0: _reset_insertion_mode: This line should never be reached";
3849  } # _reset_insertion_mode  } # _reset_insertion_mode
3850    
3851  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2223  sub _tree_construction_main ($) { Line 3867  sub _tree_construction_main ($) {
3867      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3868      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3869        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3870            !!!cp ('t32');
3871          return;          return;
3872        }        }
3873      }      }
# Line 2237  sub _tree_construction_main ($) { Line 3882  sub _tree_construction_main ($) {
3882    
3883        ## Step 6        ## Step 6
3884        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3885            !!!cp ('t33_1');
3886          #          #
3887        } else {        } else {
3888          my $in_open_elements;          my $in_open_elements;
3889          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3890            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3891                !!!cp ('t33');
3892              $in_open_elements = 1;              $in_open_elements = 1;
3893              last OE;              last OE;
3894            }            }
3895          }          }
3896          if ($in_open_elements) {          if ($in_open_elements) {
3897              !!!cp ('t34');
3898            #            #
3899          } else {          } else {
3900              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3901              !!!cp ('t35');
3902            redo S4;            redo S4;
3903          }          }
3904        }        }
# Line 2271  sub _tree_construction_main ($) { Line 3921  sub _tree_construction_main ($) {
3921    
3922        ## Step 11        ## Step 11
3923        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3924            !!!cp ('t36');
3925          ## Step 7'          ## Step 7'
3926          $i++;          $i++;
3927          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3928                    
3929          redo S7;          redo S7;
3930        }        }
3931    
3932          !!!cp ('t37');
3933      } # S7      } # S7
3934    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3935    
3936    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3937      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3938        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3939            !!!cp ('t38');
3940          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3941          return;          return;
3942        }        }
3943      }      }
3944    
3945        !!!cp ('t39');
3946    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3947    
3948    my $parse_rcdata = sub ($$) {    my $insert;
3949      my ($content_model_flag, $insert) = @_;  
3950      my $parse_rcdata = sub ($) {
3951        my ($content_model_flag) = @_;
3952    
3953      ## Step 1      ## Step 1
3954      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3955      my $el;      my $el;
3956      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3957    
3958      ## Step 2      ## Step 2
3959      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3960    
3961      ## Step 3      ## Step 3
3962      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2306  sub _tree_construction_main ($) { Line 3964  sub _tree_construction_main ($) {
3964    
3965      ## Step 4      ## Step 4
3966      my $text = '';      my $text = '';
3967        !!!nack ('t40.1');
3968      !!!next-token;      !!!next-token;
3969      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3970          !!!cp ('t40');
3971        $text .= $token->{data};        $text .= $token->{data};
3972        !!!next-token;        !!!next-token;
3973      }      }
3974    
3975      ## Step 5      ## Step 5
3976      if (length $text) {      if (length $text) {
3977          !!!cp ('t41');
3978        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3979        $el->append_child ($text);        $el->append_child ($text);
3980      }      }
# Line 2322  sub _tree_construction_main ($) { Line 3983  sub _tree_construction_main ($) {
3983      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3984    
3985      ## Step 7      ## Step 7
3986      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3987            $token->{tag_name} eq $start_tag_name) {
3988          !!!cp ('t42');
3989        ## 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});  
3990      } else {      } else {
3991        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3992          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3993            !!!cp ('t43');
3994            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3995          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3996            !!!cp ('t44');
3997            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3998          } else {
3999            die "$0: $content_model_flag in parse_rcdata";
4000          }
4001      }      }
4002      !!!next-token;      !!!next-token;
4003    }; # $parse_rcdata    }; # $parse_rcdata
4004    
4005    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
4006      my $script_el;      my $script_el;
4007      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4008      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4009    
4010      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
4011      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4012            
4013      my $text = '';      my $text = '';
4014        !!!nack ('t45.1');
4015      !!!next-token;      !!!next-token;
4016      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
4017          !!!cp ('t45');
4018        $text .= $token->{data};        $text .= $token->{data};
4019        !!!next-token;        !!!next-token;
4020      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
4021      if (length $text) {      if (length $text) {
4022          !!!cp ('t46');
4023        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
4024      }      }
4025                                
# Line 2357  sub _tree_construction_main ($) { Line 4027  sub _tree_construction_main ($) {
4027    
4028      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
4029          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4030          !!!cp ('t47');
4031        ## Ignore the token        ## Ignore the token
4032      } else {      } else {
4033        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4034          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4035        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4036        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4037      }      }
4038            
4039      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4040          !!!cp ('t49');
4041        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4042      } else {      } else {
4043          !!!cp ('t50');
4044        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4045        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4046    
# Line 2380  sub _tree_construction_main ($) { Line 4054  sub _tree_construction_main ($) {
4054      !!!next-token;      !!!next-token;
4055    }; # $script_start_tag    }; # $script_start_tag
4056    
4057      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4058      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4059      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4060    
4061    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4062      my $tag_name = shift;      my $end_tag_token = shift;
4063        my $tag_name = $end_tag_token->{tag_name};
4064    
4065        ## NOTE: The adoption agency algorithm (AAA).
4066    
4067      FET: {      FET: {
4068        ## Step 1        ## Step 1
4069        my $formatting_element;        my $formatting_element;
4070        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4071        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4072          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4073              !!!cp ('t52');
4074              last AFE;
4075            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4076                         eq $tag_name) {
4077              !!!cp ('t51');
4078            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4079            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4080            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4081          }          }
4082        } # AFE        } # AFE
4083        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4084          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4085            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4086          ## Ignore the token          ## Ignore the token
4087          !!!next-token;          !!!next-token;
4088          return;          return;
# Line 2409  sub _tree_construction_main ($) { Line 4094  sub _tree_construction_main ($) {
4094          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4095          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4096            if ($in_scope) {            if ($in_scope) {
4097                !!!cp ('t54');
4098              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4099              last INSCOPE;              last INSCOPE;
4100            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4101              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4102                !!!parse-error (type => 'unmatched end tag',
4103                                text => $token->{tag_name},
4104                                token => $end_tag_token);
4105              ## Ignore the token              ## Ignore the token
4106              !!!next-token;              !!!next-token;
4107              return;              return;
4108            }            }
4109          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4110                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4111            $in_scope = 0;            $in_scope = 0;
4112          }          }
4113        } # INSCOPE        } # INSCOPE
4114        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4115          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4116            !!!parse-error (type => 'unmatched end tag',
4117                            text => $token->{tag_name},
4118                            token => $end_tag_token);
4119          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4120          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4121          return;          return;
4122        }        }
4123        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4124          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4125            !!!parse-error (type => 'not closed',
4126                            text => $self->{open_elements}->[-1]->[0]
4127                                ->manakai_local_name,
4128                            token => $end_tag_token);
4129        }        }
4130                
4131        ## Step 2        ## Step 2
# Line 2439  sub _tree_construction_main ($) { Line 4133  sub _tree_construction_main ($) {
4133        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4134        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4135          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4136          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4137              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4138              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4139               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4140              !!!cp ('t59');
4141            $furthest_block = $node;            $furthest_block = $node;
4142            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4143          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4144              !!!cp ('t60');
4145            last OE;            last OE;
4146          }          }
4147        } # OE        } # OE
4148                
4149        ## Step 3        ## Step 3
4150        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4151            !!!cp ('t61');
4152          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4153          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4154          !!!next-token;          !!!next-token;
# Line 2464  sub _tree_construction_main ($) { Line 4161  sub _tree_construction_main ($) {
4161        ## Step 5        ## Step 5
4162        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4163        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4164            !!!cp ('t62');
4165          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4166        }        }
4167                
# Line 2486  sub _tree_construction_main ($) { Line 4184  sub _tree_construction_main ($) {
4184          S7S2: {          S7S2: {
4185            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4186              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4187                  !!!cp ('t63');
4188                $node_i_in_active = $_;                $node_i_in_active = $_;
4189                last S7S2;                last S7S2;
4190              }              }
# Line 2499  sub _tree_construction_main ($) { Line 4198  sub _tree_construction_main ($) {
4198                    
4199          ## Step 4          ## Step 4
4200          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4201              !!!cp ('t64');
4202            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4203          }          }
4204                    
4205          ## Step 5          ## Step 5
4206          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4207              !!!cp ('t65');
4208            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4209            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4210            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2521  sub _tree_construction_main ($) { Line 4222  sub _tree_construction_main ($) {
4222        } # S7          } # S7  
4223                
4224        ## Step 8        ## Step 8
4225        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4226            my $foster_parent_element;
4227            my $next_sibling;
4228            OE: for (reverse 0..$#{$self->{open_elements}}) {
4229              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4230                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4231                                 if (defined $parent and $parent->node_type == 1) {
4232                                   !!!cp ('t65.1');
4233                                   $foster_parent_element = $parent;
4234                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4235                                 } else {
4236                                   !!!cp ('t65.2');
4237                                   $foster_parent_element
4238                                     = $self->{open_elements}->[$_ - 1]->[0];
4239                                 }
4240                                 last OE;
4241                               }
4242                             } # OE
4243                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4244                               unless defined $foster_parent_element;
4245            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4246            $open_tables->[-1]->[1] = 1; # tainted
4247          } else {
4248            !!!cp ('t65.3');
4249            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4250          }
4251                
4252        ## Step 9        ## Step 9
4253        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2538  sub _tree_construction_main ($) { Line 4264  sub _tree_construction_main ($) {
4264        my $i;        my $i;
4265        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4266          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4267              !!!cp ('t66');
4268            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4269            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4270          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4271              !!!cp ('t67');
4272            $i = $_;            $i = $_;
4273          }          }
4274        } # AFE        } # AFE
# Line 2550  sub _tree_construction_main ($) { Line 4278  sub _tree_construction_main ($) {
4278        undef $i;        undef $i;
4279        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4280          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4281              !!!cp ('t68');
4282            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4283            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4284          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4285              !!!cp ('t69');
4286            $i = $_;            $i = $_;
4287          }          }
4288        } # OE        } # OE
# Line 2563  sub _tree_construction_main ($) { Line 4293  sub _tree_construction_main ($) {
4293      } # FET      } # FET
4294    }; # $formatting_end_tag    }; # $formatting_end_tag
4295    
4296    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4297      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4298    }; # $insert_to_current    }; # $insert_to_current
4299    
4300    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4301                         my $child = shift;      my $child = shift;
4302                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4303                              table => 1, tbody => 1, tfoot => 1,        # MUST
4304                              thead => 1, tr => 1,        my $foster_parent_element;
4305                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4306                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4307                           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') {  
4308                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4309                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4310                                   !!!cp ('t70');
4311                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4312                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4313                               } else {                               } else {
4314                                   !!!cp ('t71');
4315                                 $foster_parent_element                                 $foster_parent_element
4316                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4317                               }                               }
# Line 2593  sub _tree_construction_main ($) { Line 4322  sub _tree_construction_main ($) {
4322                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4323                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4324                             ($child, $next_sibling);                             ($child, $next_sibling);
4325                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4326                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4327                         }        !!!cp ('t72');
4328          $self->{open_elements}->[-1]->[0]->append_child ($child);
4329        }
4330    }; # $insert_to_foster    }; # $insert_to_foster
4331    
4332    my $insert;    B: while (1) {
   
   B: {  
4333      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4334        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
4335          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4336        ## Ignore the token        ## Ignore the token
4337        ## Stay in the phase        ## Stay in the phase
4338        !!!next-token;        !!!next-token;
4339        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;  
4340      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4341               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4342        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4343          ## Turn into the main phase          !!!cp ('t79');
4344          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4345          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4346        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4347          ## Turn into the main phase          !!!cp ('t80');
4348          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4349          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4350          } else {
4351            !!!cp ('t81');
4352        }        }
4353    
4354  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4355  ## 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');  
       }  
4356        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4357        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4358          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4359              !!!cp ('t84');
4360            $top_el->set_attribute_ns            $top_el->set_attribute_ns
4361              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
4362               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4363          }          }
4364        }        }
4365          !!!nack ('t84.1');
4366        !!!next-token;        !!!next-token;
4367        redo B;        next B;
4368      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4369        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4370        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4371            !!!cp ('t85');
4372          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
4373        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4374            !!!cp ('t86');
4375          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
4376        } else {        } else {
4377            !!!cp ('t87');
4378          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4379        }        }
4380        !!!next-token;        !!!next-token;
4381        redo B;        next B;
4382      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4383          if ($token->{type} == CHARACTER_TOKEN) {
4384            !!!cp ('t87.1');
4385            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4386            !!!next-token;
4387            next B;
4388          } elsif ($token->{type} == START_TAG_TOKEN) {
4389            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4390                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4391                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4392                ($token->{tag_name} eq 'svg' and
4393                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4394              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4395              !!!cp ('t87.2');
4396              #
4397            } elsif ({
4398                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4399                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4400                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4401                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4402                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4403                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4404                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4405                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4406                     }->{$token->{tag_name}}) {
4407              !!!cp ('t87.2');
4408              !!!parse-error (type => 'not closed',
4409                              text => $self->{open_elements}->[-1]->[0]
4410                                  ->manakai_local_name,
4411                              token => $token);
4412    
4413              pop @{$self->{open_elements}}
4414                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4415    
4416              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4417              ## Reprocess.
4418              next B;
4419            } else {
4420              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4421              my $tag_name = $token->{tag_name};
4422              if ($nsuri eq $SVG_NS) {
4423                $tag_name = {
4424                   altglyph => 'altGlyph',
4425                   altglyphdef => 'altGlyphDef',
4426                   altglyphitem => 'altGlyphItem',
4427                   animatecolor => 'animateColor',
4428                   animatemotion => 'animateMotion',
4429                   animatetransform => 'animateTransform',
4430                   clippath => 'clipPath',
4431                   feblend => 'feBlend',
4432                   fecolormatrix => 'feColorMatrix',
4433                   fecomponenttransfer => 'feComponentTransfer',
4434                   fecomposite => 'feComposite',
4435                   feconvolvematrix => 'feConvolveMatrix',
4436                   fediffuselighting => 'feDiffuseLighting',
4437                   fedisplacementmap => 'feDisplacementMap',
4438                   fedistantlight => 'feDistantLight',
4439                   feflood => 'feFlood',
4440                   fefunca => 'feFuncA',
4441                   fefuncb => 'feFuncB',
4442                   fefuncg => 'feFuncG',
4443                   fefuncr => 'feFuncR',
4444                   fegaussianblur => 'feGaussianBlur',
4445                   feimage => 'feImage',
4446                   femerge => 'feMerge',
4447                   femergenode => 'feMergeNode',
4448                   femorphology => 'feMorphology',
4449                   feoffset => 'feOffset',
4450                   fepointlight => 'fePointLight',
4451                   fespecularlighting => 'feSpecularLighting',
4452                   fespotlight => 'feSpotLight',
4453                   fetile => 'feTile',
4454                   feturbulence => 'feTurbulence',
4455                   foreignobject => 'foreignObject',
4456                   glyphref => 'glyphRef',
4457                   lineargradient => 'linearGradient',
4458                   radialgradient => 'radialGradient',
4459                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4460                   textpath => 'textPath',  
4461                }->{$tag_name} || $tag_name;
4462              }
4463    
4464              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4465    
4466              ## "adjust foreign attributes" - done in insert-element-f
4467    
4468              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4469    
4470              if ($self->{self_closing}) {
4471                pop @{$self->{open_elements}};
4472                !!!ack ('t87.3');
4473              } else {
4474                !!!cp ('t87.4');
4475              }
4476    
4477              !!!next-token;
4478              next B;
4479            }
4480          } elsif ($token->{type} == END_TAG_TOKEN) {
4481            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4482            !!!cp ('t87.5');
4483            #
4484          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4485            !!!cp ('t87.6');
4486            !!!parse-error (type => 'not closed',
4487                            text => $self->{open_elements}->[-1]->[0]
4488                                ->manakai_local_name,
4489                            token => $token);
4490    
4491            pop @{$self->{open_elements}}
4492                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4493    
4494            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4495            ## Reprocess.
4496            next B;
4497          } else {
4498            die "$0: $token->{type}: Unknown token type";        
4499          }
4500        }
4501    
4502        if ($self->{insertion_mode} & HEAD_IMS) {
4503        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4504          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4505            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4506                !!!cp ('t88.2');
4507                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4508                #
4509              } else {
4510                !!!cp ('t88.1');
4511                ## Ignore the token.
4512                #
4513              }
4514            unless (length $token->{data}) {            unless (length $token->{data}) {
4515                !!!cp ('t88');
4516              !!!next-token;              !!!next-token;
4517              redo B;              next B;
4518            }            }
4519    ## TODO: set $token->{column} appropriately
4520          }          }
4521    
4522          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4523              !!!cp ('t89');
4524            ## As if <head>            ## As if <head>
4525            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4526            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4527            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4528                  [$self->{head_element}, $el_category->{head}];
4529    
4530            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4531            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4532    
4533            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4534          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4535              !!!cp ('t90');
4536            ## As if </noscript>            ## As if </noscript>
4537            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4538            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4539                        
4540            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4541            ## As if </head>            ## As if </head>
# Line 2704  sub _tree_construction_main ($) { Line 4543  sub _tree_construction_main ($) {
4543    
4544            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4545          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4546              !!!cp ('t91');
4547            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4548    
4549            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4550            } else {
4551              !!!cp ('t92');
4552          }          }
4553    
4554              ## "after head" insertion mode          ## "after head" insertion mode
4555              ## As if <body>          ## As if <body>
4556              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4557              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4558              ## reprocess          ## reprocess
4559              redo B;          next B;
4560            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4561              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4562                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4563                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4564                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4565                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4566                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4567                  !!!next-token;              push @{$self->{open_elements}},
4568                  redo B;                  [$self->{head_element}, $el_category->{head}];
4569                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4570                  #              !!!nack ('t93.1');
4571                } else {              !!!next-token;
4572                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4573                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4574                  !!!next-token;              !!!cp ('t93.2');
4575                  redo B;              !!!parse-error (type => 'after head', text => 'head',
4576                }                              token => $token);
4577              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              ## Ignore the token
4578                ## As if <head>              !!!nack ('t93.3');
4579                !!!create-element ($self->{head_element}, 'head');              !!!next-token;
4580                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              next B;
4581                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
4582                !!!cp ('t95');
4583                !!!parse-error (type => 'in head:head',
4584                                token => $token); # or in head noscript
4585                ## Ignore the token
4586                !!!nack ('t95.1');
4587                !!!next-token;
4588                next B;
4589              }
4590            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4591              !!!cp ('t96');
4592              ## As if <head>
4593              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4594              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4595              push @{$self->{open_elements}},
4596                  [$self->{head_element}, $el_category->{head}];
4597    
4598                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4599                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4600              }          } else {
4601              !!!cp ('t97');
4602            }
4603    
4604              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4605                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4606                    !!!cp ('t98');
4607                  ## As if </noscript>                  ## As if </noscript>
4608                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4609                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4610                                    token => $token);
4611                                
4612                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4613                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4614                  } else {
4615                    !!!cp ('t99');
4616                }                }
4617    
4618                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4619                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4620                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4621                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4622                                    text => $token->{tag_name}, token => $token);
4623                    push @{$self->{open_elements}},
4624                        [$self->{head_element}, $el_category->{head}];
4625                  } else {
4626                    !!!cp ('t101');
4627                }                }
4628                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4629                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4630                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4631                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4632                  !!!nack ('t101.1');
4633                !!!next-token;                !!!next-token;
4634                redo B;                next B;
4635              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4636                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4637                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4638                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4639                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4640                                    text => $token->{tag_name}, token => $token);
4641                    push @{$self->{open_elements}},
4642                        [$self->{head_element}, $el_category->{head}];
4643                  } else {
4644                    !!!cp ('t103');
4645                }                }
4646                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4647                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4648                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4649                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4650                  !!!ack ('t103.1');
4651                !!!next-token;                !!!next-token;
4652                redo B;                next B;
4653              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4654                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4655                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4656                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4657                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4658                                    text => $token->{tag_name}, token => $token);
4659                    push @{$self->{open_elements}},
4660                        [$self->{head_element}, $el_category->{head}];
4661                  } else {
4662                    !!!cp ('t105');
4663                }                }
4664                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4665                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.
4666    
4667                unless ($self->{confident}) {                unless ($self->{confident}) {
4668                  my $charset;                  if ($token->{attributes}->{charset}) {
4669                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4670                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4671                  }                    ## in the {change_encoding} callback.
4672                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4673                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4674                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4675                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4676                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4677                          ->set_user_data (manakai_has_reference =>
4678                                               $token->{attributes}->{charset}
4679                                                   ->{has_reference});
4680                    } elsif ($token->{attributes}->{content}) {
4681                      if ($token->{attributes}->{content}->{value}
4682                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4683                              [\x09-\x0D\x20]*=
4684                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4685                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4686                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4687                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4688                        ## in the {change_encoding} callback.
4689                        $self->{change_encoding}
4690                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4691                               $token);
4692                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4693                            ->set_user_data (manakai_has_reference =>
4694                                                 $token->{attributes}->{content}
4695                                                       ->{has_reference});
4696                      } else {
4697                        !!!cp ('t108');
4698                      }
4699                    }
4700                  } else {
4701                    if ($token->{attributes}->{charset}) {
4702                      !!!cp ('t109');
4703                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4704                          ->set_user_data (manakai_has_reference =>
4705                                               $token->{attributes}->{charset}
4706                                                   ->{has_reference});
4707                    }
4708                    if ($token->{attributes}->{content}) {
4709                      !!!cp ('t110');
4710                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4711                          ->set_user_data (manakai_has_reference =>
4712                                               $token->{attributes}->{content}
4713                                                   ->{has_reference});
4714                  }                  }
                 ## TODO: Change the encoding  
4715                }                }
4716    
4717                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4718                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4719                  !!!ack ('t110.1');
4720                !!!next-token;                !!!next-token;
4721                redo B;                next B;
4722              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4723                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4724                    !!!cp ('t111');
4725                  ## As if </noscript>                  ## As if </noscript>
4726                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4727                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4728                                    token => $token);
4729                                
4730                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4731                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4732                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4733                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4734                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4735                                    text => $token->{tag_name}, token => $token);
4736                    push @{$self->{open_elements}},
4737                        [$self->{head_element}, $el_category->{head}];
4738                  } else {
4739                    !!!cp ('t113');
4740                }                }
4741    
4742                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4743                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4744                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4745                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4746                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4747                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4748                redo B;                next B;
4749              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4750                         $token->{tag_name} eq 'noframes') {
4751                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4752                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4753                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4754                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4755                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4756                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4757                                    text => $token->{tag_name}, token => $token);
4758                    push @{$self->{open_elements}},
4759                        [$self->{head_element}, $el_category->{head}];
4760                  } else {
4761                    !!!cp ('t115');
4762                }                }
4763                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4764                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4765                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4766                redo B;                next B;
4767              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4768                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4769                    !!!cp ('t116');
4770                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4771                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4772                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4773                    !!!nack ('t116.1');
4774                  !!!next-token;                  !!!next-token;
4775                  redo B;                  next B;
4776                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4777                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4778                    !!!parse-error (type => 'in noscript', text => 'noscript',
4779                                    token => $token);
4780                  ## Ignore the token                  ## Ignore the token
4781                    !!!nack ('t117.1');
4782                  !!!next-token;                  !!!next-token;
4783                  redo B;                  next B;
4784                } else {                } else {
4785                    !!!cp ('t118');
4786                  #                  #
4787                }                }
4788              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4789                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4790                    !!!cp ('t119');
4791                  ## As if </noscript>                  ## As if </noscript>
4792                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4793                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4794                                    token => $token);
4795                                
4796                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4797                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4798                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4799                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4800                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4801                                    text => $token->{tag_name}, token => $token);
4802                    push @{$self->{open_elements}},
4803                        [$self->{head_element}, $el_category->{head}];
4804                  } else {
4805                    !!!cp ('t121');
4806                }                }
4807    
4808                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4809                $script_start_tag->($insert_to_current);                $script_start_tag->();
4810                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4811                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4812                redo B;                next B;
4813              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4814                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4815                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4816                    !!!cp ('t122');
4817                  ## As if </noscript>                  ## As if </noscript>
4818                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4819                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4820                                    text => $token->{tag_name}, token => $token);
4821                                    
4822                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4823                  ## As if </head>                  ## As if </head>
# Line 2885  sub _tree_construction_main ($) { Line 4825  sub _tree_construction_main ($) {
4825                                    
4826                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4827                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4828                    !!!cp ('t124');
4829                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4830                                    
4831                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4832                  } else {
4833                    !!!cp ('t125');
4834                }                }
4835    
4836                ## "after head" insertion mode                ## "after head" insertion mode
4837                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4838                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4839                    !!!cp ('t126');
4840                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4841                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4842                    !!!cp ('t127');
4843                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4844                } else {                } else {
4845                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4846                }                }
4847                  !!!nack ('t127.1');
4848                !!!next-token;                !!!next-token;
4849                redo B;                next B;
4850              } else {              } else {
4851                  !!!cp ('t128');
4852                #                #
4853              }              }
4854    
4855              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4856                  !!!cp ('t129');
4857                ## As if </noscript>                ## As if </noscript>
4858                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4859                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4860                                  text => $token->{tag_name}, token => $token);
4861                                
4862                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4863                ## As if </head>                ## As if </head>
# Line 2916  sub _tree_construction_main ($) { Line 4865  sub _tree_construction_main ($) {
4865    
4866                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4867              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4868                  !!!cp ('t130');
4869                ## As if </head>                ## As if </head>
4870                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4871    
4872                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4873                } else {
4874                  !!!cp ('t131');
4875              }              }
4876    
4877              ## "after head" insertion mode              ## "after head" insertion mode
4878              ## As if <body>              ## As if <body>
4879              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4880              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4881              ## reprocess              ## reprocess
4882              redo B;              !!!ack-later;
4883                next B;
4884            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4885              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4886                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4887                    !!!cp ('t132');
4888                  ## As if <head>                  ## As if <head>
4889                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4890                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4891                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4892                        [$self->{head_element}, $el_category->{head}];
4893    
4894                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4895                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4896                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4897                  !!!next-token;                  !!!next-token;
4898                  redo B;                  next B;
4899                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4900                    !!!cp ('t133');
4901                  ## As if </noscript>                  ## As if </noscript>
4902                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4903                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
4904                                    text => 'head', token => $token);
4905                                    
4906                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4907                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4908                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4909                  !!!next-token;                  !!!next-token;
4910                  redo B;                  next B;
4911                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4912                    !!!cp ('t134');
4913                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4914                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4915                  !!!next-token;                  !!!next-token;
4916                  redo B;                  next B;
4917                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4918                    !!!cp ('t134.1');
4919                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4920                                    token => $token);
4921                    ## Ignore the token
4922                    !!!next-token;
4923                    next B;
4924                } else {                } else {
4925                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4926                }                }
4927              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4928                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4929                    !!!cp ('t136');
4930                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4931                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4932                  !!!next-token;                  !!!next-token;
4933                  redo B;                  next B;
4934                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4935                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4936                    !!!cp ('t137');
4937                    !!!parse-error (type => 'unmatched end tag',
4938                                    text => 'noscript', token => $token);
4939                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4940                  !!!next-token;                  !!!next-token;
4941                  redo B;                  next B;
4942                } else {                } else {
4943                    !!!cp ('t138');
4944                  #                  #
4945                }                }
4946              } elsif ({              } elsif ({
4947                        body => 1, html => 1,                        body => 1, html => 1,
4948                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4949                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4950                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4951                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4952                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4953                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag',
4954                                    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});  
4955                  ## Ignore the token                  ## Ignore the token
4956                  !!!next-token;                  !!!next-token;
4957                  redo B;                  next B;
4958                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4959                    !!!cp ('t140.1');
4960                    !!!parse-error (type => 'unmatched end tag',
4961                                    text => $token->{tag_name}, token => $token);
4962                    ## Ignore the token
4963                    !!!next-token;
4964                    next B;
4965                  } else {
4966                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4967                }                }
4968                              } elsif ($token->{tag_name} eq 'p') {
4969                #                !!!cp ('t142');
4970              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4971                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4972                       }->{$token->{tag_name}}) {                ## Ignore the token
4973                  !!!next-token;
4974                  next B;
4975                } elsif ($token->{tag_name} eq 'br') {
4976                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4977                  ## As if <head>                  !!!cp ('t142.2');
4978                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
4979                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4980                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4981                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4982      
4983                    ## Reprocess in the "after head" insertion mode...
4984                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4985                    !!!cp ('t143.2');
4986                    ## As if </head>
4987                    pop @{$self->{open_elements}};
4988                    $self->{insertion_mode} = AFTER_HEAD_IM;
4989      
4990                    ## Reprocess in the "after head" insertion mode...
4991                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4992                    !!!cp ('t143.3');
4993                    ## ISSUE: Two parse errors for <head><noscript></br>
4994                    !!!parse-error (type => 'unmatched end tag',
4995                                    text => 'br', token => $token);
4996                    ## As if </noscript>
4997                    pop @{$self->{open_elements}};
4998                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4999    
5000                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
5001                }                  ## As if </head>
5002                    pop @{$self->{open_elements}};
5003                    $self->{insertion_mode} = AFTER_HEAD_IM;
5004    
5005                #                  ## Reprocess in the "after head" insertion mode...
5006              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5007                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
5008                  #                  #
5009                } else {                } else {
5010                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5011                }                }
5012    
5013                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5014                  !!!parse-error (type => 'unmatched end tag',
5015                                  text => 'br', token => $token);
5016                  ## Ignore the token
5017                  !!!next-token;
5018                  next B;
5019                } else {
5020                  !!!cp ('t145');
5021                  !!!parse-error (type => 'unmatched end tag',
5022                                  text => $token->{tag_name}, token => $token);
5023                  ## Ignore the token
5024                  !!!next-token;
5025                  next B;
5026              }              }
5027    
5028              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5029                  !!!cp ('t146');
5030                ## As if </noscript>                ## As if </noscript>
5031                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5032                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
5033                                  text => $token->{tag_name}, token => $token);
5034                                
5035                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5036                ## As if </head>                ## As if </head>
# Line 3028  sub _tree_construction_main ($) { Line 5038  sub _tree_construction_main ($) {
5038    
5039                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
5040              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5041                  !!!cp ('t147');
5042                ## As if </head>                ## As if </head>
5043                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5044    
5045                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
5046              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5047                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
5048                  !!!cp ('t148');
5049                  !!!parse-error (type => 'unmatched end tag',
5050                                  text => $token->{tag_name}, token => $token);
5051                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5052                !!!next-token;                !!!next-token;
5053                redo B;                next B;
5054                } else {
5055                  !!!cp ('t149');
5056              }              }
5057    
5058              ## "after head" insertion mode              ## "after head" insertion mode
5059              ## As if <body>              ## As if <body>
5060              !!!insert-element ('body');              !!!insert-element ('body',, $token);
5061              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
5062              ## reprocess              ## reprocess
5063              redo B;              next B;
5064            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5065              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5066            }            !!!cp ('t149.1');
5067    
5068              ## NOTE: As if <head>
5069              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5070              $self->{open_elements}->[-1]->[0]->append_child
5071                  ($self->{head_element});
5072              #push @{$self->{open_elements}},
5073              #    [$self->{head_element}, $el_category->{head}];
5074              #$self->{insertion_mode} = IN_HEAD_IM;
5075              ## NOTE: Reprocess.
5076    
5077              ## NOTE: As if </head>
5078              #pop @{$self->{open_elements}};
5079              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5080              ## NOTE: Reprocess.
5081              
5082              #
5083            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5084              !!!cp ('t149.2');
5085    
5086              ## NOTE: As if </head>
5087              pop @{$self->{open_elements}};
5088              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5089              ## NOTE: Reprocess.
5090    
5091              #
5092            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5093              !!!cp ('t149.3');
5094    
5095              !!!parse-error (type => 'in noscript:#eof', token => $token);
5096    
5097              ## As if </noscript>
5098              pop @{$self->{open_elements}};
5099              #$self->{insertion_mode} = IN_HEAD_IM;
5100              ## NOTE: Reprocess.
5101    
5102              ## NOTE: As if </head>
5103              pop @{$self->{open_elements}};
5104              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5105              ## NOTE: Reprocess.
5106    
5107              #
5108            } else {
5109              !!!cp ('t149.4');
5110              #
5111            }
5112    
5113            ## NOTE: As if <body>
5114            !!!insert-element ('body',, $token);
5115            $self->{insertion_mode} = IN_BODY_IM;
5116            ## NOTE: Reprocess.
5117            next B;
5118          } else {
5119            die "$0: $token->{type}: Unknown token type";
5120          }
5121    
5122            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5123      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
5124            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5125                !!!cp ('t150');
5126              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
5127              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5128                            
5129              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5130    
5131              !!!next-token;              !!!next-token;
5132              redo B;              next B;
5133            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5134              if ({              if ({
5135                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3066  sub _tree_construction_main ($) { Line 5137  sub _tree_construction_main ($) {
5137                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5138                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5139                  ## have an element in table scope                  ## have an element in table scope
5140                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5141                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5142                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5143                      $tn = $node->[1];                      !!!cp ('t151');
5144                      last INSCOPE;  
5145                    } elsif ({                      ## Close the cell
5146                              table => 1, html => 1,                      !!!back-token; # <x>
5147                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
5148                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
5149                    }                                line => $token->{line},
5150                  } # INSCOPE                                column => $token->{column}};
5151                    unless (defined $tn) {                      next B;
5152                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5153                      ## Ignore the token                      !!!cp ('t152');
5154                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
5155                      redo B;                      last;
5156                    }                    }
5157                                    }
5158                  ## Close the cell  
5159                  !!!back-token; # <?>                  !!!cp ('t153');
5160                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
5161                  redo B;                      text => $token->{tag_name}, token => $token);
5162                    ## Ignore the token
5163                    !!!nack ('t153.1');
5164                    !!!next-token;
5165                    next B;
5166                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5167                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5168                                    token => $token);
5169                                    
5170                  ## As if </caption>                  ## NOTE: As if </caption>.
5171                  ## have a table element in table scope                  ## have a table element in table scope
5172                  my $i;                  my $i;
5173                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5174                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5175                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5176                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5177                      last INSCOPE;                        !!!cp ('t155');
5178                    } elsif ({                        $i = $_;
5179                              table => 1, html => 1,                        last INSCOPE;
5180                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5181                      last INSCOPE;                        !!!cp ('t156');
5182                          last;
5183                        }
5184                    }                    }
5185    
5186                      !!!cp ('t157');
5187                      !!!parse-error (type => 'start tag not allowed',
5188                                      text => $token->{tag_name}, token => $token);
5189                      ## Ignore the token
5190                      !!!nack ('t157.1');
5191                      !!!next-token;
5192                      next B;
5193                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5194                                    
5195                  ## generate implied end tags                  ## generate implied end tags
5196                  if ({                  while ($self->{open_elements}->[-1]->[1]
5197                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5198                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
5199                       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;  
5200                  }                  }
5201    
5202                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5203                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
5204                      !!!parse-error (type => 'not closed',
5205                                      text => $self->{open_elements}->[-1]->[0]
5206                                          ->manakai_local_name,
5207                                      token => $token);
5208                    } else {
5209                      !!!cp ('t160');
5210                  }                  }
5211                                    
5212                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3138  sub _tree_construction_main ($) { Line 5216  sub _tree_construction_main ($) {
5216                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5217                                    
5218                  ## reprocess                  ## reprocess
5219                  redo B;                  !!!ack-later;
5220                    next B;
5221                } else {                } else {
5222                    !!!cp ('t161');
5223                  #                  #
5224                }                }
5225              } else {              } else {
5226                  !!!cp ('t162');
5227                #                #
5228              }              }
5229            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3152  sub _tree_construction_main ($) { Line 5233  sub _tree_construction_main ($) {
5233                  my $i;                  my $i;
5234                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5235                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5236                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5237                        !!!cp ('t163');
5238                      $i = $_;                      $i = $_;
5239                      last INSCOPE;                      last INSCOPE;
5240                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5241                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
5242                      last INSCOPE;                      last INSCOPE;
5243                    }                    }
5244                  } # INSCOPE                  } # INSCOPE
5245                    unless (defined $i) {                    unless (defined $i) {
5246                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
5247                        !!!parse-error (type => 'unmatched end tag',
5248                                        text => $token->{tag_name},
5249                                        token => $token);
5250                      ## Ignore the token                      ## Ignore the token
5251                      !!!next-token;                      !!!next-token;
5252                      redo B;                      next B;
5253                    }                    }
5254                                    
5255                  ## generate implied end tags                  ## generate implied end tags
5256                  if ({                  while ($self->{open_elements}->[-1]->[1]
5257                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5258                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
5259                       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;  
5260                  }                  }
5261                    
5262                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5263                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
5264                      !!!cp ('t167');
5265                      !!!parse-error (type => 'not closed',
5266                                      text => $self->{open_elements}->[-1]->[0]
5267                                          ->manakai_local_name,
5268                                      token => $token);
5269                    } else {
5270                      !!!cp ('t168');
5271                  }                  }
5272                                    
5273                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3193  sub _tree_construction_main ($) { Line 5277  sub _tree_construction_main ($) {
5277                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5278                                    
5279                  !!!next-token;                  !!!next-token;
5280                  redo B;                  next B;
5281                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5282                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
5283                    !!!parse-error (type => 'unmatched end tag',
5284                                    text => $token->{tag_name}, token => $token);
5285                  ## Ignore the token                  ## Ignore the token
5286                  !!!next-token;                  !!!next-token;
5287                  redo B;                  next B;
5288                } else {                } else {
5289                    !!!cp ('t170');
5290                  #                  #
5291                }                }
5292              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
5293                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5294                  ## have a table element in table scope                  ## have a table element in table scope
5295                  my $i;                  my $i;
5296                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5297                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5298                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5299                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5300                      last INSCOPE;                        !!!cp ('t171');
5301                    } elsif ({                        $i = $_;
5302                              table => 1, html => 1,                        last INSCOPE;
5303                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5304                      last INSCOPE;                        !!!cp ('t172');
5305                          last;
5306                        }
5307                    }                    }
5308    
5309                      !!!cp ('t173');
5310                      !!!parse-error (type => 'unmatched end tag',
5311                                      text => $token->{tag_name}, token => $token);
5312                      ## Ignore the token
5313                      !!!next-token;
5314                      next B;
5315                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5316                                    
5317                  ## generate implied end tags                  ## generate implied end tags
5318                  if ({                  while ($self->{open_elements}->[-1]->[1]
5319                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5320                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
5321                       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;  
5322                  }                  }
5323                                    
5324                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5325                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
5326                      !!!parse-error (type => 'not closed',
5327                                      text => $self->{open_elements}->[-1]->[0]
5328                                          ->manakai_local_name,
5329                                      token => $token);
5330                    } else {
5331                      !!!cp ('t176');
5332                  }                  }
5333                                    
5334                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3247  sub _tree_construction_main ($) { Line 5338  sub _tree_construction_main ($) {
5338                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5339                                    
5340                  !!!next-token;                  !!!next-token;
5341                  redo B;                  next B;
5342                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5343                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5344                    !!!parse-error (type => 'unmatched end tag',
5345                                    text => $token->{tag_name}, token => $token);
5346                  ## Ignore the token                  ## Ignore the token
5347                  !!!next-token;                  !!!next-token;
5348                  redo B;                  next B;
5349                } else {                } else {
5350                    !!!cp ('t178');
5351                  #                  #
5352                }                }
5353              } elsif ({              } elsif ({
# Line 3264  sub _tree_construction_main ($) { Line 5358  sub _tree_construction_main ($) {
5358                ## have an element in table scope                ## have an element in table scope
5359                my $i;                my $i;
5360                my $tn;                my $tn;
5361                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5362                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5363                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5364                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5365                    last INSCOPE;                      !!!cp ('t179');
5366                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5367                    $tn = $node->[1];  
5368                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5369                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5370                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5371                            table => 1, html => 1,                                line => $token->{line},
5372                           }->{$node->[1]}) {                                column => $token->{column}};
5373                    last INSCOPE;                      next B;
5374                      } elsif ($node->[1] & TABLE_CELL_EL) {
5375                        !!!cp ('t180');
5376                        $tn = $node->[0]->manakai_local_name;
5377                        ## NOTE: There is exactly one |td| or |th| element
5378                        ## in scope in the stack of open elements by definition.
5379                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5380                        ## ISSUE: Can this be reached?
5381                        !!!cp ('t181');
5382                        last;
5383                      }
5384                  }                  }
5385                } # INSCOPE  
5386                unless (defined $i) {                  !!!cp ('t182');
5387                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5388                        text => $token->{tag_name}, token => $token);
5389                  ## Ignore the token                  ## Ignore the token
5390                  !!!next-token;                  !!!next-token;
5391                  redo B;                  next B;
5392                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5393              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5394                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5395                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5396                                  token => $token);
5397    
5398                ## As if </caption>                ## As if </caption>
5399                ## have a table element in table scope                ## have a table element in table scope
5400                my $i;                my $i;
5401                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5402                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5403                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5404                      !!!cp ('t184');
5405                    $i = $_;                    $i = $_;
5406                    last INSCOPE;                    last INSCOPE;
5407                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5408                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5409                    last INSCOPE;                    last INSCOPE;
5410                  }                  }
5411                } # INSCOPE                } # INSCOPE
5412                unless (defined $i) {                unless (defined $i) {
5413                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5414                    !!!parse-error (type => 'unmatched end tag',
5415                                    text => 'caption', token => $token);
5416                  ## Ignore the token                  ## Ignore the token
5417                  !!!next-token;                  !!!next-token;
5418                  redo B;                  next B;
5419                }                }
5420                                
5421                ## generate implied end tags                ## generate implied end tags
5422                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5423                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5424                     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;  
5425                }                }
5426    
5427                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5428                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5429                    !!!parse-error (type => 'not closed',
5430                                    text => $self->{open_elements}->[-1]->[0]
5431                                        ->manakai_local_name,
5432                                    token => $token);
5433                  } else {
5434                    !!!cp ('t189');
5435                }                }
5436    
5437                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3340  sub _tree_construction_main ($) { Line 5441  sub _tree_construction_main ($) {
5441                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5442    
5443                ## reprocess                ## reprocess
5444                redo B;                next B;
5445              } elsif ({              } elsif ({
5446                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5447                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5448                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5449                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
5450                    !!!parse-error (type => 'unmatched end tag',
5451                                    text => $token->{tag_name}, token => $token);
5452                  ## Ignore the token                  ## Ignore the token
5453                  !!!next-token;                  !!!next-token;
5454                  redo B;                  next B;
5455                } else {                } else {
5456                    !!!cp ('t191');
5457                  #                  #
5458                }                }
5459              } elsif ({              } elsif ({
# Line 3357  sub _tree_construction_main ($) { Line 5461  sub _tree_construction_main ($) {
5461                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5462                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5463                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5464                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5465                  !!!parse-error (type => 'unmatched end tag',
5466                                  text => $token->{tag_name}, token => $token);
5467                ## Ignore the token                ## Ignore the token
5468                !!!next-token;                !!!next-token;
5469                redo B;                next B;
5470              } else {              } else {
5471                  !!!cp ('t193');
5472                #                #
5473              }              }
5474          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5475            for my $entry (@{$self->{open_elements}}) {
5476              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5477                !!!cp ('t75');
5478                !!!parse-error (type => 'in body:#eof', token => $token);
5479                last;
5480              }
5481            }
5482    
5483            ## Stop parsing.
5484            last B;
5485        } else {        } else {
5486          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5487        }        }
# Line 3372  sub _tree_construction_main ($) { Line 5490  sub _tree_construction_main ($) {
5490        #        #
5491      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5492        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5493              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5494                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5495              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5496                                
5497                unless (length $token->{data}) {            unless (length $token->{data}) {
5498                  !!!next-token;              !!!cp ('t194');
5499                  redo B;              !!!next-token;
5500                }              next B;
5501              }            } else {
5502                !!!cp ('t195');
5503              }
5504            }
5505    
5506              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5507    
5508              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5509              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3389  sub _tree_construction_main ($) { Line 5511  sub _tree_construction_main ($) {
5511              ## result in a new Text node.              ## result in a new Text node.
5512              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5513                            
5514              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]}) {  
5515                # MUST                # MUST
5516                my $foster_parent_element;                my $foster_parent_element;
5517                my $next_sibling;                my $next_sibling;
5518                my $prev_sibling;                my $prev_sibling;
5519                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5520                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5521                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5522                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5523                        !!!cp ('t196');
5524                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5525                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5526                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5527                    } else {                    } else {
5528                        !!!cp ('t197');
5529                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5530                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5531                    }                    }
# Line 3416  sub _tree_construction_main ($) { Line 5537  sub _tree_construction_main ($) {
5537                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5538                if (defined $prev_sibling and                if (defined $prev_sibling and
5539                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5540                    !!!cp ('t198');
5541                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5542                } else {                } else {
5543                    !!!cp ('t199');
5544                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5545                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5546                     $next_sibling);                     $next_sibling);
5547                }                }
5548              } else {            $open_tables->[-1]->[1] = 1; # tainted
5549                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5550              }            !!!cp ('t200');
5551              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5552            }
5553                            
5554              !!!next-token;          !!!next-token;
5555              redo B;          next B;
5556        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5557              if ({          if ({
5558                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5559                   th => 1, td => 1,               th => 1, td => 1,
5560                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5561                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5562                  ## Clear back to table context              ## Clear back to table context
5563                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5564                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5565                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!cp ('t201');
5566                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5567                  }              }
5568                                
5569                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5570                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5571                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5572                }            }
5573              
5574                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5575                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5576                    !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t202');
5577                  }                !!!parse-error (type => 'missing start tag:tr', token => $token);
5578                }
5579                                    
5580                  ## Clear back to table body context              ## Clear back to table body context
5581                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5582                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5583                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5584                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## ISSUE: Can this case be reached?
5585                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5586                  }              }
5587                                    
5588                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5589                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5590                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5591                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5592                      !!!nack ('t204');
5593                    !!!next-token;                    !!!next-token;
5594                    redo B;                    next B;
5595                  } else {                  } else {
5596                    !!!insert-element ('tr');                    !!!cp ('t205');
5597                      !!!insert-element ('tr',, $token);
5598                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5599                  }                  }
5600                  } else {
5601                    !!!cp ('t206');
5602                }                }
5603    
5604                ## Clear back to table row context                ## Clear back to table row context
5605                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5606                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5607                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5608                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5609                }                }
5610                                
5611                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5612                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5613    
5614                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5615                                
5616                  !!!nack ('t207.1');
5617                !!!next-token;                !!!next-token;
5618                redo B;                next B;
5619              } elsif ({              } elsif ({
5620                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5621                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3496  sub _tree_construction_main ($) { Line 5627  sub _tree_construction_main ($) {
5627                  my $i;                  my $i;
5628                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5629                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5630                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5631                        !!!cp ('t208');
5632                      $i = $_;                      $i = $_;
5633                      last INSCOPE;                      last INSCOPE;
5634                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5635                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5636                      last INSCOPE;                      last INSCOPE;
5637                    }                    }
5638                  } # INSCOPE                  } # INSCOPE
5639                  unless (defined $i) {                  unless (defined $i) {
5640                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5641    ## TODO: This type is wrong.
5642                      !!!parse-error (type => 'unmacthed end tag',
5643                                      text => $token->{tag_name}, token => $token);
5644                    ## Ignore the token                    ## Ignore the token
5645                      !!!nack ('t210.1');
5646                    !!!next-token;                    !!!next-token;
5647                    redo B;                    next B;
5648                  }                  }
5649                                    
5650                  ## Clear back to table row context                  ## Clear back to table row context
5651                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5652                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5653                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5654                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5655                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5656                  }                  }
5657                                    
5658                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5659                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5660                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5661                      !!!cp ('t212');
5662                    ## reprocess                    ## reprocess
5663                    redo B;                    !!!ack-later;
5664                      next B;
5665                  } else {                  } else {
5666                      !!!cp ('t213');
5667                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5668                  }                  }
5669                }                }
# Line 3535  sub _tree_construction_main ($) { Line 5673  sub _tree_construction_main ($) {
5673                  my $i;                  my $i;
5674                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5675                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5676                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5677                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5678                      $i = $_;                      $i = $_;
5679                      last INSCOPE;                      last INSCOPE;
5680                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5681                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5682                      last INSCOPE;                      last INSCOPE;
5683                    }                    }
5684                  } # INSCOPE                  } # INSCOPE
5685                  unless (defined $i) {                  unless (defined $i) {
5686                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5687    ## TODO: This erorr type is wrong.
5688                      !!!parse-error (type => 'unmatched end tag',
5689                                      text => $token->{tag_name}, token => $token);
5690                    ## Ignore the token                    ## Ignore the token
5691                      !!!nack ('t216.1');
5692                    !!!next-token;                    !!!next-token;
5693                    redo B;                    next B;
5694                  }                  }
5695    
5696                  ## Clear back to table body context                  ## Clear back to table body context
5697                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5698                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5699                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5700                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5701                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5702                  }                  }
5703                                    
# Line 3571  sub _tree_construction_main ($) { Line 5711  sub _tree_construction_main ($) {
5711                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5712                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5713                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5714                  } else {
5715                    !!!cp ('t218');
5716                }                }
5717    
5718                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5719                  ## Clear back to table context                  ## Clear back to table context
5720                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5721                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5722                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5723                      ## ISSUE: Can this state be reached?
5724                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5725                  }                  }
5726                                    
5727                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5728                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5729                  ## reprocess                  ## reprocess
5730                  redo B;                  !!!ack-later;
5731                    next B;
5732                } elsif ({                } elsif ({
5733                          caption => 1,                          caption => 1,
5734                          colgroup => 1,                          colgroup => 1,
5735                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5736                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5737                  ## Clear back to table context                  ## Clear back to table context
5738                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5739                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5740                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5741                      ## ISSUE: Can this state be reached?
5742                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5743                  }                  }
5744                                    
5745                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5746                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5747                                    
5748                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5749                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5750                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5751                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3609  sub _tree_construction_main ($) { Line 5754  sub _tree_construction_main ($) {
5754                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5755                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5756                  !!!next-token;                  !!!next-token;
5757                  redo B;                  !!!nack ('t220.1');
5758                    next B;
5759                } else {                } else {
5760                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5761                }                }
5762              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5763                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5764                                  text => $self->{open_elements}->[-1]->[0]
5765                                      ->manakai_local_name,
5766                                  token => $token);
5767    
5768                ## As if </table>                ## As if </table>
5769                ## have a table element in table scope                ## have a table element in table scope
5770                my $i;                my $i;
5771                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5772                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5773                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5774                      !!!cp ('t221');
5775                    $i = $_;                    $i = $_;
5776                    last INSCOPE;                    last INSCOPE;
5777                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5778                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5779                    last INSCOPE;                    last INSCOPE;
5780                  }                  }
5781                } # INSCOPE                } # INSCOPE
5782                unless (defined $i) {                unless (defined $i) {
5783                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5784    ## TODO: The following is wrong, maybe.
5785                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5786                                    token => $token);
5787                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5788                    !!!nack ('t223.1');
5789                  !!!next-token;                  !!!next-token;
5790                  redo B;                  next B;
5791                }                }
5792                                
5793    ## TODO: Followings are removed from the latest spec.
5794                ## generate implied end tags                ## generate implied end tags
5795                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5796                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5797                     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;  
5798                }                }
5799    
5800                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5801                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5802                    ## NOTE: |<table><tr><table>|
5803                    !!!parse-error (type => 'not closed',
5804                                    text => $self->{open_elements}->[-1]->[0]
5805                                        ->manakai_local_name,
5806                                    token => $token);
5807                  } else {
5808                    !!!cp ('t226');
5809                }                }
5810    
5811                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5812                  pop @{$open_tables};
5813    
5814                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5815    
5816                ## reprocess            ## reprocess
5817                redo B;            !!!ack-later;
5818          } else {            next B;
5819            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5820              if (not $open_tables->[-1]->[1]) { # tainted
5821                !!!cp ('t227.8');
5822                ## NOTE: This is a "as if in head" code clone.
5823                $parse_rcdata->(CDATA_CONTENT_MODEL);
5824                next B;
5825              } else {
5826                !!!cp ('t227.7');
5827                #
5828              }
5829            } elsif ($token->{tag_name} eq 'script') {
5830              if (not $open_tables->[-1]->[1]) { # tainted
5831                !!!cp ('t227.6');
5832                ## NOTE: This is a "as if in head" code clone.
5833                $script_start_tag->();
5834                next B;
5835              } else {
5836                !!!cp ('t227.5');
5837                #
5838              }
5839            } elsif ($token->{tag_name} eq 'input') {
5840              if (not $open_tables->[-1]->[1]) { # tainted
5841                if ($token->{attributes}->{type}) { ## TODO: case
5842                  my $type = lc $token->{attributes}->{type}->{value};
5843                  if ($type eq 'hidden') {
5844                    !!!cp ('t227.3');
5845                    !!!parse-error (type => 'in table',
5846                                    text => $token->{tag_name}, token => $token);
5847    
5848            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5849    
5850                    ## TODO: form element pointer
5851    
5852                    pop @{$self->{open_elements}};
5853    
5854                    !!!next-token;
5855                    !!!ack ('t227.2.1');
5856                    next B;
5857                  } else {
5858                    !!!cp ('t227.2');
5859                    #
5860                  }
5861                } else {
5862                  !!!cp ('t227.1');
5863                  #
5864                }
5865              } else {
5866                !!!cp ('t227.4');
5867                #
5868              }
5869            } else {
5870              !!!cp ('t227');
5871            #            #
5872          }          }
5873    
5874            !!!parse-error (type => 'in table', text => $token->{tag_name},
5875                            token => $token);
5876    
5877            $insert = $insert_to_foster;
5878            #
5879        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5880              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5881                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3674  sub _tree_construction_main ($) { Line 5883  sub _tree_construction_main ($) {
5883                my $i;                my $i;
5884                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5885                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5886                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5887                      !!!cp ('t228');
5888                    $i = $_;                    $i = $_;
5889                    last INSCOPE;                    last INSCOPE;
5890                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5891                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5892                    last INSCOPE;                    last INSCOPE;
5893                  }                  }
5894                } # INSCOPE                } # INSCOPE
5895                unless (defined $i) {                unless (defined $i) {
5896                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5897                    !!!parse-error (type => 'unmatched end tag',
5898                                    text => $token->{tag_name}, token => $token);
5899                  ## Ignore the token                  ## Ignore the token
5900                    !!!nack ('t230.1');
5901                  !!!next-token;                  !!!next-token;
5902                  redo B;                  next B;
5903                  } else {
5904                    !!!cp ('t232');
5905                }                }
5906    
5907                ## Clear back to table row context                ## Clear back to table row context
5908                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5909                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5910                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5911                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5912                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5913                }                }
5914    
5915                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5916                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5917                !!!next-token;                !!!next-token;
5918                redo B;                !!!nack ('t231.1');
5919                  next B;
5920              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5921                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5922                  ## As if </tr>                  ## As if </tr>
# Line 3709  sub _tree_construction_main ($) { Line 5924  sub _tree_construction_main ($) {
5924                  my $i;                  my $i;
5925                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5926                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5927                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5928                        !!!cp ('t233');
5929                      $i = $_;                      $i = $_;
5930                      last INSCOPE;                      last INSCOPE;
5931                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5932                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5933                      last INSCOPE;                      last INSCOPE;
5934                    }                    }
5935                  } # INSCOPE                  } # INSCOPE
5936                  unless (defined $i) {                  unless (defined $i) {
5937                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5938    ## TODO: The following is wrong.
5939                      !!!parse-error (type => 'unmatched end tag',
5940                                      text => $token->{type}, token => $token);
5941                    ## Ignore the token                    ## Ignore the token
5942                      !!!nack ('t236.1');
5943                    !!!next-token;                    !!!next-token;
5944                    redo B;                    next B;
5945                  }                  }
5946                                    
5947                  ## Clear back to table row context                  ## Clear back to table row context
5948                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5949                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5950                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5951                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5952                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5953                  }                  }
5954                                    
# Line 3743  sub _tree_construction_main ($) { Line 5962  sub _tree_construction_main ($) {
5962                  my $i;                  my $i;
5963                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5964                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5965                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5966                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5967                      $i = $_;                      $i = $_;
5968                      last INSCOPE;                      last INSCOPE;
5969                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5970                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5971                      last INSCOPE;                      last INSCOPE;
5972                    }                    }
5973                  } # INSCOPE                  } # INSCOPE
5974                  unless (defined $i) {                  unless (defined $i) {
5975                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5976                      !!!parse-error (type => 'unmatched end tag',
5977                                      text => $token->{tag_name}, token => $token);
5978                    ## Ignore the token                    ## Ignore the token
5979                      !!!nack ('t239.1');
5980                    !!!next-token;                    !!!next-token;
5981                    redo B;                    next B;
5982                  }                  }
5983                                    
5984                  ## Clear back to table body context                  ## Clear back to table body context
5985                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5986                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5987                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5988                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5989                  }                  }
5990                                    
# Line 3781  sub _tree_construction_main ($) { Line 6000  sub _tree_construction_main ($) {
6000                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
6001                }                }
6002    
6003                  ## NOTE: </table> in the "in table" insertion mode.
6004                  ## When you edit the code fragment below, please ensure that
6005                  ## the code for <table> in the "in table" insertion mode
6006                  ## is synced with it.
6007    
6008                ## have a table element in table scope                ## have a table element in table scope
6009                my $i;                my $i;
6010                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6011                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6012                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
6013                      !!!cp ('t241');
6014                    $i = $_;                    $i = $_;
6015                    last INSCOPE;                    last INSCOPE;
6016                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6017                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
6018                    last INSCOPE;                    last INSCOPE;
6019                  }                  }
6020                } # INSCOPE                } # INSCOPE
6021                unless (defined $i) {                unless (defined $i) {
6022                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
6023                    !!!parse-error (type => 'unmatched end tag',
6024                                    text => $token->{tag_name}, token => $token);
6025                  ## Ignore the token                  ## Ignore the token
6026                    !!!nack ('t243.1');
6027                  !!!next-token;                  !!!next-token;
6028                  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]);  
6029                }                }
6030                                    
6031                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
6032                  pop @{$open_tables};
6033                                
6034                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6035                                
6036                !!!next-token;                !!!next-token;
6037                redo B;                next B;
6038              } elsif ({              } elsif ({
6039                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6040                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3832  sub _tree_construction_main ($) { Line 6044  sub _tree_construction_main ($) {
6044                  my $i;                  my $i;
6045                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6046                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6047                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6048                        !!!cp ('t247');
6049                      $i = $_;                      $i = $_;
6050                      last INSCOPE;                      last INSCOPE;
6051                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6052                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
6053                      last INSCOPE;                      last INSCOPE;
6054                    }                    }
6055                  } # INSCOPE                  } # INSCOPE
6056                    unless (defined $i) {                    unless (defined $i) {
6057                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
6058                        !!!parse-error (type => 'unmatched end tag',
6059                                        text => $token->{tag_name}, token => $token);
6060                      ## Ignore the token                      ## Ignore the token
6061                        !!!nack ('t249.1');
6062                      !!!next-token;                      !!!next-token;
6063                      redo B;                      next B;
6064                    }                    }
6065                                    
6066                  ## As if </tr>                  ## As if </tr>
# Line 3853  sub _tree_construction_main ($) { Line 6068  sub _tree_construction_main ($) {
6068                  my $i;                  my $i;
6069                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6070                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6071                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6072                        !!!cp ('t250');
6073                      $i = $_;                      $i = $_;
6074                      last INSCOPE;                      last INSCOPE;
6075                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6076                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
6077                      last INSCOPE;                      last INSCOPE;
6078                    }                    }
6079                  } # INSCOPE                  } # INSCOPE
6080                    unless (defined $i) {                    unless (defined $i) {
6081                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
6082                        !!!parse-error (type => 'unmatched end tag',
6083                                        text => 'tr', token => $token);
6084                      ## Ignore the token                      ## Ignore the token
6085                        !!!nack ('t252.1');
6086                      !!!next-token;                      !!!next-token;
6087                      redo B;                      next B;
6088                    }                    }
6089                                    
6090                  ## Clear back to table row context                  ## Clear back to table row context
6091                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6092                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
6093                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
6094                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6095                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6096                  }                  }
6097                                    
# Line 3886  sub _tree_construction_main ($) { Line 6104  sub _tree_construction_main ($) {
6104                my $i;                my $i;
6105                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6106                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6107                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6108                      !!!cp ('t254');
6109                    $i = $_;                    $i = $_;
6110                    last INSCOPE;                    last INSCOPE;
6111                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6112                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6113                    last INSCOPE;                    last INSCOPE;
6114                  }                  }
6115                } # INSCOPE                } # INSCOPE
6116                unless (defined $i) {                unless (defined $i) {
6117                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
6118                    !!!parse-error (type => 'unmatched end tag',
6119                                    text => $token->{tag_name}, token => $token);
6120                  ## Ignore the token                  ## Ignore the token
6121                    !!!nack ('t256.1');
6122                  !!!next-token;                  !!!next-token;
6123                  redo B;                  next B;
6124                }                }
6125    
6126                ## Clear back to table body context                ## Clear back to table body context
6127                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6128                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6129                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6130                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6131                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6132                }                }
6133    
6134                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6135                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6136                  !!!nack ('t257.1');
6137                !!!next-token;                !!!next-token;
6138                redo B;                next B;
6139              } elsif ({              } elsif ({
6140                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6141                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6142                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6143                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6144                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6145                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6146                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6147                !!!next-token;                            text => $token->{tag_name}, token => $token);
6148                redo B;            ## Ignore the token
6149          } else {            !!!nack ('t258.1');
6150            !!!parse-error (type => 'in table:/'.$token->{tag_name});             !!!next-token;
6151              next B;
6152            } else {
6153              !!!cp ('t259');
6154              !!!parse-error (type => 'in table:/',
6155                              text => $token->{tag_name}, token => $token);
6156    
6157            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6158            #            #
6159          }          }
6160          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6161            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6162                    @{$self->{open_elements}} == 1) { # redundant, maybe
6163              !!!parse-error (type => 'in body:#eof', token => $token);
6164              !!!cp ('t259.1');
6165              #
6166            } else {
6167              !!!cp ('t259.2');
6168              #
6169            }
6170    
6171            ## Stop parsing
6172            last B;
6173        } else {        } else {
6174          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6175        }        }
# Line 3938  sub _tree_construction_main ($) { Line 6178  sub _tree_construction_main ($) {
6178              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6179                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6180                unless (length $token->{data}) {                unless (length $token->{data}) {
6181                    !!!cp ('t260');
6182                  !!!next-token;                  !!!next-token;
6183                  redo B;                  next B;
6184                }                }
6185              }              }
6186                            
6187                !!!cp ('t261');
6188              #              #
6189            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6190              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6191                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
6192                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6193                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6194                  !!!ack ('t262.1');
6195                !!!next-token;                !!!next-token;
6196                redo B;                next B;
6197              } else {              } else {
6198                  !!!cp ('t263');
6199                #                #
6200              }              }
6201            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6202              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6203                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6204                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
6205                    !!!parse-error (type => 'unmatched end tag',
6206                                    text => 'colgroup', token => $token);
6207                  ## Ignore the token                  ## Ignore the token
6208                  !!!next-token;                  !!!next-token;
6209                  redo B;                  next B;
6210                } else {                } else {
6211                    !!!cp ('t265');
6212                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6213                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6214                  !!!next-token;                  !!!next-token;
6215                  redo B;                              next B;            
6216                }                }
6217              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6218                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
6219                  !!!parse-error (type => 'unmatched end tag',
6220                                  text => 'col', token => $token);
6221                ## Ignore the token                ## Ignore the token
6222                !!!next-token;                !!!next-token;
6223                redo B;                next B;
6224              } else {              } else {
6225                  !!!cp ('t267');
6226                #                #
6227              }              }
6228            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6229              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6230            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6231              !!!cp ('t270.2');
6232              ## Stop parsing.
6233              last B;
6234            } else {
6235              ## NOTE: As if </colgroup>.
6236              !!!cp ('t270.1');
6237              pop @{$self->{open_elements}}; # colgroup
6238              $self->{insertion_mode} = IN_TABLE_IM;
6239              ## Reprocess.
6240              next B;
6241            }
6242          } else {
6243            die "$0: $token->{type}: Unknown token type";
6244          }
6245    
6246            ## As if </colgroup>            ## As if </colgroup>
6247            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6248              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
6249    ## TODO: Wrong error type?
6250                !!!parse-error (type => 'unmatched end tag',
6251                                text => 'colgroup', token => $token);
6252              ## Ignore the token              ## Ignore the token
6253                !!!nack ('t269.1');
6254              !!!next-token;              !!!next-token;
6255              redo B;              next B;
6256            } else {            } else {
6257                !!!cp ('t270');
6258              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6259              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6260                !!!ack-later;
6261              ## reprocess              ## reprocess
6262              redo B;              next B;
6263            }            }
6264      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6265        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6266            !!!cp ('t271');
6267          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6268          !!!next-token;          !!!next-token;
6269          redo B;          next B;
6270        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6271              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6272                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6273                  ## As if </option>              !!!cp ('t272');
6274                  pop @{$self->{open_elements}};              ## As if </option>
6275                }              pop @{$self->{open_elements}};
6276              } else {
6277                !!!cp ('t273');
6278              }
6279    
6280                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6281                !!!next-token;            !!!nack ('t273.1');
6282                redo B;            !!!next-token;
6283              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6284                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6285                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6286                  pop @{$self->{open_elements}};              !!!cp ('t274');
6287                }              ## As if </option>
6288                pop @{$self->{open_elements}};
6289              } else {
6290                !!!cp ('t275');
6291              }
6292    
6293                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6294                  ## As if </optgroup>              !!!cp ('t276');
6295                  pop @{$self->{open_elements}};              ## As if </optgroup>
6296                }              pop @{$self->{open_elements}};
6297              } else {
6298                !!!cp ('t277');
6299              }
6300    
6301                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6302                !!!next-token;            !!!nack ('t277.1');
6303                redo B;            !!!next-token;
6304              } elsif ($token->{tag_name} eq 'select') {            next B;
6305                !!!parse-error (type => 'not closed:select');          } elsif ({
6306                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6307                ## have an element in table scope                   }->{$token->{tag_name}} or
6308                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6309                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6310                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6311                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6312                    $i = $_;                     tr => 1, td => 1, th => 1,
6313                    last INSCOPE;                    }->{$token->{tag_name}})) {
6314                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6315                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6316                           }->{$node->[1]}) {                            token => $token);
6317                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6318                  }            ## as if there were </select> (otherwise).
6319                } # INSCOPE            ## have an element in table scope
6320                unless (defined $i) {            my $i;
6321                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6322                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6323                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6324                  redo B;                !!!cp ('t278');
6325                }                $i = $_;
6326                  last INSCOPE;
6327                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6328                  !!!cp ('t279');
6329                  last INSCOPE;
6330                }
6331              } # INSCOPE
6332              unless (defined $i) {
6333                !!!cp ('t280');
6334                !!!parse-error (type => 'unmatched end tag',
6335                                text => 'select', token => $token);
6336                ## Ignore the token
6337                !!!nack ('t280.1');
6338                !!!next-token;
6339                next B;
6340              }
6341                                
6342                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6343              splice @{$self->{open_elements}}, $i;
6344    
6345                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6346    
6347                !!!next-token;            if ($token->{tag_name} eq 'select') {
6348                redo B;              !!!nack ('t281.2');
6349                !!!next-token;
6350                next B;
6351              } else {
6352                !!!cp ('t281.1');
6353                !!!ack-later;
6354                ## Reprocess the token.
6355                next B;
6356              }
6357          } else {          } else {
6358            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
6359              !!!parse-error (type => 'in select',
6360                              text => $token->{tag_name}, token => $token);
6361            ## Ignore the token            ## Ignore the token
6362              !!!nack ('t282.1');
6363            !!!next-token;            !!!next-token;
6364            redo B;            next B;
6365          }          }
6366        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6367              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6368                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6369                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6370                  ## As if </option>              !!!cp ('t283');
6371                  splice @{$self->{open_elements}}, -2;              ## As if </option>
6372                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
6373                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6374                } else {              !!!cp ('t284');
6375                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
6376                  ## Ignore the token            } else {
6377                }              !!!cp ('t285');
6378                !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6379                redo B;                              text => $token->{tag_name}, token => $token);
6380              } elsif ($token->{tag_name} eq 'option') {              ## Ignore the token
6381                if ($self->{open_elements}->[-1]->[1] eq 'option') {            }
6382                  pop @{$self->{open_elements}};            !!!nack ('t285.1');
6383                } else {            !!!next-token;
6384                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            next B;
6385                  ## Ignore the token          } elsif ($token->{tag_name} eq 'option') {
6386                }            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6387                !!!next-token;              !!!cp ('t286');
6388                redo B;              pop @{$self->{open_elements}};
6389              } elsif ($token->{tag_name} eq 'select') {            } else {
6390                ## have an element in table scope              !!!cp ('t287');
6391                my $i;              !!!parse-error (type => 'unmatched end tag',
6392                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                              text => $token->{tag_name}, token => $token);
6393                  my $node = $self->{open_elements}->[$_];              ## Ignore the token
6394                  if ($node->[1] eq $token->{tag_name}) {            }
6395                    $i = $_;            !!!nack ('t287.1');
6396                    last INSCOPE;            !!!next-token;
6397                  } elsif ({            next B;
6398                            table => 1, html => 1,          } elsif ($token->{tag_name} eq 'select') {
6399                           }->{$node->[1]}) {            ## have an element in table scope
6400                    last INSCOPE;            my $i;
6401                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6402                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6403                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6404                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t288');
6405                  ## Ignore the token                $i = $_;
6406                  !!!next-token;                last INSCOPE;
6407                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6408                }                !!!cp ('t289');
6409                  last INSCOPE;
6410                }
6411              } # INSCOPE
6412              unless (defined $i) {
6413                !!!cp ('t290');
6414                !!!parse-error (type => 'unmatched end tag',
6415                                text => $token->{tag_name}, token => $token);
6416                ## Ignore the token
6417                !!!nack ('t290.1');
6418                !!!next-token;
6419                next B;
6420              }
6421                                
6422                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6423              splice @{$self->{open_elements}}, $i;
6424    
6425                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6426    
6427                !!!next-token;            !!!nack ('t291.1');
6428                redo B;            !!!next-token;
6429              } elsif ({            next B;
6430                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6431                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6432                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6433                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6434                     }->{$token->{tag_name}}) {
6435    ## TODO: The following is wrong?
6436              !!!parse-error (type => 'unmatched end tag',
6437                              text => $token->{tag_name}, token => $token);
6438                                
6439                ## have an element in table scope            ## have an element in table scope
6440                my $i;            my $i;
6441                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6442                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6443                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6444                    $i = $_;                !!!cp ('t292');
6445                    last INSCOPE;                $i = $_;
6446                  } elsif ({                last INSCOPE;
6447                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6448                           }->{$node->[1]}) {                !!!cp ('t293');
6449                    last INSCOPE;                last INSCOPE;
6450                  }              }
6451                } # INSCOPE            } # INSCOPE
6452                unless (defined $i) {            unless (defined $i) {
6453                  ## Ignore the token              !!!cp ('t294');
6454                  !!!next-token;              ## Ignore the token
6455                  redo B;              !!!nack ('t294.1');
6456                }              !!!next-token;
6457                next B;
6458              }
6459                                
6460                ## As if </select>            ## As if </select>
6461                ## have an element in table scope            ## have an element in table scope
6462                undef $i;            undef $i;
6463                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6464                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6465                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6466                    $i = $_;                !!!cp ('t295');
6467                    last INSCOPE;                $i = $_;
6468                  } elsif ({                last INSCOPE;
6469                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6470                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
6471                    last INSCOPE;                !!!cp ('t296');
6472                  }                last INSCOPE;
6473                } # INSCOPE              }
6474                unless (defined $i) {            } # INSCOPE
6475                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
6476                  ## Ignore the </select> token              !!!cp ('t297');
6477                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
6478                  redo B;              !!!parse-error (type => 'unmatched end tag',
6479                }                              text => 'select', token => $token);
6480                ## Ignore the </select> token
6481                !!!nack ('t297.1');
6482                !!!next-token; ## TODO: ok?
6483                next B;
6484              }
6485                                
6486                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
6487              splice @{$self->{open_elements}}, $i;
6488    
6489                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6490    
6491                ## reprocess            !!!ack-later;
6492                redo B;            ## reprocess
6493              next B;
6494          } else {          } else {
6495            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
6496              !!!parse-error (type => 'in select:/',
6497                              text => $token->{tag_name}, token => $token);
6498            ## Ignore the token            ## Ignore the token
6499              !!!nack ('t299.3');
6500            !!!next-token;            !!!next-token;
6501            redo B;            next B;
6502            }
6503          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6504            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6505                    @{$self->{open_elements}} == 1) { # redundant, maybe
6506              !!!cp ('t299.1');
6507              !!!parse-error (type => 'in body:#eof', token => $token);
6508            } else {
6509              !!!cp ('t299.2');
6510          }          }
6511    
6512            ## Stop parsing.
6513            last B;
6514        } else {        } else {
6515          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6516        }        }
# Line 4175  sub _tree_construction_main ($) { Line 6524  sub _tree_construction_main ($) {
6524            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6525                        
6526            unless (length $token->{data}) {            unless (length $token->{data}) {
6527                !!!cp ('t300');
6528              !!!next-token;              !!!next-token;
6529              redo B;              next B;
6530            }            }
6531          }          }
6532                    
6533          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6534            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6535              !!!parse-error (type => 'after html:#text', token => $token);
6536    
6537            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6538            } else {
6539              !!!cp ('t302');
6540          }          }
6541                    
6542          ## "after body" insertion mode          ## "after body" insertion mode
6543          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6544    
6545          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6546          ## reprocess          ## reprocess
6547          redo B;          next B;
6548        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6549          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6550            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6551              !!!parse-error (type => 'after html',
6552                              text => $token->{tag_name}, token => $token);
6553                        
6554            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6555            } else {
6556              !!!cp ('t304');
6557          }          }
6558    
6559          ## "after body" insertion mode          ## "after body" insertion mode
6560          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6561                            text => $token->{tag_name}, token => $token);
6562    
6563          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6564            !!!ack-later;
6565          ## reprocess          ## reprocess
6566          redo B;          next B;
6567        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6568          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6569            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6570              !!!parse-error (type => 'after html:/',
6571                              text => $token->{tag_name}, token => $token);
6572                        
6573            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6574            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6575            } else {
6576              !!!cp ('t306');
6577          }          }
6578    
6579          ## "after body" insertion mode          ## "after body" insertion mode
6580          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6581            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6582              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6583                !!!parse-error (type => 'unmatched end tag',
6584                                text => 'html', token => $token);
6585              ## Ignore the token              ## Ignore the token
6586              !!!next-token;              !!!next-token;
6587              redo B;              next B;
6588            } else {            } else {
6589                !!!cp ('t308');
6590              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6591              !!!next-token;              !!!next-token;
6592              redo B;              next B;
6593            }            }
6594          } else {          } else {
6595            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6596              !!!parse-error (type => 'after body:/',
6597                              text => $token->{tag_name}, token => $token);
6598    
6599            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6600            ## reprocess            ## reprocess
6601            redo B;            next B;
6602          }          }
6603          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6604            !!!cp ('t309.2');
6605            ## Stop parsing
6606            last B;
6607        } else {        } else {
6608          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6609        }        }
# Line 4241  sub _tree_construction_main ($) { Line 6613  sub _tree_construction_main ($) {
6613            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6614                        
6615            unless (length $token->{data}) {            unless (length $token->{data}) {
6616                !!!cp ('t310');
6617              !!!next-token;              !!!next-token;
6618              redo B;              next B;
6619            }            }
6620          }          }
6621                    
6622          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6623            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6624              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6625                !!!parse-error (type => 'in frameset:#text', token => $token);
6626            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6627              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6628            } else { # "after html frameset"              !!!parse-error (type => 'after frameset:#text', token => $token);
6629              !!!parse-error (type => 'after html:#character');            } else { # "after after frameset"
6630                !!!cp ('t313');
6631              $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');  
6632            }            }
6633                        
6634            ## Ignore the token.            ## Ignore the token.
6635            if (length $token->{data}) {            if (length $token->{data}) {
6636                !!!cp ('t314');
6637              ## reprocess the rest of characters              ## reprocess the rest of characters
6638            } else {            } else {
6639                !!!cp ('t315');
6640              !!!next-token;              !!!next-token;
6641            }            }
6642            redo B;            next B;
6643          }          }
6644                    
6645          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6646        } 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...  
         }  
   
6647          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6648              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6649            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6650              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6651              !!!nack ('t318.1');
6652            !!!next-token;            !!!next-token;
6653            redo B;            next B;
6654          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6655                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6656            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6657              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6658            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6659              !!!ack ('t319.1');
6660            !!!next-token;            !!!next-token;
6661            redo B;            next B;
6662          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6663            ## NOTE: As if in body.            !!!cp ('t320');
6664            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6665            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6666              next B;
6667    
6668              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6669              ## has no parse error.
6670          } else {          } else {
6671            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6672              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6673            } else {              !!!parse-error (type => 'in frameset',
6674              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6675              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6676                !!!cp ('t322');
6677                !!!parse-error (type => 'after frameset',
6678                                text => $token->{tag_name}, token => $token);
6679              } else { # "after after frameset"
6680                !!!cp ('t322.2');
6681                !!!parse-error (type => 'after after frameset',
6682                                text => $token->{tag_name}, token => $token);
6683            }            }
6684            ## Ignore the token            ## Ignore the token
6685              !!!nack ('t322.1');
6686            !!!next-token;            !!!next-token;
6687            redo B;            next B;
6688          }          }
6689        } 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...  
         }  
   
6690          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6691              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6692            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6693                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6694              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6695                !!!parse-error (type => 'unmatched end tag',
6696                                text => $token->{tag_name}, token => $token);
6697              ## Ignore the token              ## Ignore the token
6698              !!!next-token;              !!!next-token;
6699            } else {            } else {
6700                !!!cp ('t326');
6701              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6702              !!!next-token;              !!!next-token;
6703            }            }
6704    
6705            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6706                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6707                !!!cp ('t327');
6708              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6709              } else {
6710                !!!cp ('t328');
6711            }            }
6712            redo B;            next B;
6713          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6714                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6715              !!!cp ('t329');
6716            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6717            !!!next-token;            !!!next-token;
6718            redo B;            next B;
6719          } else {          } else {
6720            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6721              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6722            } else {              !!!parse-error (type => 'in frameset:/',
6723              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6724              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6725                !!!cp ('t330.1');
6726                !!!parse-error (type => 'after frameset:/',
6727                                text => $token->{tag_name}, token => $token);
6728              } else { # "after after html"
6729                !!!cp ('t331');
6730                !!!parse-error (type => 'after after frameset:/',
6731                                text => $token->{tag_name}, token => $token);
6732            }            }
6733            ## Ignore the token            ## Ignore the token
6734            !!!next-token;            !!!next-token;
6735            redo B;            next B;
6736          }          }
6737          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6738            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6739                    @{$self->{open_elements}} == 1) { # redundant, maybe
6740              !!!cp ('t331.1');
6741              !!!parse-error (type => 'in body:#eof', token => $token);
6742            } else {
6743              !!!cp ('t331.2');
6744            }
6745            
6746            ## Stop parsing
6747            last B;
6748        } else {        } else {
6749          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6750        }        }
# Line 4354  sub _tree_construction_main ($) { Line 6757  sub _tree_construction_main ($) {
6757      ## "in body" insertion mode      ## "in body" insertion mode
6758      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6759        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6760            !!!cp ('t332');
6761          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6762          $script_start_tag->($insert);          $script_start_tag->();
6763          redo B;          next B;
6764        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6765            !!!cp ('t333');
6766          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6767          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6768          redo B;          next B;
6769        } elsif ({        } elsif ({
6770                  base => 1, link => 1,                  base => 1, link => 1,
6771                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6772            !!!cp ('t334');
6773          ## 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
6774          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6775          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6776            !!!ack ('t334.1');
6777          !!!next-token;          !!!next-token;
6778          redo B;          next B;
6779        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6780          ## 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
6781          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6782          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.
6783    
6784          unless ($self->{confident}) {          unless ($self->{confident}) {
6785            my $charset;            if ($token->{attributes}->{charset}) {
6786            if ($token->{attributes}->{charset}) { ## TODO: And if supported              !!!cp ('t335');
6787              $charset = $token->{attributes}->{charset}->{value};              ## NOTE: Whether the encoding is supported or not is handled
6788            }              ## in the {change_encoding} callback.
6789            if ($token->{attributes}->{'http-equiv'}) {              $self->{change_encoding}
6790              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6791              if ($token->{attributes}->{'http-equiv'}->{value}              
6792                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6793                    ->set_user_data (manakai_has_reference =>
6794                                         $token->{attributes}->{charset}
6795                                             ->{has_reference});
6796              } elsif ($token->{attributes}->{content}) {
6797                if ($token->{attributes}->{content}->{value}
6798                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6799                        [\x09-\x0D\x20]*=
6800                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6801                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6802                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
6803              } ## TODO: And if supported                ## NOTE: Whether the encoding is supported or not is handled
6804                  ## in the {change_encoding} callback.
6805                  $self->{change_encoding}
6806                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6807                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6808                      ->set_user_data (manakai_has_reference =>
6809                                           $token->{attributes}->{content}
6810                                                 ->{has_reference});
6811                }
6812              }
6813            } else {
6814              if ($token->{attributes}->{charset}) {
6815                !!!cp ('t337');
6816                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6817                    ->set_user_data (manakai_has_reference =>
6818                                         $token->{attributes}->{charset}
6819                                             ->{has_reference});
6820              }
6821              if ($token->{attributes}->{content}) {
6822                !!!cp ('t338');
6823                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6824                    ->set_user_data (manakai_has_reference =>
6825                                         $token->{attributes}->{content}
6826                                             ->{has_reference});
6827            }            }
           ## TODO: Change the encoding  
6828          }          }
6829    
6830            !!!ack ('t338.1');
6831          !!!next-token;          !!!next-token;
6832          redo B;          next B;
6833        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6834          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6835          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6836          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6837            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6838        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6839          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6840                                
6841          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6842              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6843              !!!cp ('t342');
6844            ## Ignore the token            ## Ignore the token
6845          } else {          } else {
6846            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6847            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6848              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6849                  !!!cp ('t343');
6850                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6851                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6852                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6853              }              }
6854            }            }
6855          }          }
6856            !!!nack ('t343.1');
6857          !!!next-token;          !!!next-token;
6858          redo B;          next B;
6859        } elsif ({        } elsif ({
6860                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6861                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6862                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6863                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6864                  pre => 1,                  pre => 1, listing => 1,
6865                    form => 1,
6866                    table => 1,
6867                    hr => 1,
6868                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6869            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6870              !!!cp ('t350');
6871              !!!parse-error (type => 'in form:form', token => $token);
6872              ## Ignore the token
6873              !!!nack ('t350.1');
6874              !!!next-token;
6875              next B;
6876            }
6877    
6878          ## has a p element in scope          ## has a p element in scope
6879          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6880            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6881              !!!back-token;              !!!cp ('t344');
6882              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6883              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6884            } elsif ({                        line => $token->{line}, column => $token->{column}};
6885                      table => 1, caption => 1, td => 1, th => 1,              next B;
6886                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6887                     }->{$_->[1]}) {              !!!cp ('t345');
6888              last INSCOPE;              last INSCOPE;
6889            }            }
6890          } # INSCOPE          } # INSCOPE
6891                        
6892          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6893          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6894              !!!nack ('t346.1');
6895            !!!next-token;            !!!next-token;
6896            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6897              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6898              unless (length $token->{data}) {              unless (length $token->{data}) {
6899                  !!!cp ('t346');
6900                !!!next-token;                !!!next-token;
6901                } else {
6902                  !!!cp ('t349');
6903              }              }
6904              } else {
6905                !!!cp ('t348');
6906            }            }
6907          } else {          } elsif ($token->{tag_name} eq 'form') {
6908              !!!cp ('t347.1');
6909              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6910    
6911              !!!nack ('t347.2');
6912            !!!next-token;            !!!next-token;
6913          }          } elsif ($token->{tag_name} eq 'table') {
6914          redo B;            !!!cp ('t382');
6915        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6916          if (defined $self->{form_element}) {            
6917            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6918            ## Ignore the token  
6919              !!!nack ('t382.1');
6920              !!!next-token;
6921            } elsif ($token->{tag_name} eq 'hr') {
6922              !!!cp ('t386');
6923              pop @{$self->{open_elements}};
6924            
6925              !!!nack ('t386.1');
6926            !!!next-token;            !!!next-token;
           redo B;  
6927          } else {          } else {
6928            ## 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];  
6929            !!!next-token;            !!!next-token;
           redo B;  
6930          }          }
6931        } elsif ($token->{tag_name} eq 'li') {          next B;
6932          ## 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') {  
6933          ## has a p element in scope          ## has a p element in scope
6934          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6935            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6936              !!!back-token;              !!!cp ('t353');
6937              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6938              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6939            } elsif ({                        line => $token->{line}, column => $token->{column}};
6940                      table => 1, caption => 1, td => 1, th => 1,              next B;
6941                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6942                     }->{$_->[1]}) {              !!!cp ('t354');
6943              last INSCOPE;              last INSCOPE;
6944            }            }
6945          } # INSCOPE          } # INSCOPE
# Line 4546  sub _tree_construction_main ($) { Line 6947  sub _tree_construction_main ($) {
6947          ## Step 1          ## Step 1
6948          my $i = -1;          my $i = -1;
6949          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6950            my $li_or_dtdd = {li => {li => 1},
6951                              dt => {dt => 1, dd => 1},
6952                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6953          LI: {          LI: {
6954            ## Step 2            ## Step 2
6955            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6956              if ($i != -1) {              if ($i != -1) {
6957                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6958                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6959                                  text => $self->{open_elements}->[-1]->[0]
6960                                      ->manakai_local_name,
6961                                  token => $token);
6962                } else {
6963                  !!!cp ('t356');
6964              }              }
6965              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6966              last LI;              last LI;
6967              } else {
6968                !!!cp ('t357');
6969            }            }
6970                        
6971            ## Step 3            ## Step 3
6972            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6973                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6974                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6975                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6976                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6977                  not ($node->[1] & DIV_EL)) {
6978                !!!cp ('t358');
6979              last LI;              last LI;
6980            }            }
6981                        
6982              !!!cp ('t359');
6983            ## Step 4            ## Step 4
6984            $i--;            $i--;
6985            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6986            redo LI;            redo LI;
6987          } # LI          } # LI
6988                        
6989          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6990            !!!nack ('t359.1');
6991          !!!next-token;          !!!next-token;
6992          redo B;          next B;
6993        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6994          ## has a p element in scope          ## has a p element in scope
6995          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6996            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6997              !!!back-token;              !!!cp ('t367');
6998              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6999              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
7000            } elsif ({                        line => $token->{line}, column => $token->{column}};
7001                      table => 1, caption => 1, td => 1, th => 1,              next B;
7002                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
7003                     }->{$_->[1]}) {              !!!cp ('t368');
7004              last INSCOPE;              last INSCOPE;
7005            }            }
7006          } # INSCOPE          } # INSCOPE
7007                        
7008          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7009                        
7010          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7011                        
7012            !!!nack ('t368.1');
7013          !!!next-token;          !!!next-token;
7014          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;  
7015        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
7016          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7017            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
7018            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
7019              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
7020                !!!parse-error (type => 'in a:a', token => $token);
7021                            
7022              !!!back-token;              !!!back-token; # <a>
7023              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
7024              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
7025                $formatting_end_tag->($token);
7026                            
7027              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
7028                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7029                    !!!cp ('t372');
7030                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
7031                  last AFE2;                  last AFE2;
7032                }                }
7033              } # AFE2              } # AFE2
7034              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
7035                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7036                    !!!cp ('t373');
7037                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
7038                  last OE;                  last OE;
7039                }                }
7040              } # OE              } # OE
7041              last AFE;              last AFE;
7042            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
7043                !!!cp ('t374');
7044              last AFE;              last AFE;
7045            }            }
7046          } # AFE          } # AFE
7047                        
7048          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7049    
7050          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7051          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7052    
7053            !!!nack ('t374.1');
7054          !!!next-token;          !!!next-token;
7055          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;  
7056        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
7057          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7058    
7059          ## has a |nobr| element in scope          ## has a |nobr| element in scope
7060          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7061            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7062            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
7063              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
7064              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
7065              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
7066              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7067            } elsif ({                        line => $token->{line}, column => $token->{column}};
7068                      table => 1, caption => 1, td => 1, th => 1,              next B;
7069                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
7070                     }->{$node->[1]}) {              !!!cp ('t377');
7071              last INSCOPE;              last INSCOPE;
7072            }            }
7073          } # INSCOPE          } # INSCOPE
7074                    
7075          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7076          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7077                    
7078            !!!nack ('t377.1');
7079          !!!next-token;          !!!next-token;
7080          redo B;          next B;
7081        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7082          ## has a button element in scope          ## has a button element in scope
7083          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7084            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7085            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7086              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
7087              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
7088              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
7089              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7090            } elsif ({                        line => $token->{line}, column => $token->{column}};
7091                      table => 1, caption => 1, td => 1, th => 1,              next B;
7092                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
7093                     }->{$node->[1]}) {              !!!cp ('t379');
7094              last INSCOPE;              last INSCOPE;
7095            }            }
7096          } # INSCOPE          } # INSCOPE
7097                        
7098          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7099                        
7100          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7101          push @$active_formatting_elements, ['#marker', ''];  
7102            ## TODO: associate with $self->{form_element} if defined
7103    
         !!!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});  
7104          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7105            
7106          !!!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;  
             
7107          !!!next-token;          !!!next-token;
7108          redo B;          next B;
7109        } elsif ({        } elsif ({
7110                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7111                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7112                  image => 1,                  noembed => 1,
7113                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7114                    noscript => 0, ## TODO: 1 if scripting is enabled
7115                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7116          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7117            !!!parse-error (type => 'image');            !!!cp ('t381');
7118            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
7119            } else {
7120              !!!cp ('t399');
7121          }          }
7122            ## NOTE: There is an "as if in body" code clone.
7123          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7124          $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;  
7125        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7126          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7127                    
7128          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7129              !!!cp ('t389');
7130            ## Ignore the token            ## Ignore the token
7131              !!!nack ('t389'); ## NOTE: Not acknowledged.
7132            !!!next-token;            !!!next-token;
7133            redo B;            next B;
7134          } else {          } else {
7135              !!!ack ('t391.1');
7136    
7137            my $at = $token->{attributes};            my $at = $token->{attributes};
7138            my $form_attrs;            my $form_attrs;
7139            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 4834  sub _tree_construction_main ($) { Line 7143  sub _tree_construction_main ($) {
7143            delete $at->{prompt};            delete $at->{prompt};
7144            my @tokens = (            my @tokens = (
7145                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7146                           attributes => $form_attrs},                           attributes => $form_attrs,
7147                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7148                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7149                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7150                            {type => START_TAG_TOKEN, tag_name => 'p',
7151                             line => $token->{line}, column => $token->{column}},
7152                            {type => START_TAG_TOKEN, tag_name => 'label',
7153                             line => $token->{line}, column => $token->{column}},
7154                         );                         );
7155            if ($prompt_attr) {            if ($prompt_attr) {
7156              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
7157                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7158                               #line => $token->{line}, column => $token->{column},
7159                              };
7160            } else {            } else {
7161                !!!cp ('t391');
7162              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7163                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7164                               #line => $token->{line}, column => $token->{column},
7165                              }; # SHOULD
7166              ## TODO: make this configurable              ## TODO: make this configurable
7167            }            }
7168            push @tokens,            push @tokens,
7169                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7170                             line => $token->{line}, column => $token->{column}},
7171                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7172                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7173                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7174                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7175                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7176            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7177                             line => $token->{line}, column => $token->{column}},
7178                            {type => END_TAG_TOKEN, tag_name => 'form',
7179                             line => $token->{line}, column => $token->{column}};
7180            !!!back-token (@tokens);            !!!back-token (@tokens);
7181            redo B;            !!!next-token;
7182              next B;
7183          }          }
7184        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7185          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7186          my $el;          my $el;
7187          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7188                    
7189          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7190          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4869  sub _tree_construction_main ($) { Line 7193  sub _tree_construction_main ($) {
7193          $insert->($el);          $insert->($el);
7194                    
7195          my $text = '';          my $text = '';
7196            !!!nack ('t392.1');
7197          !!!next-token;          !!!next-token;
7198          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7199            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
7200            unless (length $token->{data}) {            unless (length $token->{data}) {
7201                !!!cp ('t392');
7202              !!!next-token;              !!!next-token;
7203              } else {
7204                !!!cp ('t393');
7205            }            }
7206            } else {
7207              !!!cp ('t394');
7208          }          }
7209          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
7210              !!!cp ('t395');
7211            $text .= $token->{data};            $text .= $token->{data};
7212            !!!next-token;            !!!next-token;
7213          }          }
7214          if (length $text) {          if (length $text) {
7215              !!!cp ('t396');
7216            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
7217          }          }
7218                    
# Line 4888  sub _tree_construction_main ($) { Line 7220  sub _tree_construction_main ($) {
7220                    
7221          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
7222              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
7223              !!!cp ('t397');
7224            ## Ignore the token            ## Ignore the token
7225          } else {          } else {
7226            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
7227              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7228          }          }
7229          !!!next-token;          !!!next-token;
7230            next B;
7231          } elsif ($token->{tag_name} eq 'rt' or
7232                   $token->{tag_name} eq 'rp') {
7233            ## has a |ruby| element in scope
7234            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7235              my $node = $self->{open_elements}->[$_];
7236              if ($node->[1] & RUBY_EL) {
7237                !!!cp ('t398.1');
7238                ## generate implied end tags
7239                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7240                  !!!cp ('t398.2');
7241                  pop @{$self->{open_elements}};
7242                }
7243                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7244                  !!!cp ('t398.3');
7245                  !!!parse-error (type => 'not closed',
7246                                  text => $self->{open_elements}->[-1]->[0]
7247                                      ->manakai_local_name,
7248                                  token => $token);
7249                  pop @{$self->{open_elements}}
7250                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7251                }
7252                last INSCOPE;
7253              } elsif ($node->[1] & SCOPING_EL) {
7254                !!!cp ('t398.4');
7255                last INSCOPE;
7256              }
7257            } # INSCOPE
7258    
7259            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7260    
7261            !!!nack ('t398.5');
7262            !!!next-token;
7263          redo B;          redo B;
7264        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7265                  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') {  
7266          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7267    
7268            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7269    
7270            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7271    
7272            ## "adjust foreign attributes" - done in insert-element-f
7273                    
7274          !!!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);
7275                    
7276          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
7277              pop @{$self->{open_elements}};
7278              !!!ack ('t398.1');
7279            } else {
7280              !!!cp ('t398.2');
7281              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7282              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7283              ## mode, "in body" (not "in foreign content") secondary insertion
7284              ## mode, maybe.
7285            }
7286    
7287          !!!next-token;          !!!next-token;
7288          redo B;          next B;
7289        } elsif ({        } elsif ({
7290                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7291                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
7292                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
7293                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7294                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7295          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
7296            !!!parse-error (type => 'in body',
7297                            text => $token->{tag_name}, token => $token);
7298          ## Ignore the token          ## Ignore the token
7299            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7300          !!!next-token;          !!!next-token;
7301          redo B;          next B;
7302                    
7303          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7304        } else {        } else {
7305            if ($token->{tag_name} eq 'image') {
7306              !!!cp ('t384');
7307              !!!parse-error (type => 'image', token => $token);
7308              $token->{tag_name} = 'img';
7309            } else {
7310              !!!cp ('t385');
7311            }
7312    
7313            ## NOTE: There is an "as if <br>" code clone.
7314          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7315                    
7316          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7317    
7318            if ({
7319                 applet => 1, marquee => 1, object => 1,
7320                }->{$token->{tag_name}}) {
7321              !!!cp ('t380');
7322              push @$active_formatting_elements, ['#marker', ''];
7323              !!!nack ('t380.1');
7324            } elsif ({
7325                      b => 1, big => 1, em => 1, font => 1, i => 1,
7326                      s => 1, small => 1, strile => 1,
7327                      strong => 1, tt => 1, u => 1,
7328                     }->{$token->{tag_name}}) {
7329              !!!cp ('t375');
7330              push @$active_formatting_elements, $self->{open_elements}->[-1];
7331              !!!nack ('t375.1');
7332            } elsif ($token->{tag_name} eq 'input') {
7333              !!!cp ('t388');
7334              ## TODO: associate with $self->{form_element} if defined
7335              pop @{$self->{open_elements}};
7336              !!!ack ('t388.2');
7337            } elsif ({
7338                      area => 1, basefont => 1, bgsound => 1, br => 1,
7339                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7340                      #image => 1,
7341                     }->{$token->{tag_name}}) {
7342              !!!cp ('t388.1');
7343              pop @{$self->{open_elements}};
7344              !!!ack ('t388.3');
7345            } elsif ($token->{tag_name} eq 'select') {
7346              ## TODO: associate with $self->{form_element} if defined
7347            
7348              if ($self->{insertion_mode} & TABLE_IMS or
7349                  $self->{insertion_mode} & BODY_TABLE_IMS or
7350                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7351                !!!cp ('t400.1');
7352                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7353              } else {
7354                !!!cp ('t400.2');
7355                $self->{insertion_mode} = IN_SELECT_IM;
7356              }
7357              !!!nack ('t400.3');
7358            } else {
7359              !!!nack ('t402');
7360            }
7361                    
7362          !!!next-token;          !!!next-token;
7363          redo B;          next B;
7364        }        }
7365      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7366        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7367          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7368              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7369            for (@{$self->{open_elements}}) {          INSCOPE: {
7370              unless ({            for (reverse @{$self->{open_elements}}) {
7371                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7372                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7373                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7374                      }->{$_->[1]}) {                last INSCOPE;
7375                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
7376                  !!!cp ('t405.1');
7377                  last;
7378              }              }
7379            }            }
7380    
7381            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7382            !!!next-token;                            text => $token->{tag_name}, token => $token);
7383            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
7384            !!!next-token;            !!!next-token;
7385            redo B;            next B;
7386            } # INSCOPE
7387    
7388            for (@{$self->{open_elements}}) {
7389              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7390                !!!cp ('t403');
7391                !!!parse-error (type => 'not closed',
7392                                text => $_->[0]->manakai_local_name,
7393                                token => $token);
7394                last;
7395              } else {
7396                !!!cp ('t404');
7397              }
7398          }          }
7399    
7400            $self->{insertion_mode} = AFTER_BODY_IM;
7401            !!!next-token;
7402            next B;
7403        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7404          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
7405            ## up-to-date, though it has same effect as speced.
7406            if (@{$self->{open_elements}} > 1 and
7407                $self->{open_elements}->[1]->[1] & BODY_EL) {
7408            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7409            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7410              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
7411                !!!parse-error (type => 'not closed',
7412                                text => $self->{open_elements}->[1]->[0]
7413                                    ->manakai_local_name,
7414                                token => $token);
7415              } else {
7416                !!!cp ('t407');
7417            }            }
7418            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7419            ## reprocess            ## reprocess
7420            redo B;            next B;
7421          } else {          } else {
7422            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
7423              !!!parse-error (type => 'unmatched end tag',
7424                              text => $token->{tag_name}, token => $token);
7425            ## Ignore the token            ## Ignore the token
7426            !!!next-token;            !!!next-token;
7427            redo B;            next B;
7428          }          }
7429        } elsif ({        } elsif ({
7430                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7431                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7432                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7433                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7434                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7435                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7436          ## has an element in scope          ## has an element in scope
7437          my $i;          my $i;
7438          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7439            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7440            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7441              ## 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;  
             }  
7442              $i = $_;              $i = $_;
7443              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7444            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7445                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7446              last INSCOPE;              last INSCOPE;
7447            }            }
7448          } # INSCOPE          } # INSCOPE
7449            
7450          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7451            if (defined $i) {            !!!cp ('t413');
7452              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag',
7453                              text => $token->{tag_name}, token => $token);
7454              ## NOTE: Ignore the token.
7455            } else {
7456              ## Step 1. generate implied end tags
7457              while ({
7458                      ## END_TAG_OPTIONAL_EL
7459                      dd => ($token->{tag_name} ne 'dd'),
7460                      dt => ($token->{tag_name} ne 'dt'),
7461                      li => ($token->{tag_name} ne 'li'),
7462                      p => 1,
7463                      rt => 1,
7464                      rp => 1,
7465                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7466                !!!cp ('t409');
7467                pop @{$self->{open_elements}};
7468              }
7469    
7470              ## Step 2.
7471              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7472                      ne $token->{tag_name}) {
7473                !!!cp ('t412');
7474                !!!parse-error (type => 'not closed',
7475                                text => $self->{open_elements}->[-1]->[0]
7476                                    ->manakai_local_name,
7477                                token => $token);
7478            } else {            } else {
7479              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
7480            }            }
7481          }  
7482                      ## Step 3.
         if (defined $i) {  
7483            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7484          } elsif ($token->{tag_name} eq 'p') {  
7485            ## As if <p>, then reprocess the current token            ## Step 4.
7486            my $el;            $clear_up_to_marker->()
7487            !!!create-element ($el, 'p');                if {
7488            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
7489                  }->{$token->{tag_name}};
7490          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7491          !!!next-token;          !!!next-token;
7492          redo B;          next B;
7493        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7494            undef $self->{form_element};
7495    
7496          ## has an element in scope          ## has an element in scope
7497            my $i;
7498          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7499            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7500            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7501              ## generate implied end tags              !!!cp ('t418');
7502              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;  
             }  
7503              last INSCOPE;              last INSCOPE;
7504            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7505                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7506              last INSCOPE;              last INSCOPE;
7507            }            }
7508          } # INSCOPE          } # INSCOPE
7509            
7510          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7511            pop @{$self->{open_elements}};            !!!cp ('t421');
7512          } else {            !!!parse-error (type => 'unmatched end tag',
7513            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                            text => $token->{tag_name}, token => $token);
7514              ## NOTE: Ignore the token.
7515            } else {
7516              ## Step 1. generate implied end tags
7517              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7518                !!!cp ('t417');
7519                pop @{$self->{open_elements}};
7520              }
7521              
7522              ## Step 2.
7523              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7524                      ne $token->{tag_name}) {
7525                !!!cp ('t417.1');
7526                !!!parse-error (type => 'not closed',
7527                                text => $self->{open_elements}->[-1]->[0]
7528                                    ->manakai_local_name,
7529                                token => $token);
7530              } else {
7531                !!!cp ('t420');
7532              }  
7533              
7534              ## Step 3.
7535              splice @{$self->{open_elements}}, $i;
7536          }          }
7537    
         undef $self->{form_element};  
7538          !!!next-token;          !!!next-token;
7539          redo B;          next B;
7540        } elsif ({        } elsif ({
7541                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7542                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5069  sub _tree_construction_main ($) { Line 7544  sub _tree_construction_main ($) {
7544          my $i;          my $i;
7545          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7546            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7547            if ({            if ($node->[1] & HEADING_EL) {
7548                 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;  
             }  
7549              $i = $_;              $i = $_;
7550              last INSCOPE;              last INSCOPE;
7551            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7552                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7553              last INSCOPE;              last INSCOPE;
7554            }            }
7555          } # INSCOPE          } # INSCOPE
7556            
7557          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7558            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
7559              !!!parse-error (type => 'unmatched end tag',
7560                              text => $token->{tag_name}, token => $token);
7561              ## NOTE: Ignore the token.
7562            } else {
7563              ## Step 1. generate implied end tags
7564              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7565                !!!cp ('t422');
7566                pop @{$self->{open_elements}};
7567              }
7568              
7569              ## Step 2.
7570              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7571                      ne $token->{tag_name}) {
7572                !!!cp ('t425');
7573                !!!parse-error (type => 'unmatched end tag',
7574                                text => $token->{tag_name}, token => $token);
7575              } else {
7576                !!!cp ('t426');
7577              }
7578    
7579              ## Step 3.
7580              splice @{$self->{open_elements}}, $i;
7581          }          }
7582                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7583          !!!next-token;          !!!next-token;
7584          redo B;          next B;
7585          } elsif ($token->{tag_name} eq 'p') {
7586            ## has an element in scope
7587            my $i;
7588            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7589              my $node = $self->{open_elements}->[$_];
7590              if ($node->[1] & P_EL) {
7591                !!!cp ('t410.1');
7592                $i = $_;
7593                last INSCOPE;
7594              } elsif ($node->[1] & SCOPING_EL) {
7595                !!!cp ('t411.1');
7596                last INSCOPE;
7597              }
7598            } # INSCOPE
7599    
7600            if (defined $i) {
7601              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7602                      ne $token->{tag_name}) {
7603                !!!cp ('t412.1');
7604                !!!parse-error (type => 'not closed',
7605                                text => $self->{open_elements}->[-1]->[0]
7606                                    ->manakai_local_name,
7607                                token => $token);
7608              } else {
7609                !!!cp ('t414.1');
7610              }
7611    
7612              splice @{$self->{open_elements}}, $i;
7613            } else {
7614              !!!cp ('t413.1');
7615              !!!parse-error (type => 'unmatched end tag',
7616                              text => $token->{tag_name}, token => $token);
7617    
7618              !!!cp ('t415.1');
7619              ## As if <p>, then reprocess the current token
7620              my $el;
7621              !!!create-element ($el, $HTML_NS, 'p',, $token);
7622              $insert->($el);
7623              ## NOTE: Not inserted into |$self->{open_elements}|.
7624            }
7625    
7626            !!!next-token;
7627            next B;
7628        } elsif ({        } elsif ({
7629                  a => 1,                  a => 1,
7630                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7631                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7632                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7633                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7634          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7635          redo B;          $formatting_end_tag->($token);
7636            next B;
7637        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7638          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7639            !!!parse-error (type => 'unmatched end tag',
7640                            text => 'br', token => $token);
7641    
7642          ## As if <br>          ## As if <br>
7643          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7644                    
7645          my $el;          my $el;
7646          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7647          $insert->($el);          $insert->($el);
7648                    
7649          ## Ignore the token.          ## Ignore the token.
7650          !!!next-token;          !!!next-token;
7651          redo B;          next B;
7652        } elsif ({        } elsif ({
7653                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7654                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5133  sub _tree_construction_main ($) { Line 7661  sub _tree_construction_main ($) {
7661                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7662                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7663                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7664          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7665            !!!parse-error (type => 'unmatched end tag',
7666                            text => $token->{tag_name}, token => $token);
7667          ## Ignore the token          ## Ignore the token
7668          !!!next-token;          !!!next-token;
7669          redo B;          next B;
7670                    
7671          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7672                    
# Line 5147  sub _tree_construction_main ($) { Line 7677  sub _tree_construction_main ($) {
7677    
7678          ## Step 2          ## Step 2
7679          S2: {          S2: {
7680            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7681              ## Step 1              ## Step 1
7682              ## generate implied end tags              ## generate implied end tags
7683              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7684                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7685                   td => 1, th => 1, tr => 1,                ## NOTE: |<ruby><rt></ruby>|.
7686                   tbody => 1, tfoot => 1, thead => 1,                ## ISSUE: <ruby><rt></rt> will also take this code path,
7687                  }->{$self->{open_elements}->[-1]->[1]}) {                ## which seems wrong.
7688                !!!back-token;                pop @{$self->{open_elements}};
7689                $token = {type => END_TAG_TOKEN,                $node_i++;
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7690              }              }
7691                    
7692              ## Step 2              ## Step 2
7693              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7694                        ne $token->{tag_name}) {
7695                  !!!cp ('t431');
7696                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7697                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7698                                  text => $self->{open_elements}->[-1]->[0]
7699                                      ->manakai_local_name,
7700                                  token => $token);
7701                } else {
7702                  !!!cp ('t432');
7703              }              }
7704                            
7705              ## Step 3              ## Step 3
7706              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7707    
7708              !!!next-token;              !!!next-token;
7709              last S2;              last S2;
7710            } else {            } else {
7711              ## Step 3              ## Step 3
7712              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7713                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7714                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7715                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7716                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7717                  !!!parse-error (type => 'unmatched end tag',
7718                                  text => $token->{tag_name}, token => $token);
7719                ## Ignore the token                ## Ignore the token
7720                !!!next-token;                !!!next-token;
7721                last S2;                last S2;
7722              }              }
7723    
7724                !!!cp ('t434');
7725            }            }
7726                        
7727            ## Step 4            ## Step 4
# Line 5192  sub _tree_construction_main ($) { Line 7731  sub _tree_construction_main ($) {
7731            ## Step 5;            ## Step 5;
7732            redo S2;            redo S2;
7733          } # S2          } # S2
7734          redo B;          next B;
7735        }        }
7736      }      }
7737      redo B;      next B;
7738      } continue { # B
7739        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7740          ## NOTE: The code below is executed in cases where it does not have
7741          ## to be, but it it is harmless even in those cases.
7742          ## has an element in scope
7743          INSCOPE: {
7744            for (reverse 0..$#{$self->{open_elements}}) {
7745              my $node = $self->{open_elements}->[$_];
7746              if ($node->[1] & FOREIGN_EL) {
7747                last INSCOPE;
7748              } elsif ($node->[1] & SCOPING_EL) {
7749                last;
7750              }
7751            }
7752            
7753            ## NOTE: No foreign element in scope.
7754            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7755          } # INSCOPE
7756        }
7757    } # B    } # B
7758    
   ## 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  
   
7759    ## Stop parsing # MUST    ## Stop parsing # MUST
7760        
7761    ## TODO: script stuffs    ## TODO: script stuffs
7762  } # _tree_construct_main  } # _tree_construct_main
7763    
7764  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7765    my $class = shift;    my $class = shift;
7766    my $node = shift;    my $node = shift;
7767    my $s = \$_[0];    #my $s = \$_[0];
7768    my $onerror = $_[1];    my $onerror = $_[1];
7769      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7770    
7771      ## ISSUE: Should {confident} be true?
7772    
7773    my $nt = $node->node_type;    my $nt = $node->node_type;
7774    if ($nt == 9) {    if ($nt == 9) {
# Line 5229  sub set_inner_html ($$$) { Line 7785  sub set_inner_html ($$$) {
7785      }      }
7786    
7787      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7788      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7789    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7790      ## TODO: If non-html element      ## TODO: If non-html element
7791    
7792      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7793    
7794    ## TODO: Support for $get_wrapper
7795    
7796      ## Step 1 # MUST      ## Step 1 # MUST
7797      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7798      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5242  sub set_inner_html ($$$) { Line 7800  sub set_inner_html ($$$) {
7800      my $p = $class->new;      my $p = $class->new;
7801      $p->{document} = $doc;      $p->{document} = $doc;
7802    
7803      ## Step 9 # MUST      ## Step 8 # MUST
7804      my $i = 0;      my $i = 0;
7805      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7806      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7807      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
7808        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7809        $input = $get_wrapper->($input);
7810        $p->{set_nc} = sub {
7811        my $self = shift;        my $self = shift;
7812    
7813        pop @{$self->{prev_input_character}};        my $char = '';
7814        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        if (defined $self->{next_nc}) {
7815            $char = $self->{next_nc};
7816            delete $self->{next_nc};
7817            $self->{nc} = ord $char;
7818          } else {
7819            $self->{char_buffer} = '';
7820            $self->{char_buffer_pos} = 0;
7821            
7822            my $count = $input->manakai_read_until
7823                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7824                 $self->{char_buffer_pos});
7825            if ($count) {
7826              $self->{line_prev} = $self->{line};
7827              $self->{column_prev} = $self->{column};
7828              $self->{column}++;
7829              $self->{nc}
7830                  = ord substr ($self->{char_buffer},
7831                                $self->{char_buffer_pos}++, 1);
7832              return;
7833            }
7834            
7835            if ($input->read ($char, 1)) {
7836              $self->{nc} = ord $char;
7837            } else {
7838              $self->{nc} = -1;
7839              return;
7840            }
7841          }
7842    
7843        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7844        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7845        $column++;  
7846          if ($self->{nc} == 0x000A) { # LF
7847        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7848          $line++;          $p->{column} = 0;
7849          $column = 0;          !!!cp ('i1');
7850        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{nc} == 0x000D) { # CR
7851          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7852          $self->{next_input_character} = 0x000A; # LF # MUST          my $next = '';
7853          $line++;          if ($input->read ($next, 1) and $next ne "\x0A") {
7854          $column = 0;            $self->{next_nc} = $next;
7855        } elsif ($self->{next_input_character} > 0x10FFFF) {          }
7856          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0x000A; # LF # MUST
7857        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $p->{line}++;
7858            $p->{column} = 0;
7859            !!!cp ('i2');
7860          } elsif ($self->{nc} == 0x0000) { # NULL
7861            !!!cp ('i4');
7862          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7863          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7864        }        }
7865      };      };
7866      $p->{prev_input_character} = [-1, -1, -1];  
7867      $p->{next_input_character} = -1;      $p->{read_until} = sub {
7868              #my ($scalar, $specials_range, $offset) = @_;
7869          return 0 if defined $p->{next_nc};
7870    
7871          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7872          my $offset = $_[2] || 0;
7873          
7874          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7875            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7876            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7877              substr ($_[0], $offset)
7878                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7879              my $count = $+[0] - $-[0];
7880              if ($count) {
7881                $p->{column} += $count;
7882                $p->{char_buffer_pos} += $count;
7883                $p->{line_prev} = $p->{line};
7884                $p->{column_prev} = $p->{column} - 1;
7885                $p->{prev_char} = [-1, -1, -1];
7886                $p->{nc} = -1;
7887              }
7888              return $count;
7889            } else {
7890              return 0;
7891            }
7892          } else {
7893            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7894            if ($count) {
7895              $p->{column} += $count;
7896              $p->{column_prev} += $count;
7897              $p->{prev_char} = [-1, -1, -1];
7898              $p->{nc} = -1;
7899            }
7900            return $count;
7901          }
7902        }; # $p->{read_until}
7903    
7904      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7905        my (%opt) = @_;        my (%opt) = @_;
7906        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7907          my $column = $opt{column};
7908          if (defined $opt{token} and defined $opt{token}->{line}) {
7909            $line = $opt{token}->{line};
7910            $column = $opt{token}->{column};
7911          }
7912          warn "Parse error ($opt{type}) at line $line column $column\n";
7913      };      };
7914      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7915        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7916      };      };
7917            
7918        my $char_onerror = sub {
7919          my (undef, $type, %opt) = @_;
7920          $ponerror->(layer => 'encode',
7921                      line => $p->{line}, column => $p->{column} + 1,
7922                      %opt, type => $type);
7923        }; # $char_onerror
7924        $input->onerror ($char_onerror);
7925    
7926      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7927      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7928    
7929      ## Step 2      ## Step 2
7930      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7931      $p->{content_model} = {      $p->{content_model} = {
7932        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7933        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5303  sub set_inner_html ($$$) { Line 7944  sub set_inner_html ($$$) {
7944          unless defined $p->{content_model};          unless defined $p->{content_model};
7945          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7946    
7947      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7948          ## TODO: Foreign element OK?
7949    
7950      ## Step 4      ## Step 3
7951      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7952        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7953    
7954      ## Step 5 # MUST      ## Step 4 # MUST
7955      $doc->append_child ($root);      $doc->append_child ($root);
7956    
7957      ## Step 6 # MUST      ## Step 5 # MUST
7958      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7959    
7960      undef $p->{head_element};      undef $p->{head_element};
7961    
7962      ## Step 7 # MUST      ## Step 6 # MUST
7963      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7964    
7965      ## Step 8 # MUST      ## Step 7 # MUST
7966      my $anode = $node;      my $anode = $node;
7967      AN: while (defined $anode) {      AN: while (defined $anode) {
7968        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7969          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7970          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7971            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7972                !!!cp ('i5');
7973              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7974              last AN;              last AN;
7975            }            }
# Line 5335  sub set_inner_html ($$$) { Line 7978  sub set_inner_html ($$$) {
7978        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7979      } # AN      } # AN
7980            
7981      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7982      {      {
7983        my $self = $p;        my $self = $p;
7984        !!!next-token;        !!!next-token;
7985      }      }
7986      $p->_tree_construction_main;      $p->_tree_construction_main;
7987    
7988      ## Step 11 # MUST      ## Step 10 # MUST
7989      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7990      for (@cn) {      for (@cn) {
7991        $node->remove_child ($_);        $node->remove_child ($_);
7992      }      }
7993      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7994    
7995      ## Step 12 # MUST      ## Step 11 # MUST
7996      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7997      for (@cn) {      for (@cn) {
7998        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5359  sub set_inner_html ($$$) { Line 8001  sub set_inner_html ($$$) {
8001      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8002    
8003      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8004    
8005        delete $p->{parse_error}; # delete loop
8006    } else {    } else {
8007      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";
8008    }    }
# Line 5366  sub set_inner_html ($$$) { Line 8010  sub set_inner_html ($$$) {
8010    
8011  } # tree construction stage  } # tree construction stage
8012    
8013  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
8014    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  
8015    
8016  1;  1;
8017  # $Date$  # $Date$

Legend:
Removed from v.1.61  
changed lines
  Added in v.1.183

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24