/[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.162 by wakaba, Thu Sep 11 09:12:27 2008 UTC
# Line 1  Line 1 
1  package Whatpm::HTML;  package Whatpm::HTML;
2  use strict;  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4    use Error qw(:try);
5    
6  ## ISSUE:  ## ISSUE:
7  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  require IO::Handle;
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
13  ## is not yet clear.  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  ## "{U+FEFF}..." in GB18030?  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17  my $permitted_slash_tag_name = {  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    base => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    link => 1,  
20    meta => 1,  sub A_EL () { 0b1 }
21    hr => 1,  sub ADDRESS_EL () { 0b10 }
22    br => 1,  sub BODY_EL () { 0b100 }
23    img=> 1,  sub BUTTON_EL () { 0b1000 }
24    embed => 1,  sub CAPTION_EL () { 0b10000 }
25    param => 1,  sub DD_EL () { 0b100000 }
26    area => 1,  sub DIV_EL () { 0b1000000 }
27    col => 1,  sub DT_EL () { 0b10000000 }
28    input => 1,  sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
350  my $special_category = {  sub parse_byte_string ($$$$;$) {
351    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
352    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
353    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
356    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
357    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$$) {
358    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $self = ref $_[0] ? shift : shift->new;
360    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    my $charset_name = shift;
361  };    my $byte_stream = $_[0];
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
362    
363  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
364    my $self = shift->new;      my (%opt) = @_;
365    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
366      };
367      $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      };
372    
373      ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403        }
404    
405        ## Step 2
406        my $byte_buffer = '';
407        for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419          $self->{confident} = 1;
420          last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435        }
436    
437        ## Step 4
438        ## TODO: <meta charset>
439    
440        ## Step 5
441        ## TODO: from history
442    
443        ## Step 6
444        require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559      }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574      my @args = @_; shift @args; # $s
575      my $return;
576      try {
577        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578      } catch Whatpm::HTML::RestartParser with {
579        ## NOTE: Invoked after {change_encoding}.
580    
581        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599        $self->{confident} = 1;
600    
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605      };
606      return $return;
607    } # parse_byte_stream
608    
609    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
611    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
612    ## because the core part of our HTML parser expects a string of character,
613    ## not a string of bytes or code units or anything which might contain a BOM.
614    ## Therefore, any parser interface that accepts a string of bytes,
615    ## such as |parse_byte_string| in this module, must ensure that it does
616    ## strip the BOM and never strip any ZWNBSP.
617    
618    sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      require utf8;
622      my $s = ref $_[0] ? $_[0] : \($_[0]);
623      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631    sub parse_char_stream ($$$;$) {
632      my $self = ref $_[0] ? shift : shift->new;
633      my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635      @{$self->{document}->child_nodes} = ();
636    
637    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
638    
639      $self->{confident} = 1 unless exists $self->{confident};
640      $self->{document}->input_encoding ($self->{input_encoding})
641          if defined $self->{input_encoding};
642    
643    my $i = 0;    my $i = 0;
644    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
645    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
646    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
647      my $self = shift;      my $self = shift;
648    
649      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char;
653      $self->{next_input_character} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
654      $column++;        $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662        ($self->{line_prev}, $self->{column_prev})
663            = ($self->{line}, $self->{column});
664        $self->{column}++;
665            
666      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667        $line++;        !!!cp ('j1');
668        $column = 0;        $self->{line}++;
669      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
670        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
671        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
672        $line++;        my $next = $input->getc;
673        $column = 0;        if (defined $next and $next ne "\x0A") {
674      } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_next_char} = $next;
675        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
676      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
677          $self->{line}++;
678          $self->{column} = 0;
679        } elsif ($self->{next_char} > 0x10FFFF) {
680          !!!cp ('j3');
681          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
682        } elsif ($self->{next_char} == 0x0000) { # NULL
683          !!!cp ('j4');
684        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
685        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
686        } elsif ($self->{next_char} <= 0x0008 or
687                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
688                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
689                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
690                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
691                 {
692                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
693                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
694                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
695                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
696                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
697                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
698                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
699                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
700                  0x10FFFE => 1, 0x10FFFF => 1,
701                 }->{$self->{next_char}}) {
702          !!!cp ('j5');
703          if ($self->{next_char} < 0x10000) {
704            !!!parse-error (type => 'control char',
705                            text => (sprintf 'U+%04X', $self->{next_char}));
706          } else {
707            !!!parse-error (type => 'control char',
708                            text => (sprintf 'U-%08X', $self->{next_char}));
709          }
710      }      }
711    };    };
712    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
713    $self->{next_input_character} = -1;    $self->{next_char} = -1;
714    
715    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
716      my (%opt) = @_;      my (%opt) = @_;
717      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
718        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
719        warn "Parse error ($opt{type}) at line $line column $column\n";
720    };    };
721    $self->{parse_error} = sub {    $self->{parse_error} = sub {
722      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
723    };    };
724    
725    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 727  sub parse_string ($$$;$) {
727    $self->_construct_tree;    $self->_construct_tree;
728    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
729    
730      delete $self->{parse_error}; # remove loop
731    
732    return $self->{document};    return $self->{document};
733  } # parse_string  } # parse_char_stream
734    
735  sub new ($) {  sub new ($) {
736    my $class = shift;    my $class = shift;
737    my $self = bless {}, $class;    my $self = bless {
738    $self->{set_next_input_character} = sub {      level => {must => 'm',
739      $self->{next_input_character} = -1;                should => 's',
740                  warn => 'w',
741                  info => 'i',
742                  uncertain => 'u'},
743      }, $class;
744      $self->{set_next_char} = sub {
745        $self->{next_char} = -1;
746    };    };
747    $self->{parse_error} = sub {    $self->{parse_error} = sub {
748      #      #
749    };    };
750      $self->{change_encoding} = sub {
751        # if ($_[0] is a supported encoding) {
752        #   run "change the encoding" algorithm;
753        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
754        # }
755      };
756    $self->{application_cache_selection} = sub {    $self->{application_cache_selection} = sub {
757      #      #
758    };    };
# Line 195  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 801  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
801  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
802  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
803  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
804    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
805    sub SELF_CLOSING_START_TAG_STATE () { 34 }
806    sub CDATA_BLOCK_STATE () { 35 }
807    
808  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
809  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 211  sub TABLE_IMS ()      { 0b1000000 } Line 820  sub TABLE_IMS ()      { 0b1000000 }
820  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
821  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
822  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
823    sub SELECT_IMS ()     { 0b10000000000 }
824    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
825        ## NOTE: "in foreign content" insertion mode is special; it is combined
826        ## with the secondary insertion mode.  In this parser, they are stored
827        ## together in the bit-or'ed form.
828    
829    ## NOTE: "initial" and "before html" insertion modes have no constants.
830    
831    ## NOTE: "after after body" insertion mode.
832  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
833    
834    ## NOTE: "after after frameset" insertion mode.
835  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
836    
837  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
838  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
839  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 227  sub IN_TABLE_IM () { TABLE_IMS } Line 847  sub IN_TABLE_IM () { TABLE_IMS }
847  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
848  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
849  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
850  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
851    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
852  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
853    
854  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 240  sub _initialize_tokenizer ($) { Line 861  sub _initialize_tokenizer ($) {
861    undef $self->{current_attribute};    undef $self->{current_attribute};
862    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
863    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
864      delete $self->{self_closing};
865    $self->{char} = [];    $self->{char} = [];
866    # $self->{next_input_character}    # $self->{next_char}
867    !!!next-input-character;    !!!next-input-character;
868    $self->{token} = [];    $self->{token} = [];
869    # $self->{escape}    # $self->{escape}
# Line 254  sub _initialize_tokenizer ($) { Line 876  sub _initialize_tokenizer ($) {
876  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
877  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
878  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
879  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
880  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
881    ##        ->{name}
882    ##        ->{value}
883    ##        ->{has_reference} == 1 or 0
884  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
885    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
886    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
887    ##     while the token is pushed back to the stack.
888    
889  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
890    
# Line 283  sub _initialize_tokenizer ($) { Line 911  sub _initialize_tokenizer ($) {
911    
912  sub _get_next_token ($) {  sub _get_next_token ($) {
913    my $self = shift;    my $self = shift;
914    
915      if ($self->{self_closing}) {
916        !!!parse-error (type => 'nestc', token => $self->{current_token});
917        ## NOTE: The |self_closing| flag is only set by start tag token.
918        ## In addition, when a start tag token is emitted, it is always set to
919        ## |current_token|.
920        delete $self->{self_closing};
921      }
922    
923    if (@{$self->{token}}) {    if (@{$self->{token}}) {
924        $self->{self_closing} = $self->{token}->[0]->{self_closing};
925      return shift @{$self->{token}};      return shift @{$self->{token}};
926    }    }
927    
928    A: {    A: {
929      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
930        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
931          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
932                not $self->{escape}) {
933              !!!cp (1);
934            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
935            !!!next-input-character;            !!!next-input-character;
936            redo A;            redo A;
937          } else {          } else {
938              !!!cp (2);
939            #            #
940          }          }
941        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
942          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
943            unless ($self->{escape}) {            unless ($self->{escape}) {
944              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
945                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
946                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
947                  !!!cp (3);
948                $self->{escape} = 1;                $self->{escape} = 1;
949                } else {
950                  !!!cp (4);
951              }              }
952              } else {
953                !!!cp (5);
954            }            }
955          }          }
956                    
957          #          #
958        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
959          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
960              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
961               not $self->{escape})) {               not $self->{escape})) {
962              !!!cp (6);
963            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
964            !!!next-input-character;            !!!next-input-character;
965            redo A;            redo A;
966          } else {          } else {
967              !!!cp (7);
968            #            #
969          }          }
970        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
971          if ($self->{escape} and          if ($self->{escape} and
972              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
973            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
974                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
975                !!!cp (8);
976              delete $self->{escape};              delete $self->{escape};
977              } else {
978                !!!cp (9);
979            }            }
980            } else {
981              !!!cp (10);
982          }          }
983                    
984          #          #
985        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
986          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
987            !!!emit ({type => END_OF_FILE_TOKEN,
988                      line => $self->{line}, column => $self->{column}});
989          last A; ## TODO: ok?          last A; ## TODO: ok?
990          } else {
991            !!!cp (12);
992        }        }
993        # Anything else        # Anything else
994        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
995                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
996                       line => $self->{line}, column => $self->{column},
997                      };
998        ## Stay in the data state        ## Stay in the data state
999        !!!next-input-character;        !!!next-input-character;
1000    
# Line 344  sub _get_next_token ($) { Line 1003  sub _get_next_token ($) {
1003        redo A;        redo A;
1004      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
1005        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
1006    
1007          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
1008                
1009        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
1010    
1011        $self->{state} = DATA_STATE;        $self->{state} = DATA_STATE;
1012        # next-input-character is already done        # next-input-character is already done
1013    
1014        unless (defined $token) {        unless (defined $token) {
1015          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
1016            !!!emit ({type => CHARACTER_TOKEN, data => '&',
1017                      line => $l, column => $c,
1018                     });
1019        } else {        } else {
1020            !!!cp (14);
1021          !!!emit ($token);          !!!emit ($token);
1022        }        }
1023    
1024        redo A;        redo A;
1025      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1026        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1027          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
1028              !!!cp (15);
1029            !!!next-input-character;            !!!next-input-character;
1030            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1031            redo A;            redo A;
1032          } else {          } else {
1033              !!!cp (16);
1034            ## reconsume            ## reconsume
1035            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1036    
1037            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1038                        line => $self->{line_prev},
1039                        column => $self->{column_prev},
1040                       });
1041    
1042            redo A;            redo A;
1043          }          }
1044        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1045          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1046              !!!cp (17);
1047            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1048            !!!next-input-character;            !!!next-input-character;
1049            redo A;            redo A;
1050          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1051              !!!cp (18);
1052            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1053            !!!next-input-character;            !!!next-input-character;
1054            redo A;            redo A;
1055          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1056                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1057              !!!cp (19);
1058            $self->{current_token}            $self->{current_token}
1059              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1060                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1061                   line => $self->{line_prev},
1062                   column => $self->{column_prev}};
1063            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1064            !!!next-input-character;            !!!next-input-character;
1065            redo A;            redo A;
1066          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1067                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1068              !!!cp (20);
1069            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1070                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
1071                                        line => $self->{line_prev},
1072                                        column => $self->{column_prev}};
1073            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1074            !!!next-input-character;            !!!next-input-character;
1075            redo A;            redo A;
1076          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1077            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1078              !!!parse-error (type => 'empty start tag',
1079                              line => $self->{line_prev},
1080                              column => $self->{column_prev});
1081            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1082            !!!next-input-character;            !!!next-input-character;
1083    
1084            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1085                        line => $self->{line_prev},
1086                        column => $self->{column_prev},
1087                       });
1088    
1089            redo A;            redo A;
1090          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1091            !!!parse-error (type => 'pio');            !!!cp (22);
1092              !!!parse-error (type => 'pio',
1093                              line => $self->{line_prev},
1094                              column => $self->{column_prev});
1095            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1096            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1097                                        line => $self->{line_prev},
1098                                        column => $self->{column_prev},
1099                                       };
1100              ## $self->{next_char} is intentionally left as is
1101            redo A;            redo A;
1102          } else {          } else {
1103            !!!parse-error (type => 'bare stago');            !!!cp (23);
1104              !!!parse-error (type => 'bare stago',
1105                              line => $self->{line_prev},
1106                              column => $self->{column_prev});
1107            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1108            ## reconsume            ## reconsume
1109    
1110            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1111                        line => $self->{line_prev},
1112                        column => $self->{column_prev},
1113                       });
1114    
1115            redo A;            redo A;
1116          }          }
# Line 421  sub _get_next_token ($) { Line 1118  sub _get_next_token ($) {
1118          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1119        }        }
1120      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1121          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1122        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1123          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1124    
1125            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1126            my @next_char;            my @next_char;
1127            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {
1128              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1129              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1130              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1131              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1132                  !!!cp (24);
1133                !!!next-input-character;                !!!next-input-character;
1134                next TAGNAME;                next TAGNAME;
1135              } else {              } else {
1136                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1137                  $self->{next_char} = shift @next_char; # reconsume
1138                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1139                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1140    
1141                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1142                            line => $l, column => $c,
1143                           });
1144        
1145                redo A;                redo A;
1146              }              }
1147            }            }
1148            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1149                
1150            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1151                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1152                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1153                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1154                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1155                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1156                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1157                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1158              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1159                $self->{next_char} = shift @next_char; # reconsume
1160              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1161              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1162              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1163                          line => $l, column => $c,
1164                         });
1165              redo A;              redo A;
1166            } else {            } else {
1167              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1168                $self->{next_char} = shift @next_char;
1169              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1170              # and consume...              # and consume...
1171            }            }
1172          } else {          } else {
1173            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1174              !!!cp (28);
1175            # next-input-character is already done            # next-input-character is already done
1176            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1177            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1178                        line => $l, column => $c,
1179                       });
1180            redo A;            redo A;
1181          }          }
1182        }        }
1183                
1184        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1185            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1186          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1187                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1188                = {type => END_TAG_TOKEN,
1189                   tag_name => chr ($self->{next_char} + 0x0020),
1190                   line => $l, column => $c};
1191          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1192          !!!next-input-character;          !!!next-input-character;
1193          redo A;          redo A;
1194        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
1195                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1196            !!!cp (30);
1197          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1198                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
1199                                      line => $l, column => $c};
1200          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1201          !!!next-input-character;          !!!next-input-character;
1202          redo A;          redo A;
1203        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1204          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1205            !!!parse-error (type => 'empty end tag',
1206                            line => $self->{line_prev}, ## "<" in "</>"
1207                            column => $self->{column_prev} - 1);
1208          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1209          !!!next-input-character;          !!!next-input-character;
1210          redo A;          redo A;
1211        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1212            !!!cp (32);
1213          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1214          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1215          # reconsume          # reconsume
1216    
1217          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1218                      line => $l, column => $c,
1219                     });
1220    
1221          redo A;          redo A;
1222        } else {        } else {
1223            !!!cp (33);
1224          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1225          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1226          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1227                                      line => $self->{line_prev}, # "<" of "</"
1228                                      column => $self->{column_prev} - 1,
1229                                     };
1230            ## $self->{next_char} is intentionally left as is
1231          redo A;          redo A;
1232        }        }
1233      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1234        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1235            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1236            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1237            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1238            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1239            !!!cp (34);
1240          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1241          !!!next-input-character;          !!!next-input-character;
1242          redo A;          redo A;
1243        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1244          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1245            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
1246            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1247          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1248            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1249            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1250              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1251            }            #  !!! cp (36);
1252              #  !!! parse-error (type => 'end tag attribute');
1253              #} else {
1254                !!!cp (37);
1255              #}
1256          } else {          } else {
1257            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1258          }          }
# Line 532  sub _get_next_token ($) { Line 1262  sub _get_next_token ($) {
1262          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1263    
1264          redo A;          redo A;
1265        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1266                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1267          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1268            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1269            # start tag or end tag            # start tag or end tag
1270          ## Stay in this state          ## Stay in this state
1271          !!!next-input-character;          !!!next-input-character;
1272          redo A;          redo A;
1273        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1274          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1275          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1276            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1277            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1278          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1279            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1280            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1281              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1282            }            #  !!! cp (40);
1283              #  !!! parse-error (type => 'end tag attribute');
1284              #} else {
1285                !!!cp (41);
1286              #}
1287          } else {          } else {
1288            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1289          }          }
# Line 559  sub _get_next_token ($) { Line 1293  sub _get_next_token ($) {
1293          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1294    
1295          redo A;          redo A;
1296        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1297            !!!cp (42);
1298            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1299          !!!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  
1300          redo A;          redo A;
1301        } else {        } else {
1302          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1303            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1304            # start tag or end tag            # start tag or end tag
1305          ## Stay in the state          ## Stay in the state
1306          !!!next-input-character;          !!!next-input-character;
1307          redo A;          redo A;
1308        }        }
1309      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1310        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1311            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1312            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1313            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1314            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1315            !!!cp (45);
1316          ## Stay in the state          ## Stay in the state
1317          !!!next-input-character;          !!!next-input-character;
1318          redo A;          redo A;
1319        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1320          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1321            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1322            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1323          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1324            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1325            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1326                !!!cp (47);
1327              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1328              } else {
1329                !!!cp (48);
1330            }            }
1331          } else {          } else {
1332            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 607  sub _get_next_token ($) { Line 1337  sub _get_next_token ($) {
1337          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1338    
1339          redo A;          redo A;
1340        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1341                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1342          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1343                                value => ''};          $self->{current_attribute}
1344                = {name => chr ($self->{next_char} + 0x0020),
1345                   value => '',
1346                   line => $self->{line}, column => $self->{column}};
1347          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1348          !!!next-input-character;          !!!next-input-character;
1349          redo A;          redo A;
1350        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1351            !!!cp (50);
1352            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1353          !!!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  
1354          redo A;          redo A;
1355        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1356          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1357          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1358            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1359            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1360          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1361            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1362            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1363                !!!cp (53);
1364              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1365              } else {
1366                !!!cp (54);
1367            }            }
1368          } else {          } else {
1369            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 648  sub _get_next_token ($) { Line 1375  sub _get_next_token ($) {
1375    
1376          redo A;          redo A;
1377        } else {        } else {
1378          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1379                                value => ''};               0x0022 => 1, # "
1380                 0x0027 => 1, # '
1381                 0x003D => 1, # =
1382                }->{$self->{next_char}}) {
1383              !!!cp (55);
1384              !!!parse-error (type => 'bad attribute name');
1385            } else {
1386              !!!cp (56);
1387            }
1388            $self->{current_attribute}
1389                = {name => chr ($self->{next_char}),
1390                   value => '',
1391                   line => $self->{line}, column => $self->{column}};
1392          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1393          !!!next-input-character;          !!!next-input-character;
1394          redo A;          redo A;
# Line 658  sub _get_next_token ($) { Line 1397  sub _get_next_token ($) {
1397        my $before_leave = sub {        my $before_leave = sub {
1398          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1399              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1400            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1401              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1402            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1403          } else {          } else {
1404              !!!cp (58);
1405            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1406              = $self->{current_attribute};              = $self->{current_attribute};
1407          }          }
1408        }; # $before_leave        }; # $before_leave
1409    
1410        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1411            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1412            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1413            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1414            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1415            !!!cp (59);
1416          $before_leave->();          $before_leave->();
1417          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1418          !!!next-input-character;          !!!next-input-character;
1419          redo A;          redo A;
1420        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1421            !!!cp (60);
1422          $before_leave->();          $before_leave->();
1423          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1424          !!!next-input-character;          !!!next-input-character;
1425          redo A;          redo A;
1426        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1427          $before_leave->();          $before_leave->();
1428          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1429            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1430            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1431          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1432              !!!cp (62);
1433            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1434            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1435              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 700  sub _get_next_token ($) { Line 1443  sub _get_next_token ($) {
1443          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1444    
1445          redo A;          redo A;
1446        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1447                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1448          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1449            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1450          ## Stay in the state          ## Stay in the state
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1454            !!!cp (64);
1455          $before_leave->();          $before_leave->();
1456            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1457          !!!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  
1458          redo A;          redo A;
1459        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1460          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1461          $before_leave->();          $before_leave->();
1462          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1463            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1464            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1465          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1466            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1467            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1468                !!!cp (67);
1469              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1470              } else {
1471                ## NOTE: This state should never be reached.
1472                !!!cp (68);
1473            }            }
1474          } else {          } else {
1475            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 742  sub _get_next_token ($) { Line 1481  sub _get_next_token ($) {
1481    
1482          redo A;          redo A;
1483        } else {        } else {
1484          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1485                $self->{next_char} == 0x0027) { # '
1486              !!!cp (69);
1487              !!!parse-error (type => 'bad attribute name');
1488            } else {
1489              !!!cp (70);
1490            }
1491            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1492          ## Stay in the state          ## Stay in the state
1493          !!!next-input-character;          !!!next-input-character;
1494          redo A;          redo A;
1495        }        }
1496      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1497        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1498            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1499            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1500            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1501            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1502            !!!cp (71);
1503          ## Stay in the state          ## Stay in the state
1504          !!!next-input-character;          !!!next-input-character;
1505          redo A;          redo A;
1506        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1507            !!!cp (72);
1508          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1509          !!!next-input-character;          !!!next-input-character;
1510          redo A;          redo A;
1511        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1512          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1513            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1514            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1515          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1516            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1517            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1518                !!!cp (74);
1519              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1520              } else {
1521                ## NOTE: This state should never be reached.
1522                !!!cp (75);
1523            }            }
1524          } else {          } else {
1525            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 779  sub _get_next_token ($) { Line 1530  sub _get_next_token ($) {
1530          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1531    
1532          redo A;          redo A;
1533        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1534                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1535          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1536                                value => ''};          $self->{current_attribute}
1537                = {name => chr ($self->{next_char} + 0x0020),
1538                   value => '',
1539                   line => $self->{line}, column => $self->{column}};
1540          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1541          !!!next-input-character;          !!!next-input-character;
1542          redo A;          redo A;
1543        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1544            !!!cp (77);
1545            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1546          !!!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  
1547          redo A;          redo A;
1548        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1549          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1550          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1551            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1552            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1553          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1554            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1555            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1556                !!!cp (80);
1557              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1558              } else {
1559                ## NOTE: This state should never be reached.
1560                !!!cp (81);
1561            }            }
1562          } else {          } else {
1563            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 821  sub _get_next_token ($) { Line 1569  sub _get_next_token ($) {
1569    
1570          redo A;          redo A;
1571        } else {        } else {
1572          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1573                                value => ''};              $self->{next_char} == 0x0027) { # '
1574              !!!cp (78);
1575              !!!parse-error (type => 'bad attribute name');
1576            } else {
1577              !!!cp (82);
1578            }
1579            $self->{current_attribute}
1580                = {name => chr ($self->{next_char}),
1581                   value => '',
1582                   line => $self->{line}, column => $self->{column}};
1583          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1584          !!!next-input-character;          !!!next-input-character;
1585          redo A;                  redo A;        
1586        }        }
1587      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1588        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1589            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1590            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1591            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1592            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1593            !!!cp (83);
1594          ## Stay in the state          ## Stay in the state
1595          !!!next-input-character;          !!!next-input-character;
1596          redo A;          redo A;
1597        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1598            !!!cp (84);
1599          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1600          !!!next-input-character;          !!!next-input-character;
1601          redo A;          redo A;
1602        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1603            !!!cp (85);
1604          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1605          ## reconsume          ## reconsume
1606          redo A;          redo A;
1607        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1608            !!!cp (86);
1609          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1610          !!!next-input-character;          !!!next-input-character;
1611          redo A;          redo A;
1612        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1613            !!!parse-error (type => 'empty unquoted attribute value');
1614          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1615            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1616            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1617          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1618            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1619            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1620                !!!cp (88);
1621              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1622              } else {
1623                ## NOTE: This state should never be reached.
1624                !!!cp (89);
1625            }            }
1626          } else {          } else {
1627            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 867  sub _get_next_token ($) { Line 1632  sub _get_next_token ($) {
1632          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1633    
1634          redo A;          redo A;
1635        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1636          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1637          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1638            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1639            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1640          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1641            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1642            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1643                !!!cp (91);
1644              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1645              } else {
1646                ## NOTE: This state should never be reached.
1647                !!!cp (92);
1648            }            }
1649          } else {          } else {
1650            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 888  sub _get_next_token ($) { Line 1656  sub _get_next_token ($) {
1656    
1657          redo A;          redo A;
1658        } else {        } else {
1659          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1660              !!!cp (93);
1661              !!!parse-error (type => 'bad attribute value');
1662            } else {
1663              !!!cp (94);
1664            }
1665            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1666          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1667          !!!next-input-character;          !!!next-input-character;
1668          redo A;          redo A;
1669        }        }
1670      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1671        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1672          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1673            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1674          !!!next-input-character;          !!!next-input-character;
1675          redo A;          redo A;
1676        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1677            !!!cp (96);
1678          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1679          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1680          !!!next-input-character;          !!!next-input-character;
1681          redo A;          redo A;
1682        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1683          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1684          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1685            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1686            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1687          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1688            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1689            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1690                !!!cp (98);
1691              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1692              } else {
1693                ## NOTE: This state should never be reached.
1694                !!!cp (99);
1695            }            }
1696          } else {          } else {
1697            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 924  sub _get_next_token ($) { Line 1703  sub _get_next_token ($) {
1703    
1704          redo A;          redo A;
1705        } else {        } else {
1706          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1707            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1708          ## Stay in the state          ## Stay in the state
1709          !!!next-input-character;          !!!next-input-character;
1710          redo A;          redo A;
1711        }        }
1712      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1713        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1714          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1715            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1716          !!!next-input-character;          !!!next-input-character;
1717          redo A;          redo A;
1718        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1719            !!!cp (102);
1720          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1721          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1722          !!!next-input-character;          !!!next-input-character;
1723          redo A;          redo A;
1724        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1725          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1726          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1727            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1728            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1729          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1730            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1731            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1732                !!!cp (104);
1733              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1734              } else {
1735                ## NOTE: This state should never be reached.
1736                !!!cp (105);
1737            }            }
1738          } else {          } else {
1739            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 960  sub _get_next_token ($) { Line 1745  sub _get_next_token ($) {
1745    
1746          redo A;          redo A;
1747        } else {        } else {
1748          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1749            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1750          ## Stay in the state          ## Stay in the state
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
1753        }        }
1754      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1755        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1756            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1757            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1758            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1759            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1760            !!!cp (107);
1761          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1762          !!!next-input-character;          !!!next-input-character;
1763          redo A;          redo A;
1764        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1765            !!!cp (108);
1766          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1767          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1768          !!!next-input-character;          !!!next-input-character;
1769          redo A;          redo A;
1770        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1771          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1772            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1773            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1774          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1775            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1776            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1777                !!!cp (110);
1778              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1779              } else {
1780                ## NOTE: This state should never be reached.
1781                !!!cp (111);
1782            }            }
1783          } else {          } else {
1784            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 998  sub _get_next_token ($) { Line 1789  sub _get_next_token ($) {
1789          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1790    
1791          redo A;          redo A;
1792        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1793          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1794          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1795            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1796            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1797          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1798            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1799            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1800                !!!cp (113);
1801              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1802              } else {
1803                ## NOTE: This state should never be reached.
1804                !!!cp (114);
1805            }            }
1806          } else {          } else {
1807            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1019  sub _get_next_token ($) { Line 1813  sub _get_next_token ($) {
1813    
1814          redo A;          redo A;
1815        } else {        } else {
1816          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1817                 0x0022 => 1, # "
1818                 0x0027 => 1, # '
1819                 0x003D => 1, # =
1820                }->{$self->{next_char}}) {
1821              !!!cp (115);
1822              !!!parse-error (type => 'bad attribute value');
1823            } else {
1824              !!!cp (116);
1825            }
1826            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1827          ## Stay in the state          ## Stay in the state
1828          !!!next-input-character;          !!!next-input-character;
1829          redo A;          redo A;
1830        }        }
1831      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1832        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1833              (1,
1834               $self->{last_attribute_value_state}
1835                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1836               $self->{last_attribute_value_state}
1837                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1838               -1);
1839    
1840        unless (defined $token) {        unless (defined $token) {
1841            !!!cp (117);
1842          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1843        } else {        } else {
1844            !!!cp (118);
1845          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1846            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1847          ## ISSUE: spec says "append the returned character token to the current attribute's value"          ## ISSUE: spec says "append the returned character token to the current attribute's value"
1848        }        }
1849    
1850        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1851        # next-input-character is already done        # next-input-character is already done
1852        redo A;        redo A;
1853        } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1854          if ($self->{next_char} == 0x0009 or # HT
1855              $self->{next_char} == 0x000A or # LF
1856              $self->{next_char} == 0x000B or # VT
1857              $self->{next_char} == 0x000C or # FF
1858              $self->{next_char} == 0x0020) { # SP
1859            !!!cp (118);
1860            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1861            !!!next-input-character;
1862            redo A;
1863          } elsif ($self->{next_char} == 0x003E) { # >
1864            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1865              !!!cp (119);
1866              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1867            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1868              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1869              if ($self->{current_token}->{attributes}) {
1870                !!!cp (120);
1871                !!!parse-error (type => 'end tag attribute');
1872              } else {
1873                ## NOTE: This state should never be reached.
1874                !!!cp (121);
1875              }
1876            } else {
1877              die "$0: $self->{current_token}->{type}: Unknown token type";
1878            }
1879            $self->{state} = DATA_STATE;
1880            !!!next-input-character;
1881    
1882            !!!emit ($self->{current_token}); # start tag or end tag
1883    
1884            redo A;
1885          } elsif ($self->{next_char} == 0x002F) { # /
1886            !!!cp (122);
1887            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1888            !!!next-input-character;
1889            redo A;
1890          } elsif ($self->{next_char} == -1) {
1891            !!!parse-error (type => 'unclosed tag');
1892            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1893              !!!cp (122.3);
1894              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1895            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1896              if ($self->{current_token}->{attributes}) {
1897                !!!cp (122.1);
1898                !!!parse-error (type => 'end tag attribute');
1899              } else {
1900                ## NOTE: This state should never be reached.
1901                !!!cp (122.2);
1902              }
1903            } else {
1904              die "$0: $self->{current_token}->{type}: Unknown token type";
1905            }
1906            $self->{state} = DATA_STATE;
1907            ## Reconsume.
1908            !!!emit ($self->{current_token}); # start tag or end tag
1909            redo A;
1910          } else {
1911            !!!cp ('124.1');
1912            !!!parse-error (type => 'no space between attributes');
1913            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1914            ## reconsume
1915            redo A;
1916          }
1917        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1918          if ($self->{next_char} == 0x003E) { # >
1919            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1920              !!!cp ('124.2');
1921              !!!parse-error (type => 'nestc', token => $self->{current_token});
1922              ## TODO: Different type than slash in start tag
1923              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1924              if ($self->{current_token}->{attributes}) {
1925                !!!cp ('124.4');
1926                !!!parse-error (type => 'end tag attribute');
1927              } else {
1928                !!!cp ('124.5');
1929              }
1930              ## TODO: Test |<title></title/>|
1931            } else {
1932              !!!cp ('124.3');
1933              $self->{self_closing} = 1;
1934            }
1935    
1936            $self->{state} = DATA_STATE;
1937            !!!next-input-character;
1938    
1939            !!!emit ($self->{current_token}); # start tag or end tag
1940    
1941            redo A;
1942          } elsif ($self->{next_char} == -1) {
1943            !!!parse-error (type => 'unclosed tag');
1944            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1945              !!!cp (124.7);
1946              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1947            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1948              if ($self->{current_token}->{attributes}) {
1949                !!!cp (124.5);
1950                !!!parse-error (type => 'end tag attribute');
1951              } else {
1952                ## NOTE: This state should never be reached.
1953                !!!cp (124.6);
1954              }
1955            } else {
1956              die "$0: $self->{current_token}->{type}: Unknown token type";
1957            }
1958            $self->{state} = DATA_STATE;
1959            ## Reconsume.
1960            !!!emit ($self->{current_token}); # start tag or end tag
1961            redo A;
1962          } else {
1963            !!!cp ('124.4');
1964            !!!parse-error (type => 'nestc');
1965            ## TODO: This error type is wrong.
1966            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1967            ## Reconsume.
1968            redo A;
1969          }
1970      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1971        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1972                
1973        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1974          #my $token = {type => COMMENT_TOKEN, data => ''};
1975    
1976        BC: {        BC: {
1977          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1978              !!!cp (124);
1979            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1980            !!!next-input-character;            !!!next-input-character;
1981    
1982            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1983    
1984            redo A;            redo A;
1985          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1986              !!!cp (125);
1987            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1988            ## reconsume            ## reconsume
1989    
1990            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1991    
1992            redo A;            redo A;
1993          } else {          } else {
1994            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1995              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1996            !!!next-input-character;            !!!next-input-character;
1997            redo BC;            redo BC;
1998          }          }
1999        } # BC        } # BC
2000    
2001          die "$0: _get_next_token: unexpected case [BC]";
2002      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2003        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2004    
2005          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
2006    
2007        my @next_char;        my @next_char;
2008        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
2009                
2010        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2011          !!!next-input-character;          !!!next-input-character;
2012          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
2013          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
2014            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
2015              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2016                                        line => $l, column => $c,
2017                                       };
2018            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
2019            !!!next-input-character;            !!!next-input-character;
2020            redo A;            redo A;
2021            } else {
2022              !!!cp (128);
2023          }          }
2024        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2025                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2026          !!!next-input-character;          !!!next-input-character;
2027          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
2028          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
2029              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
2030            !!!next-input-character;            !!!next-input-character;
2031            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
2032            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
2033                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
2034              !!!next-input-character;              !!!next-input-character;
2035              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
2036              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2037                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2038                !!!next-input-character;                !!!next-input-character;
2039                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
2040                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
2041                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
2042                  !!!next-input-character;                  !!!next-input-character;
2043                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
2044                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
2045                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
2046                    !!!next-input-character;                    !!!next-input-character;
2047                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
2048                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
2049                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
2050                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
2051                        ## TODO: What a stupid code this is!
2052                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
2053                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2054                                                  quirks => 1,
2055                                                  line => $l, column => $c,
2056                                                 };
2057                        !!!next-input-character;
2058                        redo A;
2059                      } else {
2060                        !!!cp (130);
2061                      }
2062                    } else {
2063                      !!!cp (131);
2064                    }
2065                  } else {
2066                    !!!cp (132);
2067                  }
2068                } else {
2069                  !!!cp (133);
2070                }
2071              } else {
2072                !!!cp (134);
2073              }
2074            } else {
2075              !!!cp (135);
2076            }
2077          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2078                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2079                   $self->{next_char} == 0x005B) { # [
2080            !!!next-input-character;
2081            push @next_char, $self->{next_char};
2082            if ($self->{next_char} == 0x0043) { # C
2083              !!!next-input-character;
2084              push @next_char, $self->{next_char};
2085              if ($self->{next_char} == 0x0044) { # D
2086                !!!next-input-character;
2087                push @next_char, $self->{next_char};
2088                if ($self->{next_char} == 0x0041) { # A
2089                  !!!next-input-character;
2090                  push @next_char, $self->{next_char};
2091                  if ($self->{next_char} == 0x0054) { # T
2092                    !!!next-input-character;
2093                    push @next_char, $self->{next_char};
2094                    if ($self->{next_char} == 0x0041) { # A
2095                      !!!next-input-character;
2096                      push @next_char, $self->{next_char};
2097                      if ($self->{next_char} == 0x005B) { # [
2098                        !!!cp (135.1);
2099                        $self->{state} = CDATA_BLOCK_STATE;
2100                      !!!next-input-character;                      !!!next-input-character;
2101                      redo A;                      redo A;
2102                      } else {
2103                        !!!cp (135.2);
2104                    }                    }
2105                    } else {
2106                      !!!cp (135.3);
2107                  }                  }
2108                  } else {
2109                    !!!cp (135.4);                
2110                }                }
2111                } else {
2112                  !!!cp (135.5);
2113              }              }
2114              } else {
2115                !!!cp (135.6);
2116            }            }
2117            } else {
2118              !!!cp (135.7);
2119          }          }
2120          } else {
2121            !!!cp (136);
2122        }        }
2123    
2124        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2125        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2126        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2127        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2128          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2129                                    line => $l, column => $c,
2130                                   };
2131        redo A;        redo A;
2132                
2133        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2134        ## 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?        ## 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?
2135      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2136        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2137            !!!cp (137);
2138          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2139          !!!next-input-character;          !!!next-input-character;
2140          redo A;          redo A;
2141        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2142            !!!cp (138);
2143          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2144          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2145          !!!next-input-character;          !!!next-input-character;
# Line 1137  sub _get_next_token ($) { Line 2147  sub _get_next_token ($) {
2147          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2148    
2149          redo A;          redo A;
2150        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2151            !!!cp (139);
2152          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2153          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2154          ## reconsume          ## reconsume
# Line 1146  sub _get_next_token ($) { Line 2157  sub _get_next_token ($) {
2157    
2158          redo A;          redo A;
2159        } else {        } else {
2160            !!!cp (140);
2161          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2162              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2163          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2164          !!!next-input-character;          !!!next-input-character;
2165          redo A;          redo A;
2166        }        }
2167      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2168        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2169            !!!cp (141);
2170          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2171          !!!next-input-character;          !!!next-input-character;
2172          redo A;          redo A;
2173        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2174            !!!cp (142);
2175          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2176          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2177          !!!next-input-character;          !!!next-input-character;
# Line 1165  sub _get_next_token ($) { Line 2179  sub _get_next_token ($) {
2179          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2180    
2181          redo A;          redo A;
2182        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2183            !!!cp (143);
2184          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2185          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2186          ## reconsume          ## reconsume
# Line 1174  sub _get_next_token ($) { Line 2189  sub _get_next_token ($) {
2189    
2190          redo A;          redo A;
2191        } else {        } else {
2192            !!!cp (144);
2193          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2194              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2195          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2196          !!!next-input-character;          !!!next-input-character;
2197          redo A;          redo A;
2198        }        }
2199      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2200        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2201            !!!cp (145);
2202          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2203          !!!next-input-character;          !!!next-input-character;
2204          redo A;          redo A;
2205        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2206            !!!cp (146);
2207          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2208          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2209          ## reconsume          ## reconsume
# Line 1194  sub _get_next_token ($) { Line 2212  sub _get_next_token ($) {
2212    
2213          redo A;          redo A;
2214        } else {        } else {
2215          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2216            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2217          ## Stay in the state          ## Stay in the state
2218          !!!next-input-character;          !!!next-input-character;
2219          redo A;          redo A;
2220        }        }
2221      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2222        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2223            !!!cp (148);
2224          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2225          !!!next-input-character;          !!!next-input-character;
2226          redo A;          redo A;
2227        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2228            !!!cp (149);
2229          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2230          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2231          ## reconsume          ## reconsume
# Line 1213  sub _get_next_token ($) { Line 2234  sub _get_next_token ($) {
2234    
2235          redo A;          redo A;
2236        } else {        } else {
2237          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2238            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2239          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2240          !!!next-input-character;          !!!next-input-character;
2241          redo A;          redo A;
2242        }        }
2243      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2244        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2245            !!!cp (151);
2246          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2247          !!!next-input-character;          !!!next-input-character;
2248    
2249          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2250    
2251          redo A;          redo A;
2252        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2253          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2254            !!!parse-error (type => 'dash in comment',
2255                            line => $self->{line_prev},
2256                            column => $self->{column_prev});
2257          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2258          ## Stay in the state          ## Stay in the state
2259          !!!next-input-character;          !!!next-input-character;
2260          redo A;          redo A;
2261        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2262            !!!cp (153);
2263          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2264          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2265          ## reconsume          ## reconsume
# Line 1241  sub _get_next_token ($) { Line 2268  sub _get_next_token ($) {
2268    
2269          redo A;          redo A;
2270        } else {        } else {
2271          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2272          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2273                            line => $self->{line_prev},
2274                            column => $self->{column_prev});
2275            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2276          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2277          !!!next-input-character;          !!!next-input-character;
2278          redo A;          redo A;
2279        }        }
2280      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2281        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2282            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2283            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2284            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2285            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2286            !!!cp (155);
2287          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2288          !!!next-input-character;          !!!next-input-character;
2289          redo A;          redo A;
2290        } else {        } else {
2291            !!!cp (156);
2292          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2293          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2294          ## reconsume          ## reconsume
2295          redo A;          redo A;
2296        }        }
2297      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2298        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2299            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2300            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2301            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2302            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2303            !!!cp (157);
2304          ## Stay in the state          ## Stay in the state
2305          !!!next-input-character;          !!!next-input-character;
2306          redo A;          redo A;
2307        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2308            !!!cp (158);
2309          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2310          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2311          !!!next-input-character;          !!!next-input-character;
2312    
2313          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2314    
2315          redo A;          redo A;
2316        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2317            !!!cp (159);
2318          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2319          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2320          ## reconsume          ## reconsume
2321    
2322          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2323    
2324          redo A;          redo A;
2325        } else {        } else {
2326          $self->{current_token}          !!!cp (160);
2327              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
2328                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2329  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2330          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2331          !!!next-input-character;          !!!next-input-character;
# Line 1299  sub _get_next_token ($) { Line 2333  sub _get_next_token ($) {
2333        }        }
2334      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2335  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2336        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2337            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2338            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2339            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2340            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2341            !!!cp (161);
2342          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2343          !!!next-input-character;          !!!next-input-character;
2344          redo A;          redo A;
2345        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2346            !!!cp (162);
2347          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2348          !!!next-input-character;          !!!next-input-character;
2349    
2350          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2351    
2352          redo A;          redo A;
2353        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2354            !!!cp (163);
2355          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2356          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2357          ## reconsume          ## reconsume
2358    
2359          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2360          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2361    
2362          redo A;          redo A;
2363        } else {        } else {
2364            !!!cp (164);
2365          $self->{current_token}->{name}          $self->{current_token}->{name}
2366            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2367          ## Stay in the state          ## Stay in the state
2368          !!!next-input-character;          !!!next-input-character;
2369          redo A;          redo A;
2370        }        }
2371      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2372        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2373            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2374            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2375            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2376            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2377            !!!cp (165);
2378          ## Stay in the state          ## Stay in the state
2379          !!!next-input-character;          !!!next-input-character;
2380          redo A;          redo A;
2381        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2382            !!!cp (166);
2383          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2384          !!!next-input-character;          !!!next-input-character;
2385    
2386          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2387    
2388          redo A;          redo A;
2389        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2390            !!!cp (167);
2391          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2392          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2393          ## reconsume          ## reconsume
2394    
2395          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2396          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2397    
2398          redo A;          redo A;
2399        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2400                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2401          !!!next-input-character;          !!!next-input-character;
2402          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2403              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2404            !!!next-input-character;            !!!next-input-character;
2405            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2406                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2407              !!!next-input-character;              !!!next-input-character;
2408              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2409                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2410                !!!next-input-character;                !!!next-input-character;
2411                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2412                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2413                  !!!next-input-character;                  !!!next-input-character;
2414                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2415                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2416                      !!!cp (168);
2417                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2418                    !!!next-input-character;                    !!!next-input-character;
2419                    redo A;                    redo A;
2420                    } else {
2421                      !!!cp (169);
2422                  }                  }
2423                  } else {
2424                    !!!cp (170);
2425                }                }
2426                } else {
2427                  !!!cp (171);
2428              }              }
2429              } else {
2430                !!!cp (172);
2431            }            }
2432            } else {
2433              !!!cp (173);
2434          }          }
2435    
2436          #          #
2437        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2438                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2439          !!!next-input-character;          !!!next-input-character;
2440          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2441              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2442            !!!next-input-character;            !!!next-input-character;
2443            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2444                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2445              !!!next-input-character;              !!!next-input-character;
2446              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2447                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2448                !!!next-input-character;                !!!next-input-character;
2449                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2450                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2451                  !!!next-input-character;                  !!!next-input-character;
2452                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2453                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2454                      !!!cp (174);
2455                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2456                    !!!next-input-character;                    !!!next-input-character;
2457                    redo A;                    redo A;
2458                    } else {
2459                      !!!cp (175);
2460                  }                  }
2461                  } else {
2462                    !!!cp (176);
2463                }                }
2464                } else {
2465                  !!!cp (177);
2466              }              }
2467              } else {
2468                !!!cp (178);
2469            }            }
2470            } else {
2471              !!!cp (179);
2472          }          }
2473    
2474          #          #
2475        } else {        } else {
2476            !!!cp (180);
2477          !!!next-input-character;          !!!next-input-character;
2478          #          #
2479        }        }
2480    
2481        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2482          $self->{current_token}->{quirks} = 1;
2483    
2484        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
2485        # next-input-character is already done        # next-input-character is already done
2486        redo A;        redo A;
# Line 1422  sub _get_next_token ($) { Line 2488  sub _get_next_token ($) {
2488        if ({        if ({
2489              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2490              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2491            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2492            !!!cp (181);
2493          ## Stay in the state          ## Stay in the state
2494          !!!next-input-character;          !!!next-input-character;
2495          redo A;          redo A;
2496        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2497            !!!cp (182);
2498          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2499          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2500          !!!next-input-character;          !!!next-input-character;
2501          redo A;          redo A;
2502        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2503            !!!cp (183);
2504          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2505          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2506          !!!next-input-character;          !!!next-input-character;
2507          redo A;          redo A;
2508        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2509            !!!cp (184);
2510          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2511    
2512          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2513          !!!next-input-character;          !!!next-input-character;
2514    
2515          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2516          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2517    
2518          redo A;          redo A;
2519        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2520            !!!cp (185);
2521          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2522    
2523          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2524          ## reconsume          ## reconsume
2525    
2526          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2527          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2528    
2529          redo A;          redo A;
2530        } else {        } else {
2531            !!!cp (186);
2532          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2533            $self->{current_token}->{quirks} = 1;
2534    
2535          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2536          !!!next-input-character;          !!!next-input-character;
2537          redo A;          redo A;
2538        }        }
2539      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2540        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2541            !!!cp (187);
2542          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2543          !!!next-input-character;          !!!next-input-character;
2544          redo A;          redo A;
2545        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2546            !!!cp (188);
2547            !!!parse-error (type => 'unclosed PUBLIC literal');
2548    
2549            $self->{state} = DATA_STATE;
2550            !!!next-input-character;
2551    
2552            $self->{current_token}->{quirks} = 1;
2553            !!!emit ($self->{current_token}); # DOCTYPE
2554    
2555            redo A;
2556          } elsif ($self->{next_char} == -1) {
2557            !!!cp (189);
2558          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2559    
2560          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2561          ## reconsume          ## reconsume
2562    
2563          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2564          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2565    
2566          redo A;          redo A;
2567        } else {        } else {
2568            !!!cp (190);
2569          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2570              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2571          ## Stay in the state          ## Stay in the state
2572          !!!next-input-character;          !!!next-input-character;
2573          redo A;          redo A;
2574        }        }
2575      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2576        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2577            !!!cp (191);
2578          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2579          !!!next-input-character;          !!!next-input-character;
2580          redo A;          redo A;
2581        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2582            !!!cp (192);
2583            !!!parse-error (type => 'unclosed PUBLIC literal');
2584    
2585            $self->{state} = DATA_STATE;
2586            !!!next-input-character;
2587    
2588            $self->{current_token}->{quirks} = 1;
2589            !!!emit ($self->{current_token}); # DOCTYPE
2590    
2591            redo A;
2592          } elsif ($self->{next_char} == -1) {
2593            !!!cp (193);
2594          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2595    
2596          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2597          ## reconsume          ## reconsume
2598    
2599          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2600          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2601    
2602          redo A;          redo A;
2603        } else {        } else {
2604            !!!cp (194);
2605          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2606              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2607          ## Stay in the state          ## Stay in the state
2608          !!!next-input-character;          !!!next-input-character;
2609          redo A;          redo A;
# Line 1510  sub _get_next_token ($) { Line 2612  sub _get_next_token ($) {
2612        if ({        if ({
2613              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2614              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2615            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2616            !!!cp (195);
2617          ## Stay in the state          ## Stay in the state
2618          !!!next-input-character;          !!!next-input-character;
2619          redo A;          redo A;
2620        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2621            !!!cp (196);
2622          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2623          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2624          !!!next-input-character;          !!!next-input-character;
2625          redo A;          redo A;
2626        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2627            !!!cp (197);
2628          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2629          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2630          !!!next-input-character;          !!!next-input-character;
2631          redo A;          redo A;
2632        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2633            !!!cp (198);
2634          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2635          !!!next-input-character;          !!!next-input-character;
2636    
2637          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2638    
2639          redo A;          redo A;
2640        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2641            !!!cp (199);
2642          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2643    
2644          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2645          ## reconsume          ## reconsume
2646    
2647          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2648          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2649    
2650          redo A;          redo A;
2651        } else {        } else {
2652            !!!cp (200);
2653          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2654            $self->{current_token}->{quirks} = 1;
2655    
2656          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2657          !!!next-input-character;          !!!next-input-character;
2658          redo A;          redo A;
# Line 1551  sub _get_next_token ($) { Line 2661  sub _get_next_token ($) {
2661        if ({        if ({
2662              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2663              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2664            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2665            !!!cp (201);
2666          ## Stay in the state          ## Stay in the state
2667          !!!next-input-character;          !!!next-input-character;
2668          redo A;          redo A;
2669        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2670            !!!cp (202);
2671          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2672          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2673          !!!next-input-character;          !!!next-input-character;
2674          redo A;          redo A;
2675        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2676            !!!cp (203);
2677          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2678          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2679          !!!next-input-character;          !!!next-input-character;
2680          redo A;          redo A;
2681        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2682            !!!cp (204);
2683          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2684          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2685          !!!next-input-character;          !!!next-input-character;
2686    
2687          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2688          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2689    
2690          redo A;          redo A;
2691        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2692            !!!cp (205);
2693          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2694    
2695          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2696          ## reconsume          ## reconsume
2697    
2698          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2699          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2700    
2701          redo A;          redo A;
2702        } else {        } else {
2703            !!!cp (206);
2704          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2705            $self->{current_token}->{quirks} = 1;
2706    
2707          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2708          !!!next-input-character;          !!!next-input-character;
2709          redo A;          redo A;
2710        }        }
2711      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2712        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2713            !!!cp (207);
2714          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2715          !!!next-input-character;          !!!next-input-character;
2716          redo A;          redo A;
2717        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2718            !!!cp (208);
2719            !!!parse-error (type => 'unclosed SYSTEM literal');
2720    
2721            $self->{state} = DATA_STATE;
2722            !!!next-input-character;
2723    
2724            $self->{current_token}->{quirks} = 1;
2725            !!!emit ($self->{current_token}); # DOCTYPE
2726    
2727            redo A;
2728          } elsif ($self->{next_char} == -1) {
2729            !!!cp (209);
2730          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2731    
2732          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2733          ## reconsume          ## reconsume
2734    
2735          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2736          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2737    
2738          redo A;          redo A;
2739        } else {        } else {
2740            !!!cp (210);
2741          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2742              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2743          ## Stay in the state          ## Stay in the state
2744          !!!next-input-character;          !!!next-input-character;
2745          redo A;          redo A;
2746        }        }
2747      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2748        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2749            !!!cp (211);
2750          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2751          !!!next-input-character;          !!!next-input-character;
2752          redo A;          redo A;
2753        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2754            !!!cp (212);
2755            !!!parse-error (type => 'unclosed SYSTEM literal');
2756    
2757            $self->{state} = DATA_STATE;
2758            !!!next-input-character;
2759    
2760            $self->{current_token}->{quirks} = 1;
2761            !!!emit ($self->{current_token}); # DOCTYPE
2762    
2763            redo A;
2764          } elsif ($self->{next_char} == -1) {
2765            !!!cp (213);
2766          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2767    
2768          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2769          ## reconsume          ## reconsume
2770    
2771          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2772          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2773    
2774          redo A;          redo A;
2775        } else {        } else {
2776            !!!cp (214);
2777          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2778              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2779          ## Stay in the state          ## Stay in the state
2780          !!!next-input-character;          !!!next-input-character;
2781          redo A;          redo A;
# Line 1638  sub _get_next_token ($) { Line 2784  sub _get_next_token ($) {
2784        if ({        if ({
2785              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2786              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2787            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2788            !!!cp (215);
2789          ## Stay in the state          ## Stay in the state
2790          !!!next-input-character;          !!!next-input-character;
2791          redo A;          redo A;
2792        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2793            !!!cp (216);
2794          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2795          !!!next-input-character;          !!!next-input-character;
2796    
2797          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2798    
2799          redo A;          redo A;
2800        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2801            !!!cp (217);
2802          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2803          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2804          ## reconsume          ## reconsume
2805    
2806          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2807          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2808    
2809          redo A;          redo A;
2810        } else {        } else {
2811            !!!cp (218);
2812          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2813            #$self->{current_token}->{quirks} = 1;
2814    
2815          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2816          !!!next-input-character;          !!!next-input-character;
2817          redo A;          redo A;
2818        }        }
2819      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2820        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2821            !!!cp (219);
2822          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2823          !!!next-input-character;          !!!next-input-character;
2824    
         delete $self->{current_token}->{correct};  
2825          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2826    
2827          redo A;          redo A;
2828        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2829            !!!cp (220);
2830          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2831          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2832          ## reconsume          ## reconsume
2833    
         delete $self->{current_token}->{correct};  
2834          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2835    
2836          redo A;          redo A;
2837        } else {        } else {
2838            !!!cp (221);
2839          ## Stay in the state          ## Stay in the state
2840          !!!next-input-character;          !!!next-input-character;
2841          redo A;          redo A;
2842        }        }
2843        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2844          my $s = '';
2845          
2846          my ($l, $c) = ($self->{line}, $self->{column});
2847    
2848          CS: while ($self->{next_char} != -1) {
2849            if ($self->{next_char} == 0x005D) { # ]
2850              !!!next-input-character;
2851              if ($self->{next_char} == 0x005D) { # ]
2852                !!!next-input-character;
2853                MDC: {
2854                  if ($self->{next_char} == 0x003E) { # >
2855                    !!!cp (221.1);
2856                    !!!next-input-character;
2857                    last CS;
2858                  } elsif ($self->{next_char} == 0x005D) { # ]
2859                    !!!cp (221.2);
2860                    $s .= ']';
2861                    !!!next-input-character;
2862                    redo MDC;
2863                  } else {
2864                    !!!cp (221.3);
2865                    $s .= ']]';
2866                    #
2867                  }
2868                } # MDC
2869              } else {
2870                !!!cp (221.4);
2871                $s .= ']';
2872                #
2873              }
2874            } else {
2875              !!!cp (221.5);
2876              #
2877            }
2878            $s .= chr $self->{next_char};
2879            !!!next-input-character;
2880          } # CS
2881    
2882          $self->{state} = DATA_STATE;
2883          ## next-input-character done or EOF, which is reconsumed.
2884    
2885          if (length $s) {
2886            !!!cp (221.6);
2887            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2888                      line => $l, column => $c});
2889          } else {
2890            !!!cp (221.7);
2891          }
2892    
2893          redo A;
2894    
2895          ## ISSUE: "text tokens" in spec.
2896          ## TODO: Streaming support
2897      } else {      } else {
2898        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2899      }      }
# Line 1696  sub _get_next_token ($) { Line 2902  sub _get_next_token ($) {
2902    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2903  } # _get_next_token  } # _get_next_token
2904    
2905  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2906    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2907    
2908      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2909    
2910    if ({    if ({
2911         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2912         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2913        }->{$self->{next_input_character}}) {         $additional => 1,
2914          }->{$self->{next_char}}) {
2915        !!!cp (1001);
2916      ## Don't consume      ## Don't consume
2917      ## No error      ## No error
2918      return undef;      return undef;
2919    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2920      !!!next-input-character;      !!!next-input-character;
2921      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2922          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2923        my $code;        my $code;
2924        X: {        X: {
2925          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2926          !!!next-input-character;          !!!next-input-character;
2927          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2928              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2929              !!!cp (1002);
2930            $code ||= 0;            $code ||= 0;
2931            $code *= 0x10;            $code *= 0x10;
2932            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2933            redo X;            redo X;
2934          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2935                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2936              !!!cp (1003);
2937            $code ||= 0;            $code ||= 0;
2938            $code *= 0x10;            $code *= 0x10;
2939            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2940            redo X;            redo X;
2941          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2942                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2943              !!!cp (1004);
2944            $code ||= 0;            $code ||= 0;
2945            $code *= 0x10;            $code *= 0x10;
2946            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2947            redo X;            redo X;
2948          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2949            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2950            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2951            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2952              $self->{next_char} = 0x0023; # #
2953            return undef;            return undef;
2954          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2955              !!!cp (1006);
2956            !!!next-input-character;            !!!next-input-character;
2957          } else {          } else {
2958            !!!parse-error (type => 'no refc');            !!!cp (1007);
2959              !!!parse-error (type => 'no refc', line => $l, column => $c);
2960          }          }
2961    
2962          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2963            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2964              !!!parse-error (type => 'invalid character reference',
2965                              text => (sprintf 'U+%04X', $code),
2966                              line => $l, column => $c);
2967            $code = 0xFFFD;            $code = 0xFFFD;
2968          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2969            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2970              !!!parse-error (type => 'invalid character reference',
2971                              text => (sprintf 'U-%08X', $code),
2972                              line => $l, column => $c);
2973            $code = 0xFFFD;            $code = 0xFFFD;
2974          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2975            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2976              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2977            $code = 0x000A;            $code = 0x000A;
2978          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2979            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2980              !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2981            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2982          }          }
2983    
2984          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2985                    has_reference => 1,
2986                    line => $l, column => $c,
2987                   };
2988        } # X        } # X
2989      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2990               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2991        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2992        !!!next-input-character;        !!!next-input-character;
2993                
2994        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2995                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2996            !!!cp (1012);
2997          $code *= 10;          $code *= 10;
2998          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2999                    
3000          !!!next-input-character;          !!!next-input-character;
3001        }        }
3002    
3003        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
3004            !!!cp (1013);
3005          !!!next-input-character;          !!!next-input-character;
3006        } else {        } else {
3007          !!!parse-error (type => 'no refc');          !!!cp (1014);
3008            !!!parse-error (type => 'no refc', line => $l, column => $c);
3009        }        }
3010    
3011        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3012          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
3013            !!!parse-error (type => 'invalid character reference',
3014                            text => (sprintf 'U+%04X', $code),
3015                            line => $l, column => $c);
3016          $code = 0xFFFD;          $code = 0xFFFD;
3017        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3018          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
3019            !!!parse-error (type => 'invalid character reference',
3020                            text => (sprintf 'U-%08X', $code),
3021                            line => $l, column => $c);
3022          $code = 0xFFFD;          $code = 0xFFFD;
3023        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3024          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
3025            !!!parse-error (type => 'CR character reference',
3026                            line => $l, column => $c);
3027          $code = 0x000A;          $code = 0x000A;
3028        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3029          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
3030            !!!parse-error (type => 'C1 character reference',
3031                            text => (sprintf 'U+%04X', $code),
3032                            line => $l, column => $c);
3033          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3034        }        }
3035                
3036        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3037                  line => $l, column => $c,
3038                 };
3039      } else {      } else {
3040        !!!parse-error (type => 'bare nero');        !!!cp (1019);
3041        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3042        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
3043          $self->{next_char} = 0x0023; # #
3044        return undef;        return undef;
3045      }      }
3046    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
3047              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
3048             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
3049              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
3050      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
3051      !!!next-input-character;      !!!next-input-character;
3052    
3053      my $value = $entity_name;      my $value = $entity_name;
# Line 1811  sub _tokenize_attempt_to_consume_an_enti Line 3055  sub _tokenize_attempt_to_consume_an_enti
3055      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3056      our $EntityChar;      our $EntityChar;
3057    
3058      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3059             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3060             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
3061               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
3062              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
3063               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
3064              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
3065               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
3066              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
3067        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
3068        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
3069          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
3070              !!!cp (1020);
3071            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3072            $match = 1;            $match = 1;
3073            !!!next-input-character;            !!!next-input-character;
3074            last;            last;
3075          } else {          } else {
3076              !!!cp (1021);
3077            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3078            $match = -1;            $match = -1;
3079            !!!next-input-character;            !!!next-input-character;
3080          }          }
3081        } else {        } else {
3082          $value .= chr $self->{next_input_character};          !!!cp (1022);
3083            $value .= chr $self->{next_char};
3084          $match *= 2;          $match *= 2;
3085          !!!next-input-character;          !!!next-input-character;
3086        }        }
3087      }      }
3088            
3089      if ($match > 0) {      if ($match > 0) {
3090        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
3091          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3092                  line => $l, column => $c,
3093                 };
3094      } elsif ($match < 0) {      } elsif ($match < 0) {
3095        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3096        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3097          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
3098        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3099          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
3100                   };
3101          } else {
3102            !!!cp (1025);
3103            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3104                    line => $l, column => $c,
3105                   };
3106        }        }
3107      } else {      } else {
3108        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3109        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3110        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
3111          return {type => CHARACTER_TOKEN, data => '&'.$value,
3112                  line => $l, column => $c,
3113                 };
3114      }      }
3115    } else {    } else {
3116        !!!cp (1027);
3117      ## no characters are consumed      ## no characters are consumed
3118      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3119      return undef;      return undef;
3120    }    }
3121  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1867  sub _initialize_tree_constructor ($) { Line 3127  sub _initialize_tree_constructor ($) {
3127    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3128    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3129    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3130      $self->{document}->set_user_data (manakai_source_line => 1);
3131      $self->{document}->set_user_data (manakai_source_column => 1);
3132  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3133    
3134  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1893  sub _construct_tree ($) { Line 3155  sub _construct_tree ($) {
3155        
3156    !!!next-token;    !!!next-token;
3157    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3158    undef $self->{form_element};    undef $self->{form_element};
3159    undef $self->{head_element};    undef $self->{head_element};
3160    $self->{open_elements} = [];    $self->{open_elements} = [];
3161    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3162    
3163      ## NOTE: The "initial" insertion mode.
3164    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3165    
3166      ## NOTE: The "before html" insertion mode.
3167    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3168      $self->{insertion_mode} = BEFORE_HEAD_IM;
3169    
3170      ## NOTE: The "before head" insertion mode and so on.
3171    $self->_tree_construction_main;    $self->_tree_construction_main;
3172  } # _construct_tree  } # _construct_tree
3173    
3174  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3175    my $self = shift;    my $self = shift;
3176    
3177      ## NOTE: "initial" insertion mode
3178    
3179    INITIAL: {    INITIAL: {
3180      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3181        ## 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 3183  sub _tree_construction_initial ($) {
3183        ## language.        ## language.
3184        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3185        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3186        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3187        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3188            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3189          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3190            !!!parse-error (type => 'not HTML5', token => $token);
3191        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3192          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3193          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3194          } elsif (defined $token->{public_identifier}) {
3195            if ($token->{public_identifier} eq 'XSLT-compat') {
3196              !!!cp ('t1.2');
3197              !!!parse-error (type => 'XSLT-compat', token => $token,
3198                              level => $self->{level}->{should});
3199            } else {
3200              !!!parse-error (type => 'not HTML5', token => $token);
3201            }
3202          } else {
3203            !!!cp ('t3');
3204            #
3205        }        }
3206                
3207        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3208          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3209          ## NOTE: Default value for both |public_id| and |system_id| attributes
3210          ## are empty strings, so that we don't set any value in missing cases.
3211        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3212            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3213        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1933  sub _tree_construction_initial ($) { Line 3216  sub _tree_construction_initial ($) {
3216        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3217        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3218                
3219        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3220            !!!cp ('t4');
3221          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3222        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3223          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3224          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3225          if ({          my $prefix = [
3226            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3227            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3228            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3229            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3230            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3231            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3232            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3233            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3234            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3235            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3236            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3237            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3238            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3239            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3240            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3241            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3242            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3243            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3244            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3245            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3246            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3247            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3248            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3249            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3250            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3251            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3252            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3253            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3254            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3255            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3256            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3257            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3258            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3259            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3260            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3261            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3262            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3263            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3264            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3265            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3266            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3267            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3268            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3269            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3270            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3271            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3272            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3273            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3274            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3275            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3276            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3277            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3278            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3279            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3280            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3281            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3282            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3283            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3284            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3285            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3286            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3287            "-//W3C//DTD W3 HTML//EN" => 1,            }
3288            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3289            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3290            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3291            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3292            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3293            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3294            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3295          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3296                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3297            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3298                !!!cp ('t6');
3299              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3300            } else {            } else {
3301                !!!cp ('t7');
3302              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3303            }            }
3304          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3305                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3306              !!!cp ('t8');
3307            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3308            } else {
3309              !!!cp ('t9');
3310          }          }
3311          } else {
3312            !!!cp ('t10');
3313        }        }
3314        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3315          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3316          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3317          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") {
3318              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3319              ## marked as quirks.
3320            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3321              !!!cp ('t11');
3322            } else {
3323              !!!cp ('t12');
3324          }          }
3325          } else {
3326            !!!cp ('t13');
3327        }        }
3328                
3329        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3330        !!!next-token;        !!!next-token;
3331        return;        return;
3332      } elsif ({      } elsif ({
# Line 2038  sub _tree_construction_initial ($) { Line 3334  sub _tree_construction_initial ($) {
3334                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3335                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3336               }->{$token->{type}}) {               }->{$token->{type}}) {
3337        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3338          !!!parse-error (type => 'no DOCTYPE', token => $token);
3339        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3340        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3341        ## reprocess        ## reprocess
3342          !!!ack-later;
3343        return;        return;
3344      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3345        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3346          ## Ignore the token          ## Ignore the token
3347    
3348          unless (length $token->{data}) {          unless (length $token->{data}) {
3349            ## Stay in the phase            !!!cp ('t15');
3350              ## Stay in the insertion mode.
3351            !!!next-token;            !!!next-token;
3352            redo INITIAL;            redo INITIAL;
3353            } else {
3354              !!!cp ('t16');
3355          }          }
3356          } else {
3357            !!!cp ('t17');
3358        }        }
3359    
3360        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3361        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3362        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3363        ## reprocess        ## reprocess
3364        return;        return;
3365      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3366          !!!cp ('t18');
3367        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3368        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3369                
3370        ## Stay in the phase.        ## Stay in the insertion mode.
3371        !!!next-token;        !!!next-token;
3372        redo INITIAL;        redo INITIAL;
3373      } else {      } else {
3374        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3375      }      }
3376    } # INITIAL    } # INITIAL
3377    
3378      die "$0: _tree_construction_initial: This should be never reached";
3379  } # _tree_construction_initial  } # _tree_construction_initial
3380    
3381  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3382    my $self = shift;    my $self = shift;
3383    
3384      ## NOTE: "before html" insertion mode.
3385        
3386    B: {    B: {
3387        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3388          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3389            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3390          ## Ignore the token          ## Ignore the token
3391          ## Stay in the phase          ## Stay in the insertion mode.
3392          !!!next-token;          !!!next-token;
3393          redo B;          redo B;
3394        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3395            !!!cp ('t20');
3396          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3397          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3398          ## Stay in the phase          ## Stay in the insertion mode.
3399          !!!next-token;          !!!next-token;
3400          redo B;          redo B;
3401        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2093  sub _tree_construction_root_element ($) Line 3403  sub _tree_construction_root_element ($)
3403            ## Ignore the token.            ## Ignore the token.
3404    
3405            unless (length $token->{data}) {            unless (length $token->{data}) {
3406              ## Stay in the phase              !!!cp ('t21');
3407                ## Stay in the insertion mode.
3408              !!!next-token;              !!!next-token;
3409              redo B;              redo B;
3410              } else {
3411                !!!cp ('t22');
3412            }            }
3413            } else {
3414              !!!cp ('t23');
3415          }          }
3416    
3417          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3418    
3419          #          #
3420        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3421          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3422              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
3423            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3424                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3425            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3426                  [$root_element, $el_category->{html}];
3427    
3428              if ($token->{attributes}->{manifest}) {
3429                !!!cp ('t24');
3430                $self->{application_cache_selection}
3431                    ->($token->{attributes}->{manifest}->{value});
3432                ## ISSUE: Spec is unclear on relative references.
3433                ## According to Hixie (#whatwg 2008-03-19), it should be
3434                ## resolved against the base URI of the document in HTML
3435                ## or xml:base of the element in XHTML.
3436              } else {
3437                !!!cp ('t25');
3438                $self->{application_cache_selection}->(undef);
3439              }
3440    
3441              !!!nack ('t25c');
3442    
3443              !!!next-token;
3444              return; ## Go to the "before head" insertion mode.
3445          } else {          } else {
3446            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3447              #
3448          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3449        } elsif ({        } elsif ({
3450                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3451                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3452                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3453          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3454          #          #
3455        } else {        } else {
3456          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3457        }        }
3458    
3459        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3460        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3461        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3462        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3463        #redo B;  
3464        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3465    
3466        ## NOTE: Reprocess the token.
3467        !!!ack-later;
3468        return; ## Go to the "before head" insertion mode.
3469    
3470        ## ISSUE: There is an issue in the spec
3471    } # B    } # B
3472    
3473      die "$0: _tree_construction_root_element: This should never be reached";
3474  } # _tree_construction_root_element  } # _tree_construction_root_element
3475    
3476  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2147  sub _reset_insertion_mode ($) { Line 3485  sub _reset_insertion_mode ($) {
3485            
3486      ## Step 3      ## Step 3
3487      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"!?  
3488        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3489          $last = 1;          $last = 1;
3490          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3491            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3492                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3493              #          } else {
3494            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3495          }          }
3496        }        }
3497              
3498        ## Step 4..13        ## Step 4..14
3499        my $new_mode = {        my $new_mode;
3500          if ($node->[1] & FOREIGN_EL) {
3501            !!!cp ('t28.1');
3502            ## NOTE: Strictly spaking, the line below only applies to MathML and
3503            ## SVG elements.  Currently the HTML syntax supports only MathML and
3504            ## SVG elements as foreigners.
3505            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3506          } elsif ($node->[1] & TABLE_CELL_EL) {
3507            if ($last) {
3508              !!!cp ('t28.2');
3509              #
3510            } else {
3511              !!!cp ('t28.3');
3512              $new_mode = IN_CELL_IM;
3513            }
3514          } else {
3515            !!!cp ('t28.4');
3516            $new_mode = {
3517                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3518                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3519                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3520                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3521                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3522                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2179  sub _reset_insertion_mode ($) { Line 3527  sub _reset_insertion_mode ($) {
3527                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3528                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3529                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3530                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3531          }
3532        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3533                
3534        ## Step 14        ## Step 15
3535        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3536          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3537              !!!cp ('t29');
3538            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3539          } else {          } else {
3540              ## ISSUE: Can this state be reached?
3541              !!!cp ('t30');
3542            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3543          }          }
3544          return;          return;
3545          } else {
3546            !!!cp ('t31');
3547        }        }
3548                
3549        ## Step 15        ## Step 16
3550        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3551                
3552        ## Step 16        ## Step 17
3553        $i--;        $i--;
3554        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3555                
3556        ## Step 17        ## Step 18
3557        redo S3;        redo S3;
3558      } # S3      } # S3
3559    
3560      die "$0: _reset_insertion_mode: This line should never be reached";
3561  } # _reset_insertion_mode  } # _reset_insertion_mode
3562    
3563  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2223  sub _tree_construction_main ($) { Line 3579  sub _tree_construction_main ($) {
3579      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3580      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3581        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3582            !!!cp ('t32');
3583          return;          return;
3584        }        }
3585      }      }
# Line 2237  sub _tree_construction_main ($) { Line 3594  sub _tree_construction_main ($) {
3594    
3595        ## Step 6        ## Step 6
3596        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3597            !!!cp ('t33_1');
3598          #          #
3599        } else {        } else {
3600          my $in_open_elements;          my $in_open_elements;
3601          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3602            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3603                !!!cp ('t33');
3604              $in_open_elements = 1;              $in_open_elements = 1;
3605              last OE;              last OE;
3606            }            }
3607          }          }
3608          if ($in_open_elements) {          if ($in_open_elements) {
3609              !!!cp ('t34');
3610            #            #
3611          } else {          } else {
3612              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3613              !!!cp ('t35');
3614            redo S4;            redo S4;
3615          }          }
3616        }        }
# Line 2271  sub _tree_construction_main ($) { Line 3633  sub _tree_construction_main ($) {
3633    
3634        ## Step 11        ## Step 11
3635        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3636            !!!cp ('t36');
3637          ## Step 7'          ## Step 7'
3638          $i++;          $i++;
3639          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3640                    
3641          redo S7;          redo S7;
3642        }        }
3643    
3644          !!!cp ('t37');
3645      } # S7      } # S7
3646    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3647    
3648    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3649      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3650        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3651            !!!cp ('t38');
3652          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3653          return;          return;
3654        }        }
3655      }      }
3656    
3657        !!!cp ('t39');
3658    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3659    
3660    my $parse_rcdata = sub ($$) {    my $insert;
3661      my ($content_model_flag, $insert) = @_;  
3662      my $parse_rcdata = sub ($) {
3663        my ($content_model_flag) = @_;
3664    
3665      ## Step 1      ## Step 1
3666      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3667      my $el;      my $el;
3668      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3669    
3670      ## Step 2      ## Step 2
3671      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3672    
3673      ## Step 3      ## Step 3
3674      $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 3676  sub _tree_construction_main ($) {
3676    
3677      ## Step 4      ## Step 4
3678      my $text = '';      my $text = '';
3679        !!!nack ('t40.1');
3680      !!!next-token;      !!!next-token;
3681      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3682          !!!cp ('t40');
3683        $text .= $token->{data};        $text .= $token->{data};
3684        !!!next-token;        !!!next-token;
3685      }      }
3686    
3687      ## Step 5      ## Step 5
3688      if (length $text) {      if (length $text) {
3689          !!!cp ('t41');
3690        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3691        $el->append_child ($text);        $el->append_child ($text);
3692      }      }
# Line 2322  sub _tree_construction_main ($) { Line 3695  sub _tree_construction_main ($) {
3695      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3696    
3697      ## Step 7      ## Step 7
3698      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3699            $token->{tag_name} eq $start_tag_name) {
3700          !!!cp ('t42');
3701        ## 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});  
3702      } else {      } else {
3703        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3704          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3705            !!!cp ('t43');
3706            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3707          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3708            !!!cp ('t44');
3709            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3710          } else {
3711            die "$0: $content_model_flag in parse_rcdata";
3712          }
3713      }      }
3714      !!!next-token;      !!!next-token;
3715    }; # $parse_rcdata    }; # $parse_rcdata
3716    
3717    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3718      my $script_el;      my $script_el;
3719      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3720      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3721    
3722      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3723      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3724            
3725      my $text = '';      my $text = '';
3726        !!!nack ('t45.1');
3727      !!!next-token;      !!!next-token;
3728      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3729          !!!cp ('t45');
3730        $text .= $token->{data};        $text .= $token->{data};
3731        !!!next-token;        !!!next-token;
3732      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3733      if (length $text) {      if (length $text) {
3734          !!!cp ('t46');
3735        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3736      }      }
3737                                
# Line 2357  sub _tree_construction_main ($) { Line 3739  sub _tree_construction_main ($) {
3739    
3740      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3741          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3742          !!!cp ('t47');
3743        ## Ignore the token        ## Ignore the token
3744      } else {      } else {
3745        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3746          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3747        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3748        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3749      }      }
3750            
3751      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3752          !!!cp ('t49');
3753        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3754      } else {      } else {
3755          !!!cp ('t50');
3756        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3757        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3758    
# Line 2380  sub _tree_construction_main ($) { Line 3766  sub _tree_construction_main ($) {
3766      !!!next-token;      !!!next-token;
3767    }; # $script_start_tag    }; # $script_start_tag
3768    
3769      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3770      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3771      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3772    
3773    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3774      my $tag_name = shift;      my $end_tag_token = shift;
3775        my $tag_name = $end_tag_token->{tag_name};
3776    
3777        ## NOTE: The adoption agency algorithm (AAA).
3778    
3779      FET: {      FET: {
3780        ## Step 1        ## Step 1
3781        my $formatting_element;        my $formatting_element;
3782        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3783        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3784          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3785              !!!cp ('t52');
3786              last AFE;
3787            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3788                         eq $tag_name) {
3789              !!!cp ('t51');
3790            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3791            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3792            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3793          }          }
3794        } # AFE        } # AFE
3795        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3796          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3797            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3798          ## Ignore the token          ## Ignore the token
3799          !!!next-token;          !!!next-token;
3800          return;          return;
# Line 2409  sub _tree_construction_main ($) { Line 3806  sub _tree_construction_main ($) {
3806          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3807          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3808            if ($in_scope) {            if ($in_scope) {
3809                !!!cp ('t54');
3810              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3811              last INSCOPE;              last INSCOPE;
3812            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3813              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3814                !!!parse-error (type => 'unmatched end tag',
3815                                text => $token->{tag_name},
3816                                token => $end_tag_token);
3817              ## Ignore the token              ## Ignore the token
3818              !!!next-token;              !!!next-token;
3819              return;              return;
3820            }            }
3821          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3822                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3823            $in_scope = 0;            $in_scope = 0;
3824          }          }
3825        } # INSCOPE        } # INSCOPE
3826        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3827          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3828            !!!parse-error (type => 'unmatched end tag',
3829                            text => $token->{tag_name},
3830                            token => $end_tag_token);
3831          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3832          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3833          return;          return;
3834        }        }
3835        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3836          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3837            !!!parse-error (type => 'not closed',
3838                            text => $self->{open_elements}->[-1]->[0]
3839                                ->manakai_local_name,
3840                            token => $end_tag_token);
3841        }        }
3842                
3843        ## Step 2        ## Step 2
# Line 2439  sub _tree_construction_main ($) { Line 3845  sub _tree_construction_main ($) {
3845        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3846        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3847          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3848          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3849              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3850              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3851               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3852              !!!cp ('t59');
3853            $furthest_block = $node;            $furthest_block = $node;
3854            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3855          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3856              !!!cp ('t60');
3857            last OE;            last OE;
3858          }          }
3859        } # OE        } # OE
3860                
3861        ## Step 3        ## Step 3
3862        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3863            !!!cp ('t61');
3864          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3865          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3866          !!!next-token;          !!!next-token;
# Line 2464  sub _tree_construction_main ($) { Line 3873  sub _tree_construction_main ($) {
3873        ## Step 5        ## Step 5
3874        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3875        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3876            !!!cp ('t62');
3877          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3878        }        }
3879                
# Line 2486  sub _tree_construction_main ($) { Line 3896  sub _tree_construction_main ($) {
3896          S7S2: {          S7S2: {
3897            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3898              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3899                  !!!cp ('t63');
3900                $node_i_in_active = $_;                $node_i_in_active = $_;
3901                last S7S2;                last S7S2;
3902              }              }
# Line 2499  sub _tree_construction_main ($) { Line 3910  sub _tree_construction_main ($) {
3910                    
3911          ## Step 4          ## Step 4
3912          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3913              !!!cp ('t64');
3914            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3915          }          }
3916                    
3917          ## Step 5          ## Step 5
3918          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3919              !!!cp ('t65');
3920            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3921            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3922            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2521  sub _tree_construction_main ($) { Line 3934  sub _tree_construction_main ($) {
3934        } # S7          } # S7  
3935                
3936        ## Step 8        ## Step 8
3937        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3938            my $foster_parent_element;
3939            my $next_sibling;
3940            OE: for (reverse 0..$#{$self->{open_elements}}) {
3941              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3942                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3943                                 if (defined $parent and $parent->node_type == 1) {
3944                                   !!!cp ('t65.1');
3945                                   $foster_parent_element = $parent;
3946                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3947                                 } else {
3948                                   !!!cp ('t65.2');
3949                                   $foster_parent_element
3950                                     = $self->{open_elements}->[$_ - 1]->[0];
3951                                 }
3952                                 last OE;
3953                               }
3954                             } # OE
3955                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3956                               unless defined $foster_parent_element;
3957            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3958            $open_tables->[-1]->[1] = 1; # tainted
3959          } else {
3960            !!!cp ('t65.3');
3961            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3962          }
3963                
3964        ## Step 9        ## Step 9
3965        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2538  sub _tree_construction_main ($) { Line 3976  sub _tree_construction_main ($) {
3976        my $i;        my $i;
3977        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3978          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3979              !!!cp ('t66');
3980            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3981            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3982          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3983              !!!cp ('t67');
3984            $i = $_;            $i = $_;
3985          }          }
3986        } # AFE        } # AFE
# Line 2550  sub _tree_construction_main ($) { Line 3990  sub _tree_construction_main ($) {
3990        undef $i;        undef $i;
3991        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3992          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3993              !!!cp ('t68');
3994            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3995            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3996          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3997              !!!cp ('t69');
3998            $i = $_;            $i = $_;
3999          }          }
4000        } # OE        } # OE
# Line 2563  sub _tree_construction_main ($) { Line 4005  sub _tree_construction_main ($) {
4005      } # FET      } # FET
4006    }; # $formatting_end_tag    }; # $formatting_end_tag
4007    
4008    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4009      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4010    }; # $insert_to_current    }; # $insert_to_current
4011    
4012    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4013                         my $child = shift;      my $child = shift;
4014                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4015                              table => 1, tbody => 1, tfoot => 1,        # MUST
4016                              thead => 1, tr => 1,        my $foster_parent_element;
4017                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4018                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4019                           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') {  
4020                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4021                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4022                                   !!!cp ('t70');
4023                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4024                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4025                               } else {                               } else {
4026                                   !!!cp ('t71');
4027                                 $foster_parent_element                                 $foster_parent_element
4028                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4029                               }                               }
# Line 2593  sub _tree_construction_main ($) { Line 4034  sub _tree_construction_main ($) {
4034                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4035                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4036                             ($child, $next_sibling);                             ($child, $next_sibling);
4037                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4038                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4039                         }        !!!cp ('t72');
4040          $self->{open_elements}->[-1]->[0]->append_child ($child);
4041        }
4042    }; # $insert_to_foster    }; # $insert_to_foster
4043    
4044    my $insert;    B: while (1) {
   
   B: {  
4045      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4046        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
4047          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4048        ## Ignore the token        ## Ignore the token
4049        ## Stay in the phase        ## Stay in the phase
4050        !!!next-token;        !!!next-token;
4051        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;  
4052      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4053               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4054        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4055          ## Turn into the main phase          !!!cp ('t79');
4056          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4057          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4058        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4059          ## Turn into the main phase          !!!cp ('t80');
4060          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4061          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4062          } else {
4063            !!!cp ('t81');
4064        }        }
4065    
4066  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4067  ## 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');  
       }  
