/[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.159 by wakaba, Fri Sep 5 17:57:47 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 = ref $_[0] ? shift : shift->new;
359    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $charset_name = shift;
360    textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,    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  
361    
362  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
363    my $self = shift->new;      my (%opt) = @_;
364    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
365      };
366      $self->{parse_error} = $onerror; # updated later by parse_char_string
367    
368      ## HTML5 encoding sniffing algorithm
369      require Message::Charset::Info;
370      my $charset;
371      my $buffer;
372      my ($char_stream, $e_status);
373    
374      SNIFFING: {
375    
376        ## Step 1
377        if (defined $charset_name) {
378          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
379    
380          ## ISSUE: Unsupported encoding is not ignored according to the spec.
381          ($char_stream, $e_status) = $charset->get_decode_handle
382              ($byte_stream, allow_error_reporting => 1,
383               allow_fallback => 1);
384          if ($char_stream) {
385            $self->{confident} = 1;
386            last SNIFFING;
387          } else {
388            ## TODO: unsupported error
389          }
390        }
391    
392        ## Step 2
393        my $byte_buffer = '';
394        for (1..1024) {
395          my $char = $byte_stream->getc;
396          last unless defined $char;
397          $byte_buffer .= $char;
398        } ## TODO: timeout
399    
400        ## Step 3
401        if ($byte_buffer =~ /^\xFE\xFF/) {
402          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
403          ($char_stream, $e_status) = $charset->get_decode_handle
404              ($byte_stream, allow_error_reporting => 1,
405               allow_fallback => 1, byte_buffer => \$byte_buffer);
406          $self->{confident} = 1;
407          last SNIFFING;
408        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
409          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
410          ($char_stream, $e_status) = $charset->get_decode_handle
411              ($byte_stream, allow_error_reporting => 1,
412               allow_fallback => 1, byte_buffer => \$byte_buffer);
413          $self->{confident} = 1;
414          last SNIFFING;
415        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
416          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
417          ($char_stream, $e_status) = $charset->get_decode_handle
418              ($byte_stream, allow_error_reporting => 1,
419               allow_fallback => 1, byte_buffer => \$byte_buffer);
420          $self->{confident} = 1;
421          last SNIFFING;
422        }
423    
424        ## Step 4
425        ## TODO: <meta charset>
426    
427        ## Step 5
428        ## TODO: from history
429    
430        ## Step 6
431        require Whatpm::Charset::UniversalCharDet;
432        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
433            ($byte_buffer);
434        if (defined $charset_name) {
435          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
436    
437          ## ISSUE: Unsupported encoding is not ignored according to the spec.
438          require Whatpm::Charset::DecodeHandle;
439          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
440              ($byte_stream);
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($buffer, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          if ($char_stream) {
445            $buffer->{buffer} = $byte_buffer;
446            !!!parse-error (type => 'sniffing:chardet',
447                            text => $charset_name,
448                            level => $self->{level}->{info},
449                            layer => 'encode',
450                            line => 1, column => 1);
451            $self->{confident} = 0;
452            last SNIFFING;
453          }
454        }
455    
456        ## Step 7: default
457        ## TODO: Make this configurable.
458        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
459            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
460            ## detectable in the step 6.
461        require Whatpm::Charset::DecodeHandle;
462        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
463            ($byte_stream);
464        ($char_stream, $e_status)
465            = $charset->get_decode_handle ($buffer,
466                                           allow_error_reporting => 1,
467                                           allow_fallback => 1,
468                                           byte_buffer => \$byte_buffer);
469        $buffer->{buffer} = $byte_buffer;
470        !!!parse-error (type => 'sniffing:default',
471                        text => 'windows-1252',
472                        level => $self->{level}->{info},
473                        line => 1, column => 1,
474                        layer => 'encode');
475        $self->{confident} = 0;
476      } # SNIFFING
477    
478      $self->{input_encoding} = $charset->get_iana_name;
479      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
480        !!!parse-error (type => 'chardecode:fallback',
481                        text => $self->{input_encoding},
482                        level => $self->{level}->{uncertain},
483                        line => 1, column => 1,
484                        layer => 'encode');
485      } elsif (not ($e_status &
486                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
487        !!!parse-error (type => 'chardecode:no error',
488                        text => $self->{input_encoding},
489                        level => $self->{level}->{uncertain},
490                        line => 1, column => 1,
491                        layer => 'encode');
492      }
493    
494      $self->{change_encoding} = sub {
495        my $self = shift;
496        $charset_name = shift;
497        my $token = shift;
498    
499        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
500        ($char_stream, $e_status) = $charset->get_decode_handle
501            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
502             byte_buffer => \ $buffer->{buffer});
503        
504        if ($char_stream) { # if supported
505          ## "Change the encoding" algorithm:
506    
507          ## Step 1    
508          if ($charset->{category} &
509              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
510            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
511            ($char_stream, $e_status) = $charset->get_decode_handle
512                ($byte_stream,
513                 byte_buffer => \ $buffer->{buffer});
514          }
515          $charset_name = $charset->get_iana_name;
516          
517          ## Step 2
518          if (defined $self->{input_encoding} and
519              $self->{input_encoding} eq $charset_name) {
520            !!!parse-error (type => 'charset label:matching',
521                            text => $charset_name,
522                            level => $self->{level}->{info});
523            $self->{confident} = 1;
524            return;
525          }
526    
527          !!!parse-error (type => 'charset label detected',
528                          text => $self->{input_encoding},
529                          value => $charset_name,
530                          level => $self->{level}->{warn},
531                          token => $token);
532          
533          ## Step 3
534          # if (can) {
535            ## change the encoding on the fly.
536            #$self->{confident} = 1;
537            #return;
538          # }
539          
540          ## Step 4
541          throw Whatpm::HTML::RestartParser ();
542        }
543      }; # $self->{change_encoding}
544    
545      my $char_onerror = sub {
546        my (undef, $type, %opt) = @_;
547        !!!parse-error (layer => 'encode',
548                        %opt, type => $type,
549                        line => $self->{line}, column => $self->{column} + 1);
550        if ($opt{octets}) {
551          ${$opt{octets}} = "\x{FFFD}"; # relacement character
552        }
553      };
554      $char_stream->onerror ($char_onerror);
555    
556      my @args = @_; shift @args; # $s
557      my $return;
558      try {
559        $return = $self->parse_char_stream ($char_stream, @args);  
560      } catch Whatpm::HTML::RestartParser with {
561        ## NOTE: Invoked after {change_encoding}.
562    
563        $self->{input_encoding} = $charset->get_iana_name;
564        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
565          !!!parse-error (type => 'chardecode:fallback',
566                          text => $self->{input_encoding},
567                          level => $self->{level}->{uncertain},
568                          line => 1, column => 1,
569                          layer => 'encode');
570        } elsif (not ($e_status &
571                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
572          !!!parse-error (type => 'chardecode:no error',
573                          text => $self->{input_encoding},
574                          level => $self->{level}->{uncertain},
575                          line => 1, column => 1,
576                          layer => 'encode');
577        }
578        $self->{confident} = 1;
579        $char_stream->onerror ($char_onerror);
580        $return = $self->parse_char_stream ($char_stream, @args);
581      };
582      return $return;
583    } # parse_byte_stream
584    
585    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
586    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
587    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
588    ## because the core part of our HTML parser expects a string of character,
589    ## not a string of bytes or code units or anything which might contain a BOM.
590    ## Therefore, any parser interface that accepts a string of bytes,
591    ## such as |parse_byte_string| in this module, must ensure that it does
592    ## strip the BOM and never strip any ZWNBSP.
593    
594    sub parse_char_string ($$$;$) {
595      my $self = shift;
596      require utf8;
597      my $s = ref $_[0] ? $_[0] : \($_[0]);
598      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
599      return $self->parse_char_stream ($input, @_[1..$#_]);
600    } # parse_char_string
601    *parse_string = \&parse_char_string;
602    
603    sub parse_char_stream ($$$;$) {
604      my $self = ref $_[0] ? shift : shift->new;
605      my $input = $_[0];
606    $self->{document} = $_[1];    $self->{document} = $_[1];
607      @{$self->{document}->child_nodes} = ();
608    
609    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
610    
611      $self->{confident} = 1 unless exists $self->{confident};
612      $self->{document}->input_encoding ($self->{input_encoding})
613          if defined $self->{input_encoding};
614    
615    my $i = 0;    my $i = 0;
616    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
617    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
618    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
619      my $self = shift;      my $self = shift;
620    
621      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
622      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
623    
624      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char;
625      $self->{next_input_character} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
626      $column++;        $char = $self->{next_next_char};
627          delete $self->{next_next_char};
628        } else {
629          $char = $input->getc;
630        }
631        $self->{next_char} = -1 and return unless defined $char;
632        $self->{next_char} = ord $char;
633    
634        ($self->{line_prev}, $self->{column_prev})
635            = ($self->{line}, $self->{column});
636        $self->{column}++;
637            
638      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
639        $line++;        !!!cp ('j1');
640        $column = 0;        $self->{line}++;
641      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
642        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
643        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
644        $line++;        my $next = $input->getc;
645        $column = 0;        if (defined $next and $next ne "\x0A") {
646      } elsif ($self->{next_input_character} > 0x10FFFF) {          $self->{next_next_char} = $next;
647        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
648      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
649          $self->{line}++;
650          $self->{column} = 0;
651        } elsif ($self->{next_char} > 0x10FFFF) {
652          !!!cp ('j3');
653          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
654        } elsif ($self->{next_char} == 0x0000) { # NULL
655          !!!cp ('j4');
656        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
657        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
658        } elsif ($self->{next_char} <= 0x0008 or
659                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
660                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
661                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
662                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
663                 {
664                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
665                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
666                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
667                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
668                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
669                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
670                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
671                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
672                  0x10FFFE => 1, 0x10FFFF => 1,
673                 }->{$self->{next_char}}) {
674          !!!cp ('j5');
675          if ($self->{next_char} < 0x10000) {
676            !!!parse-error (type => 'control char',
677                            text => (sprintf 'U+%04X', $self->{next_char}));
678          } else {
679            !!!parse-error (type => 'control char',
680                            text => (sprintf 'U-%08X', $self->{next_char}));
681          }
682      }      }
683    };    };
684    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
685    $self->{next_input_character} = -1;    $self->{next_char} = -1;
686    
687    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
688      my (%opt) = @_;      my (%opt) = @_;
689      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
690        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
691        warn "Parse error ($opt{type}) at line $line column $column\n";
692    };    };
693    $self->{parse_error} = sub {    $self->{parse_error} = sub {
694      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
695    };    };
696    
697    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 699  sub parse_string ($$$;$) {
699    $self->_construct_tree;    $self->_construct_tree;
700    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
701    
702      delete $self->{parse_error}; # remove loop
703    
704    return $self->{document};    return $self->{document};
705  } # parse_string  } # parse_char_stream
706    
707  sub new ($) {  sub new ($) {
708    my $class = shift;    my $class = shift;
709    my $self = bless {}, $class;    my $self = bless {
710    $self->{set_next_input_character} = sub {      level => {must => 'm',
711      $self->{next_input_character} = -1;                should => 's',
712                  warn => 'w',
713                  info => 'i',
714                  uncertain => 'u'},
715      }, $class;
716      $self->{set_next_char} = sub {
717        $self->{next_char} = -1;
718    };    };
719    $self->{parse_error} = sub {    $self->{parse_error} = sub {
720      #      #
721    };    };
722      $self->{change_encoding} = sub {
723        # if ($_[0] is a supported encoding) {
724        #   run "change the encoding" algorithm;
725        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
726        # }
727      };
728    $self->{application_cache_selection} = sub {    $self->{application_cache_selection} = sub {
729      #      #
730    };    };
# Line 195  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 773  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
773  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
774  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
775  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
776    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
777    sub SELF_CLOSING_START_TAG_STATE () { 34 }
778    sub CDATA_BLOCK_STATE () { 35 }
779    
780  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
781  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 211  sub TABLE_IMS ()      { 0b1000000 } Line 792  sub TABLE_IMS ()      { 0b1000000 }
792  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
793  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
794  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
795    sub SELECT_IMS ()     { 0b10000000000 }
796    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
797        ## NOTE: "in foreign content" insertion mode is special; it is combined
798        ## with the secondary insertion mode.  In this parser, they are stored
799        ## together in the bit-or'ed form.
800    
801    ## NOTE: "initial" and "before html" insertion modes have no constants.
802    
803    ## NOTE: "after after body" insertion mode.
804  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
805    
806    ## NOTE: "after after frameset" insertion mode.
807  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
808    
809  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
810  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
811  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 227  sub IN_TABLE_IM () { TABLE_IMS } Line 819  sub IN_TABLE_IM () { TABLE_IMS }
819  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
820  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
821  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
822  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
823    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
824  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
825    
826  ## 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 833  sub _initialize_tokenizer ($) {
833    undef $self->{current_attribute};    undef $self->{current_attribute};
834    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
835    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
836      delete $self->{self_closing};
837    $self->{char} = [];    $self->{char} = [];
838    # $self->{next_input_character}    # $self->{next_char}
839    !!!next-input-character;    !!!next-input-character;
840    $self->{token} = [];    $self->{token} = [];
841    # $self->{escape}    # $self->{escape}
# Line 254  sub _initialize_tokenizer ($) { Line 848  sub _initialize_tokenizer ($) {
848  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
849  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
850  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
851  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
852  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
853    ##        ->{name}
854    ##        ->{value}
855    ##        ->{has_reference} == 1 or 0
856  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
857    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
858    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
859    ##     while the token is pushed back to the stack.
860    
861  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
862    
# Line 283  sub _initialize_tokenizer ($) { Line 883  sub _initialize_tokenizer ($) {
883    
884  sub _get_next_token ($) {  sub _get_next_token ($) {
885    my $self = shift;    my $self = shift;
886    
887      if ($self->{self_closing}) {
888        !!!parse-error (type => 'nestc', token => $self->{current_token});
889        ## NOTE: The |self_closing| flag is only set by start tag token.
890        ## In addition, when a start tag token is emitted, it is always set to
891        ## |current_token|.
892        delete $self->{self_closing};
893      }
894    
895    if (@{$self->{token}}) {    if (@{$self->{token}}) {
896        $self->{self_closing} = $self->{token}->[0]->{self_closing};
897      return shift @{$self->{token}};      return shift @{$self->{token}};
898    }    }
899    
900    A: {    A: {
901      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
902        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
903          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
904                not $self->{escape}) {
905              !!!cp (1);
906            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
907            !!!next-input-character;            !!!next-input-character;
908            redo A;            redo A;
909          } else {          } else {
910              !!!cp (2);
911            #            #
912          }          }
913        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
914          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
915            unless ($self->{escape}) {            unless ($self->{escape}) {
916              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
917                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
918                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
919                  !!!cp (3);
920                $self->{escape} = 1;                $self->{escape} = 1;
921                } else {
922                  !!!cp (4);
923              }              }
924              } else {
925                !!!cp (5);
926            }            }
927          }          }
928                    
929          #          #
930        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
931          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
932              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
933               not $self->{escape})) {               not $self->{escape})) {
934              !!!cp (6);
935            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
936            !!!next-input-character;            !!!next-input-character;
937            redo A;            redo A;
938          } else {          } else {
939              !!!cp (7);
940            #            #
941          }          }
942        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
943          if ($self->{escape} and          if ($self->{escape} and
944              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
945            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
946                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
947                !!!cp (8);
948              delete $self->{escape};              delete $self->{escape};
949              } else {
950                !!!cp (9);
951            }            }
952            } else {
953              !!!cp (10);
954          }          }
955                    
956          #          #
957        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
958          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
959            !!!emit ({type => END_OF_FILE_TOKEN,
960                      line => $self->{line}, column => $self->{column}});
961          last A; ## TODO: ok?          last A; ## TODO: ok?
962          } else {
963            !!!cp (12);
964        }        }
965        # Anything else        # Anything else
966        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
967                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
968                       line => $self->{line}, column => $self->{column},
969                      };
970        ## Stay in the data state        ## Stay in the data state
971        !!!next-input-character;        !!!next-input-character;
972    
# Line 344  sub _get_next_token ($) { Line 975  sub _get_next_token ($) {
975        redo A;        redo A;
976      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
977        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
978    
979          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
980                
981        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
982    
983        $self->{state} = DATA_STATE;        $self->{state} = DATA_STATE;
984        # next-input-character is already done        # next-input-character is already done
985    
986        unless (defined $token) {        unless (defined $token) {
987          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
988            !!!emit ({type => CHARACTER_TOKEN, data => '&',
989                      line => $l, column => $c,
990                     });
991        } else {        } else {
992            !!!cp (14);
993          !!!emit ($token);          !!!emit ($token);
994        }        }
995    
996        redo A;        redo A;
997      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
998        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
999          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
1000              !!!cp (15);
1001            !!!next-input-character;            !!!next-input-character;
1002            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1003            redo A;            redo A;
1004          } else {          } else {
1005              !!!cp (16);
1006            ## reconsume            ## reconsume
1007            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1008    
1009            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1010                        line => $self->{line_prev},
1011                        column => $self->{column_prev},
1012                       });
1013    
1014            redo A;            redo A;
1015          }          }
1016        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1017          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1018              !!!cp (17);
1019            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1020            !!!next-input-character;            !!!next-input-character;
1021            redo A;            redo A;
1022          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1023              !!!cp (18);
1024            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1025            !!!next-input-character;            !!!next-input-character;
1026            redo A;            redo A;
1027          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1028                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1029              !!!cp (19);
1030            $self->{current_token}            $self->{current_token}
1031              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1032                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1033                   line => $self->{line_prev},
1034                   column => $self->{column_prev}};
1035            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1036            !!!next-input-character;            !!!next-input-character;
1037            redo A;            redo A;
1038          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1039                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1040              !!!cp (20);
1041            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1042                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
1043                                        line => $self->{line_prev},
1044                                        column => $self->{column_prev}};
1045            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1046            !!!next-input-character;            !!!next-input-character;
1047            redo A;            redo A;
1048          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1049            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1050              !!!parse-error (type => 'empty start tag',
1051                              line => $self->{line_prev},
1052                              column => $self->{column_prev});
1053            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1054            !!!next-input-character;            !!!next-input-character;
1055    
1056            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1057                        line => $self->{line_prev},
1058                        column => $self->{column_prev},
1059                       });
1060    
1061            redo A;            redo A;
1062          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1063            !!!parse-error (type => 'pio');            !!!cp (22);
1064              !!!parse-error (type => 'pio',
1065                              line => $self->{line_prev},
1066                              column => $self->{column_prev});
1067            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1068            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1069                                        line => $self->{line_prev},
1070                                        column => $self->{column_prev},
1071                                       };
1072              ## $self->{next_char} is intentionally left as is
1073            redo A;            redo A;
1074          } else {          } else {
1075            !!!parse-error (type => 'bare stago');            !!!cp (23);
1076              !!!parse-error (type => 'bare stago',
1077                              line => $self->{line_prev},
1078                              column => $self->{column_prev});
1079            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1080            ## reconsume            ## reconsume
1081    
1082            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1083                        line => $self->{line_prev},
1084                        column => $self->{column_prev},
1085                       });
1086    
1087            redo A;            redo A;
1088          }          }
# Line 421  sub _get_next_token ($) { Line 1090  sub _get_next_token ($) {
1090          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1091        }        }
1092      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1093          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1094        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1095          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1096    
1097            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1098            my @next_char;            my @next_char;
1099            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++) {
1100              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1101              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1102              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1103              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1104                  !!!cp (24);
1105                !!!next-input-character;                !!!next-input-character;
1106                next TAGNAME;                next TAGNAME;
1107              } else {              } else {
1108                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1109                  $self->{next_char} = shift @next_char; # reconsume
1110                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1111                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1112    
1113                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1114                            line => $l, column => $c,
1115                           });
1116        
1117                redo A;                redo A;
1118              }              }
1119            }            }
1120            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1121                
1122            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1123                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1124                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1125                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1126                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1127                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1128                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1129                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1130              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1131                $self->{next_char} = shift @next_char; # reconsume
1132              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1133              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1134              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1135                          line => $l, column => $c,
1136                         });
1137              redo A;              redo A;
1138            } else {            } else {
1139              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1140                $self->{next_char} = shift @next_char;
1141              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1142              # and consume...              # and consume...
1143            }            }
1144          } else {          } else {
1145            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1146              !!!cp (28);
1147            # next-input-character is already done            # next-input-character is already done
1148            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1149            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1150                        line => $l, column => $c,
1151                       });
1152            redo A;            redo A;
1153          }          }
1154        }        }
1155                
1156        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1157            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1158          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1159                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1160                = {type => END_TAG_TOKEN,
1161                   tag_name => chr ($self->{next_char} + 0x0020),
1162                   line => $l, column => $c};
1163          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1164          !!!next-input-character;          !!!next-input-character;
1165          redo A;          redo A;
1166        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
1167                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1168            !!!cp (30);
1169          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1170                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
1171                                      line => $l, column => $c};
1172          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1173          !!!next-input-character;          !!!next-input-character;
1174          redo A;          redo A;
1175        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1176          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1177            !!!parse-error (type => 'empty end tag',
1178                            line => $self->{line_prev}, ## "<" in "</>"
1179                            column => $self->{column_prev} - 1);
1180          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1181          !!!next-input-character;          !!!next-input-character;
1182          redo A;          redo A;
1183        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1184            !!!cp (32);
1185          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1186          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1187          # reconsume          # reconsume
1188    
1189          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1190                      line => $l, column => $c,
1191                     });
1192    
1193          redo A;          redo A;
1194        } else {        } else {
1195            !!!cp (33);
1196          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1197          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1198          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1199                                      line => $self->{line_prev}, # "<" of "</"
1200                                      column => $self->{column_prev} - 1,
1201                                     };
1202            ## $self->{next_char} is intentionally left as is
1203          redo A;          redo A;
1204        }        }
1205      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1206        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1207            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1208            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1209            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1210            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1211            !!!cp (34);
1212          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1213          !!!next-input-character;          !!!next-input-character;
1214          redo A;          redo A;
1215        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1216          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1217            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
1218            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1219          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1220            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1221            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1222              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1223            }            #  !!! cp (36);
1224              #  !!! parse-error (type => 'end tag attribute');
1225              #} else {
1226                !!!cp (37);
1227              #}
1228          } else {          } else {
1229            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1230          }          }
# Line 532  sub _get_next_token ($) { Line 1234  sub _get_next_token ($) {
1234          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1235    
1236          redo A;          redo A;
1237        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1238                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1239          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1240            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1241            # start tag or end tag            # start tag or end tag
1242          ## Stay in this state          ## Stay in this state
1243          !!!next-input-character;          !!!next-input-character;
1244          redo A;          redo A;
1245        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1246          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1247          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1248            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1249            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1250          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1251            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1252            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1253              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1254            }            #  !!! cp (40);
1255              #  !!! parse-error (type => 'end tag attribute');
1256              #} else {
1257                !!!cp (41);
1258              #}
1259          } else {          } else {
1260            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1261          }          }
# Line 559  sub _get_next_token ($) { Line 1265  sub _get_next_token ($) {
1265          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1266    
1267          redo A;          redo A;
1268        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1269            !!!cp (42);
1270            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1271          !!!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  
1272          redo A;          redo A;
1273        } else {        } else {
1274          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1275            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1276            # start tag or end tag            # start tag or end tag
1277          ## Stay in the state          ## Stay in the state
1278          !!!next-input-character;          !!!next-input-character;
1279          redo A;          redo A;
1280        }        }
1281      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1282        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1283            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1284            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1285            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1286            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1287            !!!cp (45);
1288          ## Stay in the state          ## Stay in the state
1289          !!!next-input-character;          !!!next-input-character;
1290          redo A;          redo A;
1291        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1292          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1293            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1294            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1295          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1296            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1297            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1298                !!!cp (47);
1299              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1300              } else {
1301                !!!cp (48);
1302            }            }
1303          } else {          } else {
1304            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 1309  sub _get_next_token ($) {
1309          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1310    
1311          redo A;          redo A;
1312        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1313                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1314          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1315                                value => ''};          $self->{current_attribute}
1316                = {name => chr ($self->{next_char} + 0x0020),
1317                   value => '',
1318                   line => $self->{line}, column => $self->{column}};
1319          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1320          !!!next-input-character;          !!!next-input-character;
1321          redo A;          redo A;
1322        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1323            !!!cp (50);
1324            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1325          !!!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  
1326          redo A;          redo A;
1327        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1328          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1329          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1330            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1331            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1332          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1333            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1334            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1335                !!!cp (53);
1336              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1337              } else {
1338                !!!cp (54);
1339            }            }
1340          } else {          } else {
1341            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 1347  sub _get_next_token ($) {
1347    
1348          redo A;          redo A;
1349        } else {        } else {
1350          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1351                                value => ''};               0x0022 => 1, # "
1352                 0x0027 => 1, # '
1353                 0x003D => 1, # =
1354                }->{$self->{next_char}}) {
1355              !!!cp (55);
1356              !!!parse-error (type => 'bad attribute name');
1357            } else {
1358              !!!cp (56);
1359            }
1360            $self->{current_attribute}
1361                = {name => chr ($self->{next_char}),
1362                   value => '',
1363                   line => $self->{line}, column => $self->{column}};
1364          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1365          !!!next-input-character;          !!!next-input-character;
1366          redo A;          redo A;
# Line 658  sub _get_next_token ($) { Line 1369  sub _get_next_token ($) {
1369        my $before_leave = sub {        my $before_leave = sub {
1370          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1371              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1372            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1373              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1374            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1375          } else {          } else {
1376              !!!cp (58);
1377            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1378              = $self->{current_attribute};              = $self->{current_attribute};
1379          }          }
1380        }; # $before_leave        }; # $before_leave
1381    
1382        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1383            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1384            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1385            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1386            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1387            !!!cp (59);
1388          $before_leave->();          $before_leave->();
1389          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1390          !!!next-input-character;          !!!next-input-character;
1391          redo A;          redo A;
1392        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1393            !!!cp (60);
1394          $before_leave->();          $before_leave->();
1395          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1396          !!!next-input-character;          !!!next-input-character;
1397          redo A;          redo A;
1398        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1399          $before_leave->();          $before_leave->();
1400          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1401            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1402            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1403          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1404              !!!cp (62);
1405            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1406            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1407              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 700  sub _get_next_token ($) { Line 1415  sub _get_next_token ($) {
1415          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1416    
1417          redo A;          redo A;
1418        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1419                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1420          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1421            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1422          ## Stay in the state          ## Stay in the state
1423          !!!next-input-character;          !!!next-input-character;
1424          redo A;          redo A;
1425        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1426            !!!cp (64);
1427          $before_leave->();          $before_leave->();
1428            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1429          !!!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  
1430          redo A;          redo A;
1431        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1432          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1433          $before_leave->();          $before_leave->();
1434          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1435            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1436            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1437          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1438            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1439            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1440                !!!cp (67);
1441              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1442              } else {
1443                ## NOTE: This state should never be reached.
1444                !!!cp (68);
1445            }            }
1446          } else {          } else {
1447            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 1453  sub _get_next_token ($) {
1453    
1454          redo A;          redo A;
1455        } else {        } else {
1456          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1457                $self->{next_char} == 0x0027) { # '
1458              !!!cp (69);
1459              !!!parse-error (type => 'bad attribute name');
1460            } else {
1461              !!!cp (70);
1462            }
1463            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1464          ## Stay in the state          ## Stay in the state
1465          !!!next-input-character;          !!!next-input-character;
1466          redo A;          redo A;
1467        }        }
1468      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1469        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1470            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1471            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1472            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1473            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1474            !!!cp (71);
1475          ## Stay in the state          ## Stay in the state
1476          !!!next-input-character;          !!!next-input-character;
1477          redo A;          redo A;
1478        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1479            !!!cp (72);
1480          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1481          !!!next-input-character;          !!!next-input-character;
1482          redo A;          redo A;
1483        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1484          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1485            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1486            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1487          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1488            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1489            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1490                !!!cp (74);
1491              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1492              } else {
1493                ## NOTE: This state should never be reached.
1494                !!!cp (75);
1495            }            }
1496          } else {          } else {
1497            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 1502  sub _get_next_token ($) {
1502          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1503    
1504          redo A;          redo A;
1505        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1506                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1507          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1508                                value => ''};          $self->{current_attribute}
1509                = {name => chr ($self->{next_char} + 0x0020),
1510                   value => '',
1511                   line => $self->{line}, column => $self->{column}};
1512          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1513          !!!next-input-character;          !!!next-input-character;
1514          redo A;          redo A;
1515        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1516            !!!cp (77);
1517            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1518          !!!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  
1519          redo A;          redo A;
1520        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1521          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1522          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1523            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1524            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1525          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1526            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1527            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1528                !!!cp (80);
1529              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1530              } else {
1531                ## NOTE: This state should never be reached.
1532                !!!cp (81);
1533            }            }
1534          } else {          } else {
1535            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 1541  sub _get_next_token ($) {
1541    
1542          redo A;          redo A;
1543        } else {        } else {
1544          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1545                                value => ''};              $self->{next_char} == 0x0027) { # '
1546              !!!cp (78);
1547              !!!parse-error (type => 'bad attribute name');
1548            } else {
1549              !!!cp (82);
1550            }
1551            $self->{current_attribute}
1552                = {name => chr ($self->{next_char}),
1553                   value => '',
1554                   line => $self->{line}, column => $self->{column}};
1555          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1556          !!!next-input-character;          !!!next-input-character;
1557          redo A;                  redo A;        
1558        }        }
1559      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1560        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1561            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1562            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1563            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1564            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1565            !!!cp (83);
1566          ## Stay in the state          ## Stay in the state
1567          !!!next-input-character;          !!!next-input-character;
1568          redo A;          redo A;
1569        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1570            !!!cp (84);
1571          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1572          !!!next-input-character;          !!!next-input-character;
1573          redo A;          redo A;
1574        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1575            !!!cp (85);
1576          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1577          ## reconsume          ## reconsume
1578          redo A;          redo A;
1579        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1580            !!!cp (86);
1581          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1582          !!!next-input-character;          !!!next-input-character;
1583          redo A;          redo A;
1584        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1585            !!!parse-error (type => 'empty unquoted attribute value');
1586          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1587            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1588            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1589          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1590            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1591            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1592                !!!cp (88);
1593              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1594              } else {
1595                ## NOTE: This state should never be reached.
1596                !!!cp (89);
1597            }            }
1598          } else {          } else {
1599            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 1604  sub _get_next_token ($) {
1604          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1605    
1606          redo A;          redo A;
1607        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1608          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1609          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1610            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1611            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1612          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1613            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1614            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1615                !!!cp (91);
1616              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1617              } else {
1618                ## NOTE: This state should never be reached.
1619                !!!cp (92);
1620            }            }
1621          } else {          } else {
1622            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 1628  sub _get_next_token ($) {
1628    
1629          redo A;          redo A;
1630        } else {        } else {
1631          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1632              !!!cp (93);
1633              !!!parse-error (type => 'bad attribute value');
1634            } else {
1635              !!!cp (94);
1636            }
1637            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1638          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1639          !!!next-input-character;          !!!next-input-character;
1640          redo A;          redo A;
1641        }        }
1642      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1643        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1644          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1645            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1646          !!!next-input-character;          !!!next-input-character;
1647          redo A;          redo A;
1648        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1649            !!!cp (96);
1650          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1651          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1652          !!!next-input-character;          !!!next-input-character;
1653          redo A;          redo A;
1654        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1655          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1656          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1657            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1658            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1659          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1660            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1661            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1662                !!!cp (98);
1663              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1664              } else {
1665                ## NOTE: This state should never be reached.
1666                !!!cp (99);
1667            }            }
1668          } else {          } else {
1669            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 1675  sub _get_next_token ($) {
1675    
1676          redo A;          redo A;
1677        } else {        } else {
1678          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1679            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1680          ## Stay in the state          ## Stay in the state
1681          !!!next-input-character;          !!!next-input-character;
1682          redo A;          redo A;
1683        }        }
1684      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1685        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1686          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1687            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1688          !!!next-input-character;          !!!next-input-character;
1689          redo A;          redo A;
1690        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1691            !!!cp (102);
1692          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1693          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1694          !!!next-input-character;          !!!next-input-character;
1695          redo A;          redo A;
1696        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1697          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1698          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1699            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1700            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1701          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1702            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1703            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1704                !!!cp (104);
1705              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1706              } else {
1707                ## NOTE: This state should never be reached.
1708                !!!cp (105);
1709            }            }
1710          } else {          } else {
1711            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 1717  sub _get_next_token ($) {
1717    
1718          redo A;          redo A;
1719        } else {        } else {
1720          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1721            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1722          ## Stay in the state          ## Stay in the state
1723          !!!next-input-character;          !!!next-input-character;
1724          redo A;          redo A;
1725        }        }
1726      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1727        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1728            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1729            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1730            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1731            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1732            !!!cp (107);
1733          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1734          !!!next-input-character;          !!!next-input-character;
1735          redo A;          redo A;
1736        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1737            !!!cp (108);
1738          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1739          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1743          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1744            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1745            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1746          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1747            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1748            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1749                !!!cp (110);
1750              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1751              } else {
1752                ## NOTE: This state should never be reached.
1753                !!!cp (111);
1754            }            }
1755          } else {          } else {
1756            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 1761  sub _get_next_token ($) {
1761          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1762    
1763          redo A;          redo A;
1764        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1765          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1766          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1767            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1768            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1769          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1770            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1771            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1772                !!!cp (113);
1773              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1774              } else {
1775                ## NOTE: This state should never be reached.
1776                !!!cp (114);
1777            }            }
1778          } else {          } else {
1779            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 1785  sub _get_next_token ($) {
1785    
1786          redo A;          redo A;
1787        } else {        } else {
1788          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1789                 0x0022 => 1, # "
1790                 0x0027 => 1, # '
1791                 0x003D => 1, # =
1792                }->{$self->{next_char}}) {
1793              !!!cp (115);
1794              !!!parse-error (type => 'bad attribute value');
1795            } else {
1796              !!!cp (116);
1797            }
1798            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1799          ## Stay in the state          ## Stay in the state
1800          !!!next-input-character;          !!!next-input-character;
1801          redo A;          redo A;
1802        }        }
1803      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1804        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1805              (1,
1806               $self->{last_attribute_value_state}
1807                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1808               $self->{last_attribute_value_state}
1809                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1810               -1);
1811    
1812        unless (defined $token) {        unless (defined $token) {
1813            !!!cp (117);
1814          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1815        } else {        } else {
1816            !!!cp (118);
1817          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1818            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1819          ## 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"
1820        }        }
1821    
1822        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1823        # next-input-character is already done        # next-input-character is already done
1824        redo A;        redo A;
1825        } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1826          if ($self->{next_char} == 0x0009 or # HT
1827              $self->{next_char} == 0x000A or # LF
1828              $self->{next_char} == 0x000B or # VT
1829              $self->{next_char} == 0x000C or # FF
1830              $self->{next_char} == 0x0020) { # SP
1831            !!!cp (118);
1832            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1833            !!!next-input-character;
1834            redo A;
1835          } elsif ($self->{next_char} == 0x003E) { # >
1836            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1837              !!!cp (119);
1838              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1839            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1840              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1841              if ($self->{current_token}->{attributes}) {
1842                !!!cp (120);
1843                !!!parse-error (type => 'end tag attribute');
1844              } else {
1845                ## NOTE: This state should never be reached.
1846                !!!cp (121);
1847              }
1848            } else {
1849              die "$0: $self->{current_token}->{type}: Unknown token type";
1850            }
1851            $self->{state} = DATA_STATE;
1852            !!!next-input-character;
1853    
1854            !!!emit ($self->{current_token}); # start tag or end tag
1855    
1856            redo A;
1857          } elsif ($self->{next_char} == 0x002F) { # /
1858            !!!cp (122);
1859            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1860            !!!next-input-character;
1861            redo A;
1862          } elsif ($self->{next_char} == -1) {
1863            !!!parse-error (type => 'unclosed tag');
1864            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1865              !!!cp (122.3);
1866              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1867            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1868              if ($self->{current_token}->{attributes}) {
1869                !!!cp (122.1);
1870                !!!parse-error (type => 'end tag attribute');
1871              } else {
1872                ## NOTE: This state should never be reached.
1873                !!!cp (122.2);
1874              }
1875            } else {
1876              die "$0: $self->{current_token}->{type}: Unknown token type";
1877            }
1878            $self->{state} = DATA_STATE;
1879            ## Reconsume.
1880            !!!emit ($self->{current_token}); # start tag or end tag
1881            redo A;
1882          } else {
1883            !!!cp ('124.1');
1884            !!!parse-error (type => 'no space between attributes');
1885            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1886            ## reconsume
1887            redo A;
1888          }
1889        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1890          if ($self->{next_char} == 0x003E) { # >
1891            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1892              !!!cp ('124.2');
1893              !!!parse-error (type => 'nestc', token => $self->{current_token});
1894              ## TODO: Different type than slash in start tag
1895              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1896              if ($self->{current_token}->{attributes}) {
1897                !!!cp ('124.4');
1898                !!!parse-error (type => 'end tag attribute');
1899              } else {
1900                !!!cp ('124.5');
1901              }
1902              ## TODO: Test |<title></title/>|
1903            } else {
1904              !!!cp ('124.3');
1905              $self->{self_closing} = 1;
1906            }
1907    
1908            $self->{state} = DATA_STATE;
1909            !!!next-input-character;
1910    
1911            !!!emit ($self->{current_token}); # start tag or end tag
1912    
1913            redo A;
1914          } elsif ($self->{next_char} == -1) {
1915            !!!parse-error (type => 'unclosed tag');
1916            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1917              !!!cp (124.7);
1918              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1919            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1920              if ($self->{current_token}->{attributes}) {
1921                !!!cp (124.5);
1922                !!!parse-error (type => 'end tag attribute');
1923              } else {
1924                ## NOTE: This state should never be reached.
1925                !!!cp (124.6);
1926              }
1927            } else {
1928              die "$0: $self->{current_token}->{type}: Unknown token type";
1929            }
1930            $self->{state} = DATA_STATE;
1931            ## Reconsume.
1932            !!!emit ($self->{current_token}); # start tag or end tag
1933            redo A;
1934          } else {
1935            !!!cp ('124.4');
1936            !!!parse-error (type => 'nestc');
1937            ## TODO: This error type is wrong.
1938            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1939            ## Reconsume.
1940            redo A;
1941          }
1942      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1943        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1944                
1945        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1946          #my $token = {type => COMMENT_TOKEN, data => ''};
1947    
1948        BC: {        BC: {
1949          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1950              !!!cp (124);
1951            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1952            !!!next-input-character;            !!!next-input-character;
1953    
1954            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1955    
1956            redo A;            redo A;
1957          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1958              !!!cp (125);
1959            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1960            ## reconsume            ## reconsume
1961    
1962            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1963    
1964            redo A;            redo A;
1965          } else {          } else {
1966            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1967              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1968            !!!next-input-character;            !!!next-input-character;
1969            redo BC;            redo BC;
1970          }          }
1971        } # BC        } # BC
1972    
1973          die "$0: _get_next_token: unexpected case [BC]";
1974      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1975        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1976    
1977          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1978    
1979        my @next_char;        my @next_char;
1980        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1981                
1982        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1983          !!!next-input-character;          !!!next-input-character;
1984          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1985          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1986            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1987              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1988                                        line => $l, column => $c,
1989                                       };
1990            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1991            !!!next-input-character;            !!!next-input-character;
1992            redo A;            redo A;
1993            } else {
1994              !!!cp (128);
1995          }          }
1996        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1997                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1998          !!!next-input-character;          !!!next-input-character;
1999          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
2000          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
2001              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
2002            !!!next-input-character;            !!!next-input-character;
2003            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
2004            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
2005                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
2006              !!!next-input-character;              !!!next-input-character;
2007              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
2008              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2009                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2010                !!!next-input-character;                !!!next-input-character;
2011                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
2012                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
2013                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
2014                  !!!next-input-character;                  !!!next-input-character;
2015                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
2016                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
2017                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
2018                    !!!next-input-character;                    !!!next-input-character;
2019                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
2020                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
2021                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
2022                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
2023                        ## TODO: What a stupid code this is!
2024                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
2025                        $self->{current_token} = {type => DOCTYPE_TOKEN,
2026                                                  quirks => 1,
2027                                                  line => $l, column => $c,
2028                                                 };
2029                      !!!next-input-character;                      !!!next-input-character;
2030                      redo A;                      redo A;
2031                      } else {
2032                        !!!cp (130);
2033                    }                    }
2034                    } else {
2035                      !!!cp (131);
2036                  }                  }
2037                  } else {
2038                    !!!cp (132);
2039                }                }
2040                } else {
2041                  !!!cp (133);
2042              }              }
2043              } else {
2044                !!!cp (134);
2045            }            }
2046            } else {
2047              !!!cp (135);
2048          }          }
2049          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2050                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2051                   $self->{next_char} == 0x005B) { # [
2052            !!!next-input-character;
2053            push @next_char, $self->{next_char};
2054            if ($self->{next_char} == 0x0043) { # C
2055              !!!next-input-character;
2056              push @next_char, $self->{next_char};
2057              if ($self->{next_char} == 0x0044) { # D
2058                !!!next-input-character;
2059                push @next_char, $self->{next_char};
2060                if ($self->{next_char} == 0x0041) { # A
2061                  !!!next-input-character;
2062                  push @next_char, $self->{next_char};
2063                  if ($self->{next_char} == 0x0054) { # T
2064                    !!!next-input-character;
2065                    push @next_char, $self->{next_char};
2066                    if ($self->{next_char} == 0x0041) { # A
2067                      !!!next-input-character;
2068                      push @next_char, $self->{next_char};
2069                      if ($self->{next_char} == 0x005B) { # [
2070                        !!!cp (135.1);
2071                        $self->{state} = CDATA_BLOCK_STATE;
2072                        !!!next-input-character;
2073                        redo A;
2074                      } else {
2075                        !!!cp (135.2);
2076                      }
2077                    } else {
2078                      !!!cp (135.3);
2079                    }
2080                  } else {
2081                    !!!cp (135.4);                
2082                  }
2083                } else {
2084                  !!!cp (135.5);
2085                }
2086              } else {
2087                !!!cp (135.6);
2088              }
2089            } else {
2090              !!!cp (135.7);
2091            }
2092          } else {
2093            !!!cp (136);
2094        }        }
2095    
2096        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2097        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2098        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2099        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2100          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2101                                    line => $l, column => $c,
2102                                   };
2103        redo A;        redo A;
2104                
2105        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2106        ## 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?
2107      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2108        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2109            !!!cp (137);
2110          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2111          !!!next-input-character;          !!!next-input-character;
2112          redo A;          redo A;
2113        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2114            !!!cp (138);
2115          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2116          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2117          !!!next-input-character;          !!!next-input-character;
# Line 1137  sub _get_next_token ($) { Line 2119  sub _get_next_token ($) {
2119          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2120    
2121          redo A;          redo A;
2122        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2123            !!!cp (139);
2124          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2125          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2126          ## reconsume          ## reconsume
# Line 1146  sub _get_next_token ($) { Line 2129  sub _get_next_token ($) {
2129    
2130          redo A;          redo A;
2131        } else {        } else {
2132            !!!cp (140);
2133          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2134              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2135          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2136          !!!next-input-character;          !!!next-input-character;
2137          redo A;          redo A;
2138        }        }
2139      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2140        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2141            !!!cp (141);
2142          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2143          !!!next-input-character;          !!!next-input-character;
2144          redo A;          redo A;
2145        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2146            !!!cp (142);
2147          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2148          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2149          !!!next-input-character;          !!!next-input-character;
# Line 1165  sub _get_next_token ($) { Line 2151  sub _get_next_token ($) {
2151          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2152    
2153          redo A;          redo A;
2154        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2155            !!!cp (143);
2156          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2157          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2158          ## reconsume          ## reconsume
# Line 1174  sub _get_next_token ($) { Line 2161  sub _get_next_token ($) {
2161    
2162          redo A;          redo A;
2163        } else {        } else {
2164            !!!cp (144);
2165          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2166              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2167          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2168          !!!next-input-character;          !!!next-input-character;
2169          redo A;          redo A;
2170        }        }
2171      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2172        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2173            !!!cp (145);
2174          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2175          !!!next-input-character;          !!!next-input-character;
2176          redo A;          redo A;
2177        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2178            !!!cp (146);
2179          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2180          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2181          ## reconsume          ## reconsume
# Line 1194  sub _get_next_token ($) { Line 2184  sub _get_next_token ($) {
2184    
2185          redo A;          redo A;
2186        } else {        } else {
2187          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2188            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2189          ## Stay in the state          ## Stay in the state
2190          !!!next-input-character;          !!!next-input-character;
2191          redo A;          redo A;
2192        }        }
2193      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2194        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2195            !!!cp (148);
2196          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2197          !!!next-input-character;          !!!next-input-character;
2198          redo A;          redo A;
2199        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2200            !!!cp (149);
2201          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2202          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2203          ## reconsume          ## reconsume
# Line 1213  sub _get_next_token ($) { Line 2206  sub _get_next_token ($) {
2206    
2207          redo A;          redo A;
2208        } else {        } else {
2209          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2210            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2211          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2212          !!!next-input-character;          !!!next-input-character;
2213          redo A;          redo A;
2214        }        }
2215      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2216        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2217            !!!cp (151);
2218          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2219          !!!next-input-character;          !!!next-input-character;
2220    
2221          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2222    
2223          redo A;          redo A;
2224        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2225          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2226            !!!parse-error (type => 'dash in comment',
2227                            line => $self->{line_prev},
2228                            column => $self->{column_prev});
2229          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2230          ## Stay in the state          ## Stay in the state
2231          !!!next-input-character;          !!!next-input-character;
2232          redo A;          redo A;
2233        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2234            !!!cp (153);
2235          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2236          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2237          ## reconsume          ## reconsume
# Line 1241  sub _get_next_token ($) { Line 2240  sub _get_next_token ($) {
2240    
2241          redo A;          redo A;
2242        } else {        } else {
2243          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2244          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2245                            line => $self->{line_prev},
2246                            column => $self->{column_prev});
2247            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2248          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2249          !!!next-input-character;          !!!next-input-character;
2250          redo A;          redo A;
2251        }        }
2252      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2253        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2254            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2255            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2256            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2257            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2258            !!!cp (155);
2259          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2260          !!!next-input-character;          !!!next-input-character;
2261          redo A;          redo A;
2262        } else {        } else {
2263            !!!cp (156);
2264          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2265          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2266          ## reconsume          ## reconsume
2267          redo A;          redo A;
2268        }        }
2269      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2270        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2271            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2272            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2273            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2274            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2275            !!!cp (157);
2276          ## Stay in the state          ## Stay in the state
2277          !!!next-input-character;          !!!next-input-character;
2278          redo A;          redo A;
2279        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2280            !!!cp (158);
2281          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2282          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2283          !!!next-input-character;          !!!next-input-character;
2284    
2285          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2286    
2287          redo A;          redo A;
2288        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2289            !!!cp (159);
2290          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2291          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2292          ## reconsume          ## reconsume
2293    
2294          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2295    
2296          redo A;          redo A;
2297        } else {        } else {
2298          $self->{current_token}          !!!cp (160);
2299              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
2300                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2301  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2302          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2303          !!!next-input-character;          !!!next-input-character;
# Line 1299  sub _get_next_token ($) { Line 2305  sub _get_next_token ($) {
2305        }        }
2306      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2307  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2308        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2309            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2310            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2311            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2312            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2313            !!!cp (161);
2314          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2315          !!!next-input-character;          !!!next-input-character;
2316          redo A;          redo A;
2317        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2318            !!!cp (162);
2319          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2320          !!!next-input-character;          !!!next-input-character;
2321    
2322          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2323    
2324          redo A;          redo A;
2325        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2326            !!!cp (163);
2327          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2328          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2329          ## reconsume          ## reconsume
2330    
2331          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2332          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2333    
2334          redo A;          redo A;
2335        } else {        } else {
2336            !!!cp (164);
2337          $self->{current_token}->{name}          $self->{current_token}->{name}
2338            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2339          ## Stay in the state          ## Stay in the state
2340          !!!next-input-character;          !!!next-input-character;
2341          redo A;          redo A;
2342        }        }
2343      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2344        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2345            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2346            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2347            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2348            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2349            !!!cp (165);
2350          ## Stay in the state          ## Stay in the state
2351          !!!next-input-character;          !!!next-input-character;
2352          redo A;          redo A;
2353        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2354            !!!cp (166);
2355          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2356          !!!next-input-character;          !!!next-input-character;
2357    
2358          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2359    
2360          redo A;          redo A;
2361        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2362            !!!cp (167);
2363          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2364          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2365          ## reconsume          ## reconsume
2366    
2367          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2368          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2369    
2370          redo A;          redo A;
2371        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2372                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2373          !!!next-input-character;          !!!next-input-character;
2374          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2375              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2376            !!!next-input-character;            !!!next-input-character;
2377            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2378                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2379              !!!next-input-character;              !!!next-input-character;
2380              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2381                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2382                !!!next-input-character;                !!!next-input-character;
2383                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2384                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2385                  !!!next-input-character;                  !!!next-input-character;
2386                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2387                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2388                      !!!cp (168);
2389                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2390                    !!!next-input-character;                    !!!next-input-character;
2391                    redo A;                    redo A;
2392                    } else {
2393                      !!!cp (169);
2394                  }                  }
2395                  } else {
2396                    !!!cp (170);
2397                }                }
2398                } else {
2399                  !!!cp (171);
2400              }              }
2401              } else {
2402                !!!cp (172);
2403            }            }
2404            } else {
2405              !!!cp (173);
2406          }          }
2407    
2408          #          #
2409        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2410                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2411          !!!next-input-character;          !!!next-input-character;
2412          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2413              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2414            !!!next-input-character;            !!!next-input-character;
2415            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2416                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2417              !!!next-input-character;              !!!next-input-character;
2418              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2419                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2420                !!!next-input-character;                !!!next-input-character;
2421                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2422                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2423                  !!!next-input-character;                  !!!next-input-character;
2424                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2425                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2426                      !!!cp (174);
2427                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2428                    !!!next-input-character;                    !!!next-input-character;
2429                    redo A;                    redo A;
2430                    } else {
2431                      !!!cp (175);
2432                  }                  }
2433                  } else {
2434                    !!!cp (176);
2435                }                }
2436                } else {
2437                  !!!cp (177);
2438              }              }
2439              } else {
2440                !!!cp (178);
2441            }            }
2442            } else {
2443              !!!cp (179);
2444          }          }
2445    
2446          #          #
2447        } else {        } else {
2448            !!!cp (180);
2449          !!!next-input-character;          !!!next-input-character;
2450          #          #
2451        }        }
2452    
2453        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2454          $self->{current_token}->{quirks} = 1;
2455    
2456        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
2457        # next-input-character is already done        # next-input-character is already done
2458        redo A;        redo A;
# Line 1422  sub _get_next_token ($) { Line 2460  sub _get_next_token ($) {
2460        if ({        if ({
2461              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2462              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2463            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2464            !!!cp (181);
2465          ## Stay in the state          ## Stay in the state
2466          !!!next-input-character;          !!!next-input-character;
2467          redo A;          redo A;
2468        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2469            !!!cp (182);
2470          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2471          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2472          !!!next-input-character;          !!!next-input-character;
2473          redo A;          redo A;
2474        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2475            !!!cp (183);
2476          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2477          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2478          !!!next-input-character;          !!!next-input-character;
2479          redo A;          redo A;
2480        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2481            !!!cp (184);
2482          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2483    
2484          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2485          !!!next-input-character;          !!!next-input-character;
2486    
2487          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2488          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2489    
2490          redo A;          redo A;
2491        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2492            !!!cp (185);
2493          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2494    
2495          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2496          ## reconsume          ## reconsume
2497    
2498          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2499          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2500    
2501          redo A;          redo A;
2502        } else {        } else {
2503            !!!cp (186);
2504          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2505            $self->{current_token}->{quirks} = 1;
2506    
2507          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2508          !!!next-input-character;          !!!next-input-character;
2509          redo A;          redo A;
2510        }        }
2511      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2512        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2513            !!!cp (187);
2514          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2515          !!!next-input-character;          !!!next-input-character;
2516          redo A;          redo A;
2517        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2518            !!!cp (188);
2519            !!!parse-error (type => 'unclosed PUBLIC literal');
2520    
2521            $self->{state} = DATA_STATE;
2522            !!!next-input-character;
2523    
2524            $self->{current_token}->{quirks} = 1;
2525            !!!emit ($self->{current_token}); # DOCTYPE
2526    
2527            redo A;
2528          } elsif ($self->{next_char} == -1) {
2529            !!!cp (189);
2530          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2531    
2532          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2533          ## reconsume          ## reconsume
2534    
2535          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2536          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2537    
2538          redo A;          redo A;
2539        } else {        } else {
2540            !!!cp (190);
2541          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2542              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2543          ## Stay in the state          ## Stay in the state
2544          !!!next-input-character;          !!!next-input-character;
2545          redo A;          redo A;
2546        }        }
2547      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2548        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2549            !!!cp (191);
2550          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2551          !!!next-input-character;          !!!next-input-character;
2552          redo A;          redo A;
2553        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2554            !!!cp (192);
2555            !!!parse-error (type => 'unclosed PUBLIC literal');
2556    
2557            $self->{state} = DATA_STATE;
2558            !!!next-input-character;
2559    
2560            $self->{current_token}->{quirks} = 1;
2561            !!!emit ($self->{current_token}); # DOCTYPE
2562    
2563            redo A;
2564          } elsif ($self->{next_char} == -1) {
2565            !!!cp (193);
2566          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2567    
2568          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2569          ## reconsume          ## reconsume
2570    
2571          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2572          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2573    
2574          redo A;          redo A;
2575        } else {        } else {
2576            !!!cp (194);
2577          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2578              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2579          ## Stay in the state          ## Stay in the state
2580          !!!next-input-character;          !!!next-input-character;
2581          redo A;          redo A;
# Line 1510  sub _get_next_token ($) { Line 2584  sub _get_next_token ($) {
2584        if ({        if ({
2585              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2586              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2587            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2588            !!!cp (195);
2589          ## Stay in the state          ## Stay in the state
2590          !!!next-input-character;          !!!next-input-character;
2591          redo A;          redo A;
2592        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2593            !!!cp (196);
2594          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2595          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2596          !!!next-input-character;          !!!next-input-character;
2597          redo A;          redo A;
2598        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2599            !!!cp (197);
2600          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2601          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2602          !!!next-input-character;          !!!next-input-character;
2603          redo A;          redo A;
2604        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2605            !!!cp (198);
2606          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2607          !!!next-input-character;          !!!next-input-character;
2608    
2609          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2610    
2611          redo A;          redo A;
2612        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2613            !!!cp (199);
2614          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2615    
2616          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2617          ## reconsume          ## reconsume
2618    
2619          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2620          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2621    
2622          redo A;          redo A;
2623        } else {        } else {
2624            !!!cp (200);
2625          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2626            $self->{current_token}->{quirks} = 1;
2627    
2628          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2629          !!!next-input-character;          !!!next-input-character;
2630          redo A;          redo A;
# Line 1551  sub _get_next_token ($) { Line 2633  sub _get_next_token ($) {
2633        if ({        if ({
2634              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2635              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2636            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2637            !!!cp (201);
2638          ## Stay in the state          ## Stay in the state
2639          !!!next-input-character;          !!!next-input-character;
2640          redo A;          redo A;
2641        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2642            !!!cp (202);
2643          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2644          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2645          !!!next-input-character;          !!!next-input-character;
2646          redo A;          redo A;
2647        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2648            !!!cp (203);
2649          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2650          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2651          !!!next-input-character;          !!!next-input-character;
2652          redo A;          redo A;
2653        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2654            !!!cp (204);
2655          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2656          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2657          !!!next-input-character;          !!!next-input-character;
2658    
2659          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2660          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2661    
2662          redo A;          redo A;
2663        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2664            !!!cp (205);
2665          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2666    
2667          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2668          ## reconsume          ## reconsume
2669    
2670          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2671          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2672    
2673          redo A;          redo A;
2674        } else {        } else {
2675            !!!cp (206);
2676          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2677            $self->{current_token}->{quirks} = 1;
2678    
2679          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2680          !!!next-input-character;          !!!next-input-character;
2681          redo A;          redo A;
2682        }        }
2683      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2684        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2685            !!!cp (207);
2686          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2687          !!!next-input-character;          !!!next-input-character;
2688          redo A;          redo A;
2689        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2690            !!!cp (208);
2691            !!!parse-error (type => 'unclosed SYSTEM literal');
2692    
2693            $self->{state} = DATA_STATE;
2694            !!!next-input-character;
2695    
2696            $self->{current_token}->{quirks} = 1;
2697            !!!emit ($self->{current_token}); # DOCTYPE
2698    
2699            redo A;
2700          } elsif ($self->{next_char} == -1) {
2701            !!!cp (209);
2702          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2703    
2704          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2705          ## reconsume          ## reconsume
2706    
2707          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2708          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2709    
2710          redo A;          redo A;
2711        } else {        } else {
2712            !!!cp (210);
2713          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2714              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2715          ## Stay in the state          ## Stay in the state
2716          !!!next-input-character;          !!!next-input-character;
2717          redo A;          redo A;
2718        }        }
2719      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2720        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2721            !!!cp (211);
2722          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2723          !!!next-input-character;          !!!next-input-character;
2724          redo A;          redo A;
2725        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2726            !!!cp (212);
2727            !!!parse-error (type => 'unclosed SYSTEM literal');
2728    
2729            $self->{state} = DATA_STATE;
2730            !!!next-input-character;
2731    
2732            $self->{current_token}->{quirks} = 1;
2733            !!!emit ($self->{current_token}); # DOCTYPE
2734    
2735            redo A;
2736          } elsif ($self->{next_char} == -1) {
2737            !!!cp (213);
2738          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2739    
2740          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2741          ## reconsume          ## reconsume
2742    
2743          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2744          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2745    
2746          redo A;          redo A;
2747        } else {        } else {
2748            !!!cp (214);
2749          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2750              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2751          ## Stay in the state          ## Stay in the state
2752          !!!next-input-character;          !!!next-input-character;
2753          redo A;          redo A;
# Line 1638  sub _get_next_token ($) { Line 2756  sub _get_next_token ($) {
2756        if ({        if ({
2757              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2758              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2759            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2760            !!!cp (215);
2761          ## Stay in the state          ## Stay in the state
2762          !!!next-input-character;          !!!next-input-character;
2763          redo A;          redo A;
2764        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2765            !!!cp (216);
2766          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2767          !!!next-input-character;          !!!next-input-character;
2768    
2769          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2770    
2771          redo A;          redo A;
2772        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2773            !!!cp (217);
2774          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2775          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2776          ## reconsume          ## reconsume
2777    
2778          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2779          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2780    
2781          redo A;          redo A;
2782        } else {        } else {
2783            !!!cp (218);
2784          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2785            #$self->{current_token}->{quirks} = 1;
2786    
2787          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2788          !!!next-input-character;          !!!next-input-character;
2789          redo A;          redo A;
2790        }        }
2791      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2792        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2793            !!!cp (219);
2794          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2795          !!!next-input-character;          !!!next-input-character;
2796    
         delete $self->{current_token}->{correct};  
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 (220);
2802          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2803          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2804          ## reconsume          ## reconsume
2805    
         delete $self->{current_token}->{correct};  
2806          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2807    
2808          redo A;          redo A;
2809        } else {        } else {
2810            !!!cp (221);
2811          ## Stay in the state          ## Stay in the state
2812          !!!next-input-character;          !!!next-input-character;
2813          redo A;          redo A;
2814        }        }
2815        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2816          my $s = '';
2817          
2818          my ($l, $c) = ($self->{line}, $self->{column});
2819    
2820          CS: while ($self->{next_char} != -1) {
2821            if ($self->{next_char} == 0x005D) { # ]
2822              !!!next-input-character;
2823              if ($self->{next_char} == 0x005D) { # ]
2824                !!!next-input-character;
2825                MDC: {
2826                  if ($self->{next_char} == 0x003E) { # >
2827                    !!!cp (221.1);
2828                    !!!next-input-character;
2829                    last CS;
2830                  } elsif ($self->{next_char} == 0x005D) { # ]
2831                    !!!cp (221.2);
2832                    $s .= ']';
2833                    !!!next-input-character;
2834                    redo MDC;
2835                  } else {
2836                    !!!cp (221.3);
2837                    $s .= ']]';
2838                    #
2839                  }
2840                } # MDC
2841              } else {
2842                !!!cp (221.4);
2843                $s .= ']';
2844                #
2845              }
2846            } else {
2847              !!!cp (221.5);
2848              #
2849            }
2850            $s .= chr $self->{next_char};
2851            !!!next-input-character;
2852          } # CS
2853    
2854          $self->{state} = DATA_STATE;
2855          ## next-input-character done or EOF, which is reconsumed.
2856    
2857          if (length $s) {
2858            !!!cp (221.6);
2859            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2860                      line => $l, column => $c});
2861          } else {
2862            !!!cp (221.7);
2863          }
2864    
2865          redo A;
2866    
2867          ## ISSUE: "text tokens" in spec.
2868          ## TODO: Streaming support
2869      } else {      } else {
2870        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2871      }      }
# Line 1696  sub _get_next_token ($) { Line 2874  sub _get_next_token ($) {
2874    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2875  } # _get_next_token  } # _get_next_token
2876    
2877  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2878    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2879    
2880      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2881    
2882    if ({    if ({
2883         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2884         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2885        }->{$self->{next_input_character}}) {         $additional => 1,
2886          }->{$self->{next_char}}) {
2887        !!!cp (1001);
2888      ## Don't consume      ## Don't consume
2889      ## No error      ## No error
2890      return undef;      return undef;
2891    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2892      !!!next-input-character;      !!!next-input-character;
2893      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2894          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2895        my $code;        my $code;
2896        X: {        X: {
2897          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2898          !!!next-input-character;          !!!next-input-character;
2899          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2900              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2901              !!!cp (1002);
2902            $code ||= 0;            $code ||= 0;
2903            $code *= 0x10;            $code *= 0x10;
2904            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2905            redo X;            redo X;
2906          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2907                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2908              !!!cp (1003);
2909            $code ||= 0;            $code ||= 0;
2910            $code *= 0x10;            $code *= 0x10;
2911            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2912            redo X;            redo X;
2913          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2914                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2915              !!!cp (1004);
2916            $code ||= 0;            $code ||= 0;
2917            $code *= 0x10;            $code *= 0x10;
2918            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2919            redo X;            redo X;
2920          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2921            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2922            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2923            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2924              $self->{next_char} = 0x0023; # #
2925            return undef;            return undef;
2926          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2927              !!!cp (1006);
2928            !!!next-input-character;            !!!next-input-character;
2929          } else {          } else {
2930            !!!parse-error (type => 'no refc');            !!!cp (1007);
2931              !!!parse-error (type => 'no refc', line => $l, column => $c);
2932          }          }
2933    
2934          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2935            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2936              !!!parse-error (type => 'invalid character reference',
2937                              text => (sprintf 'U+%04X', $code),
2938                              line => $l, column => $c);
2939            $code = 0xFFFD;            $code = 0xFFFD;
2940          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2941            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2942              !!!parse-error (type => 'invalid character reference',
2943                              text => (sprintf 'U-%08X', $code),
2944                              line => $l, column => $c);
2945            $code = 0xFFFD;            $code = 0xFFFD;
2946          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2947            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2948              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2949            $code = 0x000A;            $code = 0x000A;
2950          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2951            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2952              !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
2953            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2954          }          }
2955    
2956          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2957                    has_reference => 1,
2958                    line => $l, column => $c,
2959                   };
2960        } # X        } # X
2961      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2962               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2963        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2964        !!!next-input-character;        !!!next-input-character;
2965                
2966        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2967                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2968            !!!cp (1012);
2969          $code *= 10;          $code *= 10;
2970          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2971                    
2972          !!!next-input-character;          !!!next-input-character;
2973        }        }
2974    
2975        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2976            !!!cp (1013);
2977          !!!next-input-character;          !!!next-input-character;
2978        } else {        } else {
2979          !!!parse-error (type => 'no refc');          !!!cp (1014);
2980            !!!parse-error (type => 'no refc', line => $l, column => $c);
2981        }        }
2982    
2983        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2984          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2985            !!!parse-error (type => 'invalid character reference',
2986                            text => (sprintf 'U+%04X', $code),
2987                            line => $l, column => $c);
2988          $code = 0xFFFD;          $code = 0xFFFD;
2989        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2990          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2991            !!!parse-error (type => 'invalid character reference',
2992                            text => (sprintf 'U-%08X', $code),
2993                            line => $l, column => $c);
2994          $code = 0xFFFD;          $code = 0xFFFD;
2995        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2996          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2997            !!!parse-error (type => 'CR character reference',
2998                            line => $l, column => $c);
2999          $code = 0x000A;          $code = 0x000A;
3000        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3001          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
3002            !!!parse-error (type => 'C1 character reference',
3003                            text => (sprintf 'U+%04X', $code),
3004                            line => $l, column => $c);
3005          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3006        }        }
3007                
3008        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
3009                  line => $l, column => $c,
3010                 };
3011      } else {      } else {
3012        !!!parse-error (type => 'bare nero');        !!!cp (1019);
3013        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
3014        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
3015          $self->{next_char} = 0x0023; # #
3016        return undef;        return undef;
3017      }      }
3018    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
3019              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
3020             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
3021              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
3022      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
3023      !!!next-input-character;      !!!next-input-character;
3024    
3025      my $value = $entity_name;      my $value = $entity_name;
# Line 1811  sub _tokenize_attempt_to_consume_an_enti Line 3027  sub _tokenize_attempt_to_consume_an_enti
3027      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
3028      our $EntityChar;      our $EntityChar;
3029    
3030      while (length $entity_name < 10 and      while (length $entity_name < 30 and
3031             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
3032             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
3033               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
3034              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
3035               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
3036              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
3037               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
3038              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
3039        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
3040        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
3041          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
3042              !!!cp (1020);
3043            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3044            $match = 1;            $match = 1;
3045            !!!next-input-character;            !!!next-input-character;
3046            last;            last;
3047          } else {          } else {
3048              !!!cp (1021);
3049            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
3050            $match = -1;            $match = -1;
3051            !!!next-input-character;            !!!next-input-character;
3052          }          }
3053        } else {        } else {
3054          $value .= chr $self->{next_input_character};          !!!cp (1022);
3055            $value .= chr $self->{next_char};
3056          $match *= 2;          $match *= 2;
3057          !!!next-input-character;          !!!next-input-character;
3058        }        }
3059      }      }
3060            
3061      if ($match > 0) {      if ($match > 0) {
3062        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
3063          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3064                  line => $l, column => $c,
3065                 };
3066      } elsif ($match < 0) {      } elsif ($match < 0) {
3067        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
3068        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
3069          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
3070        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
3071          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
3072                   };
3073          } else {
3074            !!!cp (1025);
3075            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
3076                    line => $l, column => $c,
3077                   };
3078        }        }
3079      } else {      } else {
3080        !!!parse-error (type => 'bare ero');        !!!cp (1026);
3081        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
3082        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
3083          return {type => CHARACTER_TOKEN, data => '&'.$value,
3084                  line => $l, column => $c,
3085                 };
3086      }      }
3087    } else {    } else {
3088        !!!cp (1027);
3089      ## no characters are consumed      ## no characters are consumed
3090      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
3091      return undef;      return undef;
3092    }    }
3093  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1867  sub _initialize_tree_constructor ($) { Line 3099  sub _initialize_tree_constructor ($) {
3099    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3100    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3101    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3102      $self->{document}->set_user_data (manakai_source_line => 1);
3103      $self->{document}->set_user_data (manakai_source_column => 1);
3104  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3105    
3106  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1893  sub _construct_tree ($) { Line 3127  sub _construct_tree ($) {
3127        
3128    !!!next-token;    !!!next-token;
3129    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3130    undef $self->{form_element};    undef $self->{form_element};
3131    undef $self->{head_element};    undef $self->{head_element};
3132    $self->{open_elements} = [];    $self->{open_elements} = [];
3133    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3134    
3135      ## NOTE: The "initial" insertion mode.
3136    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3137    
3138      ## NOTE: The "before html" insertion mode.
3139    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3140      $self->{insertion_mode} = BEFORE_HEAD_IM;
3141    
3142      ## NOTE: The "before head" insertion mode and so on.
3143    $self->_tree_construction_main;    $self->_tree_construction_main;
3144  } # _construct_tree  } # _construct_tree
3145    
3146  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3147    my $self = shift;    my $self = shift;
3148    
3149      ## NOTE: "initial" insertion mode
3150    
3151    INITIAL: {    INITIAL: {
3152      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3153        ## 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 3155  sub _tree_construction_initial ($) {
3155        ## language.        ## language.
3156        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3157        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3158        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3159        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3160            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3161          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3162            !!!parse-error (type => 'not HTML5', token => $token);
3163        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3164          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3165          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3166          } elsif (defined $token->{public_identifier}) {
3167            if ($token->{public_identifier} eq 'XSLT-compat') {
3168              !!!cp ('t1.2');
3169              !!!parse-error (type => 'XSLT-compat', token => $token,
3170                              level => $self->{level}->{should});
3171            } else {
3172              !!!parse-error (type => 'not HTML5', token => $token);
3173            }
3174          } else {
3175            !!!cp ('t3');
3176            #
3177        }        }
3178                
3179        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3180          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3181          ## NOTE: Default value for both |public_id| and |system_id| attributes
3182          ## are empty strings, so that we don't set any value in missing cases.
3183        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3184            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3185        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1933  sub _tree_construction_initial ($) { Line 3188  sub _tree_construction_initial ($) {
3188        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3189        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3190                
3191        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3192            !!!cp ('t4');
3193          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3194        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3195          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3196          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3197          if ({          my $prefix = [
3198            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3199            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3200            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3201            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3202            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3203            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3204            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3205            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3206            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3207            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3208            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3209            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3210            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3211            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3212            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3213            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3214            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3215            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3216            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3217            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3218            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3219            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3220            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3221            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3222            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3223            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3224            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3225            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3226            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3227            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3228            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3229            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3230            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3231            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3232            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3233            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3234            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3235            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3236            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3237            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3238            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3239            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3240            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3241            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3242            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3243            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3244            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3245            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3246            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3247            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3248            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3249            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3250            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3251            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3252            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3253            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3254            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3255            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3256            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3257            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3258            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3259            "-//W3C//DTD W3 HTML//EN" => 1,            }
3260            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3261            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3262            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3263            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3264            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3265            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3266            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3267          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3268                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3269            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3270                !!!cp ('t6');
3271              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3272            } else {            } else {
3273                !!!cp ('t7');
3274              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3275            }            }
3276          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3277                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3278              !!!cp ('t8');
3279            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3280            } else {
3281              !!!cp ('t9');
3282          }          }
3283          } else {
3284            !!!cp ('t10');
3285        }        }
3286        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3287          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3288          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3289          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") {
3290              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3291              ## marked as quirks.
3292            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3293              !!!cp ('t11');
3294            } else {
3295              !!!cp ('t12');
3296          }          }
3297          } else {
3298            !!!cp ('t13');
3299        }        }
3300                
3301        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3302        !!!next-token;        !!!next-token;
3303        return;        return;
3304      } elsif ({      } elsif ({
# Line 2038  sub _tree_construction_initial ($) { Line 3306  sub _tree_construction_initial ($) {
3306                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3307                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3308               }->{$token->{type}}) {               }->{$token->{type}}) {
3309        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3310          !!!parse-error (type => 'no DOCTYPE', token => $token);
3311        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3312        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3313        ## reprocess        ## reprocess
3314          !!!ack-later;
3315        return;        return;
3316      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3317        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3318          ## Ignore the token          ## Ignore the token
3319    
3320          unless (length $token->{data}) {          unless (length $token->{data}) {
3321            ## Stay in the phase            !!!cp ('t15');
3322              ## Stay in the insertion mode.
3323            !!!next-token;            !!!next-token;
3324            redo INITIAL;            redo INITIAL;
3325            } else {
3326              !!!cp ('t16');
3327          }          }
3328          } else {
3329            !!!cp ('t17');
3330        }        }
3331    
3332        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3333        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3334        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3335        ## reprocess        ## reprocess
3336        return;        return;
3337      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3338          !!!cp ('t18');
3339        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3340        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3341                
3342        ## Stay in the phase.        ## Stay in the insertion mode.
3343        !!!next-token;        !!!next-token;
3344        redo INITIAL;        redo INITIAL;
3345      } else {      } else {
3346        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3347      }      }
3348    } # INITIAL    } # INITIAL
3349    
3350      die "$0: _tree_construction_initial: This should be never reached";
3351  } # _tree_construction_initial  } # _tree_construction_initial
3352    
3353  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3354    my $self = shift;    my $self = shift;
3355    
3356      ## NOTE: "before html" insertion mode.
3357        
3358    B: {    B: {
3359        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3360          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3361            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3362          ## Ignore the token          ## Ignore the token
3363          ## Stay in the phase          ## Stay in the insertion mode.
3364          !!!next-token;          !!!next-token;
3365          redo B;          redo B;
3366        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3367            !!!cp ('t20');
3368          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3369          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3370          ## Stay in the phase          ## Stay in the insertion mode.
3371          !!!next-token;          !!!next-token;
3372          redo B;          redo B;
3373        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2093  sub _tree_construction_root_element ($) Line 3375  sub _tree_construction_root_element ($)
3375            ## Ignore the token.            ## Ignore the token.
3376    
3377            unless (length $token->{data}) {            unless (length $token->{data}) {
3378              ## Stay in the phase              !!!cp ('t21');
3379                ## Stay in the insertion mode.
3380              !!!next-token;              !!!next-token;
3381              redo B;              redo B;
3382              } else {
3383                !!!cp ('t22');
3384            }            }
3385            } else {
3386              !!!cp ('t23');
3387          }          }
3388    
3389          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3390    
3391          #          #
3392        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3393          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3394              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
3395            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3396                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3397            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3398                  [$root_element, $el_category->{html}];
3399    
3400              if ($token->{attributes}->{manifest}) {
3401                !!!cp ('t24');
3402                $self->{application_cache_selection}
3403                    ->($token->{attributes}->{manifest}->{value});
3404                ## ISSUE: Spec is unclear on relative references.
3405                ## According to Hixie (#whatwg 2008-03-19), it should be
3406                ## resolved against the base URI of the document in HTML
3407                ## or xml:base of the element in XHTML.
3408              } else {
3409                !!!cp ('t25');
3410                $self->{application_cache_selection}->(undef);
3411              }
3412    
3413              !!!nack ('t25c');
3414    
3415              !!!next-token;
3416              return; ## Go to the "before head" insertion mode.
3417          } else {          } else {
3418            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3419              #
3420          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3421        } elsif ({        } elsif ({
3422                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3423                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3424                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3425          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3426          #          #
3427        } else {        } else {
3428          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3429        }        }
3430    
3431        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3432        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3433        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3434        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3435        #redo B;  
3436        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3437    
3438        ## NOTE: Reprocess the token.
3439        !!!ack-later;
3440        return; ## Go to the "before head" insertion mode.
3441    
3442        ## ISSUE: There is an issue in the spec
3443    } # B    } # B
3444    
3445      die "$0: _tree_construction_root_element: This should never be reached";
3446  } # _tree_construction_root_element  } # _tree_construction_root_element
3447    
3448  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2147  sub _reset_insertion_mode ($) { Line 3457  sub _reset_insertion_mode ($) {
3457            
3458      ## Step 3      ## Step 3
3459      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"!?  
3460        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3461          $last = 1;          $last = 1;
3462          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3463            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3464                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3465              #          } else {
3466            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3467          }          }
3468        }        }
3469              
3470        ## Step 4..13        ## Step 4..14
3471        my $new_mode = {        my $new_mode;
3472          if ($node->[1] & FOREIGN_EL) {
3473            !!!cp ('t28.1');
3474            ## NOTE: Strictly spaking, the line below only applies to MathML and
3475            ## SVG elements.  Currently the HTML syntax supports only MathML and
3476            ## SVG elements as foreigners.
3477            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3478          } elsif ($node->[1] & TABLE_CELL_EL) {
3479            if ($last) {
3480              !!!cp ('t28.2');
3481              #
3482            } else {
3483              !!!cp ('t28.3');
3484              $new_mode = IN_CELL_IM;
3485            }
3486          } else {
3487            !!!cp ('t28.4');
3488            $new_mode = {
3489                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3490                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3491                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3492                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3493                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3494                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2179  sub _reset_insertion_mode ($) { Line 3499  sub _reset_insertion_mode ($) {
3499                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3500                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3501                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3502                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3503          }
3504        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3505                
3506        ## Step 14        ## Step 15
3507        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3508          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3509              !!!cp ('t29');
3510            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3511          } else {          } else {
3512              ## ISSUE: Can this state be reached?
3513              !!!cp ('t30');
3514            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3515          }          }
3516          return;          return;
3517          } else {
3518            !!!cp ('t31');
3519        }        }
3520                
3521        ## Step 15        ## Step 16
3522        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3523                
3524        ## Step 16        ## Step 17
3525        $i--;        $i--;
3526        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3527                
3528        ## Step 17        ## Step 18
3529        redo S3;        redo S3;
3530      } # S3      } # S3
3531    
3532      die "$0: _reset_insertion_mode: This line should never be reached";
3533  } # _reset_insertion_mode  } # _reset_insertion_mode
3534    
3535  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2223  sub _tree_construction_main ($) { Line 3551  sub _tree_construction_main ($) {
3551      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3552      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3553        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3554            !!!cp ('t32');
3555          return;          return;
3556        }        }
3557      }      }
# Line 2237  sub _tree_construction_main ($) { Line 3566  sub _tree_construction_main ($) {
3566    
3567        ## Step 6        ## Step 6
3568        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3569            !!!cp ('t33_1');
3570          #          #
3571        } else {        } else {
3572          my $in_open_elements;          my $in_open_elements;
3573          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3574            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3575                !!!cp ('t33');
3576              $in_open_elements = 1;              $in_open_elements = 1;
3577              last OE;              last OE;
3578            }            }
3579          }          }
3580          if ($in_open_elements) {          if ($in_open_elements) {
3581              !!!cp ('t34');
3582            #            #
3583          } else {          } else {
3584              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3585              !!!cp ('t35');
3586            redo S4;            redo S4;
3587          }          }
3588        }        }
# Line 2271  sub _tree_construction_main ($) { Line 3605  sub _tree_construction_main ($) {
3605    
3606        ## Step 11        ## Step 11
3607        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3608            !!!cp ('t36');
3609          ## Step 7'          ## Step 7'
3610          $i++;          $i++;
3611          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3612                    
3613          redo S7;          redo S7;
3614        }        }
3615    
3616          !!!cp ('t37');
3617      } # S7      } # S7
3618    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3619    
3620    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3621      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3622        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3623            !!!cp ('t38');
3624          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3625          return;          return;
3626        }        }
3627      }      }
3628    
3629        !!!cp ('t39');
3630    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3631    
3632    my $parse_rcdata = sub ($$) {    my $insert;
3633      my ($content_model_flag, $insert) = @_;  
3634      my $parse_rcdata = sub ($) {
3635        my ($content_model_flag) = @_;
3636    
3637      ## Step 1      ## Step 1
3638      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3639      my $el;      my $el;
3640      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3641    
3642      ## Step 2      ## Step 2
3643      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3644    
3645      ## Step 3      ## Step 3
3646      $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 3648  sub _tree_construction_main ($) {
3648    
3649      ## Step 4      ## Step 4
3650      my $text = '';      my $text = '';
3651        !!!nack ('t40.1');
3652      !!!next-token;      !!!next-token;
3653      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3654          !!!cp ('t40');
3655        $text .= $token->{data};        $text .= $token->{data};
3656        !!!next-token;        !!!next-token;
3657      }      }
3658    
3659      ## Step 5      ## Step 5
3660      if (length $text) {      if (length $text) {
3661          !!!cp ('t41');
3662        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3663        $el->append_child ($text);        $el->append_child ($text);
3664      }      }
# Line 2322  sub _tree_construction_main ($) { Line 3667  sub _tree_construction_main ($) {
3667      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3668    
3669      ## Step 7      ## Step 7
3670      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3671            $token->{tag_name} eq $start_tag_name) {
3672          !!!cp ('t42');
3673        ## 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});  
3674      } else {      } else {
3675        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3676          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3677            !!!cp ('t43');
3678            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3679          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3680            !!!cp ('t44');
3681            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3682          } else {
3683            die "$0: $content_model_flag in parse_rcdata";
3684          }
3685      }      }
3686      !!!next-token;      !!!next-token;
3687    }; # $parse_rcdata    }; # $parse_rcdata
3688    
3689    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3690      my $script_el;      my $script_el;
3691      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3692      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3693    
3694      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3695      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3696            
3697      my $text = '';      my $text = '';
3698        !!!nack ('t45.1');
3699      !!!next-token;      !!!next-token;
3700      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3701          !!!cp ('t45');
3702        $text .= $token->{data};        $text .= $token->{data};
3703        !!!next-token;        !!!next-token;
3704      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3705      if (length $text) {      if (length $text) {
3706          !!!cp ('t46');
3707        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3708      }      }
3709                                
# Line 2357  sub _tree_construction_main ($) { Line 3711  sub _tree_construction_main ($) {
3711    
3712      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3713          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3714          !!!cp ('t47');
3715        ## Ignore the token        ## Ignore the token
3716      } else {      } else {
3717        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3718          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3719        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3720        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3721      }      }
3722            
3723      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3724          !!!cp ('t49');
3725        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3726      } else {      } else {
3727          !!!cp ('t50');
3728        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3729        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3730    
# Line 2380  sub _tree_construction_main ($) { Line 3738  sub _tree_construction_main ($) {
3738      !!!next-token;      !!!next-token;
3739    }; # $script_start_tag    }; # $script_start_tag
3740    
3741      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3742      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3743      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3744    
3745    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3746      my $tag_name = shift;      my $end_tag_token = shift;
3747        my $tag_name = $end_tag_token->{tag_name};
3748    
3749        ## NOTE: The adoption agency algorithm (AAA).
3750    
3751      FET: {      FET: {
3752        ## Step 1        ## Step 1
3753        my $formatting_element;        my $formatting_element;
3754        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3755        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3756          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3757              !!!cp ('t52');
3758              last AFE;
3759            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3760                         eq $tag_name) {
3761              !!!cp ('t51');
3762            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3763            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3764            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3765          }          }
3766        } # AFE        } # AFE
3767        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3768          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3769            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3770          ## Ignore the token          ## Ignore the token
3771          !!!next-token;          !!!next-token;
3772          return;          return;
# Line 2409  sub _tree_construction_main ($) { Line 3778  sub _tree_construction_main ($) {
3778          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3779          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3780            if ($in_scope) {            if ($in_scope) {
3781                !!!cp ('t54');
3782              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3783              last INSCOPE;              last INSCOPE;
3784            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3785              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3786                !!!parse-error (type => 'unmatched end tag',
3787                                text => $token->{tag_name},
3788                                token => $end_tag_token);
3789              ## Ignore the token              ## Ignore the token
3790              !!!next-token;              !!!next-token;
3791              return;              return;
3792            }            }
3793          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3794                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3795            $in_scope = 0;            $in_scope = 0;
3796          }          }
3797        } # INSCOPE        } # INSCOPE
3798        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3799          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3800            !!!parse-error (type => 'unmatched end tag',
3801                            text => $token->{tag_name},
3802                            token => $end_tag_token);
3803          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3804          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3805          return;          return;
3806        }        }
3807        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3808          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3809            !!!parse-error (type => 'not closed',
3810                            text => $self->{open_elements}->[-1]->[0]
3811                                ->manakai_local_name,
3812                            token => $end_tag_token);
3813        }        }
3814                
3815        ## Step 2        ## Step 2
# Line 2439  sub _tree_construction_main ($) { Line 3817  sub _tree_construction_main ($) {
3817        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3818        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3819          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3820          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3821              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3822              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3823               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3824              !!!cp ('t59');
3825            $furthest_block = $node;            $furthest_block = $node;
3826            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3827          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3828              !!!cp ('t60');
3829            last OE;            last OE;
3830          }          }
3831        } # OE        } # OE
3832                
3833        ## Step 3        ## Step 3
3834        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3835            !!!cp ('t61');
3836          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3837          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3838          !!!next-token;          !!!next-token;
# Line 2464  sub _tree_construction_main ($) { Line 3845  sub _tree_construction_main ($) {
3845        ## Step 5        ## Step 5
3846        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3847        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3848            !!!cp ('t62');
3849          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3850        }        }
3851                
# Line 2486  sub _tree_construction_main ($) { Line 3868  sub _tree_construction_main ($) {
3868          S7S2: {          S7S2: {
3869            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3870              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3871                  !!!cp ('t63');
3872                $node_i_in_active = $_;                $node_i_in_active = $_;
3873                last S7S2;                last S7S2;
3874              }              }
# Line 2499  sub _tree_construction_main ($) { Line 3882  sub _tree_construction_main ($) {
3882                    
3883          ## Step 4          ## Step 4
3884          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3885              !!!cp ('t64');
3886            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3887          }          }
3888                    
3889          ## Step 5          ## Step 5
3890          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3891              !!!cp ('t65');
3892            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3893            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3894            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2521  sub _tree_construction_main ($) { Line 3906  sub _tree_construction_main ($) {
3906        } # S7          } # S7  
3907                
3908        ## Step 8        ## Step 8
3909        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3910            my $foster_parent_element;
3911            my $next_sibling;
3912            OE: for (reverse 0..$#{$self->{open_elements}}) {
3913              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3914                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3915                                 if (defined $parent and $parent->node_type == 1) {
3916                                   !!!cp ('t65.1');
3917                                   $foster_parent_element = $parent;
3918                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3919                                 } else {
3920                                   !!!cp ('t65.2');
3921                                   $foster_parent_element
3922                                     = $self->{open_elements}->[$_ - 1]->[0];
3923                                 }
3924                                 last OE;
3925                               }
3926                             } # OE
3927                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3928                               unless defined $foster_parent_element;
3929            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3930            $open_tables->[-1]->[1] = 1; # tainted
3931          } else {
3932            !!!cp ('t65.3');
3933            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3934          }
3935                
3936        ## Step 9        ## Step 9
3937        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2538  sub _tree_construction_main ($) { Line 3948  sub _tree_construction_main ($) {
3948        my $i;        my $i;
3949        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3950          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3951              !!!cp ('t66');
3952            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3953            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3954          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3955              !!!cp ('t67');
3956            $i = $_;            $i = $_;
3957          }          }
3958        } # AFE        } # AFE
# Line 2550  sub _tree_construction_main ($) { Line 3962  sub _tree_construction_main ($) {
3962        undef $i;        undef $i;
3963        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3964          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3965              !!!cp ('t68');
3966            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3967            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3968          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3969              !!!cp ('t69');
3970            $i = $_;            $i = $_;
3971          }          }
3972        } # OE        } # OE
# Line 2563  sub _tree_construction_main ($) { Line 3977  sub _tree_construction_main ($) {
3977      } # FET      } # FET
3978    }; # $formatting_end_tag    }; # $formatting_end_tag
3979    
3980    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3981      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3982    }; # $insert_to_current    }; # $insert_to_current
3983    
3984    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3985                         my $child = shift;      my $child = shift;
3986                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3987                              table => 1, tbody => 1, tfoot => 1,        # MUST
3988                              thead => 1, tr => 1,        my $foster_parent_element;
3989                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3990                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3991                           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') {  
3992                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3993                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3994                                   !!!cp ('t70');
3995                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3996                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3997                               } else {                               } else {
3998                                   !!!cp ('t71');
3999                                 $foster_parent_element                                 $foster_parent_element
4000                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4001                               }                               }
# Line 2593  sub _tree_construction_main ($) { Line 4006  sub _tree_construction_main ($) {
4006                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4007                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4008                             ($child, $next_sibling);                             ($child, $next_sibling);
4009                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4010                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4011                         }        !!!cp ('t72');
4012          $self->{open_elements}->[-1]->[0]->append_child ($child);
4013        }
4014    }; # $insert_to_foster    }; # $insert_to_foster
4015    
4016    my $insert;    B: while (1) {
   
   B: {  
4017      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4018        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
4019          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4020        ## Ignore the token        ## Ignore the token
4021        ## Stay in the phase        ## Stay in the phase
4022        !!!next-token;        !!!next-token;
4023        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;  
4024      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4025               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4026        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4027          ## Turn into the main phase          !!!cp ('t79');
4028          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4029          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4030        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4031          ## Turn into the main phase          !!!cp ('t80');
4032          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4033          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4034          } else {
4035            !!!cp ('t81');
4036        }        }
4037    
4038  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4039  ## 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');  
       }  
4040        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4041        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4042          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4043              !!!cp ('t84');
4044            $top_el->set_attribute_ns            $top_el->set_attribute_ns
4045              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
4046               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4047          }          }
4048        }        }
4049          !!!nack ('t84.1');
4050        !!!next-token;        !!!next-token;
4051        redo B;        next B;
4052      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4053        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4054        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4055            !!!cp ('t85');
4056          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
4057        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4058            !!!cp ('t86');
4059          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
4060        } else {        } else {
4061            !!!cp ('t87');
4062          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4063        }        }
4064        !!!next-token;        !!!next-token;
4065        redo B;        next B;
4066      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4067          if ($token->{type} == CHARACTER_TOKEN) {
4068            !!!cp ('t87.1');
4069            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4070            !!!next-token;
4071            next B;
4072          } elsif ($token->{type} == START_TAG_TOKEN) {
4073            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4074                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4075                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4076                ($token->{tag_name} eq 'svg' and
4077                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4078              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4079              !!!cp ('t87.2');
4080              #
4081            } elsif ({
4082                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4083                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4084                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4085                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4086                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4087                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4088                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4089                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4090                     }->{$token->{tag_name}}) {
4091              !!!cp ('t87.2');
4092              !!!parse-error (type => 'not closed',
4093                              text => $self->{open_elements}->[-1]->[0]
4094                                  ->manakai_local_name,
4095                              token => $token);
4096    
4097              pop @{$self->{open_elements}}
4098                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4099    
4100              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4101              ## Reprocess.
4102              next B;
4103            } else {
4104              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4105              my $tag_name = $token->{tag_name};
4106              if ($nsuri eq $SVG_NS) {
4107                $tag_name = {
4108                   altglyph => 'altGlyph',
4109                   altglyphdef => 'altGlyphDef',
4110                   altglyphitem => 'altGlyphItem',
4111                   animatecolor => 'animateColor',
4112                   animatemotion => 'animateMotion',
4113                   animatetransform => 'animateTransform',
4114                   clippath => 'clipPath',
4115                   feblend => 'feBlend',
4116                   fecolormatrix => 'feColorMatrix',
4117                   fecomponenttransfer => 'feComponentTransfer',
4118                   fecomposite => 'feComposite',
4119                   feconvolvematrix => 'feConvolveMatrix',
4120                   fediffuselighting => 'feDiffuseLighting',
4121                   fedisplacementmap => 'feDisplacementMap',
4122                   fedistantlight => 'feDistantLight',
4123                   feflood => 'feFlood',
4124                   fefunca => 'feFuncA',
4125                   fefuncb => 'feFuncB',
4126                   fefuncg => 'feFuncG',
4127                   fefuncr => 'feFuncR',
4128                   fegaussianblur => 'feGaussianBlur',
4129                   feimage => 'feImage',
4130                   femerge => 'feMerge',
4131                   femergenode => 'feMergeNode',
4132                   femorphology => 'feMorphology',
4133                   feoffset => 'feOffset',
4134                   fepointlight => 'fePointLight',
4135                   fespecularlighting => 'feSpecularLighting',
4136                   fespotlight => 'feSpotLight',
4137                   fetile => 'feTile',
4138                   feturbulence => 'feTurbulence',
4139                   foreignobject => 'foreignObject',
4140                   glyphref => 'glyphRef',
4141                   lineargradient => 'linearGradient',
4142                   radialgradient => 'radialGradient',
4143                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4144                   textpath => 'textPath',  
4145                }->{$tag_name} || $tag_name;
4146              }
4147    
4148              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4149    
4150              ## "adjust foreign attributes" - done in insert-element-f
4151    
4152              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4153    
4154              if ($self->{self_closing}) {
4155                pop @{$self->{open_elements}};
4156                !!!ack ('t87.3');
4157              } else {
4158                !!!cp ('t87.4');
4159              }
4160    
4161              !!!next-token;
4162              next B;
4163            }
4164          } elsif ($token->{type} == END_TAG_TOKEN) {
4165            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4166            !!!cp ('t87.5');
4167            #
4168          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4169            !!!cp ('t87.6');
4170            !!!parse-error (type => 'not closed',
4171                            text => $self->{open_elements}->[-1]->[0]
4172                                ->manakai_local_name,
4173                            token => $token);
4174    
4175            pop @{$self->{open_elements}}
4176                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4177    
4178            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4179            ## Reprocess.
4180            next B;
4181          } else {
4182            die "$0: $token->{type}: Unknown token type";        
4183          }
4184        }
4185    
4186        if ($self->{insertion_mode} & HEAD_IMS) {
4187        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4188          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4189            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4190                !!!cp ('t88.2');
4191                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4192              } else {
4193                !!!cp ('t88.1');
4194                ## Ignore the token.
4195                !!!next-token;
4196                next B;
4197              }
4198            unless (length $token->{data}) {            unless (length $token->{data}) {
4199                !!!cp ('t88');
4200              !!!next-token;              !!!next-token;
4201              redo B;              next B;
4202            }            }
4203          }          }
4204    
4205          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4206              !!!cp ('t89');
4207            ## As if <head>            ## As if <head>
4208            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4209            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4210            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4211                  [$self->{head_element}, $el_category->{head}];
4212    
4213            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4214            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4215    
4216            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4217          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4218              !!!cp ('t90');
4219            ## As if </noscript>            ## As if </noscript>
4220            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4221            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4222                        
4223            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4224            ## As if </head>            ## As if </head>
# Line 2704  sub _tree_construction_main ($) { Line 4226  sub _tree_construction_main ($) {
4226    
4227            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4228          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4229              !!!cp ('t91');
4230            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4231    
4232            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4233            } else {
4234              !!!cp ('t92');
4235          }          }
4236    
4237              ## "after head" insertion mode          ## "after head" insertion mode
4238              ## As if <body>          ## As if <body>
4239              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4240              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4241              ## reprocess          ## reprocess
4242              redo B;          next B;
4243            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4244              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4245                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4246                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4247                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4248                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4249                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4250                  !!!next-token;              push @{$self->{open_elements}},
4251                  redo B;                  [$self->{head_element}, $el_category->{head}];
4252                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4253                  #              !!!nack ('t93.1');
4254                } else {              !!!next-token;
4255                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4256                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4257                  !!!next-token;              !!!cp ('t93.2');
4258                  redo B;              !!!parse-error (type => 'after head', text => 'head',
4259                }                              token => $token);
4260              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              ## Ignore the token
4261                ## As if <head>              !!!nack ('t93.3');
4262                !!!create-element ($self->{head_element}, 'head');              !!!next-token;
4263                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              next B;
4264                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
4265                !!!cp ('t95');
4266                !!!parse-error (type => 'in head:head',
4267                                token => $token); # or in head noscript
4268                ## Ignore the token
4269                !!!nack ('t95.1');
4270                !!!next-token;
4271                next B;
4272              }
4273            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4274              !!!cp ('t96');
4275              ## As if <head>
4276              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4277              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4278              push @{$self->{open_elements}},
4279                  [$self->{head_element}, $el_category->{head}];
4280    
4281                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4282                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4283              }          } else {
4284              !!!cp ('t97');
4285            }
4286    
4287              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4288                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4289                    !!!cp ('t98');
4290                  ## As if </noscript>                  ## As if </noscript>
4291                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4292                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4293                                    token => $token);
4294                                
4295                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4296                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4297                  } else {
4298                    !!!cp ('t99');
4299                }                }
4300    
4301                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4302                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4303                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4304                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4305                                    text => $token->{tag_name}, token => $token);
4306                    push @{$self->{open_elements}},
4307                        [$self->{head_element}, $el_category->{head}];
4308                  } else {
4309                    !!!cp ('t101');
4310                }                }
4311                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4312                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4313                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4314                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4315                  !!!nack ('t101.1');
4316                !!!next-token;                !!!next-token;
4317                redo B;                next B;
4318              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4319                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4320                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4321                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4322                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4323                                    text => $token->{tag_name}, token => $token);
4324                    push @{$self->{open_elements}},
4325                        [$self->{head_element}, $el_category->{head}];
4326                  } else {
4327                    !!!cp ('t103');
4328                }                }
4329                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4330                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4331                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4332                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4333                  !!!ack ('t103.1');
4334                !!!next-token;                !!!next-token;
4335                redo B;                next B;
4336              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4337                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4338                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4339                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4340                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4341                                    text => $token->{tag_name}, token => $token);
4342                    push @{$self->{open_elements}},
4343                        [$self->{head_element}, $el_category->{head}];
4344                  } else {
4345                    !!!cp ('t105');
4346                }                }
4347                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4348                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.
4349    
4350                unless ($self->{confident}) {                unless ($self->{confident}) {
4351                  my $charset;                  if ($token->{attributes}->{charset}) {
4352                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4353                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4354                  }                    ## in the {change_encoding} callback.
4355                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4356                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4357                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4358                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4359                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4360                          ->set_user_data (manakai_has_reference =>
4361                                               $token->{attributes}->{charset}
4362                                                   ->{has_reference});
4363                    } elsif ($token->{attributes}->{content}) {
4364                      if ($token->{attributes}->{content}->{value}
4365                          =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4366                              [\x09-\x0D\x20]*=
4367                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4368                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4369                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4370                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4371                        ## in the {change_encoding} callback.
4372                        $self->{change_encoding}
4373                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4374                               $token);
4375                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4376                            ->set_user_data (manakai_has_reference =>
4377                                                 $token->{attributes}->{content}
4378                                                       ->{has_reference});
4379                      } else {
4380                        !!!cp ('t108');
4381                      }
4382                    }
4383                  } else {
4384                    if ($token->{attributes}->{charset}) {
4385                      !!!cp ('t109');
4386                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4387                          ->set_user_data (manakai_has_reference =>
4388                                               $token->{attributes}->{charset}
4389                                                   ->{has_reference});
4390                    }
4391                    if ($token->{attributes}->{content}) {
4392                      !!!cp ('t110');
4393                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4394                          ->set_user_data (manakai_has_reference =>
4395                                               $token->{attributes}->{content}
4396                                                   ->{has_reference});
4397                  }                  }
                 ## TODO: Change the encoding  