4068        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4069        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4070          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4071              !!!cp ('t84');
4072            $top_el->set_attribute_ns            $top_el->set_attribute_ns
4073              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
4074               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4075          }          }
4076        }        }
4077          !!!nack ('t84.1');
4078        !!!next-token;        !!!next-token;
4079        redo B;        next B;
4080      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4081        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4082        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4083            !!!cp ('t85');
4084          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
4085        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4086            !!!cp ('t86');
4087          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
4088        } else {        } else {
4089            !!!cp ('t87');
4090          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4091        }        }
4092        !!!next-token;        !!!next-token;
4093        redo B;        next B;
4094      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4095          if ($token->{type} == CHARACTER_TOKEN) {
4096            !!!cp ('t87.1');
4097            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4098            !!!next-token;
4099            next B;
4100          } elsif ($token->{type} == START_TAG_TOKEN) {
4101            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4102                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4103                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4104                ($token->{tag_name} eq 'svg' and
4105                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4106              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4107              !!!cp ('t87.2');
4108              #
4109            } elsif ({
4110                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4111                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4112                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4113                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4114                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4115                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4116                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4117                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4118                     }->{$token->{tag_name}}) {
4119              !!!cp ('t87.2');
4120              !!!parse-error (type => 'not closed',
4121                              text => $self->{open_elements}->[-1]->[0]
4122                                  ->manakai_local_name,
4123                              token => $token);
4124    
4125              pop @{$self->{open_elements}}
4126                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4127    
4128              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4129              ## Reprocess.
4130              next B;
4131            } else {
4132              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4133              my $tag_name = $token->{tag_name};
4134              if ($nsuri eq $SVG_NS) {
4135                $tag_name = {
4136                   altglyph => 'altGlyph',
4137                   altglyphdef => 'altGlyphDef',
4138                   altglyphitem => 'altGlyphItem',
4139                   animatecolor => 'animateColor',
4140                   animatemotion => 'animateMotion',
4141                   animatetransform => 'animateTransform',
4142                   clippath => 'clipPath',
4143                   feblend => 'feBlend',
4144                   fecolormatrix => 'feColorMatrix',
4145                   fecomponenttransfer => 'feComponentTransfer',
4146                   fecomposite => 'feComposite',
4147                   feconvolvematrix => 'feConvolveMatrix',
4148                   fediffuselighting => 'feDiffuseLighting',
4149                   fedisplacementmap => 'feDisplacementMap',
4150                   fedistantlight => 'feDistantLight',
4151                   feflood => 'feFlood',
4152                   fefunca => 'feFuncA',
4153                   fefuncb => 'feFuncB',
4154                   fefuncg => 'feFuncG',
4155                   fefuncr => 'feFuncR',
4156                   fegaussianblur => 'feGaussianBlur',
4157                   feimage => 'feImage',
4158                   femerge => 'feMerge',
4159                   femergenode => 'feMergeNode',
4160                   femorphology => 'feMorphology',
4161                   feoffset => 'feOffset',
4162                   fepointlight => 'fePointLight',
4163                   fespecularlighting => 'feSpecularLighting',
4164                   fespotlight => 'feSpotLight',
4165                   fetile => 'feTile',
4166                   feturbulence => 'feTurbulence',
4167                   foreignobject => 'foreignObject',
4168                   glyphref => 'glyphRef',
4169                   lineargradient => 'linearGradient',
4170                   radialgradient => 'radialGradient',
4171                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4172                   textpath => 'textPath',  
4173                }->{$tag_name} || $tag_name;
4174              }
4175    
4176              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4177    
4178              ## "adjust foreign attributes" - done in insert-element-f
4179    
4180              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4181    
4182              if ($self->{self_closing}) {
4183                pop @{$self->{open_elements}};
4184                !!!ack ('t87.3');
4185              } else {
4186                !!!cp ('t87.4');
4187              }
4188    
4189              !!!next-token;
4190              next B;
4191            }
4192          } elsif ($token->{type} == END_TAG_TOKEN) {
4193            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4194            !!!cp ('t87.5');
4195            #
4196          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4197            !!!cp ('t87.6');
4198            !!!parse-error (type => 'not closed',
4199                            text => $self->{open_elements}->[-1]->[0]
4200                                ->manakai_local_name,
4201                            token => $token);
4202    
4203            pop @{$self->{open_elements}}
4204                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4205    
4206            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4207            ## Reprocess.
4208            next B;
4209          } else {
4210            die "$0: $token->{type}: Unknown token type";        
4211          }
4212        }
4213    
4214        if ($self->{insertion_mode} & HEAD_IMS) {
4215        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4216          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4217            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4218                !!!cp ('t88.2');
4219                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4220              } else {
4221                !!!cp ('t88.1');
4222                ## Ignore the token.
4223                !!!next-token;
4224                next B;
4225              }
4226            unless (length $token->{data}) {            unless (length $token->{data}) {
4227                !!!cp ('t88');
4228              !!!next-token;              !!!next-token;
4229              redo B;              next B;
4230            }            }
4231          }          }
4232    
4233          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4234              !!!cp ('t89');
4235            ## As if <head>            ## As if <head>
4236            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4237            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4238            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4239                  [$self->{head_element}, $el_category->{head}];
4240    
4241            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4242            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4243    
4244            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4245          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4246              !!!cp ('t90');
4247            ## As if </noscript>            ## As if </noscript>
4248            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4249            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4250                        
4251            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4252            ## As if </head>            ## As if </head>
# Line 2704  sub _tree_construction_main ($) { Line 4254  sub _tree_construction_main ($) {
4254    
4255            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4256          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4257              !!!cp ('t91');
4258            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4259    
4260            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4261            } else {
4262              !!!cp ('t92');
4263          }          }
4264    
4265              ## "after head" insertion mode          ## "after head" insertion mode
4266              ## As if <body>          ## As if <body>
4267              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4268              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4269              ## reprocess          ## reprocess
4270              redo B;          next B;
4271            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4272              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4273                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4274                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4275                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4276                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4277                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4278                  !!!next-token;              push @{$self->{open_elements}},
4279                  redo B;                  [$self->{head_element}, $el_category->{head}];
4280                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4281                  #              !!!nack ('t93.1');
4282                } else {              !!!next-token;
4283                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4284                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4285                  !!!next-token;              !!!cp ('t93.2');
4286                  redo B;              !!!parse-error (type => 'after head', text => 'head',
4287                }                              token => $token);
4288              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              ## Ignore the token
4289                ## As if <head>              !!!nack ('t93.3');
4290                !!!create-element ($self->{head_element}, 'head');              !!!next-token;
4291                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              next B;
4292                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
4293                !!!cp ('t95');
4294                !!!parse-error (type => 'in head:head',
4295                                token => $token); # or in head noscript
4296                ## Ignore the token
4297                !!!nack ('t95.1');
4298                !!!next-token;
4299                next B;
4300              }
4301            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4302              !!!cp ('t96');
4303              ## As if <head>
4304              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4305              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4306              push @{$self->{open_elements}},
4307                  [$self->{head_element}, $el_category->{head}];
4308    
4309                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4310                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4311              }          } else {
4312              !!!cp ('t97');
4313            }
4314    
4315              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4316                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4317                    !!!cp ('t98');
4318                  ## As if </noscript>                  ## As if </noscript>
4319                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4320                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4321                                    token => $token);
4322                                
4323                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4324                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4325                  } else {
4326                    !!!cp ('t99');
4327                }                }
4328    
4329                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4330                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4331                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4332                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4333                                    text => $token->{tag_name}, token => $token);
4334                    push @{$self->{open_elements}},
4335                        [$self->{head_element}, $el_category->{head}];
4336                  } else {
4337                    !!!cp ('t101');
4338                }                }
4339                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4340                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4341                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4342                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4343                  !!!nack ('t101.1');
4344                !!!next-token;                !!!next-token;
4345                redo B;                next B;
4346              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4347                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4348                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4349                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4350                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4351                                    text => $token->{tag_name}, token => $token);
4352                    push @{$self->{open_elements}},
4353                        [$self->{head_element}, $el_category->{head}];
4354                  } else {
4355                    !!!cp ('t103');
4356                }                }
4357                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4358                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4359                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4360                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4361                  !!!ack ('t103.1');
4362                !!!next-token;                !!!next-token;
4363                redo B;                next B;
4364              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4365                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4366                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4367                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4368                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4369                                    text => $token->{tag_name}, token => $token);
4370                    push @{$self->{open_elements}},
4371                        [$self->{head_element}, $el_category->{head}];
4372                  } else {
4373                    !!!cp ('t105');
4374                }                }
4375                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4376                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.
4377    
4378                unless ($self->{confident}) {                unless ($self->{confident}) {
4379                  my $charset;                  if ($token->{attributes}->{charset}) {
4380                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4381                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4382                  }                    ## in the {change_encoding} callback.
4383                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4384                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4385                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4386                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4387                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4388                          ->set_user_data (manakai_has_reference =>
4389                                               $token->{attributes}->{charset}
4390                                                   ->{has_reference});
4391                    } elsif ($token->{attributes}->{content}) {
4392                      if ($token->{attributes}->{content}->{value}
4393                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4394                              [\x09-\x0D\x20]*=
4395                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4396                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4397                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4398                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4399                        ## in the {change_encoding} callback.
4400                        $self->{change_encoding}
4401                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4402                               $token);
4403                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4404                            ->set_user_data (manakai_has_reference =>
4405                                                 $token->{attributes}->{content}
4406                                                       ->{has_reference});
4407                      } else {
4408                        !!!cp ('t108');
4409                      }
4410                    }
4411                  } else {
4412                    if ($token->{attributes}->{charset}) {
4413                      !!!cp ('t109');
4414                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4415                          ->set_user_data (manakai_has_reference =>
4416                                               $token->{attributes}->{charset}
4417                                                   ->{has_reference});
4418                    }
4419                    if ($token->{attributes}->{content}) {
4420                      !!!cp ('t110');
4421                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4422                          ->set_user_data (manakai_has_reference =>
4423                                               $token->{attributes}->{content}
4424                                                   ->{has_reference});
4425                  }                  }
                 ## TODO: Change the encoding  