4398                }                }
4399    
4400                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4401                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4402                  !!!ack ('t110.1');
4403                !!!next-token;                !!!next-token;
4404                redo B;                next B;
4405              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4406                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4407                    !!!cp ('t111');
4408                  ## As if </noscript>                  ## As if </noscript>
4409                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4410                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4411                                    token => $token);
4412                                
4413                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4414                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4415                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4416                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4417                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4418                                    text => $token->{tag_name}, token => $token);
4419                    push @{$self->{open_elements}},
4420                        [$self->{head_element}, $el_category->{head}];
4421                  } else {
4422                    !!!cp ('t113');
4423                }                }
4424    
4425                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4426                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4427                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4428                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4429                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4430                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4431                redo B;                next B;
4432              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4433                         $token->{tag_name} eq 'noframes') {
4434                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4435                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4436                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4437                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4438                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4439                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4440                                    text => $token->{tag_name}, token => $token);
4441                    push @{$self->{open_elements}},
4442                        [$self->{head_element}, $el_category->{head}];
4443                  } else {
4444                    !!!cp ('t115');
4445                }                }
4446                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4447                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4448                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4449                redo B;                next B;
4450              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4451                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4452                    !!!cp ('t116');
4453                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4454                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4455                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4456                    !!!nack ('t116.1');
4457                  !!!next-token;                  !!!next-token;
4458                  redo B;                  next B;
4459                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4460                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4461                    !!!parse-error (type => 'in noscript', text => 'noscript',
4462                                    token => $token);
4463                  ## Ignore the token                  ## Ignore the token
4464                    !!!nack ('t117.1');
4465                  !!!next-token;                  !!!next-token;
4466                  redo B;                  next B;
4467                } else {                } else {
4468                    !!!cp ('t118');
4469                  #                  #
4470                }                }
4471              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4472                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4473                    !!!cp ('t119');
4474                  ## As if </noscript>                  ## As if </noscript>
4475                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4476                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4477                                    token => $token);
4478                                
4479                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4480                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4481                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4482                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4483                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4484                                    text => $token->{tag_name}, token => $token);
4485                    push @{$self->{open_elements}},
4486                        [$self->{head_element}, $el_category->{head}];
4487                  } else {
4488                    !!!cp ('t121');
4489                }                }
4490    
4491                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4492                $script_start_tag->($insert_to_current);                $script_start_tag->();
4493                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4494                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4495                redo B;                next B;
4496              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4497                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4498                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4499                    !!!cp ('t122');
4500                  ## As if </noscript>                  ## As if </noscript>
4501                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4502                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4503                                    text => $token->{tag_name}, token => $token);
4504                                    
4505                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4506                  ## As if </head>                  ## As if </head>
# Line 2885  sub _tree_construction_main ($) { Line 4508  sub _tree_construction_main ($) {
4508                                    
4509                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4510                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4511                    !!!cp ('t124');
4512                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4513                                    
4514                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4515                  } else {
4516                    !!!cp ('t125');
4517                }                }
4518    
4519                ## "after head" insertion mode                ## "after head" insertion mode
4520                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4521                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4522                    !!!cp ('t126');
4523                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4524                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4525                    !!!cp ('t127');
4526                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4527                } else {                } else {
4528                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4529                }                }
4530                  !!!nack ('t127.1');
4531                !!!next-token;                !!!next-token;
4532                redo B;                next B;
4533              } else {              } else {
4534                  !!!cp ('t128');
4535                #                #
4536              }              }
4537    
4538              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4539                  !!!cp ('t129');
4540                ## As if </noscript>                ## As if </noscript>
4541                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4542                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4543                                  text => $token->{tag_name}, token => $token);
4544                                
4545                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4546                ## As if </head>                ## As if </head>
# Line 2916  sub _tree_construction_main ($) { Line 4548  sub _tree_construction_main ($) {
4548    
4549                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4550              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4551                  !!!cp ('t130');
4552                ## As if </head>                ## As if </head>
4553                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4554    
4555                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4556                } else {
4557                  !!!cp ('t131');
4558              }              }
4559    
4560              ## "after head" insertion mode              ## "after head" insertion mode
4561              ## As if <body>              ## As if <body>
4562              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4563              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4564              ## reprocess              ## reprocess
4565              redo B;              !!!ack-later;
4566                next B;
4567            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4568              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4569                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4570                    !!!cp ('t132');
4571                  ## As if <head>                  ## As if <head>
4572                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4573                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4574                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4575                        [$self->{head_element}, $el_category->{head}];
4576    
4577                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4578                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4579                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4580                  !!!next-token;                  !!!next-token;
4581                  redo B;                  next B;
4582                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4583                    !!!cp ('t133');
4584                  ## As if </noscript>                  ## As if </noscript>
4585                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4586                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
4587                                    text => 'head', token => $token);
4588                                    
4589                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4590                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4591                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4592                  !!!next-token;                  !!!next-token;
4593                  redo B;                  next B;
4594                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4595                    !!!cp ('t134');
4596                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4597                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4598                  !!!next-token;                  !!!next-token;
4599                  redo B;                  next B;
4600                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4601                    !!!cp ('t134.1');
4602                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4603                                    token => $token);
4604                    ## Ignore the token
4605                    !!!next-token;
4606                    next B;
4607                } else {                } else {
4608                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4609                }                }
4610              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4611                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4612                    !!!cp ('t136');
4613                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4614                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4615                  !!!next-token;                  !!!next-token;
4616                  redo B;                  next B;
4617                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4618                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4619                    !!!cp ('t137');
4620                    !!!parse-error (type => 'unmatched end tag',
4621                                    text => 'noscript', token => $token);
4622                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4623                  !!!next-token;                  !!!next-token;
4624                  redo B;                  next B;
4625                } else {                } else {
4626                    !!!cp ('t138');
4627                  #                  #
4628                }                }
4629              } elsif ({              } elsif ({
4630                        body => 1, html => 1,                        body => 1, html => 1,
4631                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4632                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4633                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4634                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4635                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4636                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag',
4637                                    text => $token->{tag_name}, token => $token);
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
4638                  ## Ignore the token                  ## Ignore the token
4639                  !!!next-token;                  !!!next-token;
4640                  redo B;                  next B;
4641                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4642                    !!!cp ('t140.1');
4643                    !!!parse-error (type => 'unmatched end tag',
4644                                    text => $token->{tag_name}, token => $token);
4645                    ## Ignore the token
4646                    !!!next-token;
4647                    next B;
4648                  } else {
4649                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4650                }                }
4651                              } elsif ($token->{tag_name} eq 'p') {
4652                #                !!!cp ('t142');
4653              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4654                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4655                       }->{$token->{tag_name}}) {                ## Ignore the token
4656                  !!!next-token;
4657                  next B;
4658                } elsif ($token->{tag_name} eq 'br') {
4659                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4660                  ## As if <head>                  !!!cp ('t142.2');
4661                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
4662                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4663                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4664                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4665      
4666                    ## Reprocess in the "after head" insertion mode...
4667                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4668                    !!!cp ('t143.2');
4669                    ## As if </head>
4670                    pop @{$self->{open_elements}};
4671                    $self->{insertion_mode} = AFTER_HEAD_IM;
4672      
4673                    ## Reprocess in the "after head" insertion mode...
4674                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4675                    !!!cp ('t143.3');
4676                    ## ISSUE: Two parse errors for <head><noscript></br>
4677                    !!!parse-error (type => 'unmatched end tag',
4678                                    text => 'br', token => $token);
4679                    ## As if </noscript>
4680                    pop @{$self->{open_elements}};
4681                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4682    
4683                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4684                }                  ## As if </head>
4685                    pop @{$self->{open_elements}};
4686                    $self->{insertion_mode} = AFTER_HEAD_IM;
4687    
4688                #                  ## Reprocess in the "after head" insertion mode...
4689              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4690                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
4691                  #                  #
4692                } else {                } else {
4693                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4694                }                }
4695    
4696                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4697                  !!!parse-error (type => 'unmatched end tag',
4698                                  text => 'br', token => $token);
4699                  ## Ignore the token
4700                  !!!next-token;
4701                  next B;
4702                } else {
4703                  !!!cp ('t145');
4704                  !!!parse-error (type => 'unmatched end tag',
4705                                  text => $token->{tag_name}, token => $token);
4706                  ## Ignore the token
4707                  !!!next-token;
4708                  next B;
4709              }              }
4710    
4711              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4712                  !!!cp ('t146');
4713                ## As if </noscript>                ## As if </noscript>
4714                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4715                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4716                                  text => $token->{tag_name}, token => $token);
4717                                
4718                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4719                ## As if </head>                ## As if </head>
# Line 3028  sub _tree_construction_main ($) { Line 4721  sub _tree_construction_main ($) {
4721    
4722                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4723              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4724                  !!!cp ('t147');
4725                ## As if </head>                ## As if </head>
4726                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4727    
4728                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4729              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4730                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4731                  !!!cp ('t148');
4732                  !!!parse-error (type => 'unmatched end tag',
4733                                  text => $token->{tag_name}, token => $token);
4734                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4735                !!!next-token;                !!!next-token;
4736                redo B;                next B;
4737                } else {
4738                  !!!cp ('t149');
4739              }              }
4740    
4741              ## "after head" insertion mode              ## "after head" insertion mode
4742              ## As if <body>              ## As if <body>
4743              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4744              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4745              ## reprocess              ## reprocess
4746              redo B;              next B;
4747            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4748              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4749            }            !!!cp ('t149.1');
4750    
4751              ## NOTE: As if <head>
4752              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4753              $self->{open_elements}->[-1]->[0]->append_child
4754                  ($self->{head_element});
4755              #push @{$self->{open_elements}},
4756              #    [$self->{head_element}, $el_category->{head}];
4757              #$self->{insertion_mode} = IN_HEAD_IM;
4758              ## NOTE: Reprocess.
4759    
4760              ## NOTE: As if </head>
4761              #pop @{$self->{open_elements}};
4762              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4763              ## NOTE: Reprocess.
4764              
4765              #
4766            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4767              !!!cp ('t149.2');
4768    
4769              ## NOTE: As if </head>
4770              pop @{$self->{open_elements}};
4771              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4772              ## NOTE: Reprocess.
4773    
4774              #
4775            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4776              !!!cp ('t149.3');
4777    
4778              !!!parse-error (type => 'in noscript:#eof', token => $token);
4779    
4780              ## As if </noscript>
4781              pop @{$self->{open_elements}};
4782              #$self->{insertion_mode} = IN_HEAD_IM;
4783              ## NOTE: Reprocess.
4784    
4785              ## NOTE: As if </head>
4786              pop @{$self->{open_elements}};
4787              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4788              ## NOTE: Reprocess.
4789    
4790              #
4791            } else {
4792              !!!cp ('t149.4');
4793              #
4794            }
4795    
4796            ## NOTE: As if <body>
4797            !!!insert-element ('body',, $token);
4798            $self->{insertion_mode} = IN_BODY_IM;
4799            ## NOTE: Reprocess.
4800            next B;
4801          } else {
4802            die "$0: $token->{type}: Unknown token type";
4803          }
4804    
4805            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4806      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4807            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4808                !!!cp ('t150');
4809              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4810              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4811                            
4812              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4813    
4814              !!!next-token;              !!!next-token;
4815              redo B;              next B;
4816            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4817              if ({              if ({
4818                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3066  sub _tree_construction_main ($) { Line 4820  sub _tree_construction_main ($) {
4820                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4821                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4822                  ## have an element in table scope                  ## have an element in table scope
4823                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4824                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4825                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4826                      $tn = $node->[1];                      !!!cp ('t151');
4827                      last INSCOPE;  
4828                    } elsif ({                      ## Close the cell
4829                              table => 1, html => 1,                      !!!back-token; # <x>
4830                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4831                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4832                    }                                line => $token->{line},
4833                  } # INSCOPE                                column => $token->{column}};
4834                    unless (defined $tn) {                      next B;
4835                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4836                      ## Ignore the token                      !!!cp ('t152');
4837                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4838                      redo B;                      last;
4839                    }                    }
4840                                    }
4841                  ## Close the cell  
4842                  !!!back-token; # <?>                  !!!cp ('t153');
4843                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4844                  redo B;                      text => $token->{tag_name}, token => $token);
4845                    ## Ignore the token
4846                    !!!nack ('t153.1');
4847                    !!!next-token;
4848                    next B;
4849                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4850                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
4851                                    token => $token);
4852                                    
4853                  ## As if </caption>                  ## NOTE: As if </caption>.
4854                  ## have a table element in table scope                  ## have a table element in table scope
4855                  my $i;                  my $i;
4856                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4857                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4858                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4859                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4860                      last INSCOPE;                        !!!cp ('t155');
4861                    } elsif ({                        $i = $_;
4862                              table => 1, html => 1,                        last INSCOPE;
4863                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4864                      last INSCOPE;                        !!!cp ('t156');
4865                          last;
4866                        }
4867                    }                    }
4868    
4869                      !!!cp ('t157');
4870                      !!!parse-error (type => 'start tag not allowed',
4871                                      text => $token->{tag_name}, token => $token);
4872                      ## Ignore the token
4873                      !!!nack ('t157.1');
4874                      !!!next-token;
4875                      next B;
4876                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4877                                    
4878                  ## generate implied end tags                  ## generate implied end tags
4879                  if ({                  while ($self->{open_elements}->[-1]->[1]
4880                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4881                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4882                       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;  
4883                  }                  }
4884    
4885                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4886                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4887                      !!!parse-error (type => 'not closed',
4888                                      text => $self->{open_elements}->[-1]->[0]
4889                                          ->manakai_local_name,
4890                                      token => $token);
4891                    } else {
4892                      !!!cp ('t160');
4893                  }                  }
4894                                    
4895                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3138  sub _tree_construction_main ($) { Line 4899  sub _tree_construction_main ($) {
4899                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4900                                    
4901                  ## reprocess                  ## reprocess
4902                  redo B;                  !!!ack-later;
4903                    next B;
4904                } else {                } else {
4905                    !!!cp ('t161');
4906                  #                  #
4907                }                }
4908              } else {              } else {
4909                  !!!cp ('t162');
4910                #                #
4911              }              }
4912            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3152  sub _tree_construction_main ($) { Line 4916  sub _tree_construction_main ($) {
4916                  my $i;                  my $i;
4917                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4918                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4919                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4920                        !!!cp ('t163');
4921                      $i = $_;                      $i = $_;
4922                      last INSCOPE;                      last INSCOPE;
4923                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4924                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4925                      last INSCOPE;                      last INSCOPE;
4926                    }                    }
4927                  } # INSCOPE                  } # INSCOPE
4928                    unless (defined $i) {                    unless (defined $i) {
4929                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4930                        !!!parse-error (type => 'unmatched end tag',
4931                                        text => $token->{tag_name},
4932                                        token => $token);
4933                      ## Ignore the token                      ## Ignore the token
4934                      !!!next-token;                      !!!next-token;
4935                      redo B;                      next B;
4936                    }                    }
4937                                    
4938                  ## generate implied end tags                  ## generate implied end tags
4939                  if ({                  while ($self->{open_elements}->[-1]->[1]
4940                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4941                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4942                       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;  
4943                  }                  }
4944                    
4945                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4946                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4947                      !!!cp ('t167');
4948                      !!!parse-error (type => 'not closed',
4949                                      text => $self->{open_elements}->[-1]->[0]
4950                                          ->manakai_local_name,
4951                                      token => $token);
4952                    } else {
4953                      !!!cp ('t168');
4954                  }                  }
4955                                    
4956                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3193  sub _tree_construction_main ($) { Line 4960  sub _tree_construction_main ($) {
4960                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4961                                    
4962                  !!!next-token;                  !!!next-token;
4963                  redo B;                  next B;
4964                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4965                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4966                    !!!parse-error (type => 'unmatched end tag',
4967                                    text => $token->{tag_name}, token => $token);
4968                  ## Ignore the token                  ## Ignore the token
4969                  !!!next-token;                  !!!next-token;
4970                  redo B;                  next B;
4971                } else {                } else {
4972                    !!!cp ('t170');
4973                  #                  #
4974                }                }
4975              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4976                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4977                  ## have a table element in table scope                  ## have a table element in table scope
4978                  my $i;                  my $i;
4979                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4980                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4981                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4982                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4983                      last INSCOPE;                        !!!cp ('t171');
4984                    } elsif ({                        $i = $_;
4985                              table => 1, html => 1,                        last INSCOPE;
4986                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4987                      last INSCOPE;                        !!!cp ('t172');
4988                          last;
4989                        }
4990                    }                    }
4991    
4992                      !!!cp ('t173');
4993                      !!!parse-error (type => 'unmatched end tag',
4994                                      text => $token->{tag_name}, token => $token);
4995                      ## Ignore the token
4996                      !!!next-token;
4997                      next B;
4998                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4999                                    
5000                  ## generate implied end tags                  ## generate implied end tags
5001                  if ({                  while ($self->{open_elements}->[-1]->[1]
5002                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5003                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
5004                       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;  
5005                  }                  }
5006                                    
5007                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5008                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
5009                      !!!parse-error (type => 'not closed',
5010                                      text => $self->{open_elements}->[-1]->[0]
5011                                          ->manakai_local_name,
5012                                      token => $token);
5013                    } else {
5014                      !!!cp ('t176');
5015                  }                  }
5016                                    
5017                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3247  sub _tree_construction_main ($) { Line 5021  sub _tree_construction_main ($) {
5021                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5022                                    
5023                  !!!next-token;                  !!!next-token;
5024                  redo B;                  next B;
5025                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5026                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5027                    !!!parse-error (type => 'unmatched end tag',
5028                                    text => $token->{tag_name}, token => $token);
5029                  ## Ignore the token                  ## Ignore the token
5030                  !!!next-token;                  !!!next-token;
5031                  redo B;                  next B;
5032                } else {                } else {
5033                    !!!cp ('t178');
5034                  #                  #
5035                }                }
5036              } elsif ({              } elsif ({
# Line 3264  sub _tree_construction_main ($) { Line 5041  sub _tree_construction_main ($) {
5041                ## have an element in table scope                ## have an element in table scope
5042                my $i;                my $i;
5043                my $tn;                my $tn;
5044                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5045                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5046                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5047                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5048                    last INSCOPE;                      !!!cp ('t179');
5049                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5050                    $tn = $node->[1];  
5051                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5052                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5053                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5054                            table => 1, html => 1,                                line => $token->{line},
5055                           }->{$node->[1]}) {                                column => $token->{column}};
5056                    last INSCOPE;                      next B;
5057                      } elsif ($node->[1] & TABLE_CELL_EL) {
5058                        !!!cp ('t180');
5059                        $tn = $node->[0]->manakai_local_name;
5060                        ## NOTE: There is exactly one |td| or |th| element
5061                        ## in scope in the stack of open elements by definition.
5062                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5063                        ## ISSUE: Can this be reached?
5064                        !!!cp ('t181');
5065                        last;
5066                      }
5067                  }                  }
5068                } # INSCOPE  
5069                unless (defined $i) {                  !!!cp ('t182');
5070                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5071                        text => $token->{tag_name}, token => $token);
5072                  ## Ignore the token                  ## Ignore the token
5073                  !!!next-token;                  !!!next-token;
5074                  redo B;                  next B;
5075                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5076              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5077                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5078                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5079                                  token => $token);
5080    
5081                ## As if </caption>                ## As if </caption>
5082                ## have a table element in table scope                ## have a table element in table scope
5083                my $i;                my $i;
5084                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5085                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5086                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5087                      !!!cp ('t184');
5088                    $i = $_;                    $i = $_;
5089                    last INSCOPE;                    last INSCOPE;
5090                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5091                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5092                    last INSCOPE;                    last INSCOPE;
5093                  }                  }
5094                } # INSCOPE                } # INSCOPE
5095                unless (defined $i) {                unless (defined $i) {
5096                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5097                    !!!parse-error (type => 'unmatched end tag',
5098                                    text => 'caption', token => $token);
5099                  ## Ignore the token                  ## Ignore the token
5100                  !!!next-token;                  !!!next-token;
5101                  redo B;                  next B;
5102                }                }
5103                                
5104                ## generate implied end tags                ## generate implied end tags
5105                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5106                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5107                     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;  
5108                }                }
5109    
5110                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5111                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5112                    !!!parse-error (type => 'not closed',
5113                                    text => $self->{open_elements}->[-1]->[0]
5114                                        ->manakai_local_name,
5115                                    token => $token);
5116                  } else {
5117                    !!!cp ('t189');
5118                }                }
5119    
5120                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3340  sub _tree_construction_main ($) { Line 5124  sub _tree_construction_main ($) {
5124                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5125    
5126                ## reprocess                ## reprocess
5127                redo B;                next B;
5128              } elsif ({              } elsif ({
5129                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5130                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5131                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5132                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
5133                    !!!parse-error (type => 'unmatched end tag',
5134                                    text => $token->{tag_name}, token => $token);
5135                  ## Ignore the token                  ## Ignore the token
5136                  !!!next-token;                  !!!next-token;
5137                  redo B;                  next B;
5138                } else {                } else {
5139                    !!!cp ('t191');
5140                  #                  #
5141                }                }
5142              } elsif ({              } elsif ({
# Line 3357  sub _tree_construction_main ($) { Line 5144  sub _tree_construction_main ($) {
5144                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5145                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5146                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5147                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5148                  !!!parse-error (type => 'unmatched end tag',
5149                                  text => $token->{tag_name}, token => $token);
5150                ## Ignore the token                ## Ignore the token
5151                !!!next-token;                !!!next-token;
5152                redo B;                next B;
5153              } else {              } else {
5154                  !!!cp ('t193');
5155                #                #
5156              }              }
5157          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5158            for my $entry (@{$self->{open_elements}}) {
5159              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5160                !!!cp ('t75');
5161                !!!parse-error (type => 'in body:#eof', token => $token);
5162                last;
5163              }
5164            }
5165    
5166            ## Stop parsing.
5167            last B;
5168        } else {        } else {
5169          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5170        }        }
# Line 3372  sub _tree_construction_main ($) { Line 5173  sub _tree_construction_main ($) {
5173        #        #
5174      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5175        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5176              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5177                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5178              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5179                                
5180                unless (length $token->{data}) {            unless (length $token->{data}) {
5181                  !!!next-token;              !!!cp ('t194');
5182                  redo B;              !!!next-token;
5183                }              next B;
5184              }            } else {
5185                !!!cp ('t195');
5186              }
5187            }
5188    
5189              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5190    
5191              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5192              ## 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 5194  sub _tree_construction_main ($) {
5194              ## result in a new Text node.              ## result in a new Text node.
5195              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5196                            
5197              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]}) {  
5198                # MUST                # MUST
5199                my $foster_parent_element;                my $foster_parent_element;
5200                my $next_sibling;                my $next_sibling;
5201                my $prev_sibling;                my $prev_sibling;
5202                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5203                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5204                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5205                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5206                        !!!cp ('t196');
5207                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5208                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5209                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5210                    } else {                    } else {
5211                        !!!cp ('t197');
5212                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5213                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5214                    }                    }
# Line 3416  sub _tree_construction_main ($) { Line 5220  sub _tree_construction_main ($) {
5220                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5221                if (defined $prev_sibling and                if (defined $prev_sibling and
5222                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5223                    !!!cp ('t198');
5224                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5225                } else {                } else {
5226                    !!!cp ('t199');
5227                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5228                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5229                     $next_sibling);                     $next_sibling);
5230                }                }
5231              } else {            $open_tables->[-1]->[1] = 1; # tainted
5232                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5233              }            !!!cp ('t200');
5234              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5235            }
5236                            
5237              !!!next-token;          !!!next-token;
5238              redo B;          next B;
5239        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5240              if ({          if ({
5241                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5242                   th => 1, td => 1,               th => 1, td => 1,
5243                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5244                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5245                  ## Clear back to table context              ## Clear back to table context
5246                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5247                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5248                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!cp ('t201');
5249                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5250                  }              }
5251                                
5252                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5253                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5254                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5255                }            }
5256              
5257                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5258                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5259                    !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t202');
5260                  }                !!!parse-error (type => 'missing start tag:tr', token => $token);
5261                }
5262                                    
5263                  ## Clear back to table body context              ## Clear back to table body context
5264                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5265                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5266                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5267                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## ISSUE: Can this case be reached?
5268                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5269                  }              }
5270                                    
5271                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5272                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5273                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5274                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5275                      !!!nack ('t204');
5276                    !!!next-token;                    !!!next-token;
5277                    redo B;                    next B;
5278                  } else {                  } else {
5279                    !!!insert-element ('tr');                    !!!cp ('t205');
5280                      !!!insert-element ('tr',, $token);
5281                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5282                  }                  }
5283                  } else {
5284                    !!!cp ('t206');
5285                }                }
5286    
5287                ## Clear back to table row context                ## Clear back to table row context
5288                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5289                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5290                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5291                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5292                }                }
5293                                
5294                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5295                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5296    
5297                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5298                                
5299                  !!!nack ('t207.1');
5300                !!!next-token;                !!!next-token;
5301                redo B;                next B;
5302              } elsif ({              } elsif ({
5303                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5304                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3496  sub _tree_construction_main ($) { Line 5310  sub _tree_construction_main ($) {
5310                  my $i;                  my $i;
5311                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5312                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5313                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5314                        !!!cp ('t208');
5315                      $i = $_;                      $i = $_;
5316                      last INSCOPE;                      last INSCOPE;
5317                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5318                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5319                      last INSCOPE;                      last INSCOPE;
5320                    }                    }
5321                  } # INSCOPE                  } # INSCOPE
5322                  unless (defined $i) {                  unless (defined $i) {
5323                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5324    ## TODO: This type is wrong.
5325                      !!!parse-error (type => 'unmacthed end tag',
5326                                      text => $token->{tag_name}, token => $token);
5327                    ## Ignore the token                    ## Ignore the token
5328                      !!!nack ('t210.1');
5329                    !!!next-token;                    !!!next-token;
5330                    redo B;                    next B;
5331                  }                  }
5332                                    
5333                  ## Clear back to table row context                  ## Clear back to table row context
5334                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5335                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5336                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5337                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5338                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5339                  }                  }
5340                                    
5341                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5342                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5343                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5344                      !!!cp ('t212');
5345                    ## reprocess                    ## reprocess
5346                    redo B;                    !!!ack-later;
5347                      next B;
5348                  } else {                  } else {
5349                      !!!cp ('t213');
5350                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5351                  }                  }
5352                }                }
# Line 3535  sub _tree_construction_main ($) { Line 5356  sub _tree_construction_main ($) {
5356                  my $i;                  my $i;
5357                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5358                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5359                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5360                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5361                      $i = $_;                      $i = $_;
5362                      last INSCOPE;                      last INSCOPE;
5363                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5364                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5365                      last INSCOPE;                      last INSCOPE;
5366                    }                    }
5367                  } # INSCOPE                  } # INSCOPE
5368                  unless (defined $i) {                  unless (defined $i) {
5369                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5370    ## TODO: This erorr type is wrong.
5371                      !!!parse-error (type => 'unmatched end tag',
5372                                      text => $token->{tag_name}, token => $token);
5373                    ## Ignore the token                    ## Ignore the token
5374                      !!!nack ('t216.1');
5375                    !!!next-token;                    !!!next-token;
5376                    redo B;                    next B;
5377                  }                  }
5378    
5379                  ## Clear back to table body context                  ## Clear back to table body context
5380                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5381                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5382                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5383                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5384                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5385                  }                  }
5386                                    
# Line 3571  sub _tree_construction_main ($) { Line 5394  sub _tree_construction_main ($) {
5394                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5395                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5396                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5397                  } else {
5398                    !!!cp ('t218');
5399                }                }
5400    
5401                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5402                  ## Clear back to table context                  ## Clear back to table context
5403                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5404                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5405                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5406                      ## ISSUE: Can this state be reached?
5407                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5408                  }                  }
5409                                    
5410                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5411                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5412                  ## reprocess                  ## reprocess
5413                  redo B;                  !!!ack-later;
5414                    next B;
5415                } elsif ({                } elsif ({
5416                          caption => 1,                          caption => 1,
5417                          colgroup => 1,                          colgroup => 1,
5418                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5419                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5420                  ## Clear back to table context                  ## Clear back to table context
5421                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5422                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5423                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5424                      ## ISSUE: Can this state be reached?
5425                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5426                  }                  }
5427                                    
5428                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5429                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5430                                    
5431                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5432                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5433                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5434                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3609  sub _tree_construction_main ($) { Line 5437  sub _tree_construction_main ($) {
5437                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5438                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5439                  !!!next-token;                  !!!next-token;
5440                  redo B;                  !!!nack ('t220.1');
5441                    next B;
5442                } else {                } else {
5443                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5444                }                }
5445              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5446                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5447                                  text => $self->{open_elements}->[-1]->[0]
5448                                      ->manakai_local_name,
5449                                  token => $token);
5450    
5451                ## As if </table>                ## As if </table>
5452                ## have a table element in table scope                ## have a table element in table scope
5453                my $i;                my $i;
5454                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5455                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5456                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5457                      !!!cp ('t221');
5458                    $i = $_;                    $i = $_;
5459                    last INSCOPE;                    last INSCOPE;
5460                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5461                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5462                    last INSCOPE;                    last INSCOPE;
5463                  }                  }
5464                } # INSCOPE                } # INSCOPE
5465                unless (defined $i) {                unless (defined $i) {
5466                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5467    ## TODO: The following is wrong, maybe.
5468                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5469                                    token => $token);
5470                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5471                    !!!nack ('t223.1');
5472                  !!!next-token;                  !!!next-token;
5473                  redo B;                  next B;
5474                }                }
5475                                
5476    ## TODO: Followings are removed from the latest spec.
5477                ## generate implied end tags                ## generate implied end tags
5478                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5479                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5480                     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;  
5481                }                }
5482    
5483                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5484                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5485                    ## NOTE: |<table><tr><table>|
5486                    !!!parse-error (type => 'not closed',
5487                                    text => $self->{open_elements}->[-1]->[0]
5488                                        ->manakai_local_name,
5489                                    token => $token);
5490                  } else {
5491                    !!!cp ('t226');
5492                }                }
5493    
5494                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5495                  pop @{$open_tables};
5496    
5497                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5498    
5499                ## reprocess            ## reprocess
5500                redo B;            !!!ack-later;
5501          } else {            next B;
5502            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5503              if (not $open_tables->[-1]->[1]) { # tainted
5504                !!!cp ('t227.8');
5505                ## NOTE: This is a "as if in head" code clone.
5506                $parse_rcdata->(CDATA_CONTENT_MODEL);
5507                next B;
5508              } else {
5509                !!!cp ('t227.7');
5510                #
5511              }
5512            } elsif ($token->{tag_name} eq 'script') {
5513              if (not $open_tables->[-1]->[1]) { # tainted
5514                !!!cp ('t227.6');
5515                ## NOTE: This is a "as if in head" code clone.
5516                $script_start_tag->();
5517                next B;
5518              } else {
5519                !!!cp ('t227.5');
5520                #
5521              }
5522            } elsif ($token->{tag_name} eq 'input') {
5523              if (not $open_tables->[-1]->[1]) { # tainted
5524                if ($token->{attributes}->{type}) { ## TODO: case
5525                  my $type = lc $token->{attributes}->{type}->{value};
5526                  if ($type eq 'hidden') {
5527                    !!!cp ('t227.3');
5528                    !!!parse-error (type => 'in table',
5529                                    text => $token->{tag_name}, token => $token);
5530    
5531            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5532    
5533                    ## TODO: form element pointer
5534    
5535                    pop @{$self->{open_elements}};
5536    
5537                    !!!next-token;
5538                    !!!ack ('t227.2.1');
5539                    next B;
5540                  } else {
5541                    !!!cp ('t227.2');
5542                    #
5543                  }
5544                } else {
5545                  !!!cp ('t227.1');
5546                  #
5547                }
5548              } else {
5549                !!!cp ('t227.4');
5550                #
5551              }
5552            } else {
5553              !!!cp ('t227');
5554            #            #
5555          }          }
5556    
5557            !!!parse-error (type => 'in table', text => $token->{tag_name},
5558                            token => $token);
5559    
5560            $insert = $insert_to_foster;
5561            #
5562        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5563              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5564                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3674  sub _tree_construction_main ($) { Line 5566  sub _tree_construction_main ($) {
5566                my $i;                my $i;
5567                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5568                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5569                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5570                      !!!cp ('t228');
5571                    $i = $_;                    $i = $_;
5572                    last INSCOPE;                    last INSCOPE;
5573                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5574                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5575                    last INSCOPE;                    last INSCOPE;
5576                  }                  }
5577                } # INSCOPE                } # INSCOPE
5578                unless (defined $i) {                unless (defined $i) {
5579                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5580                    !!!parse-error (type => 'unmatched end tag',
5581                                    text => $token->{tag_name}, token => $token);
5582                  ## Ignore the token                  ## Ignore the token
5583                    !!!nack ('t230.1');
5584                  !!!next-token;                  !!!next-token;
5585                  redo B;                  next B;
5586                  } else {
5587                    !!!cp ('t232');
5588                }                }
5589    
5590                ## Clear back to table row context                ## Clear back to table row context
5591                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5592                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5593                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5594                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5595                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5596                }                }
5597    
5598                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5599                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5600                !!!next-token;                !!!next-token;
5601                redo B;                !!!nack ('t231.1');
5602                  next B;
5603              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5604                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5605                  ## As if </tr>                  ## As if </tr>
# Line 3709  sub _tree_construction_main ($) { Line 5607  sub _tree_construction_main ($) {
5607                  my $i;                  my $i;
5608                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5609                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5610                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5611                        !!!cp ('t233');
5612                      $i = $_;                      $i = $_;
5613                      last INSCOPE;                      last INSCOPE;
5614                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5615                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5616                      last INSCOPE;                      last INSCOPE;
5617                    }                    }
5618                  } # INSCOPE                  } # INSCOPE
5619                  unless (defined $i) {                  unless (defined $i) {
5620                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5621    ## TODO: The following is wrong.
5622                      !!!parse-error (type => 'unmatched end tag',
5623                                      text => $token->{type}, token => $token);
5624                    ## Ignore the token                    ## Ignore the token
5625                      !!!nack ('t236.1');
5626                    !!!next-token;                    !!!next-token;
5627                    redo B;                    next B;
5628                  }                  }
5629                                    
5630                  ## Clear back to table row context                  ## Clear back to table row context
5631                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5632                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5633                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5634                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5635                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5636                  }                  }
5637                                    
# Line 3743  sub _tree_construction_main ($) { Line 5645  sub _tree_construction_main ($) {
5645                  my $i;                  my $i;
5646                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5647                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5648                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5649                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5650                      $i = $_;                      $i = $_;
5651                      last INSCOPE;                      last INSCOPE;
5652                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5653                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5654                      last INSCOPE;                      last INSCOPE;
5655                    }                    }
5656                  } # INSCOPE                  } # INSCOPE
5657                  unless (defined $i) {                  unless (defined $i) {
5658                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5659                      !!!parse-error (type => 'unmatched end tag',
5660                                      text => $token->{tag_name}, token => $token);
5661                    ## Ignore the token                    ## Ignore the token
5662                      !!!nack ('t239.1');
5663                    !!!next-token;                    !!!next-token;
5664                    redo B;                    next B;
5665                  }                  }
5666                                    
5667                  ## Clear back to table body context                  ## Clear back to table body context
5668                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5669                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5670                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5671                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5672                  }                  }
5673                                    
# Line 3781  sub _tree_construction_main ($) { Line 5683  sub _tree_construction_main ($) {
5683                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5684                }                }
5685    
5686                  ## NOTE: </table> in the "in table" insertion mode.
5687                  ## When you edit the code fragment below, please ensure that
5688                  ## the code for <table> in the "in table" insertion mode
5689                  ## is synced with it.
5690    
5691                ## have a table element in table scope                ## have a table element in table scope
5692                my $i;                my $i;
5693                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5694                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5695                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5696                      !!!cp ('t241');
5697                    $i = $_;                    $i = $_;
5698                    last INSCOPE;                    last INSCOPE;
5699                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5700                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5701                    last INSCOPE;                    last INSCOPE;
5702                  }                  }
5703                } # INSCOPE                } # INSCOPE
5704                unless (defined $i) {                unless (defined $i) {
5705                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5706                    !!!parse-error (type => 'unmatched end tag',
5707                                    text => $token->{tag_name}, token => $token);
5708                  ## Ignore the token                  ## Ignore the token
5709                    !!!nack ('t243.1');
5710                  !!!next-token;                  !!!next-token;
5711                  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]);  
5712                }                }
5713                                    
5714                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5715                  pop @{$open_tables};
5716                                
5717                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5718                                
5719                !!!next-token;                !!!next-token;
5720                redo B;                next B;
5721              } elsif ({              } elsif ({
5722                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5723                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3832  sub _tree_construction_main ($) { Line 5727  sub _tree_construction_main ($) {
5727                  my $i;                  my $i;
5728                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5729                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5730                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5731                        !!!cp ('t247');
5732                      $i = $_;                      $i = $_;
5733                      last INSCOPE;                      last INSCOPE;
5734                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5735                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5736                      last INSCOPE;                      last INSCOPE;
5737                    }                    }
5738                  } # INSCOPE                  } # INSCOPE
5739                    unless (defined $i) {                    unless (defined $i) {
5740                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5741                        !!!parse-error (type => 'unmatched end tag',
5742                                        text => $token->{tag_name}, token => $token);
5743                      ## Ignore the token                      ## Ignore the token
5744                        !!!nack ('t249.1');
5745                      !!!next-token;                      !!!next-token;
5746                      redo B;                      next B;
5747                    }                    }
5748                                    
5749                  ## As if </tr>                  ## As if </tr>
# Line 3853  sub _tree_construction_main ($) { Line 5751  sub _tree_construction_main ($) {
5751                  my $i;                  my $i;
5752                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5753                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5754                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5755                        !!!cp ('t250');
5756                      $i = $_;                      $i = $_;
5757                      last INSCOPE;                      last INSCOPE;
5758                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5759                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5760                      last INSCOPE;                      last INSCOPE;
5761                    }                    }
5762                  } # INSCOPE                  } # INSCOPE
5763                    unless (defined $i) {                    unless (defined $i) {
5764                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5765                        !!!parse-error (type => 'unmatched end tag',
5766                                        text => 'tr', token => $token);
5767                      ## Ignore the token                      ## Ignore the token
5768                        !!!nack ('t252.1');
5769                      !!!next-token;                      !!!next-token;
5770                      redo B;                      next B;
5771                    }                    }
5772                                    
5773                  ## Clear back to table row context                  ## Clear back to table row context
5774                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5775                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5776                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5777                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5778                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5779                  }                  }
5780                                    
# Line 3886  sub _tree_construction_main ($) { Line 5787  sub _tree_construction_main ($) {
5787                my $i;                my $i;
5788                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5789                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5790                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5791                      !!!cp ('t254');
5792                    $i = $_;                    $i = $_;
5793                    last INSCOPE;                    last INSCOPE;
5794                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5795                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5796                    last INSCOPE;                    last INSCOPE;
5797                  }                  }
5798                } # INSCOPE                } # INSCOPE
5799                unless (defined $i) {                unless (defined $i) {
5800                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5801                    !!!parse-error (type => 'unmatched end tag',
5802                                    text => $token->{tag_name}, token => $token);
5803                  ## Ignore the token                  ## Ignore the token
5804                    !!!nack ('t256.1');
5805                  !!!next-token;                  !!!next-token;
5806                  redo B;                  next B;
5807                }                }
5808    
5809                ## Clear back to table body context                ## Clear back to table body context
5810                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5811                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5812                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5813                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5814                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5815                }                }
5816    
5817                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5818                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5819                  !!!nack ('t257.1');
5820                !!!next-token;                !!!next-token;
5821                redo B;                next B;
5822              } elsif ({              } elsif ({
5823                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5824                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5825                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5826                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5827                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5828                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5829                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
5830                !!!next-token;                            text => $token->{tag_name}, token => $token);
5831                redo B;            ## Ignore the token
5832          } else {            !!!nack ('t258.1');
5833            !!!parse-error (type => 'in table:/'.$token->{tag_name});             !!!next-token;
5834              next B;
5835            } else {
5836              !!!cp ('t259');
5837              !!!parse-error (type => 'in table:/',
5838                              text => $token->{tag_name}, token => $token);
5839    
5840            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5841            #            #
5842          }          }
5843          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5844            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5845                    @{$self->{open_elements}} == 1) { # redundant, maybe
5846              !!!parse-error (type => 'in body:#eof', token => $token);
5847              !!!cp ('t259.1');
5848              #
5849            } else {
5850              !!!cp ('t259.2');
5851              #
5852            }
5853    
5854            ## Stop parsing
5855            last B;
5856        } else {        } else {
5857          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5858        }        }
# Line 3938  sub _tree_construction_main ($) { Line 5861  sub _tree_construction_main ($) {
5861              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5862                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5863                unless (length $token->{data}) {                unless (length $token->{data}) {
5864                    !!!cp ('t260');
5865                  !!!next-token;                  !!!next-token;
5866                  redo B;                  next B;
5867                }                }
5868              }              }
5869                            
5870                !!!cp ('t261');
5871              #              #
5872            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5873              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5874                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5875                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5876                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5877                  !!!ack ('t262.1');
5878                !!!next-token;                !!!next-token;
5879                redo B;                next B;
5880              } else {              } else {
5881                  !!!cp ('t263');
5882                #                #
5883              }              }
5884            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5885              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5886                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5887                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5888                    !!!parse-error (type => 'unmatched end tag',
5889                                    text => 'colgroup', token => $token);
5890                  ## Ignore the token                  ## Ignore the token
5891                  !!!next-token;                  !!!next-token;
5892                  redo B;                  next B;
5893                } else {                } else {
5894                    !!!cp ('t265');
5895                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5896                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5897                  !!!next-token;                  !!!next-token;
5898                  redo B;                              next B;            
5899                }                }
5900              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5901                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5902                  !!!parse-error (type => 'unmatched end tag',
5903                                  text => 'col', token => $token);
5904                ## Ignore the token                ## Ignore the token
5905                !!!next-token;                !!!next-token;
5906                redo B;                next B;
5907              } else {              } else {
5908                  !!!cp ('t267');
5909                #                #
5910              }              }
5911            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5912              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5913            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5914              !!!cp ('t270.2');
5915              ## Stop parsing.
5916              last B;
5917            } else {
5918              ## NOTE: As if </colgroup>.
5919              !!!cp ('t270.1');
5920              pop @{$self->{open_elements}}; # colgroup
5921              $self->{insertion_mode} = IN_TABLE_IM;
5922              ## Reprocess.
5923              next B;
5924            }
5925          } else {
5926            die "$0: $token->{type}: Unknown token type";
5927          }
5928    
5929            ## As if </colgroup>            ## As if </colgroup>
5930            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5931              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5932    ## TODO: Wrong error type?
5933                !!!parse-error (type => 'unmatched end tag',
5934                                text => 'colgroup', token => $token);
5935              ## Ignore the token              ## Ignore the token
5936                !!!nack ('t269.1');
5937              !!!next-token;              !!!next-token;
5938              redo B;              next B;
5939            } else {            } else {
5940                !!!cp ('t270');
5941              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5942              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5943                !!!ack-later;
5944              ## reprocess              ## reprocess
5945              redo B;              next B;
5946            }            }
5947      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5948        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5949            !!!cp ('t271');
5950          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5951          !!!next-token;          !!!next-token;
5952          redo B;          next B;
5953        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5954              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5955                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5956                  ## As if </option>              !!!cp ('t272');
5957                  pop @{$self->{open_elements}};              ## As if </option>
5958                }              pop @{$self->{open_elements}};
5959              } else {
5960                !!!cp ('t273');
5961              }
5962    
5963                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5964                !!!next-token;            !!!nack ('t273.1');
5965                redo B;            !!!next-token;
5966              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5967                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5968                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5969                  pop @{$self->{open_elements}};              !!!cp ('t274');
5970                }              ## As if </option>
5971                pop @{$self->{open_elements}};
5972              } else {
5973                !!!cp ('t275');
5974              }
5975    
5976                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5977                  ## As if </optgroup>              !!!cp ('t276');
5978                  pop @{$self->{open_elements}};              ## As if </optgroup>
5979                }              pop @{$self->{open_elements}};
5980              } else {
5981                !!!cp ('t277');
5982              }
5983    
5984                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5985                !!!next-token;            !!!nack ('t277.1');
5986                redo B;            !!!next-token;
5987              } elsif ($token->{tag_name} eq 'select') {            next B;
5988                !!!parse-error (type => 'not closed:select');          } elsif ({
5989                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
5990                ## have an element in table scope                   }->{$token->{tag_name}} or
5991                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5992                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
5993                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
5994                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
5995                    $i = $_;                     tr => 1, td => 1, th => 1,
5996                    last INSCOPE;                    }->{$token->{tag_name}})) {
5997                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
5998                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
5999                           }->{$node->[1]}) {                            token => $token);
6000                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6001                  }            ## as if there were </select> (otherwise).
6002                } # INSCOPE            ## have an element in table scope
6003                unless (defined $i) {            my $i;
6004                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6005                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6006                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6007                  redo B;                !!!cp ('t278');
6008                }                $i = $_;
6009                  last INSCOPE;
6010                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6011                  !!!cp ('t279');
6012                  last INSCOPE;
6013                }
6014              } # INSCOPE
6015              unless (defined $i) {
6016                !!!cp ('t280');
6017                !!!parse-error (type => 'unmatched end tag',
6018                                text => 'select', token => $token);
6019                ## Ignore the token
6020                !!!nack ('t280.1');
6021                !!!next-token;
6022                next B;
6023              }
6024                                
6025                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6026              splice @{$self->{open_elements}}, $i;
6027    
6028                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6029    
6030                !!!next-token;            if ($token->{tag_name} eq 'select') {
6031                redo B;              !!!nack ('t281.2');
6032                !!!next-token;
6033                next B;
6034              } else {
6035                !!!cp ('t281.1');
6036                !!!ack-later;
6037                ## Reprocess the token.
6038                next B;
6039              }
6040          } else {          } else {
6041            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
6042              !!!parse-error (type => 'in select',
6043                              text => $token->{tag_name}, token => $token);
6044            ## Ignore the token            ## Ignore the token
6045              !!!nack ('t282.1');
6046            !!!next-token;            !!!next-token;
6047            redo B;            next B;
6048          }          }
6049        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6050              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6051                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6052                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6053                  ## As if </option>              !!!cp ('t283');
6054                  splice @{$self->{open_elements}}, -2;              ## As if </option>
6055                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
6056                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6057                } else {              !!!cp ('t284');
6058                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
6059                  ## Ignore the token            } else {
6060                }              !!!cp ('t285');
6061                !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6062                redo B;                              text => $token->{tag_name}, token => $token);
6063              } elsif ($token->{tag_name} eq 'option') {              ## Ignore the token
6064                if ($self->{open_elements}->[-1]->[1] eq 'option') {            }
6065                  pop @{$self->{open_elements}};            !!!nack ('t285.1');
6066                } else {            !!!next-token;
6067                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            next B;
6068                  ## Ignore the token          } elsif ($token->{tag_name} eq 'option') {
6069                }            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6070                !!!next-token;              !!!cp ('t286');
6071                redo B;              pop @{$self->{open_elements}};
6072              } elsif ($token->{tag_name} eq 'select') {            } else {
6073                ## have an element in table scope              !!!cp ('t287');
6074                my $i;              !!!parse-error (type => 'unmatched end tag',
6075                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                              text => $token->{tag_name}, token => $token);
6076                  my $node = $self->{open_elements}->[$_];              ## Ignore the token
6077                  if ($node->[1] eq $token->{tag_name}) {            }
6078                    $i = $_;            !!!nack ('t287.1');
6079                    last INSCOPE;            !!!next-token;
6080                  } elsif ({            next B;
6081                            table => 1, html => 1,          } elsif ($token->{tag_name} eq 'select') {
6082                           }->{$node->[1]}) {            ## have an element in table scope
6083                    last INSCOPE;            my $i;
6084                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6085                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6086                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6087                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t288');
6088                  ## Ignore the token                $i = $_;
6089                  !!!next-token;                last INSCOPE;
6090                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6091                }                !!!cp ('t289');
6092                  last INSCOPE;
6093                }
6094              } # INSCOPE
6095              unless (defined $i) {
6096                !!!cp ('t290');
6097                !!!parse-error (type => 'unmatched end tag',
6098                                text => $token->{tag_name}, token => $token);
6099                ## Ignore the token
6100                !!!nack ('t290.1');
6101                !!!next-token;
6102                next B;
6103              }
6104                                
6105                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6106              splice @{$self->{open_elements}}, $i;
6107    
6108                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6109    
6110                !!!next-token;            !!!nack ('t291.1');
6111                redo B;            !!!next-token;
6112              } elsif ({            next B;
6113                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6114                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6115                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6116                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6117                     }->{$token->{tag_name}}) {
6118    ## TODO: The following is wrong?
6119              !!!parse-error (type => 'unmatched end tag',
6120                              text => $token->{tag_name}, token => $token);
6121                                
6122                ## have an element in table scope            ## have an element in table scope
6123                my $i;            my $i;
6124                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6125                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6126                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6127                    $i = $_;                !!!cp ('t292');
6128                    last INSCOPE;                $i = $_;
6129                  } elsif ({                last INSCOPE;
6130                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6131                           }->{$node->[1]}) {                !!!cp ('t293');
6132                    last INSCOPE;                last INSCOPE;
6133                  }              }
6134                } # INSCOPE            } # INSCOPE
6135                unless (defined $i) {            unless (defined $i) {
6136                  ## Ignore the token              !!!cp ('t294');
6137                  !!!next-token;              ## Ignore the token
6138                  redo B;              !!!nack ('t294.1');
6139                }              !!!next-token;
6140                next B;
6141              }
6142                                
6143                ## As if </select>            ## As if </select>
6144                ## have an element in table scope            ## have an element in table scope
6145                undef $i;            undef $i;
6146                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6147                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6148                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6149                    $i = $_;                !!!cp ('t295');
6150                    last INSCOPE;                $i = $_;
6151                  } elsif ({                last INSCOPE;
6152                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6153                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
6154                    last INSCOPE;                !!!cp ('t296');
6155                  }                last INSCOPE;
6156                } # INSCOPE              }
6157                unless (defined $i) {            } # INSCOPE
6158                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
6159                  ## Ignore the </select> token              !!!cp ('t297');
6160                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
6161                  redo B;              !!!parse-error (type => 'unmatched end tag',
6162                }                              text => 'select', token => $token);
6163                ## Ignore the </select> token
6164                !!!nack ('t297.1');
6165                !!!next-token; ## TODO: ok?
6166                next B;
6167              }
6168                                
6169                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
6170              splice @{$self->{open_elements}}, $i;
6171    
6172                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6173    
6174                ## reprocess            !!!ack-later;
6175                redo B;            ## reprocess
6176              next B;
6177          } else {          } else {
6178            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
6179              !!!parse-error (type => 'in select:/',
6180                              text => $token->{tag_name}, token => $token);
6181            ## Ignore the token            ## Ignore the token
6182              !!!nack ('t299.3');
6183            !!!next-token;            !!!next-token;
6184            redo B;            next B;
6185          }          }
6186          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6187            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6188                    @{$self->{open_elements}} == 1) { # redundant, maybe
6189              !!!cp ('t299.1');
6190              !!!parse-error (type => 'in body:#eof', token => $token);
6191            } else {
6192              !!!cp ('t299.2');
6193            }
6194    
6195            ## Stop parsing.
6196            last B;
6197        } else {        } else {
6198          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6199        }        }
# Line 4175  sub _tree_construction_main ($) { Line 6207  sub _tree_construction_main ($) {
6207            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6208                        
6209            unless (length $token->{data}) {            unless (length $token->{data}) {
6210                !!!cp ('t300');
6211              !!!next-token;              !!!next-token;
6212              redo B;              next B;
6213            }            }
6214          }          }
6215                    
6216          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6217            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6218              !!!parse-error (type => 'after html:#text', token => $token);
6219    
6220            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6221            } else {
6222              !!!cp ('t302');
6223          }          }
6224                    
6225          ## "after body" insertion mode          ## "after body" insertion mode
6226          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6227    
6228          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6229          ## reprocess          ## reprocess
6230          redo B;          next B;
6231        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6232          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6233            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6234              !!!parse-error (type => 'after html',
6235                              text => $token->{tag_name}, token => $token);
6236                        
6237            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6238            } else {
6239              !!!cp ('t304');
6240          }          }
6241    
6242          ## "after body" insertion mode          ## "after body" insertion mode
6243          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6244                            text => $token->{tag_name}, token => $token);
6245    
6246          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6247            !!!ack-later;
6248          ## reprocess          ## reprocess
6249          redo B;          next B;
6250        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6251          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6252            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6253              !!!parse-error (type => 'after html:/',
6254                              text => $token->{tag_name}, token => $token);
6255                        
6256            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6257            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6258            } else {
6259              !!!cp ('t306');
6260          }          }
6261    
6262          ## "after body" insertion mode          ## "after body" insertion mode
6263          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6264            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6265              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6266                !!!parse-error (type => 'unmatched end tag',
6267                                text => 'html', token => $token);
6268              ## Ignore the token              ## Ignore the token
6269              !!!next-token;              !!!next-token;
6270              redo B;              next B;
6271            } else {            } else {
6272                !!!cp ('t308');
6273              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6274              !!!next-token;              !!!next-token;
6275              redo B;              next B;
6276            }            }
6277          } else {          } else {
6278            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6279              !!!parse-error (type => 'after body:/',
6280                              text => $token->{tag_name}, token => $token);
6281    
6282            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6283            ## reprocess            ## reprocess
6284            redo B;            next B;
6285          }          }
6286          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6287            !!!cp ('t309.2');
6288            ## Stop parsing
6289            last B;
6290        } else {        } else {
6291          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6292        }        }
# Line 4241  sub _tree_construction_main ($) { Line 6296  sub _tree_construction_main ($) {
6296            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6297                        
6298            unless (length $token->{data}) {            unless (length $token->{data}) {
6299                !!!cp ('t310');
6300              !!!next-token;              !!!next-token;
6301              redo B;              next B;
6302            }            }
6303          }          }
6304                    
6305          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6306            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6307              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6308                !!!parse-error (type => 'in frameset:#text', token => $token);
6309            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6310              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6311            } else { # "after html frameset"              !!!parse-error (type => 'after frameset:#text', token => $token);
6312              !!!parse-error (type => 'after html:#character');            } else { # "after after frameset"
6313                !!!cp ('t313');
6314              $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');  
6315            }            }
6316                        
6317            ## Ignore the token.            ## Ignore the token.
6318            if (length $token->{data}) {            if (length $token->{data}) {
6319                !!!cp ('t314');
6320              ## reprocess the rest of characters              ## reprocess the rest of characters
6321            } else {            } else {
6322                !!!cp ('t315');
6323              !!!next-token;              !!!next-token;
6324            }            }
6325            redo B;            next B;
6326          }          }
6327                    
6328          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6329        } 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...  
         }  
   