4426                }                }
4427    
4428                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4429                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4430                  !!!ack ('t110.1');
4431                !!!next-token;                !!!next-token;
4432                redo B;                next B;
4433              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4434                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4435                    !!!cp ('t111');
4436                  ## As if </noscript>                  ## As if </noscript>
4437                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4438                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4439                                    token => $token);
4440                                
4441                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4442                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4443                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4444                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4445                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4446                                    text => $token->{tag_name}, token => $token);
4447                    push @{$self->{open_elements}},
4448                        [$self->{head_element}, $el_category->{head}];
4449                  } else {
4450                    !!!cp ('t113');
4451                }                }
4452    
4453                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4454                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4455                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4456                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4457                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4458                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4459                redo B;                next B;
4460              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4461                         $token->{tag_name} eq 'noframes') {
4462                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4463                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4464                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4465                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4466                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4467                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4468                                    text => $token->{tag_name}, token => $token);
4469                    push @{$self->{open_elements}},
4470                        [$self->{head_element}, $el_category->{head}];
4471                  } else {
4472                    !!!cp ('t115');
4473                }                }
4474                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4475                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4476                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4477                redo B;                next B;
4478              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4479                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4480                    !!!cp ('t116');
4481                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4482                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4483                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4484                    !!!nack ('t116.1');
4485                  !!!next-token;                  !!!next-token;
4486                  redo B;                  next B;
4487                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4488                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4489                    !!!parse-error (type => 'in noscript', text => 'noscript',
4490                                    token => $token);
4491                  ## Ignore the token                  ## Ignore the token
4492                    !!!nack ('t117.1');
4493                  !!!next-token;                  !!!next-token;
4494                  redo B;                  next B;
4495                } else {                } else {
4496                    !!!cp ('t118');
4497                  #                  #
4498                }                }
4499              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4500                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4501                    !!!cp ('t119');
4502                  ## As if </noscript>                  ## As if </noscript>
4503                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4504                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4505                                    token => $token);
4506                                
4507                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4508                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4509                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4510                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4511                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4512                                    text => $token->{tag_name}, token => $token);
4513                    push @{$self->{open_elements}},
4514                        [$self->{head_element}, $el_category->{head}];
4515                  } else {
4516                    !!!cp ('t121');
4517                }                }
4518    
4519                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4520                $script_start_tag->($insert_to_current);                $script_start_tag->();
4521                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4522                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4523                redo B;                next B;
4524              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4525                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4526                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4527                    !!!cp ('t122');
4528                  ## As if </noscript>                  ## As if </noscript>
4529                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4530                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4531                                    text => $token->{tag_name}, token => $token);
4532                                    
4533                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4534                  ## As if </head>                  ## As if </head>
# Line 2885  sub _tree_construction_main ($) { Line 4536  sub _tree_construction_main ($) {
4536                                    
4537                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4538                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4539                    !!!cp ('t124');
4540                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4541                                    
4542                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4543                  } else {
4544                    !!!cp ('t125');
4545                }                }
4546    
4547                ## "after head" insertion mode                ## "after head" insertion mode
4548                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4549                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4550                    !!!cp ('t126');
4551                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4552                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4553                    !!!cp ('t127');
4554                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4555                } else {                } else {
4556                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4557                }                }
4558                  !!!nack ('t127.1');
4559                !!!next-token;                !!!next-token;
4560                redo B;                next B;
4561              } else {              } else {
4562                  !!!cp ('t128');
4563                #                #
4564              }              }
4565    
4566              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4567                  !!!cp ('t129');
4568                ## As if </noscript>                ## As if </noscript>
4569                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4570                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4571                                  text => $token->{tag_name}, token => $token);
4572                                
4573                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4574                ## As if </head>                ## As if </head>
# Line 2916  sub _tree_construction_main ($) { Line 4576  sub _tree_construction_main ($) {
4576    
4577                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4578              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4579                  !!!cp ('t130');
4580                ## As if </head>                ## As if </head>
4581                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4582    
4583                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4584                } else {
4585                  !!!cp ('t131');
4586              }              }
4587    
4588              ## "after head" insertion mode              ## "after head" insertion mode
4589              ## As if <body>              ## As if <body>
4590              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4591              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4592              ## reprocess              ## reprocess
4593              redo B;              !!!ack-later;
4594                next B;
4595            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4596              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4597                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4598                    !!!cp ('t132');
4599                  ## As if <head>                  ## As if <head>
4600                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4601                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4602                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4603                        [$self->{head_element}, $el_category->{head}];
4604    
4605                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4606                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4607                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4608                  !!!next-token;                  !!!next-token;
4609                  redo B;                  next B;
4610                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4611                    !!!cp ('t133');
4612                  ## As if </noscript>                  ## As if </noscript>
4613                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4614                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
4615                                    text => 'head', token => $token);
4616                                    
4617                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4618                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4619                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4620                  !!!next-token;                  !!!next-token;
4621                  redo B;                  next B;
4622                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4623                    !!!cp ('t134');
4624                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4625                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4626                  !!!next-token;                  !!!next-token;
4627                  redo B;                  next B;
4628                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4629                    !!!cp ('t134.1');
4630                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4631                                    token => $token);
4632                    ## Ignore the token
4633                    !!!next-token;
4634                    next B;
4635                } else {                } else {
4636                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4637                }                }
4638              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4639                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4640                    !!!cp ('t136');
4641                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4642                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4643                  !!!next-token;                  !!!next-token;
4644                  redo B;                  next B;
4645                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4646                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4647                    !!!cp ('t137');
4648                    !!!parse-error (type => 'unmatched end tag',
4649                                    text => 'noscript', token => $token);
4650                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4651                  !!!next-token;                  !!!next-token;
4652                  redo B;                  next B;
4653                } else {                } else {
4654                    !!!cp ('t138');
4655                  #                  #
4656                }                }
4657              } elsif ({              } elsif ({
4658                        body => 1, html => 1,                        body => 1, html => 1,
4659                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4660                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4661                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4662                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4663                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4664                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag',
4665                                    text => $token->{tag_name}, token => $token);
4666                  $self->{insertion_mode} = IN_HEAD_IM;                  ## Ignore the token
4667                  ## Reprocess in the "in head" insertion mode...                  !!!next-token;
4668                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                  next B;
4669                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4670                    !!!cp ('t140.1');
4671                    !!!parse-error (type => 'unmatched end tag',
4672                                    text => $token->{tag_name}, token => $token);
4673                  ## Ignore the token                  ## Ignore the token
4674                  !!!next-token;                  !!!next-token;
4675                  redo B;                  next B;
4676                  } else {
4677                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4678                }                }
4679                              } elsif ($token->{tag_name} eq 'p') {
4680                #                !!!cp ('t142');
4681              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4682                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4683                       }->{$token->{tag_name}}) {                ## Ignore the token
4684                  !!!next-token;
4685                  next B;
4686                } elsif ($token->{tag_name} eq 'br') {
4687                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4688                  ## As if <head>                  !!!cp ('t142.2');
4689                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
4690                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4691                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4692                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4693      
4694                    ## Reprocess in the "after head" insertion mode...
4695                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4696                    !!!cp ('t143.2');
4697                    ## As if </head>
4698                    pop @{$self->{open_elements}};
4699                    $self->{insertion_mode} = AFTER_HEAD_IM;
4700      
4701                    ## Reprocess in the "after head" insertion mode...
4702                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4703                    !!!cp ('t143.3');
4704                    ## ISSUE: Two parse errors for <head><noscript></br>
4705                    !!!parse-error (type => 'unmatched end tag',
4706                                    text => 'br', token => $token);
4707                    ## As if </noscript>
4708                    pop @{$self->{open_elements}};
4709                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4710    
4711                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4712                }                  ## As if </head>
4713                    pop @{$self->{open_elements}};
4714                    $self->{insertion_mode} = AFTER_HEAD_IM;
4715    
4716                #                  ## Reprocess in the "after head" insertion mode...
4717              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4718                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
4719                  #                  #
4720                } else {                } else {
4721                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4722                }                }
4723    
4724                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4725                  !!!parse-error (type => 'unmatched end tag',
4726                                  text => 'br', token => $token);
4727                  ## Ignore the token
4728                  !!!next-token;
4729                  next B;
4730                } else {
4731                  !!!cp ('t145');
4732                  !!!parse-error (type => 'unmatched end tag',
4733                                  text => $token->{tag_name}, token => $token);
4734                  ## Ignore the token
4735                  !!!next-token;
4736                  next B;
4737              }              }
4738    
4739              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4740                  !!!cp ('t146');
4741                ## As if </noscript>                ## As if </noscript>
4742                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4743                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4744                                  text => $token->{tag_name}, token => $token);
4745                                
4746                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4747                ## As if </head>                ## As if </head>
# Line 3028  sub _tree_construction_main ($) { Line 4749  sub _tree_construction_main ($) {
4749    
4750                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4751              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4752                  !!!cp ('t147');
4753                ## As if </head>                ## As if </head>
4754                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4755    
4756                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4757              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4758                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4759                  !!!cp ('t148');
4760                  !!!parse-error (type => 'unmatched end tag',
4761                                  text => $token->{tag_name}, token => $token);
4762                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4763                !!!next-token;                !!!next-token;
4764                redo B;                next B;
4765                } else {
4766                  !!!cp ('t149');
4767              }              }
4768    
4769              ## "after head" insertion mode              ## "after head" insertion mode
4770              ## As if <body>              ## As if <body>
4771              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4772              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4773              ## reprocess              ## reprocess
4774              redo B;              next B;
4775            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4776              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4777            }            !!!cp ('t149.1');
4778    
4779              ## NOTE: As if <head>
4780              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4781              $self->{open_elements}->[-1]->[0]->append_child
4782                  ($self->{head_element});
4783              #push @{$self->{open_elements}},
4784              #    [$self->{head_element}, $el_category->{head}];
4785              #$self->{insertion_mode} = IN_HEAD_IM;
4786              ## NOTE: Reprocess.
4787    
4788              ## NOTE: As if </head>
4789              #pop @{$self->{open_elements}};
4790              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4791              ## NOTE: Reprocess.
4792              
4793              #
4794            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4795              !!!cp ('t149.2');
4796    
4797              ## NOTE: As if </head>
4798              pop @{$self->{open_elements}};
4799              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4800              ## NOTE: Reprocess.
4801    
4802              #
4803            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4804              !!!cp ('t149.3');
4805    
4806              !!!parse-error (type => 'in noscript:#eof', token => $token);
4807    
4808              ## As if </noscript>
4809              pop @{$self->{open_elements}};
4810              #$self->{insertion_mode} = IN_HEAD_IM;
4811              ## NOTE: Reprocess.
4812    
4813              ## NOTE: As if </head>
4814              pop @{$self->{open_elements}};
4815              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4816              ## NOTE: Reprocess.
4817    
4818              #
4819            } else {
4820              !!!cp ('t149.4');
4821              #
4822            }
4823    
4824            ## NOTE: As if <body>
4825            !!!insert-element ('body',, $token);
4826            $self->{insertion_mode} = IN_BODY_IM;
4827            ## NOTE: Reprocess.
4828            next B;
4829          } else {
4830            die "$0: $token->{type}: Unknown token type";
4831          }
4832    
4833            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4834      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4835            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4836                !!!cp ('t150');
4837              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4838              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4839                            
4840              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4841    
4842              !!!next-token;              !!!next-token;
4843              redo B;              next B;
4844            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4845              if ({              if ({
4846                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3066  sub _tree_construction_main ($) { Line 4848  sub _tree_construction_main ($) {
4848                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4849                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4850                  ## have an element in table scope                  ## have an element in table scope
4851                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4852                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4853                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4854                      $tn = $node->[1];                      !!!cp ('t151');
4855                      last INSCOPE;  
4856                    } elsif ({                      ## Close the cell
4857                              table => 1, html => 1,                      !!!back-token; # <x>
4858                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4859                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4860                    }                                line => $token->{line},
4861                  } # INSCOPE                                column => $token->{column}};
4862                    unless (defined $tn) {                      next B;
4863                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4864                      ## Ignore the token                      !!!cp ('t152');
4865                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4866                      redo B;                      last;
4867                    }                    }
4868                                    }
4869                  ## Close the cell  
4870                  !!!back-token; # <?>                  !!!cp ('t153');
4871                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4872                  redo B;                      text => $token->{tag_name}, token => $token);
4873                    ## Ignore the token
4874                    !!!nack ('t153.1');
4875                    !!!next-token;
4876                    next B;
4877                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4878                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
4879                                    token => $token);
4880                                    
4881                  ## As if </caption>                  ## NOTE: As if </caption>.
4882                  ## have a table element in table scope                  ## have a table element in table scope
4883                  my $i;                  my $i;
4884                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4885                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4886                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4887                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4888                      last INSCOPE;                        !!!cp ('t155');
4889                    } elsif ({                        $i = $_;
4890                              table => 1, html => 1,                        last INSCOPE;
4891                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4892                      last INSCOPE;                        !!!cp ('t156');
4893                          last;
4894                        }
4895                    }                    }
4896    
4897                      !!!cp ('t157');
4898                      !!!parse-error (type => 'start tag not allowed',
4899                                      text => $token->{tag_name}, token => $token);
4900                      ## Ignore the token
4901                      !!!nack ('t157.1');
4902                      !!!next-token;
4903                      next B;
4904                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4905                                    
4906                  ## generate implied end tags                  ## generate implied end tags
4907                  if ({                  while ($self->{open_elements}->[-1]->[1]
4908                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4909                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4910                       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;  
4911                  }                  }
4912    
4913                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4914                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4915                      !!!parse-error (type => 'not closed',
4916                                      text => $self->{open_elements}->[-1]->[0]
4917                                          ->manakai_local_name,
4918                                      token => $token);
4919                    } else {
4920                      !!!cp ('t160');
4921                  }                  }
4922                                    
4923                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3138  sub _tree_construction_main ($) { Line 4927  sub _tree_construction_main ($) {
4927                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4928                                    
4929                  ## reprocess                  ## reprocess
4930                  redo B;                  !!!ack-later;
4931                    next B;
4932                } else {                } else {
4933                    !!!cp ('t161');
4934                  #                  #
4935                }                }
4936              } else {              } else {
4937                  !!!cp ('t162');
4938                #                #
4939              }              }
4940            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3152  sub _tree_construction_main ($) { Line 4944  sub _tree_construction_main ($) {
4944                  my $i;                  my $i;
4945                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4946                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4947                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4948                        !!!cp ('t163');
4949                      $i = $_;                      $i = $_;
4950                      last INSCOPE;                      last INSCOPE;
4951                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4952                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4953                      last INSCOPE;                      last INSCOPE;
4954                    }                    }
4955                  } # INSCOPE                  } # INSCOPE
4956                    unless (defined $i) {                    unless (defined $i) {
4957                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4958                        !!!parse-error (type => 'unmatched end tag',
4959                                        text => $token->{tag_name},
4960                                        token => $token);
4961                      ## Ignore the token                      ## Ignore the token
4962                      !!!next-token;                      !!!next-token;
4963                      redo B;                      next B;
4964                    }                    }
4965                                    
4966                  ## generate implied end tags                  ## generate implied end tags
4967                  if ({                  while ($self->{open_elements}->[-1]->[1]
4968                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4969                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4970                       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;  
4971                  }                  }
4972                    
4973                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4974                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4975                      !!!cp ('t167');
4976                      !!!parse-error (type => 'not closed',
4977                                      text => $self->{open_elements}->[-1]->[0]
4978                                          ->manakai_local_name,
4979                                      token => $token);
4980                    } else {
4981                      !!!cp ('t168');
4982                  }                  }
4983                                    
4984                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3193  sub _tree_construction_main ($) { Line 4988  sub _tree_construction_main ($) {
4988                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4989                                    
4990                  !!!next-token;                  !!!next-token;
4991                  redo B;                  next B;
4992                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4993                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4994                    !!!parse-error (type => 'unmatched end tag',
4995                                    text => $token->{tag_name}, token => $token);
4996                  ## Ignore the token                  ## Ignore the token
4997                  !!!next-token;                  !!!next-token;
4998                  redo B;                  next B;
4999                } else {                } else {
5000                    !!!cp ('t170');
5001                  #                  #
5002                }                }
5003              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
5004                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5005                  ## have a table element in table scope                  ## have a table element in table scope
5006                  my $i;                  my $i;
5007                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5008                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5009                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5010                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5011                      last INSCOPE;                        !!!cp ('t171');
5012                    } elsif ({                        $i = $_;
5013                              table => 1, html => 1,                        last INSCOPE;
5014                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5015                      last INSCOPE;                        !!!cp ('t172');
5016                          last;
5017                        }
5018                    }                    }
5019    
5020                      !!!cp ('t173');
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                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5027                                    
5028                  ## generate implied end tags                  ## generate implied end tags
5029                  if ({                  while ($self->{open_elements}->[-1]->[1]
5030                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5031                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
5032                       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;  
5033                  }                  }
5034                                    
5035                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5036                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
5037                      !!!parse-error (type => 'not closed',
5038                                      text => $self->{open_elements}->[-1]->[0]
5039                                          ->manakai_local_name,
5040                                      token => $token);
5041                    } else {
5042                      !!!cp ('t176');
5043                  }                  }
5044                                    
5045                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3247  sub _tree_construction_main ($) { Line 5049  sub _tree_construction_main ($) {
5049                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5050                                    
5051                  !!!next-token;                  !!!next-token;
5052                  redo B;                  next B;
5053                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5054                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5055                    !!!parse-error (type => 'unmatched end tag',
5056                                    text => $token->{tag_name}, token => $token);
5057                  ## Ignore the token                  ## Ignore the token
5058                  !!!next-token;                  !!!next-token;
5059                  redo B;                  next B;
5060                } else {                } else {
5061                    !!!cp ('t178');
5062                  #                  #
5063                }                }
5064              } elsif ({              } elsif ({
# Line 3264  sub _tree_construction_main ($) { Line 5069  sub _tree_construction_main ($) {
5069                ## have an element in table scope                ## have an element in table scope
5070                my $i;                my $i;
5071                my $tn;                my $tn;
5072                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5073                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5074                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5075                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5076                    last INSCOPE;                      !!!cp ('t179');
5077                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5078                    $tn = $node->[1];  
5079                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5080                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5081                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5082                            table => 1, html => 1,                                line => $token->{line},
5083                           }->{$node->[1]}) {                                column => $token->{column}};
5084                    last INSCOPE;                      next B;
5085                      } elsif ($node->[1] & TABLE_CELL_EL) {
5086                        !!!cp ('t180');
5087                        $tn = $node->[0]->manakai_local_name;
5088                        ## NOTE: There is exactly one |td| or |th| element
5089                        ## in scope in the stack of open elements by definition.
5090                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5091                        ## ISSUE: Can this be reached?
5092                        !!!cp ('t181');
5093                        last;
5094                      }
5095                  }                  }
5096                } # INSCOPE  
5097                unless (defined $i) {                  !!!cp ('t182');
5098                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5099                        text => $token->{tag_name}, token => $token);
5100                  ## Ignore the token                  ## Ignore the token
5101                  !!!next-token;                  !!!next-token;
5102                  redo B;                  next B;
5103                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5104              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5105                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5106                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5107                                  token => $token);
5108    
5109                ## As if </caption>                ## As if </caption>
5110                ## have a table element in table scope                ## have a table element in table scope
5111                my $i;                my $i;
5112                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5113                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5114                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5115                      !!!cp ('t184');
5116                    $i = $_;                    $i = $_;
5117                    last INSCOPE;                    last INSCOPE;
5118                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5119                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5120                    last INSCOPE;                    last INSCOPE;
5121                  }                  }
5122                } # INSCOPE                } # INSCOPE
5123                unless (defined $i) {                unless (defined $i) {
5124                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5125                    !!!parse-error (type => 'unmatched end tag',
5126                                    text => 'caption', token => $token);
5127                  ## Ignore the token                  ## Ignore the token
5128                  !!!next-token;                  !!!next-token;
5129                  redo B;                  next B;
5130                }                }
5131                                
5132                ## generate implied end tags                ## generate implied end tags
5133                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5134                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5135                     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;  
5136                }                }
5137    
5138                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5139                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5140                    !!!parse-error (type => 'not closed',
5141                                    text => $self->{open_elements}->[-1]->[0]
5142                                        ->manakai_local_name,
5143                                    token => $token);
5144                  } else {
5145                    !!!cp ('t189');
5146                }                }
5147    
5148                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3340  sub _tree_construction_main ($) { Line 5152  sub _tree_construction_main ($) {
5152                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5153    
5154                ## reprocess                ## reprocess
5155                redo B;                next B;
5156              } elsif ({              } elsif ({
5157                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5158                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5159                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5160                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
5161                    !!!parse-error (type => 'unmatched end tag',
5162                                    text => $token->{tag_name}, token => $token);
5163                  ## Ignore the token                  ## Ignore the token
5164                  !!!next-token;                  !!!next-token;
5165                  redo B;                  next B;
5166                } else {                } else {
5167                    !!!cp ('t191');
5168                  #                  #
5169                }                }
5170              } elsif ({              } elsif ({
# Line 3357  sub _tree_construction_main ($) { Line 5172  sub _tree_construction_main ($) {
5172                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5173                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5174                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5175                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5176                  !!!parse-error (type => 'unmatched end tag',
5177                                  text => $token->{tag_name}, token => $token);
5178                ## Ignore the token                ## Ignore the token
5179                !!!next-token;                !!!next-token;
5180                redo B;                next B;
5181              } else {              } else {
5182                  !!!cp ('t193');
5183                #                #
5184              }              }
5185          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5186            for my $entry (@{$self->{open_elements}}) {
5187              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5188                !!!cp ('t75');
5189                !!!parse-error (type => 'in body:#eof', token => $token);
5190                last;
5191              }
5192            }
5193    
5194            ## Stop parsing.
5195            last B;
5196        } else {        } else {
5197          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5198        }        }
# Line 3372  sub _tree_construction_main ($) { Line 5201  sub _tree_construction_main ($) {
5201        #        #
5202      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5203        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5204              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5205                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5206              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5207                                
5208                unless (length $token->{data}) {            unless (length $token->{data}) {
5209                  !!!next-token;              !!!cp ('t194');
5210                  redo B;              !!!next-token;
5211                }              next B;
5212              }            } else {
5213                !!!cp ('t195');
5214              }
5215            }
5216    
5217              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5218    
5219              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5220              ## 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 5222  sub _tree_construction_main ($) {
5222              ## result in a new Text node.              ## result in a new Text node.
5223              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5224                            
5225              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]}) {  
5226                # MUST                # MUST
5227                my $foster_parent_element;                my $foster_parent_element;
5228                my $next_sibling;                my $next_sibling;
5229                my $prev_sibling;                my $prev_sibling;
5230                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5231                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5232                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5233                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5234                        !!!cp ('t196');
5235                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5236                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5237                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5238                    } else {                    } else {
5239                        !!!cp ('t197');
5240                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5241                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5242                    }                    }
# Line 3416  sub _tree_construction_main ($) { Line 5248  sub _tree_construction_main ($) {
5248                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5249                if (defined $prev_sibling and                if (defined $prev_sibling and
5250                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5251                    !!!cp ('t198');
5252                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5253                } else {                } else {
5254                    !!!cp ('t199');
5255                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5256                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5257                     $next_sibling);                     $next_sibling);
5258                }                }
5259              } else {            $open_tables->[-1]->[1] = 1; # tainted
5260                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5261              }            !!!cp ('t200');
5262              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5263            }
5264                            
5265              !!!next-token;          !!!next-token;
5266              redo B;          next B;
5267        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5268              if ({          if ({
5269                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5270                   th => 1, td => 1,               th => 1, td => 1,
5271                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5272                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5273                  ## Clear back to table context              ## Clear back to table context
5274                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5275                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5276                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!cp ('t201');
5277                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5278                  }              }
5279                                
5280                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5281                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5282                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5283                }            }
5284              
5285                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5286                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5287                    !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t202');
5288                  }                !!!parse-error (type => 'missing start tag:tr', token => $token);
5289                }
5290                                    
5291                  ## Clear back to table body context              ## Clear back to table body context
5292                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5293                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5294                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5295                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## ISSUE: Can this case be reached?
5296                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5297                  }              }
5298                                    
5299                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5300                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5301                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5302                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5303                      !!!nack ('t204');
5304                    !!!next-token;                    !!!next-token;
5305                    redo B;                    next B;
5306                  } else {                  } else {
5307                    !!!insert-element ('tr');                    !!!cp ('t205');
5308                      !!!insert-element ('tr',, $token);
5309                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5310                  }                  }
5311                  } else {
5312                    !!!cp ('t206');
5313                }                }
5314    
5315                ## Clear back to table row context                ## Clear back to table row context
5316                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5317                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5318                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5319                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5320                }                }
5321                                
5322                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5323                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5324    
5325                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5326                                
5327                  !!!nack ('t207.1');
5328                !!!next-token;                !!!next-token;
5329                redo B;                next B;
5330              } elsif ({              } elsif ({
5331                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5332                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3496  sub _tree_construction_main ($) { Line 5338  sub _tree_construction_main ($) {
5338                  my $i;                  my $i;
5339                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5340                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5341                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5342                        !!!cp ('t208');
5343                      $i = $_;                      $i = $_;
5344                      last INSCOPE;                      last INSCOPE;
5345                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5346                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5347                      last INSCOPE;                      last INSCOPE;
5348                    }                    }
5349                  } # INSCOPE                  } # INSCOPE
5350                  unless (defined $i) {                  unless (defined $i) {
5351                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5352    ## TODO: This type is wrong.
5353                      !!!parse-error (type => 'unmacthed end tag',
5354                                      text => $token->{tag_name}, token => $token);
5355                    ## Ignore the token                    ## Ignore the token
5356                      !!!nack ('t210.1');
5357                    !!!next-token;                    !!!next-token;
5358                    redo B;                    next B;
5359                  }                  }
5360                                    
5361                  ## Clear back to table row context                  ## Clear back to table row context
5362                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5363                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5364                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5365                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5366                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5367                  }                  }
5368                                    
5369                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5370                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5371                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5372                      !!!cp ('t212');
5373                    ## reprocess                    ## reprocess
5374                    redo B;                    !!!ack-later;
5375                      next B;
5376                  } else {                  } else {
5377                      !!!cp ('t213');
5378                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5379                  }                  }
5380                }                }
# Line 3535  sub _tree_construction_main ($) { Line 5384  sub _tree_construction_main ($) {
5384                  my $i;                  my $i;
5385                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5386                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5387                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5388                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5389                      $i = $_;                      $i = $_;
5390                      last INSCOPE;                      last INSCOPE;
5391                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5392                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5393                      last INSCOPE;                      last INSCOPE;
5394                    }                    }
5395                  } # INSCOPE                  } # INSCOPE
5396                  unless (defined $i) {                  unless (defined $i) {
5397                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5398    ## TODO: This erorr type is wrong.
5399                      !!!parse-error (type => 'unmatched end tag',
5400                                      text => $token->{tag_name}, token => $token);
5401                    ## Ignore the token                    ## Ignore the token
5402                      !!!nack ('t216.1');
5403                    !!!next-token;                    !!!next-token;
5404                    redo B;                    next B;
5405                  }                  }
5406    
5407                  ## Clear back to table body context                  ## Clear back to table body context
5408                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5409                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5410                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5411                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5412                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5413                  }                  }
5414                                    
# Line 3571  sub _tree_construction_main ($) { Line 5422  sub _tree_construction_main ($) {
5422                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5423                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5424                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5425                  } else {
5426                    !!!cp ('t218');
5427                }                }
5428    
5429                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5430                  ## Clear back to table context                  ## Clear back to table context
5431                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5432                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5433                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5434                      ## ISSUE: Can this state be reached?
5435                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5436                  }                  }
5437                                    
5438                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5439                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5440                  ## reprocess                  ## reprocess
5441                  redo B;                  !!!ack-later;
5442                    next B;
5443                } elsif ({                } elsif ({
5444                          caption => 1,                          caption => 1,
5445                          colgroup => 1,                          colgroup => 1,
5446                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5447                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5448                  ## Clear back to table context                  ## Clear back to table context
5449                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5450                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5451                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5452                      ## ISSUE: Can this state be reached?
5453                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5454                  }                  }
5455                                    
5456                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5457                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5458                                    
5459                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5460                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5461                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5462                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3609  sub _tree_construction_main ($) { Line 5465  sub _tree_construction_main ($) {
5465                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5466                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5467                  !!!next-token;                  !!!next-token;
5468                  redo B;                  !!!nack ('t220.1');
5469                    next B;
5470                } else {                } else {
5471                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5472                }                }
5473              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5474                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5475                                  text => $self->{open_elements}->[-1]->[0]
5476                                      ->manakai_local_name,
5477                                  token => $token);
5478    
5479                ## As if </table>                ## As if </table>
5480                ## have a table element in table scope                ## have a table element in table scope
5481                my $i;                my $i;
5482                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5483                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5484                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5485                      !!!cp ('t221');
5486                    $i = $_;                    $i = $_;
5487                    last INSCOPE;                    last INSCOPE;
5488                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5489                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5490                    last INSCOPE;                    last INSCOPE;
5491                  }                  }
5492                } # INSCOPE                } # INSCOPE
5493                unless (defined $i) {                unless (defined $i) {
5494                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5495    ## TODO: The following is wrong, maybe.
5496                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5497                                    token => $token);
5498                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5499                    !!!nack ('t223.1');
5500                  !!!next-token;                  !!!next-token;
5501                  redo B;                  next B;
5502                }                }
5503                                
5504    ## TODO: Followings are removed from the latest spec.
5505                ## generate implied end tags                ## generate implied end tags
5506                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5507                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5508                     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;  
5509                }                }
5510    
5511                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5512                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5513                    ## NOTE: |<table><tr><table>|
5514                    !!!parse-error (type => 'not closed',
5515                                    text => $self->{open_elements}->[-1]->[0]
5516                                        ->manakai_local_name,
5517                                    token => $token);
5518                  } else {
5519                    !!!cp ('t226');
5520                }                }
5521    
5522                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5523                  pop @{$open_tables};
5524    
5525                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5526    
5527                ## reprocess            ## reprocess
5528                redo B;            !!!ack-later;
5529          } else {            next B;
5530            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5531              if (not $open_tables->[-1]->[1]) { # tainted
5532                !!!cp ('t227.8');
5533                ## NOTE: This is a "as if in head" code clone.
5534                $parse_rcdata->(CDATA_CONTENT_MODEL);
5535                next B;
5536              } else {
5537                !!!cp ('t227.7');
5538                #
5539              }
5540            } elsif ($token->{tag_name} eq 'script') {
5541              if (not $open_tables->[-1]->[1]) { # tainted
5542                !!!cp ('t227.6');
5543                ## NOTE: This is a "as if in head" code clone.
5544                $script_start_tag->();
5545                next B;
5546              } else {
5547                !!!cp ('t227.5');
5548                #
5549              }
5550            } elsif ($token->{tag_name} eq 'input') {
5551              if (not $open_tables->[-1]->[1]) { # tainted
5552                if ($token->{attributes}->{type}) { ## TODO: case
5553                  my $type = lc $token->{attributes}->{type}->{value};
5554                  if ($type eq 'hidden') {
5555                    !!!cp ('t227.3');
5556                    !!!parse-error (type => 'in table',
5557                                    text => $token->{tag_name}, token => $token);
5558    
5559            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5560    
5561                    ## TODO: form element pointer
5562    
5563                    pop @{$self->{open_elements}};
5564    
5565                    !!!next-token;
5566                    !!!ack ('t227.2.1');
5567                    next B;
5568                  } else {
5569                    !!!cp ('t227.2');
5570                    #
5571                  }
5572                } else {
5573                  !!!cp ('t227.1');
5574                  #
5575                }
5576              } else {
5577                !!!cp ('t227.4');
5578                #
5579              }
5580            } else {
5581              !!!cp ('t227');
5582            #            #
5583          }          }
5584    
5585            !!!parse-error (type => 'in table', text => $token->{tag_name},
5586                            token => $token);
5587    
5588            $insert = $insert_to_foster;
5589            #
5590        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5591              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5592                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3674  sub _tree_construction_main ($) { Line 5594  sub _tree_construction_main ($) {
5594                my $i;                my $i;
5595                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5596                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5597                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5598                      !!!cp ('t228');
5599                    $i = $_;                    $i = $_;
5600                    last INSCOPE;                    last INSCOPE;
5601                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5602                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5603                    last INSCOPE;                    last INSCOPE;
5604                  }                  }
5605                } # INSCOPE                } # INSCOPE
5606                unless (defined $i) {                unless (defined $i) {
5607                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5608                    !!!parse-error (type => 'unmatched end tag',
5609                                    text => $token->{tag_name}, token => $token);
5610                  ## Ignore the token                  ## Ignore the token
5611                    !!!nack ('t230.1');
5612                  !!!next-token;                  !!!next-token;
5613                  redo B;                  next B;
5614                  } else {
5615                    !!!cp ('t232');
5616                }                }
5617    
5618                ## Clear back to table row context                ## Clear back to table row context
5619                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5620                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5621                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5622                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5623                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5624                }                }
5625    
5626                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5627                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5628                !!!next-token;                !!!next-token;
5629                redo B;                !!!nack ('t231.1');
5630                  next B;
5631              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5632                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5633                  ## As if </tr>                  ## As if </tr>
# Line 3709  sub _tree_construction_main ($) { Line 5635  sub _tree_construction_main ($) {
5635                  my $i;                  my $i;
5636                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5637                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5638                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5639                        !!!cp ('t233');
5640                      $i = $_;                      $i = $_;
5641                      last INSCOPE;                      last INSCOPE;
5642                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5643                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5644                      last INSCOPE;                      last INSCOPE;
5645                    }                    }
5646                  } # INSCOPE                  } # INSCOPE
5647                  unless (defined $i) {                  unless (defined $i) {
5648                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5649    ## TODO: The following is wrong.
5650                      !!!parse-error (type => 'unmatched end tag',
5651                                      text => $token->{type}, token => $token);
5652                    ## Ignore the token                    ## Ignore the token
5653                      !!!nack ('t236.1');
5654                    !!!next-token;                    !!!next-token;
5655                    redo B;                    next B;
5656                  }                  }
5657                                    
5658                  ## Clear back to table row context                  ## Clear back to table row context
5659                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5660                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5661                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5662                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5663                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5664                  }                  }
5665                                    
# Line 3743  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 ('t237');
                       }->{$node->[1]}) {  
5678                      $i = $_;                      $i = $_;
5679                      last INSCOPE;                      last INSCOPE;
5680                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5681                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$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 ('t239');
5687                      !!!parse-error (type => 'unmatched end tag',
5688                                      text => $token->{tag_name}, token => $token);
5689                    ## Ignore the token                    ## Ignore the token
5690                      !!!nack ('t239.1');
5691                    !!!next-token;                    !!!next-token;
5692                    redo B;                    next B;
5693                  }                  }
5694                                    
5695                  ## Clear back to table body context                  ## Clear back to table body context
5696                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5697                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5698                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5699                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5700                  }                  }
5701                                    
# Line 3781  sub _tree_construction_main ($) { Line 5711  sub _tree_construction_main ($) {
5711                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5712                }                }
5713    
5714                  ## NOTE: </table> in the "in table" insertion mode.
5715                  ## When you edit the code fragment below, please ensure that
5716                  ## the code for <table> in the "in table" insertion mode
5717                  ## is synced with it.
5718    
5719                ## have a table element in table scope                ## have a table element in table scope
5720                my $i;                my $i;
5721                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5722                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5723                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5724                      !!!cp ('t241');
5725                    $i = $_;                    $i = $_;
5726                    last INSCOPE;                    last INSCOPE;
5727                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5728                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5729                    last INSCOPE;                    last INSCOPE;
5730                  }                  }
5731                } # INSCOPE                } # INSCOPE
5732                unless (defined $i) {                unless (defined $i) {
5733                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5734                    !!!parse-error (type => 'unmatched end tag',
5735                                    text => $token->{tag_name}, token => $token);
5736                  ## Ignore the token                  ## Ignore the token
5737                    !!!nack ('t243.1');
5738                  !!!next-token;                  !!!next-token;
5739                  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]);  
5740                }                }
5741                                    
5742                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5743                  pop @{$open_tables};
5744                                
5745                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5746                                
5747                !!!next-token;                !!!next-token;
5748                redo B;                next B;
5749              } elsif ({              } elsif ({
5750                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5751                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3832  sub _tree_construction_main ($) { Line 5755  sub _tree_construction_main ($) {
5755                  my $i;                  my $i;
5756                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5757                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5758                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5759                        !!!cp ('t247');
5760                      $i = $_;                      $i = $_;
5761                      last INSCOPE;                      last INSCOPE;
5762                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5763                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5764                      last INSCOPE;                      last INSCOPE;
5765                    }                    }
5766                  } # INSCOPE                  } # INSCOPE
5767                    unless (defined $i) {                    unless (defined $i) {
5768                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5769                        !!!parse-error (type => 'unmatched end tag',
5770                                        text => $token->{tag_name}, token => $token);
5771                      ## Ignore the token                      ## Ignore the token
5772                        !!!nack ('t249.1');
5773                      !!!next-token;                      !!!next-token;
5774                      redo B;                      next B;
5775                    }                    }
5776                                    
5777                  ## As if </tr>                  ## As if </tr>
# Line 3853  sub _tree_construction_main ($) { Line 5779  sub _tree_construction_main ($) {
5779                  my $i;                  my $i;
5780                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5781                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5782                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5783                        !!!cp ('t250');
5784                      $i = $_;                      $i = $_;
5785                      last INSCOPE;                      last INSCOPE;
5786                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5787                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5788                      last INSCOPE;                      last INSCOPE;
5789                    }                    }
5790                  } # INSCOPE                  } # INSCOPE
5791                    unless (defined $i) {                    unless (defined $i) {
5792                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5793                        !!!parse-error (type => 'unmatched end tag',
5794                                        text => 'tr', token => $token);
5795                      ## Ignore the token                      ## Ignore the token
5796                        !!!nack ('t252.1');
5797                      !!!next-token;                      !!!next-token;
5798                      redo B;                      next B;
5799                    }                    }
5800                                    
5801                  ## Clear back to table row context                  ## Clear back to table row context
5802                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5803                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5804                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5805                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5806                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5807                  }                  }
5808                                    
# Line 3886  sub _tree_construction_main ($) { Line 5815  sub _tree_construction_main ($) {
5815                my $i;                my $i;
5816                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5817                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5818                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5819                      !!!cp ('t254');
5820                    $i = $_;                    $i = $_;
5821                    last INSCOPE;                    last INSCOPE;
5822                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5823                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5824                    last INSCOPE;                    last INSCOPE;
5825                  }                  }
5826                } # INSCOPE                } # INSCOPE
5827                unless (defined $i) {                unless (defined $i) {
5828                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5829                    !!!parse-error (type => 'unmatched end tag',
5830                                    text => $token->{tag_name}, token => $token);
5831                  ## Ignore the token                  ## Ignore the token
5832                    !!!nack ('t256.1');
5833                  !!!next-token;                  !!!next-token;
5834                  redo B;                  next B;
5835                }                }
5836    
5837                ## Clear back to table body context                ## Clear back to table body context
5838                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5839                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5840                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5841                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5842                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5843                }                }
5844    
5845                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5846                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5847                  !!!nack ('t257.1');
5848                !!!next-token;                !!!next-token;
5849                redo B;                next B;
5850              } elsif ({              } elsif ({
5851                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5852                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5853                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5854                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5855                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5856                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5857                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
5858                !!!next-token;                            text => $token->{tag_name}, token => $token);
5859                redo B;            ## Ignore the token
5860          } else {            !!!nack ('t258.1');
5861            !!!parse-error (type => 'in table:/'.$token->{tag_name});             !!!next-token;
5862              next B;
5863            } else {
5864              !!!cp ('t259');
5865              !!!parse-error (type => 'in table:/',
5866                              text => $token->{tag_name}, token => $token);
5867    
5868            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5869            #            #
5870          }          }
5871          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5872            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5873                    @{$self->{open_elements}} == 1) { # redundant, maybe
5874              !!!parse-error (type => 'in body:#eof', token => $token);
5875              !!!cp ('t259.1');
5876              #
5877            } else {
5878              !!!cp ('t259.2');
5879              #
5880            }
5881    
5882            ## Stop parsing
5883            last B;
5884        } else {        } else {
5885          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5886        }        }
# Line 3938  sub _tree_construction_main ($) { Line 5889  sub _tree_construction_main ($) {
5889              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5890                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5891                unless (length $token->{data}) {                unless (length $token->{data}) {
5892                    !!!cp ('t260');
5893                  !!!next-token;                  !!!next-token;
5894                  redo B;                  next B;
5895                }                }
5896              }              }
5897                            
5898                !!!cp ('t261');
5899              #              #
5900            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5901              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5902                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5903                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5904                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5905                  !!!ack ('t262.1');
5906                !!!next-token;                !!!next-token;
5907                redo B;                next B;
5908              } else {              } else {
5909                  !!!cp ('t263');
5910                #                #
5911              }              }
5912            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5913              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5914                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5915                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5916                    !!!parse-error (type => 'unmatched end tag',
5917                                    text => 'colgroup', token => $token);
5918                  ## Ignore the token                  ## Ignore the token
5919                  !!!next-token;                  !!!next-token;
5920                  redo B;                  next B;
5921                } else {                } else {
5922                    !!!cp ('t265');
5923                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5924                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5925                  !!!next-token;                  !!!next-token;
5926                  redo B;                              next B;            
5927                }                }
5928              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5929                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5930                  !!!parse-error (type => 'unmatched end tag',
5931                                  text => 'col', token => $token);
5932                ## Ignore the token                ## Ignore the token
5933                !!!next-token;                !!!next-token;
5934                redo B;                next B;
5935              } else {              } else {
5936                  !!!cp ('t267');
5937                #                #
5938              }              }
5939            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5940              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5941            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5942              !!!cp ('t270.2');
5943              ## Stop parsing.
5944              last B;
5945            } else {
5946              ## NOTE: As if </colgroup>.
5947              !!!cp ('t270.1');
5948              pop @{$self->{open_elements}}; # colgroup
5949              $self->{insertion_mode} = IN_TABLE_IM;
5950              ## Reprocess.
5951              next B;
5952            }
5953          } else {
5954            die "$0: $token->{type}: Unknown token type";
5955          }
5956    
5957            ## As if </colgroup>            ## As if </colgroup>
5958            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5959              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5960    ## TODO: Wrong error type?
5961                !!!parse-error (type => 'unmatched end tag',
5962                                text => 'colgroup', token => $token);
5963              ## Ignore the token              ## Ignore the token
5964                !!!nack ('t269.1');
5965              !!!next-token;              !!!next-token;
5966              redo B;              next B;
5967            } else {            } else {
5968                !!!cp ('t270');
5969              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5970              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5971                !!!ack-later;
5972              ## reprocess              ## reprocess
5973              redo B;              next B;
5974            }            }
5975      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5976        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5977            !!!cp ('t271');
5978          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5979          !!!next-token;          !!!next-token;
5980          redo B;          next B;
5981        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5982              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5983                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5984                  ## As if </option>              !!!cp ('t272');
5985                  pop @{$self->{open_elements}};              ## As if </option>
5986                }              pop @{$self->{open_elements}};
5987              } else {
5988                !!!cp ('t273');
5989              }
5990    
5991                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5992                !!!next-token;            !!!nack ('t273.1');
5993                redo B;            !!!next-token;
5994              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5995                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5996                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5997                  pop @{$self->{open_elements}};              !!!cp ('t274');
5998                }              ## As if </option>
5999                pop @{$self->{open_elements}};
6000              } else {
6001                !!!cp ('t275');
6002              }
6003    
6004                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6005                  ## As if </optgroup>              !!!cp ('t276');
6006                  pop @{$self->{open_elements}};              ## As if </optgroup>
6007                }              pop @{$self->{open_elements}};
6008              } else {
6009                !!!cp ('t277');
6010              }
6011    
6012                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6013                !!!next-token;            !!!nack ('t277.1');
6014                redo B;            !!!next-token;
6015              } elsif ($token->{tag_name} eq 'select') {            next B;
6016                !!!parse-error (type => 'not closed:select');          } elsif ({
6017                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6018                ## have an element in table scope                   }->{$token->{tag_name}} or
6019                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6020                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6021                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6022                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6023                    $i = $_;                     tr => 1, td => 1, th => 1,
6024                    last INSCOPE;                    }->{$token->{tag_name}})) {
6025                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6026                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6027                           }->{$node->[1]}) {                            token => $token);
6028                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6029                  }            ## as if there were </select> (otherwise).
6030                } # INSCOPE            ## have an element in table scope
6031                unless (defined $i) {            my $i;
6032                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6033                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6034                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6035                  redo B;                !!!cp ('t278');
6036                }                $i = $_;
6037                  last INSCOPE;
6038                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6039                  !!!cp ('t279');
6040                  last INSCOPE;
6041                }
6042              } # INSCOPE
6043              unless (defined $i) {
6044                !!!cp ('t280');
6045                !!!parse-error (type => 'unmatched end tag',
6046                                text => 'select', token => $token);
6047                ## Ignore the token
6048                !!!nack ('t280.1');
6049                !!!next-token;
6050                next B;
6051              }
6052                                
6053                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6054              splice @{$self->{open_elements}}, $i;
6055    
6056                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6057    
6058                !!!next-token;            if ($token->{tag_name} eq 'select') {
6059                redo B;              !!!nack ('t281.2');
6060                !!!next-token;
6061                next B;
6062              } else {
6063                !!!cp ('t281.1');
6064                !!!ack-later;
6065                ## Reprocess the token.
6066                next B;
6067              }
6068          } else {          } else {
6069            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
6070              !!!parse-error (type => 'in select',
6071                              text => $token->{tag_name}, token => $token);
6072            ## Ignore the token            ## Ignore the token
6073              !!!nack ('t282.1');
6074            !!!next-token;            !!!next-token;
6075            redo B;            next B;
6076          }          }
6077        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6078              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6079                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6080                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6081                  ## As if </option>              !!!cp ('t283');
6082                  splice @{$self->{open_elements}}, -2;              ## As if </option>
6083                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
6084                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6085                } else {              !!!cp ('t284');
6086                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
6087                  ## Ignore the token            } else {
6088                }              !!!cp ('t285');
6089                !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6090                redo B;                              text => $token->{tag_name}, token => $token);
6091              } elsif ($token->{tag_name} eq 'option') {              ## Ignore the token
6092                if ($self->{open_elements}->[-1]->[1] eq 'option') {            }
6093                  pop @{$self->{open_elements}};            !!!nack ('t285.1');
6094                } else {            !!!next-token;
6095                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            next B;
6096                  ## Ignore the token          } elsif ($token->{tag_name} eq 'option') {
6097                }            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6098                !!!next-token;              !!!cp ('t286');
6099                redo B;              pop @{$self->{open_elements}};
6100              } elsif ($token->{tag_name} eq 'select') {            } else {
6101                ## have an element in table scope              !!!cp ('t287');
6102                my $i;              !!!parse-error (type => 'unmatched end tag',
6103                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                              text => $token->{tag_name}, token => $token);
6104                  my $node = $self->{open_elements}->[$_];              ## Ignore the token
6105                  if ($node->[1] eq $token->{tag_name}) {            }
6106                    $i = $_;            !!!nack ('t287.1');
6107                    last INSCOPE;            !!!next-token;
6108                  } elsif ({            next B;
6109                            table => 1, html => 1,          } elsif ($token->{tag_name} eq 'select') {
6110                           }->{$node->[1]}) {            ## have an element in table scope
6111                    last INSCOPE;            my $i;
6112                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6113                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6114                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6115                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t288');
6116                  ## Ignore the token                $i = $_;
6117                  !!!next-token;                last INSCOPE;
6118                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6119                }                !!!cp ('t289');
6120                  last INSCOPE;
6121                }
6122              } # INSCOPE
6123              unless (defined $i) {
6124                !!!cp ('t290');
6125                !!!parse-error (type => 'unmatched end tag',
6126                                text => $token->{tag_name}, token => $token);
6127                ## Ignore the token
6128                !!!nack ('t290.1');
6129                !!!next-token;
6130                next B;
6131              }
6132                                
6133                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6134              splice @{$self->{open_elements}}, $i;
6135    
6136                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6137    
6138                !!!next-token;            !!!nack ('t291.1');
6139                redo B;            !!!next-token;
6140              } elsif ({            next B;
6141                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6142                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6143                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6144                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6145                     }->{$token->{tag_name}}) {
6146    ## TODO: The following is wrong?
6147              !!!parse-error (type => 'unmatched end tag',
6148                              text => $token->{tag_name}, token => $token);
6149                                
6150                ## have an element in table scope            ## have an element in table scope
6151                my $i;            my $i;
6152                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6153                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6154                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6155                    $i = $_;                !!!cp ('t292');
6156                    last INSCOPE;                $i = $_;
6157                  } elsif ({                last INSCOPE;
6158                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6159                           }->{$node->[1]}) {                !!!cp ('t293');
6160                    last INSCOPE;                last INSCOPE;
6161                  }              }
6162                } # INSCOPE            } # INSCOPE
6163                unless (defined $i) {            unless (defined $i) {
6164                  ## Ignore the token              !!!cp ('t294');
6165                  !!!next-token;              ## Ignore the token
6166                  redo B;              !!!nack ('t294.1');
6167                }              !!!next-token;
6168                next B;
6169              }
6170                                
6171                ## As if </select>            ## As if </select>
6172                ## have an element in table scope            ## have an element in table scope
6173                undef $i;            undef $i;
6174                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6175                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6176                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6177                    $i = $_;                !!!cp ('t295');
6178                    last INSCOPE;                $i = $_;
6179                  } elsif ({                last INSCOPE;
6180                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6181                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
6182                    last INSCOPE;                !!!cp ('t296');
6183                  }                last INSCOPE;
6184                } # INSCOPE              }
6185                unless (defined $i) {            } # INSCOPE
6186                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
6187                  ## Ignore the </select> token              !!!cp ('t297');
6188                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
6189                  redo B;              !!!parse-error (type => 'unmatched end tag',
6190                }                              text => 'select', token => $token);
6191                ## Ignore the </select> token
6192                !!!nack ('t297.1');
6193                !!!next-token; ## TODO: ok?
6194                next B;
6195              }
6196                                
6197                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
6198              splice @{$self->{open_elements}}, $i;
6199    
6200                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6201    
6202                ## reprocess            !!!ack-later;
6203                redo B;            ## reprocess
6204              next B;
6205          } else {          } else {
6206            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
6207              !!!parse-error (type => 'in select:/',
6208                              text => $token->{tag_name}, token => $token);
6209            ## Ignore the token            ## Ignore the token
6210              !!!nack ('t299.3');
6211            !!!next-token;            !!!next-token;
6212            redo B;            next B;
6213            }
6214          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6215            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6216                    @{$self->{open_elements}} == 1) { # redundant, maybe
6217              !!!cp ('t299.1');
6218              !!!parse-error (type => 'in body:#eof', token => $token);
6219            } else {
6220              !!!cp ('t299.2');
6221          }          }
6222    
6223            ## Stop parsing.
6224            last B;
6225        } else {        } else {
6226          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6227        }        }
# Line 4175  sub _tree_construction_main ($) { Line 6235  sub _tree_construction_main ($) {
6235            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6236                        
6237            unless (length $token->{data}) {            unless (length $token->{data}) {
6238                !!!cp ('t300');
6239              !!!next-token;              !!!next-token;
6240              redo B;              next B;
6241            }            }
6242          }          }
6243                    
6244          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6245            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6246              !!!parse-error (type => 'after html:#text', token => $token);
6247    
6248            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6249            } else {
6250              !!!cp ('t302');
6251          }          }
6252                    
6253          ## "after body" insertion mode          ## "after body" insertion mode
6254          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6255    
6256          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6257          ## reprocess          ## reprocess
6258          redo B;          next B;
6259        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6260          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6261            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6262              !!!parse-error (type => 'after html',
6263                              text => $token->{tag_name}, token => $token);
6264                        
6265            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6266            } else {
6267              !!!cp ('t304');
6268          }          }
6269    
6270          ## "after body" insertion mode          ## "after body" insertion mode
6271          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6272                            text => $token->{tag_name}, token => $token);
6273    
6274          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6275            !!!ack-later;
6276          ## reprocess          ## reprocess
6277          redo B;          next B;
6278        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6279          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6280            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6281              !!!parse-error (type => 'after html:/',
6282                              text => $token->{tag_name}, token => $token);
6283                        
6284            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6285            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6286            } else {
6287              !!!cp ('t306');
6288          }          }
6289    
6290          ## "after body" insertion mode          ## "after body" insertion mode
6291          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6292            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6293              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6294                !!!parse-error (type => 'unmatched end tag',
6295                                text => 'html', token => $token);
6296              ## Ignore the token              ## Ignore the token
6297              !!!next-token;              !!!next-token;
6298              redo B;              next B;
6299            } else {            } else {
6300                !!!cp ('t308');
6301              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6302              !!!next-token;              !!!next-token;
6303              redo B;              next B;
6304            }            }
6305          } else {          } else {
6306            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6307              !!!parse-error (type => 'after body:/',
6308                              text => $token->{tag_name}, token => $token);
6309    
6310            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6311            ## reprocess            ## reprocess
6312            redo B;            next B;
6313          }          }
6314          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6315            !!!cp ('t309.2');
6316            ## Stop parsing
6317            last B;
6318        } else {        } else {
6319          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6320        }        }
# Line 4241  sub _tree_construction_main ($) { Line 6324  sub _tree_construction_main ($) {
6324            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6325                        
6326            unless (length $token->{data}) {            unless (length $token->{data}) {
6327                !!!cp ('t310');
6328              !!!next-token;              !!!next-token;
6329              redo B;              next B;
6330            }            }
6331          }          }
6332                    
6333          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6334            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6335              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6336                !!!parse-error (type => 'in frameset:#text', token => $token);
6337            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6338              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6339            } else { # "after html frameset"              !!!parse-error (type => 'after frameset:#text', token => $token);
6340              !!!parse-error (type => 'after html:#character');            } else { # "after after frameset"
6341                !!!cp ('t313');
6342              $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');  
6343            }            }
6344                        
6345            ## Ignore the token.            ## Ignore the token.
6346            if (length $token->{data}) {            if (length $token->{data}) {
6347                !!!cp ('t314');
6348              ## reprocess the rest of characters              ## reprocess the rest of characters
6349            } else {            } else {
6350                !!!cp ('t315');
6351              !!!next-token;              !!!next-token;
6352            }            }
6353            redo B;            next B;
6354          }          }
6355                    
6356          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6357        } 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...  
         }  
   
6358          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6359              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6360            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6361              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6362              !!!nack ('t318.1');
6363            !!!next-token;            !!!next-token;
6364            redo B;            next B;
6365          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6366                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6367            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6368              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6369            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6370              !!!ack ('t319.1');
6371            !!!next-token;            !!!next-token;
6372            redo B;            next B;
6373          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6374            ## NOTE: As if in body.            !!!cp ('t320');
6375            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6376            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6377              next B;
6378    
6379              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6380              ## has no parse error.
6381          } else {          } else {
6382            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6383              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6384            } else {              !!!parse-error (type => 'in frameset',
6385              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6386              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6387                !!!cp ('t322');
6388                !!!parse-error (type => 'after frameset',
6389                                text => $token->{tag_name}, token => $token);
6390              } else { # "after after frameset"
6391                !!!cp ('t322.2');
6392                !!!parse-error (type => 'after after frameset',
6393                                text => $token->{tag_name}, token => $token);
6394            }            }
6395            ## Ignore the token            ## Ignore the token
6396              !!!nack ('t322.1');
6397            !!!next-token;            !!!next-token;
6398            redo B;            next B;
6399          }          }
6400        } 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...  
         }  
   
6401          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6402              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6403            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6404                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6405              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6406                !!!parse-error (type => 'unmatched end tag',
6407                                text => $token->{tag_name}, token => $token);
6408              ## Ignore the token              ## Ignore the token
6409              !!!next-token;              !!!next-token;
6410            } else {            } else {
6411                !!!cp ('t326');
6412              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6413              !!!next-token;              !!!next-token;
6414            }            }
6415    
6416            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6417                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6418                !!!cp ('t327');
6419              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6420              } else {
6421                !!!cp ('t328');
6422            }            }
6423            redo B;            next B;
6424          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6425                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6426              !!!cp ('t329');
6427            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6428            !!!next-token;            !!!next-token;
6429            redo B;            next B;
6430          } else {          } else {
6431            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6432              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6433            } else {              !!!parse-error (type => 'in frameset:/',
6434              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6435              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6436                !!!cp ('t330.1');
6437                !!!parse-error (type => 'after frameset:/',
6438                                text => $token->{tag_name}, token => $token);
6439              } else { # "after after html"
6440                !!!cp ('t331');
6441                !!!parse-error (type => 'after after frameset:/',
6442                                text => $token->{tag_name}, token => $token);
6443            }            }
6444            ## Ignore the token            ## Ignore the token
6445            !!!next-token;            !!!next-token;
6446            redo B;            next B;
6447          }          }
6448          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6449            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6450                    @{$self->{open_elements}} == 1) { # redundant, maybe
6451              !!!cp ('t331.1');
6452              !!!parse-error (type => 'in body:#eof', token => $token);
6453            } else {
6454              !!!cp ('t331.2');
6455            }
6456            
6457            ## Stop parsing
6458            last B;
6459        } else {        } else {
6460          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6461        }        }
# Line 4354  sub _tree_construction_main ($) { Line 6468  sub _tree_construction_main ($) {
6468      ## "in body" insertion mode      ## "in body" insertion mode
6469      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6470        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6471            !!!cp ('t332');
6472          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6473          $script_start_tag->($insert);          $script_start_tag->();
6474          redo B;          next B;
6475        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6476            !!!cp ('t333');
6477          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6478          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6479          redo B;          next B;
6480        } elsif ({        } elsif ({
6481                  base => 1, link => 1,                  base => 1, link => 1,
6482                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6483            !!!cp ('t334');
6484          ## 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
6485          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6486          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6487            !!!ack ('t334.1');
6488          !!!next-token;          !!!next-token;
6489          redo B;          next B;
6490        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6491          ## 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
6492          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6493          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.
6494    
6495          unless ($self->{confident}) {          unless ($self->{confident}) {
6496            my $charset;            if ($token->{attributes}->{charset}) {
6497            if ($token->{attributes}->{charset}) { ## TODO: And if supported              !!!cp ('t335');
6498              $charset = $token->{attributes}->{charset}->{value};              ## NOTE: Whether the encoding is supported or not is handled
6499            }              ## in the {change_encoding} callback.
6500            if ($token->{attributes}->{'http-equiv'}) {              $self->{change_encoding}
6501              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6502              if ($token->{attributes}->{'http-equiv'}->{value}              
6503                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6504                    ->set_user_data (manakai_has_reference =>
6505                                         $token->{attributes}->{charset}
6506                                             ->{has_reference});
6507              } elsif ($token->{attributes}->{content}) {
6508                if ($token->{attributes}->{content}->{value}
6509                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6510                        [\x09-\x0D\x20]*=
6511                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6512                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6513                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
6514              } ## TODO: And if supported                ## NOTE: Whether the encoding is supported or not is handled
6515                  ## in the {change_encoding} callback.
6516                  $self->{change_encoding}
6517                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6518                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6519                      ->set_user_data (manakai_has_reference =>
6520                                           $token->{attributes}->{content}
6521                                                 ->{has_reference});
6522                }
6523              }
6524            } else {
6525              if ($token->{attributes}->{charset}) {
6526                !!!cp ('t337');
6527                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6528                    ->set_user_data (manakai_has_reference =>
6529                                         $token->{attributes}->{charset}
6530                                             ->{has_reference});
6531              }
6532              if ($token->{attributes}->{content}) {
6533                !!!cp ('t338');
6534                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6535                    ->set_user_data (manakai_has_reference =>
6536                                         $token->{attributes}->{content}
6537                                             ->{has_reference});
6538            }            }
           ## TODO: Change the encoding  
6539          }          }
6540    
6541            !!!ack ('t338.1');
6542          !!!next-token;          !!!next-token;
6543          redo B;          next B;
6544        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6545          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6546          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6547          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6548            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6549        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6550          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6551                                
6552          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6553              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6554              !!!cp ('t342');
6555            ## Ignore the token            ## Ignore the token
6556          } else {          } else {
6557            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6558            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6559              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6560                  !!!cp ('t343');
6561                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6562                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6563                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6564              }              }
6565            }            }
6566          }          }
6567            !!!nack ('t343.1');
6568          !!!next-token;          !!!next-token;
6569          redo B;          next B;
6570        } elsif ({        } elsif ({
6571                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6572                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6573                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6574                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6575                  pre => 1,                  pre => 1, listing => 1,
6576                    form => 1,
6577                    table => 1,
6578                    hr => 1,
6579                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6580            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6581              !!!cp ('t350');
6582              !!!parse-error (type => 'in form:form', token => $token);
6583              ## Ignore the token
6584              !!!nack ('t350.1');
6585              !!!next-token;
6586              next B;
6587            }
6588    
6589          ## has a p element in scope          ## has a p element in scope
6590          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6591            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6592              !!!back-token;              !!!cp ('t344');
6593              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6594              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6595            } elsif ({                        line => $token->{line}, column => $token->{column}};
6596                      table => 1, caption => 1, td => 1, th => 1,              next B;
6597                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6598                     }->{$_->[1]}) {              !!!cp ('t345');
6599              last INSCOPE;              last INSCOPE;
6600            }            }
6601          } # INSCOPE          } # INSCOPE
6602                        
6603          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6604          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6605              !!!nack ('t346.1');
6606            !!!next-token;            !!!next-token;
6607            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6608              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6609              unless (length $token->{data}) {              unless (length $token->{data}) {
6610                  !!!cp ('t346');
6611                !!!next-token;                !!!next-token;
6612                } else {
6613                  !!!cp ('t349');
6614              }              }
6615              } else {
6616                !!!cp ('t348');
6617            }            }
6618          } else {          } elsif ($token->{tag_name} eq 'form') {
6619              !!!cp ('t347.1');
6620              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6621    
6622              !!!nack ('t347.2');
6623            !!!next-token;            !!!next-token;
6624          }          } elsif ($token->{tag_name} eq 'table') {
6625          redo B;            !!!cp ('t382');
6626        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6627          if (defined $self->{form_element}) {            
6628            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6629            ## Ignore the token  
6630              !!!nack ('t382.1');
6631              !!!next-token;
6632            } elsif ($token->{tag_name} eq 'hr') {
6633              !!!cp ('t386');
6634              pop @{$self->{open_elements}};
6635            
6636              !!!nack ('t386.1');
6637            !!!next-token;            !!!next-token;
           redo B;  
6638          } else {          } else {
6639            ## 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];  
6640            !!!next-token;            !!!next-token;
           redo B;  
6641          }          }
6642        } elsif ($token->{tag_name} eq 'li') {          next B;
6643          ## 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') {  
6644          ## has a p element in scope          ## has a p element in scope
6645          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6646            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6647              !!!back-token;              !!!cp ('t353');
6648              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6649              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6650            } elsif ({                        line => $token->{line}, column => $token->{column}};
6651                      table => 1, caption => 1, td => 1, th => 1,              next B;
6652                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6653                     }->{$_->[1]}) {              !!!cp ('t354');
6654              last INSCOPE;              last INSCOPE;
6655            }            }
6656          } # INSCOPE          } # INSCOPE
# Line 4546  sub _tree_construction_main ($) { Line 6658  sub _tree_construction_main ($) {
6658          ## Step 1          ## Step 1
6659          my $i = -1;          my $i = -1;
6660          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6661            my $li_or_dtdd = {li => {li => 1},
6662                              dt => {dt => 1, dd => 1},
6663                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6664          LI: {          LI: {
6665            ## Step 2            ## Step 2
6666            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6667              if ($i != -1) {              if ($i != -1) {
6668                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6669                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6670                                  text => $self->{open_elements}->[-1]->[0]
6671                                      ->manakai_local_name,
6672                                  token => $token);
6673                } else {
6674                  !!!cp ('t356');
6675              }              }
6676              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6677              last LI;              last LI;
6678              } else {
6679                !!!cp ('t357');
6680            }            }
6681                        
6682            ## Step 3            ## Step 3
6683            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6684                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6685                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6686                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6687                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6688                  not ($node->[1] & DIV_EL)) {
6689                !!!cp ('t358');
6690              last LI;              last LI;
6691            }            }
6692                        
6693              !!!cp ('t359');
6694            ## Step 4            ## Step 4
6695            $i--;            $i--;
6696            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6697            redo LI;            redo LI;
6698          } # LI          } # LI
6699                        
6700          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6701            !!!nack ('t359.1');
6702          !!!next-token;          !!!next-token;
6703          redo B;          next B;
6704        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6705          ## has a p element in scope          ## has a p element in scope
6706          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6707            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6708              !!!back-token;              !!!cp ('t367');
6709              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6710              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6711            } elsif ({                        line => $token->{line}, column => $token->{column}};
6712                      table => 1, caption => 1, td => 1, th => 1,              next B;
6713                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6714                     }->{$_->[1]}) {              !!!cp ('t368');
6715              last INSCOPE;              last INSCOPE;
6716            }            }
6717          } # INSCOPE          } # INSCOPE
6718                        
6719          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6720                        
6721          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6722                        
6723            !!!nack ('t368.1');
6724          !!!next-token;          !!!next-token;
6725          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;  
6726        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6727          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6728            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6729            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6730              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6731                !!!parse-error (type => 'in a:a', token => $token);
6732                            
6733              !!!back-token;              !!!back-token; # <a>
6734              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6735              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6736                $formatting_end_tag->($token);
6737                            
6738              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6739                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6740                    !!!cp ('t372');
6741                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6742                  last AFE2;                  last AFE2;
6743                }                }
6744              } # AFE2              } # AFE2
6745              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6746                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6747                    !!!cp ('t373');
6748                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6749                  last OE;                  last OE;
6750                }                }
6751              } # OE              } # OE
6752              last AFE;              last AFE;
6753            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6754                !!!cp ('t374');
6755              last AFE;              last AFE;
6756            }            }
6757          } # AFE          } # AFE
6758                        
6759          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6760    
6761          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6762          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6763    
6764            !!!nack ('t374.1');
6765          !!!next-token;          !!!next-token;
6766          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;  
6767        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6768          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6769    
6770          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6771          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6772            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6773            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6774              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
6775              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6776              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6777              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6778            } elsif ({                        line => $token->{line}, column => $token->{column}};
6779                      table => 1, caption => 1, td => 1, th => 1,              next B;
6780                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6781                     }->{$node->[1]}) {              !!!cp ('t377');
6782              last INSCOPE;              last INSCOPE;
6783            }            }
6784          } # INSCOPE          } # INSCOPE
6785                    
6786          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6787          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6788                    
6789            !!!nack ('t377.1');
6790          !!!next-token;          !!!next-token;
6791          redo B;          next B;
6792        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6793          ## has a button element in scope          ## has a button element in scope
6794          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6795            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6796            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6797              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6798              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6799              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6800              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6801            } elsif ({                        line => $token->{line}, column => $token->{column}};
6802                      table => 1, caption => 1, td => 1, th => 1,              next B;
6803                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6804                     }->{$node->[1]}) {              !!!cp ('t379');
6805              last INSCOPE;              last INSCOPE;
6806            }            }
6807          } # INSCOPE          } # INSCOPE
6808                        
6809          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6810                        
6811          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6812          push @$active_formatting_elements, ['#marker', ''];  
6813            ## TODO: associate with $self->{form_element} if defined
6814    
         !!!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});  