6330          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6331              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6332            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6333              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6334              !!!nack ('t318.1');
6335            !!!next-token;            !!!next-token;
6336            redo B;            next B;
6337          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6338                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6339            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6340              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6341            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6342              !!!ack ('t319.1');
6343            !!!next-token;            !!!next-token;
6344            redo B;            next B;
6345          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6346            ## NOTE: As if in body.            !!!cp ('t320');
6347            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6348            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6349              next B;
6350    
6351              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6352              ## has no parse error.
6353          } else {          } else {
6354            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6355              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6356            } else {              !!!parse-error (type => 'in frameset',
6357              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6358              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6359                !!!cp ('t322');
6360                !!!parse-error (type => 'after frameset',
6361                                text => $token->{tag_name}, token => $token);
6362              } else { # "after after frameset"
6363                !!!cp ('t322.2');
6364                !!!parse-error (type => 'after after frameset',
6365                                text => $token->{tag_name}, token => $token);
6366            }            }
6367            ## Ignore the token            ## Ignore the token
6368              !!!nack ('t322.1');
6369            !!!next-token;            !!!next-token;
6370            redo B;            next B;
6371          }          }
6372        } 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...  
         }  
   
6373          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6374              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6375            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6376                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6377              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6378                !!!parse-error (type => 'unmatched end tag',
6379                                text => $token->{tag_name}, token => $token);
6380              ## Ignore the token              ## Ignore the token
6381              !!!next-token;              !!!next-token;
6382            } else {            } else {
6383                !!!cp ('t326');
6384              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6385              !!!next-token;              !!!next-token;
6386            }            }
6387    
6388            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6389                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6390                !!!cp ('t327');
6391              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6392              } else {
6393                !!!cp ('t328');
6394            }            }
6395            redo B;            next B;
6396          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6397                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6398              !!!cp ('t329');
6399            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6400            !!!next-token;            !!!next-token;
6401            redo B;            next B;
6402          } else {          } else {
6403            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6404              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6405            } else {              !!!parse-error (type => 'in frameset:/',
6406              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6407              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6408                !!!cp ('t330.1');
6409                !!!parse-error (type => 'after frameset:/',
6410                                text => $token->{tag_name}, token => $token);
6411              } else { # "after after html"
6412                !!!cp ('t331');
6413                !!!parse-error (type => 'after after frameset:/',
6414                                text => $token->{tag_name}, token => $token);
6415            }            }
6416            ## Ignore the token            ## Ignore the token
6417            !!!next-token;            !!!next-token;
6418            redo B;            next B;
6419            }
6420          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6421            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6422                    @{$self->{open_elements}} == 1) { # redundant, maybe
6423              !!!cp ('t331.1');
6424              !!!parse-error (type => 'in body:#eof', token => $token);
6425            } else {
6426              !!!cp ('t331.2');
6427          }          }
6428            
6429            ## Stop parsing
6430            last B;
6431        } else {        } else {
6432          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6433        }        }
# Line 4354  sub _tree_construction_main ($) { Line 6440  sub _tree_construction_main ($) {
6440      ## "in body" insertion mode      ## "in body" insertion mode
6441      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6442        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6443            !!!cp ('t332');
6444          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6445          $script_start_tag->($insert);          $script_start_tag->();
6446          redo B;          next B;
6447        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6448            !!!cp ('t333');
6449          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6450          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6451          redo B;          next B;
6452        } elsif ({        } elsif ({
6453                  base => 1, link => 1,                  base => 1, link => 1,
6454                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6455            !!!cp ('t334');
6456          ## 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
6457          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6458          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6459            !!!ack ('t334.1');
6460          !!!next-token;          !!!next-token;
6461          redo B;          next B;
6462        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6463          ## 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
6464          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6465          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.
6466    
6467          unless ($self->{confident}) {          unless ($self->{confident}) {
6468            my $charset;            if ($token->{attributes}->{charset}) {
6469            if ($token->{attributes}->{charset}) { ## TODO: And if supported              !!!cp ('t335');
6470              $charset = $token->{attributes}->{charset}->{value};              ## NOTE: Whether the encoding is supported or not is handled
6471            }              ## in the {change_encoding} callback.
6472            if ($token->{attributes}->{'http-equiv'}) {              $self->{change_encoding}
6473              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6474              if ($token->{attributes}->{'http-equiv'}->{value}              
6475                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6476                    ->set_user_data (manakai_has_reference =>
6477                                         $token->{attributes}->{charset}
6478                                             ->{has_reference});
6479              } elsif ($token->{attributes}->{content}) {
6480                if ($token->{attributes}->{content}->{value}
6481                    =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6482                        [\x09-\x0D\x20]*=
6483                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6484                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6485                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
6486              } ## TODO: And if supported                ## NOTE: Whether the encoding is supported or not is handled
6487                  ## in the {change_encoding} callback.
6488                  $self->{change_encoding}
6489                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6490                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6491                      ->set_user_data (manakai_has_reference =>
6492                                           $token->{attributes}->{content}
6493                                                 ->{has_reference});
6494                }
6495              }
6496            } else {
6497              if ($token->{attributes}->{charset}) {
6498                !!!cp ('t337');
6499                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6500                    ->set_user_data (manakai_has_reference =>
6501                                         $token->{attributes}->{charset}
6502                                             ->{has_reference});
6503              }
6504              if ($token->{attributes}->{content}) {
6505                !!!cp ('t338');
6506                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6507                    ->set_user_data (manakai_has_reference =>
6508                                         $token->{attributes}->{content}
6509                                             ->{has_reference});
6510            }            }
           ## TODO: Change the encoding  
6511          }          }
6512    
6513            !!!ack ('t338.1');
6514          !!!next-token;          !!!next-token;
6515          redo B;          next B;
6516        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6517          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6518          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6519          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6520            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6521        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6522          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6523                                
6524          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6525              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6526              !!!cp ('t342');
6527            ## Ignore the token            ## Ignore the token
6528          } else {          } else {
6529            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6530            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6531              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6532                  !!!cp ('t343');
6533                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6534                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6535                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6536              }              }
6537            }            }
6538          }          }
6539            !!!nack ('t343.1');
6540          !!!next-token;          !!!next-token;
6541          redo B;          next B;
6542        } elsif ({        } elsif ({
6543                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6544                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6545                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6546                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6547                  pre => 1,                  pre => 1, listing => 1,
6548                    form => 1,
6549                    table => 1,
6550                    hr => 1,
6551                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6552            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6553              !!!cp ('t350');
6554              !!!parse-error (type => 'in form:form', token => $token);
6555              ## Ignore the token
6556              !!!nack ('t350.1');
6557              !!!next-token;
6558              next B;
6559            }
6560    
6561          ## has a p element in scope          ## has a p element in scope
6562          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6563            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6564              !!!back-token;              !!!cp ('t344');
6565              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6566              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6567            } elsif ({                        line => $token->{line}, column => $token->{column}};
6568                      table => 1, caption => 1, td => 1, th => 1,              next B;
6569                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6570                     }->{$_->[1]}) {              !!!cp ('t345');
6571              last INSCOPE;              last INSCOPE;
6572            }            }
6573          } # INSCOPE          } # INSCOPE
6574                        
6575          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6576          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6577              !!!nack ('t346.1');
6578            !!!next-token;            !!!next-token;
6579            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6580              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6581              unless (length $token->{data}) {              unless (length $token->{data}) {
6582                  !!!cp ('t346');
6583                !!!next-token;                !!!next-token;
6584                } else {
6585                  !!!cp ('t349');
6586              }              }
6587              } else {
6588                !!!cp ('t348');
6589            }            }
6590          } else {          } elsif ($token->{tag_name} eq 'form') {
6591              !!!cp ('t347.1');
6592              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6593    
6594              !!!nack ('t347.2');
6595            !!!next-token;            !!!next-token;
6596          }          } elsif ($token->{tag_name} eq 'table') {
6597          redo B;            !!!cp ('t382');
6598        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6599          if (defined $self->{form_element}) {            
6600            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6601            ## Ignore the token  
6602              !!!nack ('t382.1');
6603              !!!next-token;
6604            } elsif ($token->{tag_name} eq 'hr') {
6605              !!!cp ('t386');
6606              pop @{$self->{open_elements}};
6607            
6608              !!!nack ('t386.1');
6609            !!!next-token;            !!!next-token;
           redo B;  
6610          } else {          } else {
6611            ## 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];  
6612            !!!next-token;            !!!next-token;
           redo B;  
6613          }          }
6614        } elsif ($token->{tag_name} eq 'li') {          next B;
6615          ## 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') {  
6616          ## has a p element in scope          ## has a p element in scope
6617          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6618            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6619              !!!back-token;              !!!cp ('t353');
6620              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6621              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6622            } elsif ({                        line => $token->{line}, column => $token->{column}};
6623                      table => 1, caption => 1, td => 1, th => 1,              next B;
6624                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6625                     }->{$_->[1]}) {              !!!cp ('t354');
6626              last INSCOPE;              last INSCOPE;
6627            }            }
6628          } # INSCOPE          } # INSCOPE
# Line 4546  sub _tree_construction_main ($) { Line 6630  sub _tree_construction_main ($) {
6630          ## Step 1          ## Step 1
6631          my $i = -1;          my $i = -1;
6632          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6633            my $li_or_dtdd = {li => {li => 1},
6634                              dt => {dt => 1, dd => 1},
6635                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6636          LI: {          LI: {
6637            ## Step 2            ## Step 2
6638            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6639              if ($i != -1) {              if ($i != -1) {
6640                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6641                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6642                                  text => $self->{open_elements}->[-1]->[0]
6643                                      ->manakai_local_name,
6644                                  token => $token);
6645                } else {
6646                  !!!cp ('t356');
6647              }              }
6648              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6649              last LI;              last LI;
6650              } else {
6651                !!!cp ('t357');
6652            }            }
6653                        
6654            ## Step 3            ## Step 3
6655            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6656                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6657                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6658                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6659                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6660                  not ($node->[1] & DIV_EL)) {
6661                !!!cp ('t358');
6662              last LI;              last LI;
6663            }            }
6664                        
6665              !!!cp ('t359');
6666            ## Step 4            ## Step 4
6667            $i--;            $i--;
6668            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6669            redo LI;            redo LI;
6670          } # LI          } # LI
6671                        
6672          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6673            !!!nack ('t359.1');
6674          !!!next-token;          !!!next-token;
6675          redo B;          next B;
6676        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6677          ## has a p element in scope          ## has a p element in scope
6678          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6679            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6680              !!!back-token;              !!!cp ('t367');
6681              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6682              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6683            } elsif ({                        line => $token->{line}, column => $token->{column}};
6684                      table => 1, caption => 1, td => 1, th => 1,              next B;
6685                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6686                     }->{$_->[1]}) {              !!!cp ('t368');
6687              last INSCOPE;              last INSCOPE;
6688            }            }
6689          } # INSCOPE          } # INSCOPE
6690                        
6691          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6692                        
6693          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6694                        
6695            !!!nack ('t368.1');
6696          !!!next-token;          !!!next-token;
6697          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;  
6698        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6699          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6700            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6701            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6702              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6703                !!!parse-error (type => 'in a:a', token => $token);
6704                            
6705              !!!back-token;              !!!back-token; # <a>
6706              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6707              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6708                $formatting_end_tag->($token);
6709                            
6710              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6711                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6712                    !!!cp ('t372');
6713                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6714                  last AFE2;                  last AFE2;
6715                }                }
6716              } # AFE2              } # AFE2
6717              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6718                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6719                    !!!cp ('t373');
6720                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6721                  last OE;                  last OE;
6722                }                }
6723              } # OE              } # OE
6724              last AFE;              last AFE;
6725            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6726                !!!cp ('t374');
6727              last AFE;              last AFE;
6728            }            }
6729          } # AFE          } # AFE
6730                        
6731          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6732    
6733          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6734          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6735    
6736            !!!nack ('t374.1');
6737          !!!next-token;          !!!next-token;
6738          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;  
6739        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6740          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6741    
6742          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6743          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6744            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6745            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6746              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
6747              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6748              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6749              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6750            } elsif ({                        line => $token->{line}, column => $token->{column}};
6751                      table => 1, caption => 1, td => 1, th => 1,              next B;
6752                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6753                     }->{$node->[1]}) {              !!!cp ('t377');
6754              last INSCOPE;              last INSCOPE;
6755            }            }
6756          } # INSCOPE          } # INSCOPE
6757                    
6758          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6759          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6760                    
6761            !!!nack ('t377.1');
6762          !!!next-token;          !!!next-token;
6763          redo B;          next B;
6764        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6765          ## has a button element in scope          ## has a button element in scope
6766          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6767            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6768            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6769              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6770              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6771              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6772              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6773            } elsif ({                        line => $token->{line}, column => $token->{column}};
6774                      table => 1, caption => 1, td => 1, th => 1,              next B;
6775                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6776                     }->{$node->[1]}) {              !!!cp ('t379');
6777              last INSCOPE;              last INSCOPE;
6778            }            }
6779          } # INSCOPE          } # INSCOPE
6780                        
6781          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6782                        
6783          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6784          push @$active_formatting_elements, ['#marker', ''];  
6785            ## TODO: associate with $self->{form_element} if defined
6786    
         !!!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});  
6787          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6788            
6789          !!!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;  
             
6790          !!!next-token;          !!!next-token;
6791          redo B;          next B;
6792        } elsif ({        } elsif ({
6793                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6794                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6795                  image => 1,                  noembed => 1,
6796                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
6797                    noscript => 0, ## TODO: 1 if scripting is enabled
6798                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6799          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6800            !!!parse-error (type => 'image');            !!!cp ('t381');
6801            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6802            } else {
6803              !!!cp ('t399');
6804          }          }
6805            ## NOTE: There is an "as if in body" code clone.
6806          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6807          $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;  
6808        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6809          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6810                    
6811          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6812              !!!cp ('t389');
6813            ## Ignore the token            ## Ignore the token
6814              !!!nack ('t389'); ## NOTE: Not acknowledged.
6815            !!!next-token;            !!!next-token;
6816            redo B;            next B;
6817          } else {          } else {
6818              !!!ack ('t391.1');
6819    
6820            my $at = $token->{attributes};            my $at = $token->{attributes};
6821            my $form_attrs;            my $form_attrs;
6822            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 4834  sub _tree_construction_main ($) { Line 6826  sub _tree_construction_main ($) {
6826            delete $at->{prompt};            delete $at->{prompt};
6827            my @tokens = (            my @tokens = (
6828                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6829                           attributes => $form_attrs},                           attributes => $form_attrs,
6830                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6831                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6832                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6833                            {type => START_TAG_TOKEN, tag_name => 'p',
6834                             line => $token->{line}, column => $token->{column}},
6835                            {type => START_TAG_TOKEN, tag_name => 'label',
6836                             line => $token->{line}, column => $token->{column}},
6837                         );                         );
6838            if ($prompt_attr) {            if ($prompt_attr) {
6839              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6840                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6841                               #line => $token->{line}, column => $token->{column},
6842                              };
6843            } else {            } else {
6844                !!!cp ('t391');
6845              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6846                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6847                               #line => $token->{line}, column => $token->{column},
6848                              }; # SHOULD
6849              ## TODO: make this configurable              ## TODO: make this configurable
6850            }            }
6851            push @tokens,            push @tokens,
6852                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6853                             line => $token->{line}, column => $token->{column}},
6854                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6855                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6856                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6857                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6858                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6859            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6860                             line => $token->{line}, column => $token->{column}},
6861                            {type => END_TAG_TOKEN, tag_name => 'form',
6862                             line => $token->{line}, column => $token->{column}};
6863            !!!back-token (@tokens);            !!!back-token (@tokens);
6864            redo B;            !!!next-token;
6865              next B;
6866          }          }
6867        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6868          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6869          my $el;          my $el;
6870          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6871                    
6872          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6873          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4869  sub _tree_construction_main ($) { Line 6876  sub _tree_construction_main ($) {
6876          $insert->($el);          $insert->($el);
6877                    
6878          my $text = '';          my $text = '';
6879            !!!nack ('t392.1');
6880          !!!next-token;          !!!next-token;
6881          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6882            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6883            unless (length $token->{data}) {            unless (length $token->{data}) {
6884                !!!cp ('t392');
6885              !!!next-token;              !!!next-token;
6886              } else {
6887                !!!cp ('t393');
6888            }            }
6889            } else {
6890              !!!cp ('t394');
6891          }          }
6892          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6893              !!!cp ('t395');
6894            $text .= $token->{data};            $text .= $token->{data};
6895            !!!next-token;            !!!next-token;
6896          }          }
6897          if (length $text) {          if (length $text) {
6898              !!!cp ('t396');
6899            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6900          }          }
6901                    
# Line 4888  sub _tree_construction_main ($) { Line 6903  sub _tree_construction_main ($) {
6903                    
6904          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6905              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6906              !!!cp ('t397');
6907            ## Ignore the token            ## Ignore the token
6908          } else {          } else {
6909            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6910              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
6911          }          }
6912          !!!next-token;          !!!next-token;
6913            next B;
6914          } elsif ($token->{tag_name} eq 'rt' or
6915                   $token->{tag_name} eq 'rp') {
6916            ## has a |ruby| element in scope
6917            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6918              my $node = $self->{open_elements}->[$_];
6919              if ($node->[1] & RUBY_EL) {
6920                !!!cp ('t398.1');
6921                ## generate implied end tags
6922                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6923                  !!!cp ('t398.2');
6924                  pop @{$self->{open_elements}};
6925                }
6926                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
6927                  !!!cp ('t398.3');
6928                  !!!parse-error (type => 'not closed',
6929                                  text => $self->{open_elements}->[-1]->[0]
6930                                      ->manakai_local_name,
6931                                  token => $token);
6932                  pop @{$self->{open_elements}}
6933                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
6934                }
6935                last INSCOPE;
6936              } elsif ($node->[1] & SCOPING_EL) {
6937                !!!cp ('t398.4');
6938                last INSCOPE;
6939              }
6940            } # INSCOPE
6941    
6942            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6943    
6944            !!!nack ('t398.5');
6945            !!!next-token;
6946          redo B;          redo B;
6947        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6948                  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') {  
6949          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6950    
6951            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
6952    
6953            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6954    
6955            ## "adjust foreign attributes" - done in insert-element-f
6956                    
6957          !!!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);
6958                    
6959          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6960              pop @{$self->{open_elements}};
6961              !!!ack ('t398.1');
6962            } else {
6963              !!!cp ('t398.2');
6964              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6965              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6966              ## mode, "in body" (not "in foreign content") secondary insertion
6967              ## mode, maybe.
6968            }
6969    
6970          !!!next-token;          !!!next-token;
6971          redo B;          next B;
6972        } elsif ({        } elsif ({
6973                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6974                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6975                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6976                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6977                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6978          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6979            !!!parse-error (type => 'in body',
6980                            text => $token->{tag_name}, token => $token);
6981          ## Ignore the token          ## Ignore the token
6982            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6983          !!!next-token;          !!!next-token;
6984          redo B;          next B;
6985                    
6986          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6987        } else {        } else {
6988            if ($token->{tag_name} eq 'image') {
6989              !!!cp ('t384');
6990              !!!parse-error (type => 'image', token => $token);
6991              $token->{tag_name} = 'img';
6992            } else {
6993              !!!cp ('t385');
6994            }
6995    
6996            ## NOTE: There is an "as if <br>" code clone.
6997          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6998                    
6999          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7000    
7001            if ({
7002                 applet => 1, marquee => 1, object => 1,
7003                }->{$token->{tag_name}}) {
7004              !!!cp ('t380');
7005              push @$active_formatting_elements, ['#marker', ''];
7006              !!!nack ('t380.1');
7007            } elsif ({
7008                      b => 1, big => 1, em => 1, font => 1, i => 1,
7009                      s => 1, small => 1, strile => 1,
7010                      strong => 1, tt => 1, u => 1,
7011                     }->{$token->{tag_name}}) {
7012              !!!cp ('t375');
7013              push @$active_formatting_elements, $self->{open_elements}->[-1];
7014              !!!nack ('t375.1');
7015            } elsif ($token->{tag_name} eq 'input') {
7016              !!!cp ('t388');
7017              ## TODO: associate with $self->{form_element} if defined
7018              pop @{$self->{open_elements}};
7019              !!!ack ('t388.2');
7020            } elsif ({
7021                      area => 1, basefont => 1, bgsound => 1, br => 1,
7022                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7023                      #image => 1,
7024                     }->{$token->{tag_name}}) {
7025              !!!cp ('t388.1');
7026              pop @{$self->{open_elements}};
7027              !!!ack ('t388.3');
7028            } elsif ($token->{tag_name} eq 'select') {
7029              ## TODO: associate with $self->{form_element} if defined
7030            
7031              if ($self->{insertion_mode} & TABLE_IMS or
7032                  $self->{insertion_mode} & BODY_TABLE_IMS or
7033                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7034                !!!cp ('t400.1');
7035                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7036              } else {
7037                !!!cp ('t400.2');
7038                $self->{insertion_mode} = IN_SELECT_IM;
7039              }
7040              !!!nack ('t400.3');
7041            } else {
7042              !!!nack ('t402');
7043            }
7044                    
7045          !!!next-token;          !!!next-token;
7046          redo B;          next B;
7047        }        }
7048      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7049        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7050          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7051              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7052            for (@{$self->{open_elements}}) {          INSCOPE: {
7053              unless ({            for (reverse @{$self->{open_elements}}) {
7054                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7055                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7056                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7057                      }->{$_->[1]}) {                last INSCOPE;
7058                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
7059                  !!!cp ('t405.1');
7060                  last;
7061              }              }
7062            }            }
7063    
7064            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7065            !!!next-token;                            text => $token->{tag_name}, token => $token);
7066            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
7067            !!!next-token;            !!!next-token;
7068            redo B;            next B;
7069            } # INSCOPE
7070    
7071            for (@{$self->{open_elements}}) {
7072              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7073                !!!cp ('t403');
7074                !!!parse-error (type => 'not closed',
7075                                text => $_->[0]->manakai_local_name,
7076                                token => $token);
7077                last;
7078              } else {
7079                !!!cp ('t404');
7080              }
7081          }          }
7082    
7083            $self->{insertion_mode} = AFTER_BODY_IM;
7084            !!!next-token;
7085            next B;
7086        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7087          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
7088            ## up-to-date, though it has same effect as speced.
7089            if (@{$self->{open_elements}} > 1 and
7090                $self->{open_elements}->[1]->[1] & BODY_EL) {
7091            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7092            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7093              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
7094                !!!parse-error (type => 'not closed',
7095                                text => $self->{open_elements}->[1]->[0]
7096                                    ->manakai_local_name,
7097                                token => $token);
7098              } else {
7099                !!!cp ('t407');
7100            }            }
7101            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7102            ## reprocess            ## reprocess
7103            redo B;            next B;
7104          } else {          } else {
7105            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
7106              !!!parse-error (type => 'unmatched end tag',
7107                              text => $token->{tag_name}, token => $token);
7108            ## Ignore the token            ## Ignore the token
7109            !!!next-token;            !!!next-token;
7110            redo B;            next B;
7111          }          }
7112        } elsif ({        } elsif ({
7113                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7114                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7115                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7116                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7117                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7118                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7119          ## has an element in scope          ## has an element in scope
7120          my $i;          my $i;
7121          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7122            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7123            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7124              ## 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;  
             }  
7125              $i = $_;              $i = $_;
7126              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7127            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7128                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7129              last INSCOPE;              last INSCOPE;
7130            }            }
7131          } # INSCOPE          } # INSCOPE
7132            
7133          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7134            if (defined $i) {            !!!cp ('t413');
7135              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag',
7136                              text => $token->{tag_name}, token => $token);
7137              ## NOTE: Ignore the token.
7138            } else {
7139              ## Step 1. generate implied end tags
7140              while ({
7141                      ## END_TAG_OPTIONAL_EL
7142                      dd => ($token->{tag_name} ne 'dd'),
7143                      dt => ($token->{tag_name} ne 'dt'),
7144                      li => ($token->{tag_name} ne 'li'),
7145                      p => 1,
7146                      rt => 1,
7147                      rp => 1,
7148                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7149                !!!cp ('t409');
7150                pop @{$self->{open_elements}};
7151              }
7152    
7153              ## Step 2.
7154              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7155                      ne $token->{tag_name}) {
7156                !!!cp ('t412');
7157                !!!parse-error (type => 'not closed',
7158                                text => $self->{open_elements}->[-1]->[0]
7159                                    ->manakai_local_name,
7160                                token => $token);
7161            } else {            } else {
7162              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
7163            }            }
7164          }  
7165                      ## Step 3.
         if (defined $i) {  
7166            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7167          } elsif ($token->{tag_name} eq 'p') {  
7168            ## As if <p>, then reprocess the current token            ## Step 4.
7169            my $el;            $clear_up_to_marker->()
7170            !!!create-element ($el, 'p');                if {
7171            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
7172                  }->{$token->{tag_name}};
7173          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7174          !!!next-token;          !!!next-token;
7175          redo B;          next B;
7176        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7177            undef $self->{form_element};
7178    
7179          ## has an element in scope          ## has an element in scope
7180            my $i;
7181          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7182            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7183            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7184              ## generate implied end tags              !!!cp ('t418');
7185              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;  
             }  