6815          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6816            
6817          !!!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;  
             
6818          !!!next-token;          !!!next-token;
6819          redo B;          next B;
6820        } elsif ({        } elsif ({
6821                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6822                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6823                  image => 1,                  noembed => 1,
6824                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6825                    noscript => 0, ## TODO: 1 if scripting is enabled
6826                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6827          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6828            !!!parse-error (type => 'image');            !!!cp ('t381');
6829            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6830            } else {
6831              !!!cp ('t399');
6832          }          }
6833            ## NOTE: There is an "as if in body" code clone.
6834          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6835          $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;  
6836        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6837          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6838                    
6839          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6840              !!!cp ('t389');
6841            ## Ignore the token            ## Ignore the token
6842              !!!nack ('t389'); ## NOTE: Not acknowledged.
6843            !!!next-token;            !!!next-token;
6844            redo B;            next B;
6845          } else {          } else {
6846              !!!ack ('t391.1');
6847    
6848            my $at = $token->{attributes};            my $at = $token->{attributes};
6849            my $form_attrs;            my $form_attrs;
6850            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 4834  sub _tree_construction_main ($) { Line 6854  sub _tree_construction_main ($) {
6854            delete $at->{prompt};            delete $at->{prompt};
6855            my @tokens = (            my @tokens = (
6856                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6857                           attributes => $form_attrs},                           attributes => $form_attrs,
6858                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6859                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6860                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6861                            {type => START_TAG_TOKEN, tag_name => 'p',
6862                             line => $token->{line}, column => $token->{column}},
6863                            {type => START_TAG_TOKEN, tag_name => 'label',
6864                             line => $token->{line}, column => $token->{column}},
6865                         );                         );
6866            if ($prompt_attr) {            if ($prompt_attr) {
6867              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6868                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6869                               #line => $token->{line}, column => $token->{column},
6870                              };
6871            } else {            } else {
6872                !!!cp ('t391');
6873              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6874                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6875                               #line => $token->{line}, column => $token->{column},
6876                              }; # SHOULD
6877              ## TODO: make this configurable              ## TODO: make this configurable
6878            }            }
6879            push @tokens,            push @tokens,
6880                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6881                             line => $token->{line}, column => $token->{column}},
6882                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6883                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6884                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6885                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6886                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6887            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6888                             line => $token->{line}, column => $token->{column}},
6889                            {type => END_TAG_TOKEN, tag_name => 'form',
6890                             line => $token->{line}, column => $token->{column}};
6891            !!!back-token (@tokens);            !!!back-token (@tokens);
6892            redo B;            !!!next-token;
6893              next B;
6894          }          }
6895        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6896          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6897          my $el;          my $el;
6898          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6899                    
6900          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6901          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4869  sub _tree_construction_main ($) { Line 6904  sub _tree_construction_main ($) {
6904          $insert->($el);          $insert->($el);
6905                    
6906          my $text = '';          my $text = '';
6907            !!!nack ('t392.1');
6908          !!!next-token;          !!!next-token;
6909          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6910            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6911            unless (length $token->{data}) {            unless (length $token->{data}) {
6912                !!!cp ('t392');
6913              !!!next-token;              !!!next-token;
6914              } else {
6915                !!!cp ('t393');
6916            }            }
6917            } else {
6918              !!!cp ('t394');
6919          }          }
6920          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6921              !!!cp ('t395');
6922            $text .= $token->{data};            $text .= $token->{data};
6923            !!!next-token;            !!!next-token;
6924          }          }
6925          if (length $text) {          if (length $text) {
6926              !!!cp ('t396');
6927            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6928          }          }
6929                    
# Line 4888  sub _tree_construction_main ($) { Line 6931  sub _tree_construction_main ($) {
6931                    
6932          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6933              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6934              !!!cp ('t397');
6935            ## Ignore the token            ## Ignore the token
6936          } else {          } else {
6937            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6938              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6939          }          }
6940          !!!next-token;          !!!next-token;
6941            next B;
6942          } elsif ($token->{tag_name} eq 'rt' or
6943                   $token->{tag_name} eq 'rp') {
6944            ## has a |ruby| element in scope
6945            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6946              my $node = $self->{open_elements}->[$_];
6947              if ($node->[1] & RUBY_EL) {
6948                !!!cp ('t398.1');
6949                ## generate implied end tags
6950                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6951                  !!!cp ('t398.2');
6952                  pop @{$self->{open_elements}};
6953                }
6954                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6955                  !!!cp ('t398.3');
6956                  !!!parse-error (type => 'not closed',
6957                                  text => $self->{open_elements}->[-1]->[0]
6958                                      ->manakai_local_name,
6959                                  token => $token);
6960                  pop @{$self->{open_elements}}
6961                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6962                }
6963                last INSCOPE;
6964              } elsif ($node->[1] & SCOPING_EL) {
6965                !!!cp ('t398.4');
6966                last INSCOPE;
6967              }
6968            } # INSCOPE
6969    
6970            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6971    
6972            !!!nack ('t398.5');
6973            !!!next-token;
6974          redo B;          redo B;
6975        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6976                  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') {  
6977          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6978    
6979            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6980    
6981            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6982    
6983            ## "adjust foreign attributes" - done in insert-element-f
6984                    
6985          !!!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);
6986                    
6987          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6988              pop @{$self->{open_elements}};
6989              !!!ack ('t398.1');
6990            } else {
6991              !!!cp ('t398.2');
6992              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6993              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6994              ## mode, "in body" (not "in foreign content") secondary insertion
6995              ## mode, maybe.
6996            }
6997    
6998          !!!next-token;          !!!next-token;
6999          redo B;          next B;
7000        } elsif ({        } elsif ({
7001                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7002                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
7003                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
7004                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7005                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7006          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
7007            !!!parse-error (type => 'in body',
7008                            text => $token->{tag_name}, token => $token);
7009          ## Ignore the token          ## Ignore the token
7010            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7011          !!!next-token;          !!!next-token;
7012          redo B;          next B;
7013                    
7014          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7015        } else {        } else {
7016            if ($token->{tag_name} eq 'image') {
7017              !!!cp ('t384');
7018              !!!parse-error (type => 'image', token => $token);
7019              $token->{tag_name} = 'img';
7020            } else {
7021              !!!cp ('t385');
7022            }
7023    
7024            ## NOTE: There is an "as if <br>" code clone.
7025          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7026                    
7027          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7028    
7029            if ({
7030                 applet => 1, marquee => 1, object => 1,
7031                }->{$token->{tag_name}}) {
7032              !!!cp ('t380');
7033              push @$active_formatting_elements, ['#marker', ''];
7034              !!!nack ('t380.1');
7035            } elsif ({
7036                      b => 1, big => 1, em => 1, font => 1, i => 1,
7037                      s => 1, small => 1, strile => 1,
7038                      strong => 1, tt => 1, u => 1,
7039                     }->{$token->{tag_name}}) {
7040              !!!cp ('t375');
7041              push @$active_formatting_elements, $self->{open_elements}->[-1];
7042              !!!nack ('t375.1');
7043            } elsif ($token->{tag_name} eq 'input') {
7044              !!!cp ('t388');
7045              ## TODO: associate with $self->{form_element} if defined
7046              pop @{$self->{open_elements}};
7047              !!!ack ('t388.2');
7048            } elsif ({
7049                      area => 1, basefont => 1, bgsound => 1, br => 1,
7050                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7051                      #image => 1,
7052                     }->{$token->{tag_name}}) {
7053              !!!cp ('t388.1');
7054              pop @{$self->{open_elements}};
7055              !!!ack ('t388.3');
7056            } elsif ($token->{tag_name} eq 'select') {
7057              ## TODO: associate with $self->{form_element} if defined
7058            
7059              if ($self->{insertion_mode} & TABLE_IMS or
7060                  $self->{insertion_mode} & BODY_TABLE_IMS or
7061                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7062                !!!cp ('t400.1');
7063                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7064              } else {
7065                !!!cp ('t400.2');
7066                $self->{insertion_mode} = IN_SELECT_IM;
7067              }
7068              !!!nack ('t400.3');
7069            } else {
7070              !!!nack ('t402');
7071            }
7072                    
7073          !!!next-token;          !!!next-token;
7074          redo B;          next B;
7075        }        }
7076      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7077        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7078          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7079              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7080            for (@{$self->{open_elements}}) {          INSCOPE: {
7081              unless ({            for (reverse @{$self->{open_elements}}) {
7082                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7083                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7084                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7085                      }->{$_->[1]}) {                last INSCOPE;
7086                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
7087                  !!!cp ('t405.1');
7088                  last;
7089              }              }
7090            }            }
7091    
7092            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7093            !!!next-token;                            text => $token->{tag_name}, token => $token);
7094            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
7095            !!!next-token;            !!!next-token;
7096            redo B;            next B;
7097            } # INSCOPE
7098    
7099            for (@{$self->{open_elements}}) {
7100              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7101                !!!cp ('t403');
7102                !!!parse-error (type => 'not closed',
7103                                text => $_->[0]->manakai_local_name,
7104                                token => $token);
7105                last;
7106              } else {
7107                !!!cp ('t404');
7108              }
7109          }          }
7110    
7111            $self->{insertion_mode} = AFTER_BODY_IM;
7112            !!!next-token;
7113            next B;
7114        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7115          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
7116            ## up-to-date, though it has same effect as speced.
7117            if (@{$self->{open_elements}} > 1 and
7118                $self->{open_elements}->[1]->[1] & BODY_EL) {
7119            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7120            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7121              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
7122                !!!parse-error (type => 'not closed',
7123                                text => $self->{open_elements}->[1]->[0]
7124                                    ->manakai_local_name,
7125                                token => $token);
7126              } else {
7127                !!!cp ('t407');
7128            }            }
7129            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7130            ## reprocess            ## reprocess
7131            redo B;            next B;
7132          } else {          } else {
7133            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
7134              !!!parse-error (type => 'unmatched end tag',
7135                              text => $token->{tag_name}, token => $token);
7136            ## Ignore the token            ## Ignore the token
7137            !!!next-token;            !!!next-token;
7138            redo B;            next B;
7139          }          }
7140        } elsif ({        } elsif ({
7141                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7142                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7143                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7144                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7145                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7146                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7147          ## has an element in scope          ## has an element in scope
7148          my $i;          my $i;
7149          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7150            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7151            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7152              ## 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;  
             }  
7153              $i = $_;              $i = $_;
7154              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7155            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7156                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7157              last INSCOPE;              last INSCOPE;
7158            }            }
7159          } # INSCOPE          } # INSCOPE
7160            
7161          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7162            if (defined $i) {            !!!cp ('t413');
7163              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag',
7164                              text => $token->{tag_name}, token => $token);
7165              ## NOTE: Ignore the token.
7166            } else {
7167              ## Step 1. generate implied end tags
7168              while ({
7169                      ## END_TAG_OPTIONAL_EL
7170                      dd => ($token->{tag_name} ne 'dd'),
7171                      dt => ($token->{tag_name} ne 'dt'),
7172                      li => ($token->{tag_name} ne 'li'),
7173                      p => 1,
7174                      rt => 1,
7175                      rp => 1,
7176                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7177                !!!cp ('t409');
7178                pop @{$self->{open_elements}};
7179              }
7180    
7181              ## Step 2.
7182              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7183                      ne $token->{tag_name}) {
7184                !!!cp ('t412');
7185                !!!parse-error (type => 'not closed',
7186                                text => $self->{open_elements}->[-1]->[0]
7187                                    ->manakai_local_name,
7188                                token => $token);
7189            } else {            } else {
7190              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
7191            }            }
7192          }  
7193                      ## Step 3.
         if (defined $i) {  
7194            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7195          } elsif ($token->{tag_name} eq 'p') {  
7196            ## As if <p>, then reprocess the current token            ## Step 4.
7197            my $el;            $clear_up_to_marker->()
7198            !!!create-element ($el, 'p');                if {
7199            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
7200                  }->{$token->{tag_name}};
7201          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7202          !!!next-token;          !!!next-token;
7203          redo B;          next B;
7204        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7205            undef $self->{form_element};
7206    
7207          ## has an element in scope          ## has an element in scope
7208            my $i;
7209          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7210            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7211            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7212              ## generate implied end tags              !!!cp ('t418');
7213              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;  
             }  