7186              last INSCOPE;              last INSCOPE;
7187            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7188                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7189              last INSCOPE;              last INSCOPE;
7190            }            }
7191          } # INSCOPE          } # INSCOPE
7192            
7193          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7194            pop @{$self->{open_elements}};            !!!cp ('t421');
7195          } else {            !!!parse-error (type => 'unmatched end tag',
7196            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                            text => $token->{tag_name}, token => $token);
7197              ## NOTE: Ignore the token.
7198            } else {
7199              ## Step 1. generate implied end tags
7200              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7201                !!!cp ('t417');
7202                pop @{$self->{open_elements}};
7203              }
7204              
7205              ## Step 2.
7206              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7207                      ne $token->{tag_name}) {
7208                !!!cp ('t417.1');
7209                !!!parse-error (type => 'not closed',
7210                                text => $self->{open_elements}->[-1]->[0]
7211                                    ->manakai_local_name,
7212                                token => $token);
7213              } else {
7214                !!!cp ('t420');
7215              }  
7216              
7217              ## Step 3.
7218              splice @{$self->{open_elements}}, $i;
7219          }          }
7220    
         undef $self->{form_element};  
7221          !!!next-token;          !!!next-token;
7222          redo B;          next B;
7223        } elsif ({        } elsif ({
7224                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7225                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5069  sub _tree_construction_main ($) { Line 7227  sub _tree_construction_main ($) {
7227          my $i;          my $i;
7228          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7229            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7230            if ({            if ($node->[1] & HEADING_EL) {
7231                 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;  
             }  
7232              $i = $_;              $i = $_;
7233              last INSCOPE;              last INSCOPE;
7234            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7235                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7236              last INSCOPE;              last INSCOPE;
7237            }            }
7238          } # INSCOPE          } # INSCOPE
7239            
7240          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7241            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
7242              !!!parse-error (type => 'unmatched end tag',
7243                              text => $token->{tag_name}, token => $token);
7244              ## NOTE: Ignore the token.
7245            } else {
7246              ## Step 1. generate implied end tags
7247              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7248                !!!cp ('t422');
7249                pop @{$self->{open_elements}};
7250              }
7251              
7252              ## Step 2.
7253              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7254                      ne $token->{tag_name}) {
7255                !!!cp ('t425');
7256                !!!parse-error (type => 'unmatched end tag',
7257                                text => $token->{tag_name}, token => $token);
7258              } else {
7259                !!!cp ('t426');
7260              }
7261    
7262              ## Step 3.
7263              splice @{$self->{open_elements}}, $i;
7264          }          }
7265                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7266          !!!next-token;          !!!next-token;
7267          redo B;          next B;
7268          } elsif ($token->{tag_name} eq 'p') {
7269            ## has an element in scope
7270            my $i;
7271            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7272              my $node = $self->{open_elements}->[$_];
7273              if ($node->[1] & P_EL) {
7274                !!!cp ('t410.1');
7275                $i = $_;
7276                last INSCOPE;
7277              } elsif ($node->[1] & SCOPING_EL) {
7278                !!!cp ('t411.1');
7279                last INSCOPE;
7280              }
7281            } # INSCOPE
7282    
7283            if (defined $i) {
7284              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7285                      ne $token->{tag_name}) {
7286                !!!cp ('t412.1');
7287                !!!parse-error (type => 'not closed',
7288                                text => $self->{open_elements}->[-1]->[0]
7289                                    ->manakai_local_name,
7290                                token => $token);
7291              } else {
7292                !!!cp ('t414.1');
7293              }
7294    
7295              splice @{$self->{open_elements}}, $i;
7296            } else {
7297              !!!cp ('t413.1');
7298              !!!parse-error (type => 'unmatched end tag',
7299                              text => $token->{tag_name}, token => $token);
7300    
7301              !!!cp ('t415.1');
7302              ## As if <p>, then reprocess the current token
7303              my $el;
7304              !!!create-element ($el, $HTML_NS, 'p',, $token);
7305              $insert->($el);
7306              ## NOTE: Not inserted into |$self->{open_elements}|.
7307            }
7308    
7309            !!!next-token;
7310            next B;
7311        } elsif ({        } elsif ({
7312                  a => 1,                  a => 1,
7313                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7314                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7315                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7316                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7317          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7318          redo B;          $formatting_end_tag->($token);
7319            next B;
7320        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7321          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7322            !!!parse-error (type => 'unmatched end tag',
7323                            text => 'br', token => $token);
7324    
7325          ## As if <br>          ## As if <br>
7326          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7327                    
7328          my $el;          my $el;
7329          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7330          $insert->($el);          $insert->($el);
7331                    
7332          ## Ignore the token.          ## Ignore the token.
7333          !!!next-token;          !!!next-token;
7334          redo B;          next B;
7335        } elsif ({        } elsif ({
7336                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7337                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5133  sub _tree_construction_main ($) { Line 7344  sub _tree_construction_main ($) {
7344                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7345                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7346                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7347          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7348            !!!parse-error (type => 'unmatched end tag',
7349                            text => $token->{tag_name}, token => $token);
7350          ## Ignore the token          ## Ignore the token
7351          !!!next-token;          !!!next-token;
7352          redo B;          next B;
7353                    
7354          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7355                    
# Line 5147  sub _tree_construction_main ($) { Line 7360  sub _tree_construction_main ($) {
7360    
7361          ## Step 2          ## Step 2
7362          S2: {          S2: {
7363            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7364              ## Step 1              ## Step 1
7365              ## generate implied end tags              ## generate implied end tags
7366              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7367                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7368                   td => 1, th => 1, tr => 1,                ## NOTE: |<ruby><rt></ruby>|.
7369                   tbody => 1, tfoot => 1, thead => 1,                ## ISSUE: <ruby><rt></rt> will also take this code path,
7370                  }->{$self->{open_elements}->[-1]->[1]}) {                ## which seems wrong.
7371                !!!back-token;                pop @{$self->{open_elements}};
7372                $token = {type => END_TAG_TOKEN,                $node_i++;
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7373              }              }
7374                    
7375              ## Step 2              ## Step 2
7376              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7377                        ne $token->{tag_name}) {
7378                  !!!cp ('t431');
7379                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7380                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7381                                  text => $self->{open_elements}->[-1]->[0]
7382                                      ->manakai_local_name,
7383                                  token => $token);
7384                } else {
7385                  !!!cp ('t432');
7386              }              }
7387                            
7388              ## Step 3              ## Step 3
7389              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7390    
7391              !!!next-token;              !!!next-token;
7392              last S2;              last S2;
7393            } else {            } else {
7394              ## Step 3              ## Step 3
7395              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7396                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7397                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7398                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7399                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7400                  !!!parse-error (type => 'unmatched end tag',
7401                                  text => $token->{tag_name}, token => $token);
7402                ## Ignore the token                ## Ignore the token
7403                !!!next-token;                !!!next-token;
7404                last S2;                last S2;
7405              }              }
7406    
7407                !!!cp ('t434');
7408            }            }
7409                        
7410            ## Step 4            ## Step 4
# Line 5192  sub _tree_construction_main ($) { Line 7414  sub _tree_construction_main ($) {
7414            ## Step 5;            ## Step 5;
7415            redo S2;            redo S2;
7416          } # S2          } # S2
7417          redo B;          next B;
7418        }        }
7419      }      }
7420      redo B;      next B;
7421      } continue { # B
7422        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7423          ## NOTE: The code below is executed in cases where it does not have
7424          ## to be, but it it is harmless even in those cases.
7425          ## has an element in scope
7426          INSCOPE: {
7427            for (reverse 0..$#{$self->{open_elements}}) {
7428              my $node = $self->{open_elements}->[$_];
7429              if ($node->[1] & FOREIGN_EL) {
7430                last INSCOPE;
7431              } elsif ($node->[1] & SCOPING_EL) {
7432                last;
7433              }
7434            }
7435            
7436            ## NOTE: No foreign element in scope.
7437            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7438          } # INSCOPE
7439        }
7440    } # B    } # B
7441    
   ## 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  
   
7442    ## Stop parsing # MUST    ## Stop parsing # MUST
7443        
7444    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5214  sub set_inner_html ($$$) { Line 7450  sub set_inner_html ($$$) {
7450    my $s = \$_[0];    my $s = \$_[0];
7451    my $onerror = $_[1];    my $onerror = $_[1];
7452    
7453      ## ISSUE: Should {confident} be true?
7454    
7455    my $nt = $node->node_type;    my $nt = $node->node_type;
7456    if ($nt == 9) {    if ($nt == 9) {
7457      # MUST      # MUST
# Line 5242  sub set_inner_html ($$$) { Line 7480  sub set_inner_html ($$$) {
7480      my $p = $class->new;      my $p = $class->new;
7481      $p->{document} = $doc;      $p->{document} = $doc;
7482    
7483      ## Step 9 # MUST      ## Step 8 # MUST
7484      my $i = 0;      my $i = 0;
7485      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7486      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7487      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7488        my $self = shift;        my $self = shift;
7489    
7490        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7491        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7492    
7493        $self->{next_input_character} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7494        $self->{next_input_character} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7495        $column++;  
7496          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7497        if ($self->{next_input_character} == 0x000A) { # LF        $p->{column}++;
7498          $line++;  
7499          $column = 0;        if ($self->{next_char} == 0x000A) { # LF
7500        } elsif ($self->{next_input_character} == 0x000D) { # CR          $p->{line}++;
7501            $p->{column} = 0;
7502            !!!cp ('i1');
7503          } elsif ($self->{next_char} == 0x000D) { # CR
7504          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7505          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7506          $line++;          $p->{line}++;
7507          $column = 0;          $p->{column} = 0;
7508        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7509          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7510        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7511            !!!cp ('i3');
7512          } elsif ($self->{next_char} == 0x0000) { # NULL
7513            !!!cp ('i4');
7514          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7515          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7516          } elsif ($self->{next_char} <= 0x0008 or
7517                   (0x000E <= $self->{next_char} and
7518                    $self->{next_char} <= 0x001F) or
7519                   (0x007F <= $self->{next_char} and
7520                    $self->{next_char} <= 0x009F) or
7521                   (0xD800 <= $self->{next_char} and
7522                    $self->{next_char} <= 0xDFFF) or
7523                   (0xFDD0 <= $self->{next_char} and
7524                    $self->{next_char} <= 0xFDDF) or
7525                   {
7526                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7527                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7528                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7529                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7530                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7531                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7532                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7533                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7534                    0x10FFFE => 1, 0x10FFFF => 1,
7535                   }->{$self->{next_char}}) {
7536            !!!cp ('i4.1');
7537            if ($self->{next_char} < 0x10000) {
7538              !!!parse-error (type => 'control char',
7539                              text => (sprintf 'U+%04X', $self->{next_char}));
7540            } else {
7541              !!!parse-error (type => 'control char',
7542                              text => (sprintf 'U-%08X', $self->{next_char}));
7543            }
7544        }        }
7545      };      };
7546      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7547      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7548            
7549      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7550        my (%opt) = @_;        my (%opt) = @_;
7551        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7552          my $column = $opt{column};
7553          if (defined $opt{token} and defined $opt{token}->{line}) {
7554            $line = $opt{token}->{line};
7555            $column = $opt{token}->{column};
7556          }
7557          warn "Parse error ($opt{type}) at line $line column $column\n";
7558      };      };
7559      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7560        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7561      };      };
7562            
7563      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7564      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7565    
7566      ## Step 2      ## Step 2
7567      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7568      $p->{content_model} = {      $p->{content_model} = {
7569        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7570        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5303  sub set_inner_html ($$$) { Line 7581  sub set_inner_html ($$$) {
7581          unless defined $p->{content_model};          unless defined $p->{content_model};
7582          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7583    
7584      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7585          ## TODO: Foreign element OK?
7586    
7587      ## Step 4      ## Step 3
7588      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7589        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7590    
7591      ## Step 5 # MUST      ## Step 4 # MUST
7592      $doc->append_child ($root);      $doc->append_child ($root);
7593    
7594      ## Step 6 # MUST      ## Step 5 # MUST
7595      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7596    
7597      undef $p->{head_element};      undef $p->{head_element};
7598    
7599      ## Step 7 # MUST      ## Step 6 # MUST
7600      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7601    
7602      ## Step 8 # MUST      ## Step 7 # MUST
7603      my $anode = $node;      my $anode = $node;
7604      AN: while (defined $anode) {      AN: while (defined $anode) {
7605        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7606          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7607          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7608            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7609                !!!cp ('i5');
7610              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7611              last AN;              last AN;
7612            }            }
# Line 5335  sub set_inner_html ($$$) { Line 7615  sub set_inner_html ($$$) {
7615        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7616      } # AN      } # AN
7617            
7618      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7619      {      {
7620        my $self = $p;        my $self = $p;
7621        !!!next-token;        !!!next-token;
7622      }      }
7623      $p->_tree_construction_main;      $p->_tree_construction_main;
7624    
7625      ## Step 11 # MUST      ## Step 10 # MUST
7626      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7627      for (@cn) {      for (@cn) {
7628        $node->remove_child ($_);        $node->remove_child ($_);
7629      }      }
7630      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7631    
7632      ## Step 12 # MUST      ## Step 11 # MUST
7633      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7634      for (@cn) {      for (@cn) {
7635        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5359  sub set_inner_html ($$$) { Line 7638  sub set_inner_html ($$$) {
7638      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7639    
7640      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7641    
7642        delete $p->{parse_error}; # delete loop
7643    } else {    } else {
7644      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";
7645    }    }
# Line 5366  sub set_inner_html ($$$) { Line 7647  sub set_inner_html ($$$) {
7647    
7648  } # tree construction stage  } # tree construction stage
7649    
7650  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7651    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  
7652    
7653  1;  1;
7654  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24