7214              last INSCOPE;              last INSCOPE;
7215            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7216                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7217              last INSCOPE;              last INSCOPE;
7218            }            }
7219          } # INSCOPE          } # INSCOPE
7220            
7221          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7222            pop @{$self->{open_elements}};            !!!cp ('t421');
7223          } else {            !!!parse-error (type => 'unmatched end tag',
7224            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                            text => $token->{tag_name}, token => $token);
7225              ## NOTE: Ignore the token.
7226            } else {
7227              ## Step 1. generate implied end tags
7228              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7229                !!!cp ('t417');
7230                pop @{$self->{open_elements}};
7231              }
7232              
7233              ## Step 2.
7234              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7235                      ne $token->{tag_name}) {
7236                !!!cp ('t417.1');
7237                !!!parse-error (type => 'not closed',
7238                                text => $self->{open_elements}->[-1]->[0]
7239                                    ->manakai_local_name,
7240                                token => $token);
7241              } else {
7242                !!!cp ('t420');
7243              }  
7244              
7245              ## Step 3.
7246              splice @{$self->{open_elements}}, $i;
7247          }          }
7248    
         undef $self->{form_element};  
7249          !!!next-token;          !!!next-token;
7250          redo B;          next B;
7251        } elsif ({        } elsif ({
7252                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7253                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5069  sub _tree_construction_main ($) { Line 7255  sub _tree_construction_main ($) {
7255          my $i;          my $i;
7256          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7257            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7258            if ({            if ($node->[1] & HEADING_EL) {
7259                 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;  
             }  
7260              $i = $_;              $i = $_;
7261              last INSCOPE;              last INSCOPE;
7262            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7263                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7264              last INSCOPE;              last INSCOPE;
7265            }            }
7266          } # INSCOPE          } # INSCOPE
7267            
7268          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7269            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
7270              !!!parse-error (type => 'unmatched end tag',
7271                              text => $token->{tag_name}, token => $token);
7272              ## NOTE: Ignore the token.
7273            } else {
7274              ## Step 1. generate implied end tags
7275              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7276                !!!cp ('t422');
7277                pop @{$self->{open_elements}};
7278              }
7279              
7280              ## Step 2.
7281              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7282                      ne $token->{tag_name}) {
7283                !!!cp ('t425');
7284                !!!parse-error (type => 'unmatched end tag',
7285                                text => $token->{tag_name}, token => $token);
7286              } else {
7287                !!!cp ('t426');
7288              }
7289    
7290              ## Step 3.
7291              splice @{$self->{open_elements}}, $i;
7292          }          }
7293                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7294          !!!next-token;          !!!next-token;
7295          redo B;          next B;
7296          } elsif ($token->{tag_name} eq 'p') {
7297            ## has an element in scope
7298            my $i;
7299            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7300              my $node = $self->{open_elements}->[$_];
7301              if ($node->[1] & P_EL) {
7302                !!!cp ('t410.1');
7303                $i = $_;
7304                last INSCOPE;
7305              } elsif ($node->[1] & SCOPING_EL) {
7306                !!!cp ('t411.1');
7307                last INSCOPE;
7308              }
7309            } # INSCOPE
7310    
7311            if (defined $i) {
7312              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7313                      ne $token->{tag_name}) {
7314                !!!cp ('t412.1');
7315                !!!parse-error (type => 'not closed',
7316                                text => $self->{open_elements}->[-1]->[0]
7317                                    ->manakai_local_name,
7318                                token => $token);
7319              } else {
7320                !!!cp ('t414.1');
7321              }
7322    
7323              splice @{$self->{open_elements}}, $i;
7324            } else {
7325              !!!cp ('t413.1');
7326              !!!parse-error (type => 'unmatched end tag',
7327                              text => $token->{tag_name}, token => $token);
7328    
7329              !!!cp ('t415.1');
7330              ## As if <p>, then reprocess the current token
7331              my $el;
7332              !!!create-element ($el, $HTML_NS, 'p',, $token);
7333              $insert->($el);
7334              ## NOTE: Not inserted into |$self->{open_elements}|.
7335            }
7336    
7337            !!!next-token;
7338            next B;
7339        } elsif ({        } elsif ({
7340                  a => 1,                  a => 1,
7341                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7342                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7343                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7344                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7345          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7346          redo B;          $formatting_end_tag->($token);
7347            next B;
7348        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7349          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7350            !!!parse-error (type => 'unmatched end tag',
7351                            text => 'br', token => $token);
7352    
7353          ## As if <br>          ## As if <br>
7354          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7355                    
7356          my $el;          my $el;
7357          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7358          $insert->($el);          $insert->($el);
7359                    
7360          ## Ignore the token.          ## Ignore the token.
7361          !!!next-token;          !!!next-token;
7362          redo B;          next B;
7363        } elsif ({        } elsif ({
7364                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7365                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5133  sub _tree_construction_main ($) { Line 7372  sub _tree_construction_main ($) {
7372                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7373                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7374                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7375          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7376            !!!parse-error (type => 'unmatched end tag',
7377                            text => $token->{tag_name}, token => $token);
7378          ## Ignore the token          ## Ignore the token
7379          !!!next-token;          !!!next-token;
7380          redo B;          next B;
7381                    
7382          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7383                    
# Line 5147  sub _tree_construction_main ($) { Line 7388  sub _tree_construction_main ($) {
7388    
7389          ## Step 2          ## Step 2
7390          S2: {          S2: {
7391            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7392              ## Step 1              ## Step 1
7393              ## generate implied end tags              ## generate implied end tags
7394              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7395                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7396                   td => 1, th => 1, tr => 1,                ## NOTE: |<ruby><rt></ruby>|.
7397                   tbody => 1, tfoot => 1, thead => 1,                ## ISSUE: <ruby><rt></rt> will also take this code path,
7398                  }->{$self->{open_elements}->[-1]->[1]}) {                ## which seems wrong.
7399                !!!back-token;                pop @{$self->{open_elements}};
7400                $token = {type => END_TAG_TOKEN,                $node_i++;
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7401              }              }
7402                    
7403              ## Step 2              ## Step 2
7404              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7405                        ne $token->{tag_name}) {
7406                  !!!cp ('t431');
7407                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7408                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7409                                  text => $self->{open_elements}->[-1]->[0]
7410                                      ->manakai_local_name,
7411                                  token => $token);
7412                } else {
7413                  !!!cp ('t432');
7414              }              }
7415                            
7416              ## Step 3              ## Step 3
7417              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7418    
7419              !!!next-token;              !!!next-token;
7420              last S2;              last S2;
7421            } else {            } else {
7422              ## Step 3              ## Step 3
7423              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7424                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7425                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7426                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7427                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7428                  !!!parse-error (type => 'unmatched end tag',
7429                                  text => $token->{tag_name}, token => $token);
7430                ## Ignore the token                ## Ignore the token
7431                !!!next-token;                !!!next-token;
7432                last S2;                last S2;
7433              }              }
7434    
7435                !!!cp ('t434');
7436            }            }
7437                        
7438            ## Step 4            ## Step 4
# Line 5192  sub _tree_construction_main ($) { Line 7442  sub _tree_construction_main ($) {
7442            ## Step 5;            ## Step 5;
7443            redo S2;            redo S2;
7444          } # S2          } # S2
7445          redo B;          next B;
7446        }        }
7447      }      }
7448      redo B;      next B;
7449      } continue { # B
7450        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7451          ## NOTE: The code below is executed in cases where it does not have
7452          ## to be, but it it is harmless even in those cases.
7453          ## has an element in scope
7454          INSCOPE: {
7455            for (reverse 0..$#{$self->{open_elements}}) {
7456              my $node = $self->{open_elements}->[$_];
7457              if ($node->[1] & FOREIGN_EL) {
7458                last INSCOPE;
7459              } elsif ($node->[1] & SCOPING_EL) {
7460                last;
7461              }
7462            }
7463            
7464            ## NOTE: No foreign element in scope.
7465            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7466          } # INSCOPE
7467        }
7468    } # B    } # B
7469    
   ## 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  
   
7470    ## Stop parsing # MUST    ## Stop parsing # MUST
7471        
7472    ## TODO: script stuffs    ## TODO: script stuffs
7473  } # _tree_construct_main  } # _tree_construct_main
7474    
7475  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7476    my $class = shift;    my $class = shift;
7477    my $node = shift;    my $node = shift;
7478    my $s = \$_[0];    my $s = \$_[0];
7479    my $onerror = $_[1];    my $onerror = $_[1];
7480      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7481    
7482      ## ISSUE: Should {confident} be true?
7483    
7484    my $nt = $node->node_type;    my $nt = $node->node_type;
7485    if ($nt == 9) {    if ($nt == 9) {
# Line 5229  sub set_inner_html ($$$) { Line 7496  sub set_inner_html ($$$) {
7496      }      }
7497    
7498      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7499      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7500    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7501      ## TODO: If non-html element      ## TODO: If non-html element
7502    
7503      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7504    
7505    ## TODO: Support for $get_wrapper
7506    
7507      ## Step 1 # MUST      ## Step 1 # MUST
7508      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7509      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5242  sub set_inner_html ($$$) { Line 7511  sub set_inner_html ($$$) {
7511      my $p = $class->new;      my $p = $class->new;
7512      $p->{document} = $doc;      $p->{document} = $doc;
7513    
7514      ## Step 9 # MUST      ## Step 8 # MUST
7515      my $i = 0;      my $i = 0;
7516      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7517      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7518      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7519        my $self = shift;        my $self = shift;
7520    
7521        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7522        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7523    
7524        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7525        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7526        $column++;  
7527          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7528        if ($self->{next_input_character} == 0x000A) { # LF        $p->{column}++;
7529          $line++;  
7530          $column = 0;        if ($self->{next_char} == 0x000A) { # LF
7531        } elsif ($self->{next_input_character} == 0x000D) { # CR          $p->{line}++;
7532            $p->{column} = 0;
7533            !!!cp ('i1');
7534          } elsif ($self->{next_char} == 0x000D) { # CR
7535          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7536          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7537          $line++;          $p->{line}++;
7538          $column = 0;          $p->{column} = 0;
7539        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7540          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7541        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7542            !!!cp ('i3');
7543          } elsif ($self->{next_char} == 0x0000) { # NULL
7544            !!!cp ('i4');
7545          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7546          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7547          } elsif ($self->{next_char} <= 0x0008 or
7548                   (0x000E <= $self->{next_char} and
7549                    $self->{next_char} <= 0x001F) or
7550                   (0x007F <= $self->{next_char} and
7551                    $self->{next_char} <= 0x009F) or
7552                   (0xD800 <= $self->{next_char} and
7553                    $self->{next_char} <= 0xDFFF) or
7554                   (0xFDD0 <= $self->{next_char} and
7555                    $self->{next_char} <= 0xFDDF) or
7556                   {
7557                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7558                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7559                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7560                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7561                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7562                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7563                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7564                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7565                    0x10FFFE => 1, 0x10FFFF => 1,
7566                   }->{$self->{next_char}}) {
7567            !!!cp ('i4.1');
7568            if ($self->{next_char} < 0x10000) {
7569              !!!parse-error (type => 'control char',
7570                              text => (sprintf 'U+%04X', $self->{next_char}));
7571            } else {
7572              !!!parse-error (type => 'control char',
7573                              text => (sprintf 'U-%08X', $self->{next_char}));
7574            }
7575        }        }
7576      };      };
7577      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7578      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7579            
7580      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7581        my (%opt) = @_;        my (%opt) = @_;
7582        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7583          my $column = $opt{column};
7584          if (defined $opt{token} and defined $opt{token}->{line}) {
7585            $line = $opt{token}->{line};
7586            $column = $opt{token}->{column};
7587          }
7588          warn "Parse error ($opt{type}) at line $line column $column\n";
7589      };      };
7590      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7591        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7592      };      };
7593            
7594      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7595      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7596    
7597      ## Step 2      ## Step 2
7598      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7599      $p->{content_model} = {      $p->{content_model} = {
7600        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7601        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5303  sub set_inner_html ($$$) { Line 7612  sub set_inner_html ($$$) {
7612          unless defined $p->{content_model};          unless defined $p->{content_model};
7613          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7614    
7615      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7616          ## TODO: Foreign element OK?
7617    
7618      ## Step 4      ## Step 3
7619      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7620        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7621    
7622      ## Step 5 # MUST      ## Step 4 # MUST
7623      $doc->append_child ($root);      $doc->append_child ($root);
7624    
7625      ## Step 6 # MUST      ## Step 5 # MUST
7626      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7627    
7628      undef $p->{head_element};      undef $p->{head_element};
7629    
7630      ## Step 7 # MUST      ## Step 6 # MUST
7631      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7632    
7633      ## Step 8 # MUST      ## Step 7 # MUST
7634      my $anode = $node;      my $anode = $node;
7635      AN: while (defined $anode) {      AN: while (defined $anode) {
7636        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7637          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7638          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7639            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7640                !!!cp ('i5');
7641              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7642              last AN;              last AN;
7643            }            }
# Line 5335  sub set_inner_html ($$$) { Line 7646  sub set_inner_html ($$$) {
7646        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7647      } # AN      } # AN
7648            
7649      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7650      {      {
7651        my $self = $p;        my $self = $p;
7652        !!!next-token;        !!!next-token;
7653      }      }
7654      $p->_tree_construction_main;      $p->_tree_construction_main;
7655    
7656      ## Step 11 # MUST      ## Step 10 # MUST
7657      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7658      for (@cn) {      for (@cn) {
7659        $node->remove_child ($_);        $node->remove_child ($_);
7660      }      }
7661      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7662    
7663      ## Step 12 # MUST      ## Step 11 # MUST
7664      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7665      for (@cn) {      for (@cn) {
7666        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5359  sub set_inner_html ($$$) { Line 7669  sub set_inner_html ($$$) {
7669      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7670    
7671      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7672    
7673        delete $p->{parse_error}; # delete loop
7674    } else {    } else {
7675      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";
7676    }    }
# Line 5366  sub set_inner_html ($$$) { Line 7678  sub set_inner_html ($$$) {
7678    
7679  } # tree construction stage  } # tree construction stage
7680    
7681  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7682    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  
7683    
7684  1;  1;
7685  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24