/[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.138 by wakaba, Sun May 18 04:15:48 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  ## TODO: 1252 parse error (revision 1264)
12  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  ## TODO: 8859-11 = 874 (revision 1271)
13  ## is not yet clear.  
14  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
15  ## "{U+FEFF}..." in GB18030?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
16    my $SVG_NS = q<http://www.w3.org/2000/svg>;
17  my $permitted_slash_tag_name = {  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
18    base => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
19    link => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
20    meta => 1,  
21    hr => 1,  sub A_EL () { 0b1 }
22    br => 1,  sub ADDRESS_EL () { 0b10 }
23    img=> 1,  sub BODY_EL () { 0b100 }
24    embed => 1,  sub BUTTON_EL () { 0b1000 }
25    param => 1,  sub CAPTION_EL () { 0b10000 }
26    area => 1,  sub DD_EL () { 0b100000 }
27    col => 1,  sub DIV_EL () { 0b1000000 }
28    input => 1,  sub DT_EL () { 0b10000000 }
29    sub FORM_EL () { 0b100000000 }
30    sub FORMATTING_EL () { 0b1000000000 }
31    sub FRAMESET_EL () { 0b10000000000 }
32    sub HEADING_EL () { 0b100000000000 }
33    sub HTML_EL () { 0b1000000000000 }
34    sub LI_EL () { 0b10000000000000 }
35    sub NOBR_EL () { 0b100000000000000 }
36    sub OPTION_EL () { 0b1000000000000000 }
37    sub OPTGROUP_EL () { 0b10000000000000000 }
38    sub P_EL () { 0b100000000000000000 }
39    sub SELECT_EL () { 0b1000000000000000000 }
40    sub TABLE_EL () { 0b10000000000000000000 }
41    sub TABLE_CELL_EL () { 0b100000000000000000000 }
42    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
43    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
44    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
45    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
46    sub FOREIGN_EL () { 0b10000000000000000000000000 }
47    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
48    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
49    
50    sub TABLE_ROWS_EL () {
51      TABLE_EL |
52      TABLE_ROW_EL |
53      TABLE_ROW_GROUP_EL
54    }
55    
56    sub END_TAG_OPTIONAL_EL () {
57      DD_EL |
58      DT_EL |
59      LI_EL |
60      P_EL
61    }
62    
63    sub ALL_END_TAG_OPTIONAL_EL () {
64      END_TAG_OPTIONAL_EL |
65      BODY_EL |
66      HTML_EL |
67      TABLE_CELL_EL |
68      TABLE_ROW_EL |
69      TABLE_ROW_GROUP_EL
70    }
71    
72    sub SCOPING_EL () {
73      BUTTON_EL |
74      CAPTION_EL |
75      HTML_EL |
76      TABLE_EL |
77      TABLE_CELL_EL |
78      MISC_SCOPING_EL
79    }
80    
81    sub TABLE_SCOPING_EL () {
82      HTML_EL |
83      TABLE_EL
84    }
85    
86    sub TABLE_ROWS_SCOPING_EL () {
87      HTML_EL |
88      TABLE_ROW_GROUP_EL
89    }
90    
91    sub TABLE_ROW_SCOPING_EL () {
92      HTML_EL |
93      TABLE_ROW_EL
94    }
95    
96    sub SPECIAL_EL () {
97      ADDRESS_EL |
98      BODY_EL |
99      DIV_EL |
100      END_TAG_OPTIONAL_EL |
101      FORM_EL |
102      FRAMESET_EL |
103      HEADING_EL |
104      OPTION_EL |
105      OPTGROUP_EL |
106      SELECT_EL |
107      TABLE_ROW_EL |
108      TABLE_ROW_GROUP_EL |
109      MISC_SPECIAL_EL
110    }
111    
112    my $el_category = {
113      a => A_EL | FORMATTING_EL,
114      address => ADDRESS_EL,
115      applet => MISC_SCOPING_EL,
116      area => MISC_SPECIAL_EL,
117      b => FORMATTING_EL,
118      base => MISC_SPECIAL_EL,
119      basefont => MISC_SPECIAL_EL,
120      bgsound => MISC_SPECIAL_EL,
121      big => FORMATTING_EL,
122      blockquote => MISC_SPECIAL_EL,
123      body => BODY_EL,
124      br => MISC_SPECIAL_EL,
125      button => BUTTON_EL,
126      caption => CAPTION_EL,
127      center => MISC_SPECIAL_EL,
128      col => MISC_SPECIAL_EL,
129      colgroup => MISC_SPECIAL_EL,
130      dd => DD_EL,
131      dir => MISC_SPECIAL_EL,
132      div => DIV_EL,
133      dl => MISC_SPECIAL_EL,
134      dt => DT_EL,
135      em => FORMATTING_EL,
136      embed => MISC_SPECIAL_EL,
137      fieldset => MISC_SPECIAL_EL,
138      font => FORMATTING_EL,
139      form => FORM_EL,
140      frame => MISC_SPECIAL_EL,
141      frameset => FRAMESET_EL,
142      h1 => HEADING_EL,
143      h2 => HEADING_EL,
144      h3 => HEADING_EL,
145      h4 => HEADING_EL,
146      h5 => HEADING_EL,
147      h6 => HEADING_EL,
148      head => MISC_SPECIAL_EL,
149      hr => MISC_SPECIAL_EL,
150      html => HTML_EL,
151      i => FORMATTING_EL,
152      iframe => MISC_SPECIAL_EL,
153      img => MISC_SPECIAL_EL,
154      input => MISC_SPECIAL_EL,
155      isindex => MISC_SPECIAL_EL,
156      li => LI_EL,
157      link => MISC_SPECIAL_EL,
158      listing => MISC_SPECIAL_EL,
159      marquee => MISC_SCOPING_EL,
160      menu => MISC_SPECIAL_EL,
161      meta => MISC_SPECIAL_EL,
162      nobr => NOBR_EL | FORMATTING_EL,
163      noembed => MISC_SPECIAL_EL,
164      noframes => MISC_SPECIAL_EL,
165      noscript => MISC_SPECIAL_EL,
166      object => MISC_SCOPING_EL,
167      ol => MISC_SPECIAL_EL,
168      optgroup => OPTGROUP_EL,
169      option => OPTION_EL,
170      p => P_EL,
171      param => MISC_SPECIAL_EL,
172      plaintext => MISC_SPECIAL_EL,
173      pre => MISC_SPECIAL_EL,
174      s => FORMATTING_EL,
175      script => MISC_SPECIAL_EL,
176      select => SELECT_EL,
177      small => FORMATTING_EL,
178      spacer => MISC_SPECIAL_EL,
179      strike => FORMATTING_EL,
180      strong => FORMATTING_EL,
181      style => MISC_SPECIAL_EL,
182      table => TABLE_EL,
183      tbody => TABLE_ROW_GROUP_EL,
184      td => TABLE_CELL_EL,
185      textarea => MISC_SPECIAL_EL,
186      tfoot => TABLE_ROW_GROUP_EL,
187      th => TABLE_CELL_EL,
188      thead => TABLE_ROW_GROUP_EL,
189      title => MISC_SPECIAL_EL,
190      tr => TABLE_ROW_EL,
191      tt => FORMATTING_EL,
192      u => FORMATTING_EL,
193      ul => MISC_SPECIAL_EL,
194      wbr => MISC_SPECIAL_EL,
195    };
196    
197    my $el_category_f = {
198      $MML_NS => {
199        'annotation-xml' => MML_AXML_EL,
200        mi => FOREIGN_FLOW_CONTENT_EL,
201        mo => FOREIGN_FLOW_CONTENT_EL,
202        mn => FOREIGN_FLOW_CONTENT_EL,
203        ms => FOREIGN_FLOW_CONTENT_EL,
204        mtext => FOREIGN_FLOW_CONTENT_EL,
205      },
206      $SVG_NS => {
207        foreignObject => FOREIGN_FLOW_CONTENT_EL,
208        desc => FOREIGN_FLOW_CONTENT_EL,
209        title => FOREIGN_FLOW_CONTENT_EL,
210      },
211      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
212    };
213    
214    my $svg_attr_name = {
215      attributetype => 'attributeType',
216      basefrequency => 'baseFrequency',
217      baseprofile => 'baseProfile',
218      calcmode => 'calcMode',
219      clippathunits => 'clipPathUnits',
220      contentscripttype => 'contentScriptType',
221      contentstyletype => 'contentStyleType',
222      diffuseconstant => 'diffuseConstant',
223      edgemode => 'edgeMode',
224      externalresourcesrequired => 'externalResourcesRequired',
225      fecolormatrix => 'feColorMatrix',
226      fecomposite => 'feComposite',
227      fegaussianblur => 'feGaussianBlur',
228      femorphology => 'feMorphology',
229      fetile => 'feTile',
230      filterres => 'filterRes',
231      filterunits => 'filterUnits',
232      glyphref => 'glyphRef',
233      gradienttransform => 'gradientTransform',
234      gradientunits => 'gradientUnits',
235      kernelmatrix => 'kernelMatrix',
236      kernelunitlength => 'kernelUnitLength',
237      keypoints => 'keyPoints',
238      keysplines => 'keySplines',
239      keytimes => 'keyTimes',
240      lengthadjust => 'lengthAdjust',
241      limitingconeangle => 'limitingConeAngle',
242      markerheight => 'markerHeight',
243      markerunits => 'markerUnits',
244      markerwidth => 'markerWidth',
245      maskcontentunits => 'maskContentUnits',
246      maskunits => 'maskUnits',
247      numoctaves => 'numOctaves',
248      pathlength => 'pathLength',
249      patterncontentunits => 'patternContentUnits',
250      patterntransform => 'patternTransform',
251      patternunits => 'patternUnits',
252      pointsatx => 'pointsAtX',
253      pointsaty => 'pointsAtY',
254      pointsatz => 'pointsAtZ',
255      preservealpha => 'preserveAlpha',
256      preserveaspectratio => 'preserveAspectRatio',
257      primitiveunits => 'primitiveUnits',
258      refx => 'refX',
259      refy => 'refY',
260      repeatcount => 'repeatCount',
261      repeatdur => 'repeatDur',
262      requiredextensions => 'requiredExtensions',
263      specularconstant => 'specularConstant',
264      specularexponent => 'specularExponent',
265      spreadmethod => 'spreadMethod',
266      startoffset => 'startOffset',
267      stddeviation => 'stdDeviation',
268      stitchtiles => 'stitchTiles',
269      surfacescale => 'surfaceScale',
270      systemlanguage => 'systemLanguage',
271      tablevalues => 'tableValues',
272      targetx => 'targetX',
273      targety => 'targetY',
274      textlength => 'textLength',
275      viewbox => 'viewBox',
276      viewtarget => 'viewTarget',
277      xchannelselector => 'xChannelSelector',
278      ychannelselector => 'yChannelSelector',
279      zoomandpan => 'zoomAndPan',
280  };  };
281    
282    my $foreign_attr_xname = {
283      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
284      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
285      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
286      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
287      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
288      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
289      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
290      'xml:base' => [$XML_NS, ['xml', 'base']],
291      'xml:lang' => [$XML_NS, ['xml', 'lang']],
292      'xml:space' => [$XML_NS, ['xml', 'space']],
293      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
294      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
295    };
296    
297    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
298    
299  my $c1_entity_char = {  my $c1_entity_char = {
300    0x80 => 0x20AC,    0x80 => 0x20AC,
301    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 62  my $c1_entity_char = { Line 331  my $c1_entity_char = {
331    0x9F => 0x0178,    0x9F => 0x0178,
332  }; # $c1_entity_char  }; # $c1_entity_char
333    
334  my $special_category = {  sub parse_byte_string ($$$$;$) {
335    address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,    my $self = shift;
336    blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,    my $charset_name = shift;
337    dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,    open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
338    form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,    return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
339    h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  } # parse_byte_string
340    img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
341    menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  sub parse_byte_stream ($$$$;$) {
342    ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,    my $self = ref $_[0] ? shift : shift->new;
343    pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,    my $charset_name = shift;
344    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  
345    
346  sub parse_string ($$$;$) {    my $onerror = $_[2] || sub {
347    my $self = shift->new;      my (%opt) = @_;
348    my $s = \$_[0];      warn "Parse error ($opt{type})\n";
349      };
350      $self->{parse_error} = $onerror; # updated later by parse_char_string
351    
352      ## HTML5 encoding sniffing algorithm
353      require Message::Charset::Info;
354      my $charset;
355      my $buffer;
356      my ($char_stream, $e_status);
357    
358      SNIFFING: {
359    
360        ## Step 1
361        if (defined $charset_name) {
362          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
363    
364          ## ISSUE: Unsupported encoding is not ignored according to the spec.
365          ($char_stream, $e_status) = $charset->get_decode_handle
366              ($byte_stream, allow_error_reporting => 1,
367               allow_fallback => 1);
368          if ($char_stream) {
369            $self->{confident} = 1;
370            last SNIFFING;
371          } else {
372            ## TODO: unsupported error
373          }
374        }
375    
376        ## Step 2
377        my $byte_buffer = '';
378        for (1..1024) {
379          my $char = $byte_stream->getc;
380          last unless defined $char;
381          $byte_buffer .= $char;
382        } ## TODO: timeout
383    
384        ## Step 3
385        if ($byte_buffer =~ /^\xFE\xFF/) {
386          $charset = Message::Charset::Info->get_by_iana_name ('utf-16be');
387          ($char_stream, $e_status) = $charset->get_decode_handle
388              ($byte_stream, allow_error_reporting => 1,
389               allow_fallback => 1, byte_buffer => \$byte_buffer);
390          $self->{confident} = 1;
391          last SNIFFING;
392        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
393          $charset = Message::Charset::Info->get_by_iana_name ('utf-16le');
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1, byte_buffer => \$byte_buffer);
397          $self->{confident} = 1;
398          last SNIFFING;
399        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
400          $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
401          ($char_stream, $e_status) = $charset->get_decode_handle
402              ($byte_stream, allow_error_reporting => 1,
403               allow_fallback => 1, byte_buffer => \$byte_buffer);
404          $self->{confident} = 1;
405          last SNIFFING;
406        }
407    
408        ## Step 4
409        ## TODO: <meta charset>
410    
411        ## Step 5
412        ## TODO: from history
413    
414        ## Step 6
415        require Whatpm::Charset::UniversalCharDet;
416        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
417            ($byte_buffer);
418        if (defined $charset_name) {
419          $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
420    
421          ## ISSUE: Unsupported encoding is not ignored according to the spec.
422          require Whatpm::Charset::DecodeHandle;
423          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
424              ($byte_stream);
425          ($char_stream, $e_status) = $charset->get_decode_handle
426              ($buffer, allow_error_reporting => 1,
427               allow_fallback => 1, byte_buffer => \$byte_buffer);
428          if ($char_stream) {
429            $buffer->{buffer} = $byte_buffer;
430            !!!parse-error (type => 'sniffing:chardet', ## TODO: type name
431                            value => $charset_name,
432                            level => $self->{info_level},
433                            line => 1, column => 1);
434            $self->{confident} = 0;
435            last SNIFFING;
436          }
437        }
438    
439        ## Step 7: default
440        ## TODO: Make this configurable.
441        $charset = Message::Charset::Info->get_by_iana_name ('windows-1252');
442            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
443            ## detectable in the step 6.
444        require Whatpm::Charset::DecodeHandle;
445        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
446            ($byte_stream);
447        ($char_stream, $e_status)
448            = $charset->get_decode_handle ($buffer,
449                                           allow_error_reporting => 1,
450                                           allow_fallback => 1,
451                                           byte_buffer => \$byte_buffer);
452        $buffer->{buffer} = $byte_buffer;
453        !!!parse-error (type => 'sniffing:default', ## TODO: type name
454                        value => 'windows-1252',
455                        level => $self->{info_level},
456                        line => 1, column => 1);
457        $self->{confident} = 0;
458      } # SNIFFING
459    
460      $self->{input_encoding} = $charset->get_iana_name;
461      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
462        !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
463                        value => $self->{input_encoding},
464                        level => $self->{unsupported_level},
465                        line => 1, column => 1);
466      } elsif (not ($e_status &
467                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
468        !!!parse-error (type => 'chardecode:no error', ## TODO: type name
469                        value => $self->{input_encoding},
470                        level => $self->{unsupported_level},
471                        line => 1, column => 1);
472      }
473    
474      $self->{change_encoding} = sub {
475        my $self = shift;
476        $charset_name = shift;
477        my $token = shift;
478    
479        $charset = Message::Charset::Info->get_by_iana_name ($charset_name);
480        ($char_stream, $e_status) = $charset->get_decode_handle
481            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
482             byte_buffer => \ $buffer->{buffer});
483        
484        if ($char_stream) { # if supported
485          ## "Change the encoding" algorithm:
486    
487          ## Step 1    
488          if ($charset->{iana_names}->{'utf-16'}) { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?
489            $charset = Message::Charset::Info->get_by_iana_name ('utf-8');
490            ($char_stream, $e_status) = $charset->get_decode_handle
491                ($byte_stream,
492                 byte_buffer => \ $buffer->{buffer});
493          }
494          $charset_name = $charset->get_iana_name;
495          
496          ## Step 2
497          if (defined $self->{input_encoding} and
498              $self->{input_encoding} eq $charset_name) {
499            !!!parse-error (type => 'charset label:matching', ## TODO: type
500                            value => $charset_name,
501                            level => $self->{info_level});
502            $self->{confident} = 1;
503            return;
504          }
505    
506          !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.
507              ':'.$charset_name, level => 'w', token => $token);
508          
509          ## Step 3
510          # if (can) {
511            ## change the encoding on the fly.
512            #$self->{confident} = 1;
513            #return;
514          # }
515          
516          ## Step 4
517          throw Whatpm::HTML::RestartParser ();
518        }
519      }; # $self->{change_encoding}
520    
521      my $char_onerror = sub {
522        my (undef, $type, %opt) = @_;
523        !!!parse-error (%opt, type => $type,
524                        line => $self->{line}, column => $self->{column} + 1);
525        if ($opt{octets}) {
526          ${$opt{octets}} = "\x{FFFD}"; # relacement character
527        }
528      };
529      $char_stream->onerror ($char_onerror);
530    
531      my @args = @_; shift @args; # $s
532      my $return;
533      try {
534        $return = $self->parse_char_stream ($char_stream, @args);  
535      } catch Whatpm::HTML::RestartParser with {
536        ## NOTE: Invoked after {change_encoding}.
537    
538        $self->{input_encoding} = $charset->get_iana_name;
539        if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
540          !!!parse-error (type => 'chardecode:fallback', ## TODO: type name
541                          value => $self->{input_encoding},
542                          level => $self->{unsupported_level},
543                          line => 1, column => 1);
544        } elsif (not ($e_status &
545                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
546          !!!parse-error (type => 'chardecode:no error', ## TODO: type name
547                          value => $self->{input_encoding},
548                          level => $self->{unsupported_level},
549                          line => 1, column => 1);
550        }
551        $self->{confident} = 1;
552        $char_stream->onerror ($char_onerror);
553        $return = $self->parse_char_stream ($char_stream, @args);
554      };
555      return $return;
556    } # parse_byte_stream
557    
558    ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
559    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
560    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
561    ## because the core part of our HTML parser expects a string of character,
562    ## not a string of bytes or code units or anything which might contain a BOM.
563    ## Therefore, any parser interface that accepts a string of bytes,
564    ## such as |parse_byte_string| in this module, must ensure that it does
565    ## strip the BOM and never strip any ZWNBSP.
566    
567    sub parse_char_string ($$$;$) {
568      my $self = shift;
569      open my $input, '<:utf8', ref $_[0] ? $_[0] : \($_[0]);
570      return $self->parse_char_stream ($input, @_[1..$#_]);
571    } # parse_char_string
572    *parse_string = \&parse_char_string;
573    
574    sub parse_char_stream ($$$;$) {
575      my $self = ref $_[0] ? shift : shift->new;
576      my $input = $_[0];
577    $self->{document} = $_[1];    $self->{document} = $_[1];
578      @{$self->{document}->child_nodes} = ();
579    
580    ## NOTE: |set_inner_html| copies most of this method's code    ## NOTE: |set_inner_html| copies most of this method's code
581    
582      $self->{confident} = 1 unless exists $self->{confident};
583      $self->{document}->input_encoding ($self->{input_encoding})
584          if defined $self->{input_encoding};
585    
586    my $i = 0;    my $i = 0;
587    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
588    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
589    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
590      my $self = shift;      my $self = shift;
591    
592      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
593      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
594    
595      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char = $input->getc;
596      $self->{next_input_character} = ord substr $$s, $i++, 1;      $self->{next_char} = -1 and return unless defined $char;
597      $column++;      $self->{next_char} = ord $char;
598    
599        ($self->{line_prev}, $self->{column_prev})
600            = ($self->{line}, $self->{column});
601        $self->{column}++;
602            
603      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
604        $line++;        !!!cp ('j1');
605        $column = 0;        $self->{line}++;
606      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
607        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
608        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
609        $line++;        my $next = $input->getc;
610        $column = 0;        if ($next ne "\x0A") {
611      } elsif ($self->{next_input_character} > 0x10FFFF) {          $input->ungetc ($next);
612        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        }
613      } elsif ($self->{next_input_character} == 0x0000) { # NULL        $self->{next_char} = 0x000A; # LF # MUST
614          $self->{line}++;
615          $self->{column} = 0;
616        } elsif ($self->{next_char} > 0x10FFFF) {
617          !!!cp ('j3');
618          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
619        } elsif ($self->{next_char} == 0x0000) { # NULL
620          !!!cp ('j4');
621        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
622        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
623        } elsif ($self->{next_char} <= 0x0008 or
624                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
625                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
626                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
627                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
628                 {
629                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
630                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
631                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
632                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
633                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
634                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
635                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
636                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
637                  0x10FFFE => 1, 0x10FFFF => 1,
638                 }->{$self->{next_char}}) {
639          !!!cp ('j5');
640          !!!parse-error (type => 'control char', level => $self->{must_level});
641    ## TODO: error type documentation
642      }      }
643    };    };
644    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
645    $self->{next_input_character} = -1;    $self->{next_char} = -1;
646    
647    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
648      my (%opt) = @_;      my (%opt) = @_;
649      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
650        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
651        warn "Parse error ($opt{type}) at line $line column $column\n";
652    };    };
653    $self->{parse_error} = sub {    $self->{parse_error} = sub {
654      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
655    };    };
656    
657    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 135  sub parse_string ($$$;$) { Line 659  sub parse_string ($$$;$) {
659    $self->_construct_tree;    $self->_construct_tree;
660    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
661    
662      delete $self->{parse_error}; # remove loop
663    
664    return $self->{document};    return $self->{document};
665  } # parse_string  } # parse_char_stream
666    
667  sub new ($) {  sub new ($) {
668    my $class = shift;    my $class = shift;
669    my $self = bless {}, $class;    my $self = bless {
670    $self->{set_next_input_character} = sub {      must_level => 'm',
671      $self->{next_input_character} = -1;      should_level => 's',
672        good_level => 'w',
673        warn_level => 'w',
674        info_level => 'i',
675        unsupported_level => 'u',
676      }, $class;
677      $self->{set_next_char} = sub {
678        $self->{next_char} = -1;
679    };    };
680    $self->{parse_error} = sub {    $self->{parse_error} = sub {
681      #      #
682    };    };
683      $self->{change_encoding} = sub {
684        # if ($_[0] is a supported encoding) {
685        #   run "change the encoding" algorithm;
686        #   throw Whatpm::HTML::RestartParser (charset => $new_encoding);
687        # }
688      };
689    $self->{application_cache_selection} = sub {    $self->{application_cache_selection} = sub {
690      #      #
691    };    };
# Line 195  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 734  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
734  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
735  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
736  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
737    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
738    sub SELF_CLOSING_START_TAG_STATE () { 34 }
739    sub CDATA_BLOCK_STATE () { 35 }
740    
741  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
742  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 211  sub TABLE_IMS ()      { 0b1000000 } Line 753  sub TABLE_IMS ()      { 0b1000000 }
753  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
754  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
755  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
756    sub SELECT_IMS ()     { 0b10000000000 }
757    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
758        ## NOTE: "in foreign content" insertion mode is special; it is combined
759        ## with the secondary insertion mode.  In this parser, they are stored
760        ## together in the bit-or'ed form.
761    
762    ## NOTE: "initial" and "before html" insertion modes have no constants.
763    
764    ## NOTE: "after after body" insertion mode.
765  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
766    
767    ## NOTE: "after after frameset" insertion mode.
768  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
769    
770  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
771  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
772  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 227  sub IN_TABLE_IM () { TABLE_IMS } Line 780  sub IN_TABLE_IM () { TABLE_IMS }
780  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
781  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
782  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
783  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
784    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
785  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
786    
787  ## 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 794  sub _initialize_tokenizer ($) {
794    undef $self->{current_attribute};    undef $self->{current_attribute};
795    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
796    undef $self->{last_attribute_value_state};    undef $self->{last_attribute_value_state};
797      delete $self->{self_closing};
798    $self->{char} = [];    $self->{char} = [];
799    # $self->{next_input_character}    # $self->{next_char}
800    !!!next-input-character;    !!!next-input-character;
801    $self->{token} = [];    $self->{token} = [];
802    # $self->{escape}    # $self->{escape}
# Line 254  sub _initialize_tokenizer ($) { Line 809  sub _initialize_tokenizer ($) {
809  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
810  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
811  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
812  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
813  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
814    ##        ->{name}
815    ##        ->{value}
816    ##        ->{has_reference} == 1 or 0
817  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
818    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
819    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
820    ##     while the token is pushed back to the stack.
821    
822    ## ISSUE: "When a DOCTYPE token is created, its
823    ## <i>self-closing flag</i> must be unset (its other state is that it
824    ## be set), and its attributes list must be empty.": Wrong subject?
825    
826  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
827    
# Line 283  sub _initialize_tokenizer ($) { Line 848  sub _initialize_tokenizer ($) {
848    
849  sub _get_next_token ($) {  sub _get_next_token ($) {
850    my $self = shift;    my $self = shift;
851    
852      if ($self->{self_closing}) {
853        !!!parse-error (type => 'nestc', token => $self->{current_token});
854        ## NOTE: The |self_closing| flag is only set by start tag token.
855        ## In addition, when a start tag token is emitted, it is always set to
856        ## |current_token|.
857        delete $self->{self_closing};
858      }
859    
860    if (@{$self->{token}}) {    if (@{$self->{token}}) {
861        $self->{self_closing} = $self->{token}->[0]->{self_closing};
862      return shift @{$self->{token}};      return shift @{$self->{token}};
863    }    }
864    
865    A: {    A: {
866      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
867        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
868          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
869                not $self->{escape}) {
870              !!!cp (1);
871            $self->{state} = ENTITY_DATA_STATE;            $self->{state} = ENTITY_DATA_STATE;
872            !!!next-input-character;            !!!next-input-character;
873            redo A;            redo A;
874          } else {          } else {
875              !!!cp (2);
876            #            #
877          }          }
878        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
879          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
880            unless ($self->{escape}) {            unless ($self->{escape}) {
881              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
882                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
883                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
884                  !!!cp (3);
885                $self->{escape} = 1;                $self->{escape} = 1;
886                } else {
887                  !!!cp (4);
888              }              }
889              } else {
890                !!!cp (5);
891            }            }
892          }          }
893                    
894          #          #
895        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
896          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
897              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
898               not $self->{escape})) {               not $self->{escape})) {
899              !!!cp (6);
900            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
901            !!!next-input-character;            !!!next-input-character;
902            redo A;            redo A;
903          } else {          } else {
904              !!!cp (7);
905            #            #
906          }          }
907        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
908          if ($self->{escape} and          if ($self->{escape} and
909              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
910            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
911                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
912                !!!cp (8);
913              delete $self->{escape};              delete $self->{escape};
914              } else {
915                !!!cp (9);
916            }            }
917            } else {
918              !!!cp (10);
919          }          }
920                    
921          #          #
922        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
923          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
924            !!!emit ({type => END_OF_FILE_TOKEN,
925                      line => $self->{line}, column => $self->{column}});
926          last A; ## TODO: ok?          last A; ## TODO: ok?
927          } else {
928            !!!cp (12);
929        }        }
930        # Anything else        # Anything else
931        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
932                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
933                       line => $self->{line}, column => $self->{column},
934                      };
935        ## Stay in the data state        ## Stay in the data state
936        !!!next-input-character;        !!!next-input-character;
937    
# Line 344  sub _get_next_token ($) { Line 940  sub _get_next_token ($) {
940        redo A;        redo A;
941      } elsif ($self->{state} == ENTITY_DATA_STATE) {      } elsif ($self->{state} == ENTITY_DATA_STATE) {
942        ## (cannot happen in CDATA state)        ## (cannot happen in CDATA state)
943    
944          my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
945                
946        my $token = $self->_tokenize_attempt_to_consume_an_entity (0);        my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);
947    
948        $self->{state} = DATA_STATE;        $self->{state} = DATA_STATE;
949        # next-input-character is already done        # next-input-character is already done
950    
951        unless (defined $token) {        unless (defined $token) {
952          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          !!!cp (13);
953            !!!emit ({type => CHARACTER_TOKEN, data => '&',
954                      line => $l, column => $c,
955                     });
956        } else {        } else {
957            !!!cp (14);
958          !!!emit ($token);          !!!emit ($token);
959        }        }
960    
961        redo A;        redo A;
962      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
963        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
964          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
965              !!!cp (15);
966            !!!next-input-character;            !!!next-input-character;
967            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
968            redo A;            redo A;
969          } else {          } else {
970              !!!cp (16);
971            ## reconsume            ## reconsume
972            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
973    
974            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
975                        line => $self->{line_prev},
976                        column => $self->{column_prev},
977                       });
978    
979            redo A;            redo A;
980          }          }
981        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
982          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
983              !!!cp (17);
984            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
985            !!!next-input-character;            !!!next-input-character;
986            redo A;            redo A;
987          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
988              !!!cp (18);
989            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
990            !!!next-input-character;            !!!next-input-character;
991            redo A;            redo A;
992          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
993                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
994              !!!cp (19);
995            $self->{current_token}            $self->{current_token}
996              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
997                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
998                   line => $self->{line_prev},
999                   column => $self->{column_prev}};
1000            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1001            !!!next-input-character;            !!!next-input-character;
1002            redo A;            redo A;
1003          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1004                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1005              !!!cp (20);
1006            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1007                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
1008                                        line => $self->{line_prev},
1009                                        column => $self->{column_prev}};
1010            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1011            !!!next-input-character;            !!!next-input-character;
1012            redo A;            redo A;
1013          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1014            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1015              !!!parse-error (type => 'empty start tag',
1016                              line => $self->{line_prev},
1017                              column => $self->{column_prev});
1018            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1019            !!!next-input-character;            !!!next-input-character;
1020    
1021            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1022                        line => $self->{line_prev},
1023                        column => $self->{column_prev},
1024                       });
1025    
1026            redo A;            redo A;
1027          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1028            !!!parse-error (type => 'pio');            !!!cp (22);
1029              !!!parse-error (type => 'pio',
1030                              line => $self->{line_prev},
1031                              column => $self->{column_prev});
1032            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1033            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1034                                        line => $self->{line_prev},
1035                                        column => $self->{column_prev},
1036                                       };
1037              ## $self->{next_char} is intentionally left as is
1038            redo A;            redo A;
1039          } else {          } else {
1040            !!!parse-error (type => 'bare stago');            !!!cp (23);
1041              !!!parse-error (type => 'bare stago',
1042                              line => $self->{line_prev},
1043                              column => $self->{column_prev});
1044            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1045            ## reconsume            ## reconsume
1046    
1047            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1048                        line => $self->{line_prev},
1049                        column => $self->{column_prev},
1050                       });
1051    
1052            redo A;            redo A;
1053          }          }
# Line 421  sub _get_next_token ($) { Line 1055  sub _get_next_token ($) {
1055          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1056        }        }
1057      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1058          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1059        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1060          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1061    
1062            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>
1063            my @next_char;            my @next_char;
1064            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++) {
1065              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1066              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);              my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);
1067              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;              my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;
1068              if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {              if ($self->{next_char} == $c or $self->{next_char} == $C) {
1069                  !!!cp (24);
1070                !!!next-input-character;                !!!next-input-character;
1071                next TAGNAME;                next TAGNAME;
1072              } else {              } else {
1073                $self->{next_input_character} = shift @next_char; # reconsume                !!!cp (25);
1074                  $self->{next_char} = shift @next_char; # reconsume
1075                !!!back-next-input-character (@next_char);                !!!back-next-input-character (@next_char);
1076                $self->{state} = DATA_STATE;                $self->{state} = DATA_STATE;
1077    
1078                !!!emit ({type => CHARACTER_TOKEN, data => '</'});                !!!emit ({type => CHARACTER_TOKEN, data => '</',
1079                            line => $l, column => $c,
1080                           });
1081        
1082                redo A;                redo A;
1083              }              }
1084            }            }
1085            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1086                
1087            unless ($self->{next_input_character} == 0x0009 or # HT            unless ($self->{next_char} == 0x0009 or # HT
1088                    $self->{next_input_character} == 0x000A or # LF                    $self->{next_char} == 0x000A or # LF
1089                    $self->{next_input_character} == 0x000B or # VT                    $self->{next_char} == 0x000B or # VT
1090                    $self->{next_input_character} == 0x000C or # FF                    $self->{next_char} == 0x000C or # FF
1091                    $self->{next_input_character} == 0x0020 or # SP                    $self->{next_char} == 0x0020 or # SP
1092                    $self->{next_input_character} == 0x003E or # >                    $self->{next_char} == 0x003E or # >
1093                    $self->{next_input_character} == 0x002F or # /                    $self->{next_char} == 0x002F or # /
1094                    $self->{next_input_character} == -1) {                    $self->{next_char} == -1) {
1095              $self->{next_input_character} = shift @next_char; # reconsume              !!!cp (26);
1096                $self->{next_char} = shift @next_char; # reconsume
1097              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1098              $self->{state} = DATA_STATE;              $self->{state} = DATA_STATE;
1099              !!!emit ({type => CHARACTER_TOKEN, data => '</'});              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1100                          line => $l, column => $c,
1101                         });
1102              redo A;              redo A;
1103            } else {            } else {
1104              $self->{next_input_character} = shift @next_char;              !!!cp (27);
1105                $self->{next_char} = shift @next_char;
1106              !!!back-next-input-character (@next_char);              !!!back-next-input-character (@next_char);
1107              # and consume...              # and consume...
1108            }            }
1109          } else {          } else {
1110            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1111              !!!cp (28);
1112            # next-input-character is already done            # next-input-character is already done
1113            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1114            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            !!!emit ({type => CHARACTER_TOKEN, data => '</',
1115                        line => $l, column => $c,
1116                       });
1117            redo A;            redo A;
1118          }          }
1119        }        }
1120                
1121        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1122            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1123          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1124                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1125                = {type => END_TAG_TOKEN,
1126                   tag_name => chr ($self->{next_char} + 0x0020),
1127                   line => $l, column => $c};
1128          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1129          !!!next-input-character;          !!!next-input-character;
1130          redo A;          redo A;
1131        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
1132                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1133            !!!cp (30);
1134          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1135                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
1136                                      line => $l, column => $c};
1137          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1138          !!!next-input-character;          !!!next-input-character;
1139          redo A;          redo A;
1140        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1141          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1142            !!!parse-error (type => 'empty end tag',
1143                            line => $self->{line_prev}, ## "<" in "</>"
1144                            column => $self->{column_prev} - 1);
1145          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1146          !!!next-input-character;          !!!next-input-character;
1147          redo A;          redo A;
1148        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1149            !!!cp (32);
1150          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1151          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1152          # reconsume          # reconsume
1153    
1154          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1155                      line => $l, column => $c,
1156                     });
1157    
1158          redo A;          redo A;
1159        } else {        } else {
1160            !!!cp (33);
1161          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1162          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1163          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1164                                      line => $self->{line_prev}, # "<" of "</"
1165                                      column => $self->{column_prev} - 1,
1166                                     };
1167            ## $self->{next_char} is intentionally left as is
1168          redo A;          redo A;
1169        }        }
1170      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1171        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1172            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1173            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1174            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1175            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1176            !!!cp (34);
1177          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1178          !!!next-input-character;          !!!next-input-character;
1179          redo A;          redo A;
1180        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1181          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1182            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
1183            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1184          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1185            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1186            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1187              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1188            }            #  !!! cp (36);
1189              #  !!! parse-error (type => 'end tag attribute');
1190              #} else {
1191                !!!cp (37);
1192              #}
1193          } else {          } else {
1194            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1195          }          }
# Line 532  sub _get_next_token ($) { Line 1199  sub _get_next_token ($) {
1199          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1200    
1201          redo A;          redo A;
1202        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1203                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1204          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1205            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1206            # start tag or end tag            # start tag or end tag
1207          ## Stay in this state          ## Stay in this state
1208          !!!next-input-character;          !!!next-input-character;
1209          redo A;          redo A;
1210        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1211          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1212          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1213            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1214            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1215          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1216            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1217            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1218              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1219            }            #  !!! cp (40);
1220              #  !!! parse-error (type => 'end tag attribute');
1221              #} else {
1222                !!!cp (41);
1223              #}
1224          } else {          } else {
1225            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1226          }          }
# Line 559  sub _get_next_token ($) { Line 1230  sub _get_next_token ($) {
1230          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1231    
1232          redo A;          redo A;
1233        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1234            !!!cp (42);
1235            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1236          !!!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  
1237          redo A;          redo A;
1238        } else {        } else {
1239          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1240            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1241            # start tag or end tag            # start tag or end tag
1242          ## Stay in the state          ## Stay in the state
1243          !!!next-input-character;          !!!next-input-character;
1244          redo A;          redo A;
1245        }        }
1246      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1247        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1248            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1249            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1250            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1251            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1252            !!!cp (45);
1253          ## Stay in the state          ## Stay in the state
1254          !!!next-input-character;          !!!next-input-character;
1255          redo A;          redo A;
1256        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1257          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1258            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1259            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1260          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1261            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1262            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1263                !!!cp (47);
1264              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1265              } else {
1266                !!!cp (48);
1267            }            }
1268          } else {          } else {
1269            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 1274  sub _get_next_token ($) {
1274          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1275    
1276          redo A;          redo A;
1277        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1278                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1279          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1280                                value => ''};          $self->{current_attribute}
1281                = {name => chr ($self->{next_char} + 0x0020),
1282                   value => '',
1283                   line => $self->{line}, column => $self->{column}};
1284          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1285          !!!next-input-character;          !!!next-input-character;
1286          redo A;          redo A;
1287        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1288            !!!cp (50);
1289            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1290          !!!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  
1291          redo A;          redo A;
1292        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1293          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1294          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1295            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1296            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1297          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1298            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1299            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1300                !!!cp (53);
1301              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1302              } else {
1303                !!!cp (54);
1304            }            }
1305          } else {          } else {
1306            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 1312  sub _get_next_token ($) {
1312    
1313          redo A;          redo A;
1314        } else {        } else {
1315          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1316                                value => ''};               0x0022 => 1, # "
1317                 0x0027 => 1, # '
1318                 0x003D => 1, # =
1319                }->{$self->{next_char}}) {
1320              !!!cp (55);
1321              !!!parse-error (type => 'bad attribute name');
1322            } else {
1323              !!!cp (56);
1324            }
1325            $self->{current_attribute}
1326                = {name => chr ($self->{next_char}),
1327                   value => '',
1328                   line => $self->{line}, column => $self->{column}};
1329          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1330          !!!next-input-character;          !!!next-input-character;
1331          redo A;          redo A;
# Line 658  sub _get_next_token ($) { Line 1334  sub _get_next_token ($) {
1334        my $before_leave = sub {        my $before_leave = sub {
1335          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1336              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1337            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1338              !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1339            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1340          } else {          } else {
1341              !!!cp (58);
1342            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1343              = $self->{current_attribute};              = $self->{current_attribute};
1344          }          }
1345        }; # $before_leave        }; # $before_leave
1346    
1347        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1348            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1349            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1350            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1351            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1352            !!!cp (59);
1353          $before_leave->();          $before_leave->();
1354          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1355          !!!next-input-character;          !!!next-input-character;
1356          redo A;          redo A;
1357        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1358            !!!cp (60);
1359          $before_leave->();          $before_leave->();
1360          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1361          !!!next-input-character;          !!!next-input-character;
1362          redo A;          redo A;
1363        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1364          $before_leave->();          $before_leave->();
1365          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1366            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1367            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1368          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1369              !!!cp (62);
1370            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1371            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1372              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 700  sub _get_next_token ($) { Line 1380  sub _get_next_token ($) {
1380          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1381    
1382          redo A;          redo A;
1383        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1384                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1385          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1386            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1387          ## Stay in the state          ## Stay in the state
1388          !!!next-input-character;          !!!next-input-character;
1389          redo A;          redo A;
1390        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1391            !!!cp (64);
1392          $before_leave->();          $before_leave->();
1393            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1394          !!!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  
1395          redo A;          redo A;
1396        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1397          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1398          $before_leave->();          $before_leave->();
1399          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1400            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1401            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1402          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1403            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1404            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1405                !!!cp (67);
1406              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1407              } else {
1408                ## NOTE: This state should never be reached.
1409                !!!cp (68);
1410            }            }
1411          } else {          } else {
1412            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 1418  sub _get_next_token ($) {
1418    
1419          redo A;          redo A;
1420        } else {        } else {
1421          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x0022 or # "
1422                $self->{next_char} == 0x0027) { # '
1423              !!!cp (69);
1424              !!!parse-error (type => 'bad attribute name');
1425            } else {
1426              !!!cp (70);
1427            }
1428            $self->{current_attribute}->{name} .= chr ($self->{next_char});
1429          ## Stay in the state          ## Stay in the state
1430          !!!next-input-character;          !!!next-input-character;
1431          redo A;          redo A;
1432        }        }
1433      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1434        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1435            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1436            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1437            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1438            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1439            !!!cp (71);
1440          ## Stay in the state          ## Stay in the state
1441          !!!next-input-character;          !!!next-input-character;
1442          redo A;          redo A;
1443        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1444            !!!cp (72);
1445          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1446          !!!next-input-character;          !!!next-input-character;
1447          redo A;          redo A;
1448        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1449          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1450            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1451            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1452          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1453            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1454            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1455                !!!cp (74);
1456              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1457              } else {
1458                ## NOTE: This state should never be reached.
1459                !!!cp (75);
1460            }            }
1461          } else {          } else {
1462            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 1467  sub _get_next_token ($) {
1467          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1468    
1469          redo A;          redo A;
1470        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1471                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1472          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1473                                value => ''};          $self->{current_attribute}
1474                = {name => chr ($self->{next_char} + 0x0020),
1475                   value => '',
1476                   line => $self->{line}, column => $self->{column}};
1477          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1478          !!!next-input-character;          !!!next-input-character;
1479          redo A;          redo A;
1480        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1481            !!!cp (77);
1482            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1483          !!!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  
1484          redo A;          redo A;
1485        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1486          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1487          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1488            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1489            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1490          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1491            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1492            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1493                !!!cp (80);
1494              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1495              } else {
1496                ## NOTE: This state should never be reached.
1497                !!!cp (81);
1498            }            }
1499          } else {          } else {
1500            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 1506  sub _get_next_token ($) {
1506    
1507          redo A;          redo A;
1508        } else {        } else {
1509          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          !!!cp (82);
1510                                value => ''};          $self->{current_attribute}
1511                = {name => chr ($self->{next_char}),
1512                   value => '',
1513                   line => $self->{line}, column => $self->{column}};
1514          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1515          !!!next-input-character;          !!!next-input-character;
1516          redo A;                  redo A;        
1517        }        }
1518      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1519        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1520            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1521            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1522            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1523            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1524            !!!cp (83);
1525          ## Stay in the state          ## Stay in the state
1526          !!!next-input-character;          !!!next-input-character;
1527          redo A;          redo A;
1528        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1529            !!!cp (84);
1530          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1531          !!!next-input-character;          !!!next-input-character;
1532          redo A;          redo A;
1533        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1534            !!!cp (85);
1535          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1536          ## reconsume          ## reconsume
1537          redo A;          redo A;
1538        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1539            !!!cp (86);
1540          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1541          !!!next-input-character;          !!!next-input-character;
1542          redo A;          redo A;
1543        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1544          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1545            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1546            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1547          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1548            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1549            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1550                !!!cp (88);
1551              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1552              } else {
1553                ## NOTE: This state should never be reached.
1554                !!!cp (89);
1555            }            }
1556          } else {          } else {
1557            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 1562  sub _get_next_token ($) {
1562          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1563    
1564          redo A;          redo A;
1565        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1566          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1567          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1568            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1569            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1570          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1571            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1572            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1573                !!!cp (91);
1574              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1575              } else {
1576                ## NOTE: This state should never be reached.
1577                !!!cp (92);
1578            }            }
1579          } else {          } else {
1580            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 1586  sub _get_next_token ($) {
1586    
1587          redo A;          redo A;
1588        } else {        } else {
1589          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{next_char} == 0x003D) { # =
1590              !!!cp (93);
1591              !!!parse-error (type => 'bad attribute value');
1592            } else {
1593              !!!cp (94);
1594            }
1595            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1596          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1597          !!!next-input-character;          !!!next-input-character;
1598          redo A;          redo A;
1599        }        }
1600      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1601        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1602          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1603            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1604          !!!next-input-character;          !!!next-input-character;
1605          redo A;          redo A;
1606        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1607            !!!cp (96);
1608          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1609          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1610          !!!next-input-character;          !!!next-input-character;
1611          redo A;          redo A;
1612        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1613          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1614          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1615            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1616            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1617          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1618            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1619            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1620                !!!cp (98);
1621              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1622              } else {
1623                ## NOTE: This state should never be reached.
1624                !!!cp (99);
1625            }            }
1626          } else {          } else {
1627            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 1633  sub _get_next_token ($) {
1633    
1634          redo A;          redo A;
1635        } else {        } else {
1636          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1637            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1638          ## Stay in the state          ## Stay in the state
1639          !!!next-input-character;          !!!next-input-character;
1640          redo A;          redo A;
1641        }        }
1642      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1643        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1644          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
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 (102);
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 (103);
               = 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 (104);
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 (105);
1667            }            }
1668          } else {          } else {
1669            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 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 (106);
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_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1685        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1686            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1687            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1688            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1689            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1690            !!!cp (107);
1691          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1692          !!!next-input-character;          !!!next-input-character;
1693          redo A;          redo A;
1694        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1695            !!!cp (108);
1696          $self->{last_attribute_value_state} = $self->{state};          $self->{last_attribute_value_state} = $self->{state};
1697          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;
1698          !!!next-input-character;          !!!next-input-character;
1699          redo A;          redo A;
1700        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1701          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1702            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1703            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1704          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1705            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1706            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1707                !!!cp (110);
1708              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1709              } else {
1710                ## NOTE: This state should never be reached.
1711                !!!cp (111);
1712            }            }
1713          } else {          } else {
1714            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 1719  sub _get_next_token ($) {
1719          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1720    
1721          redo A;          redo A;
1722        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1723          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1724          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1725            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1726            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1727          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1728            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1729            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1730                !!!cp (113);
1731              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1732              } else {
1733                ## NOTE: This state should never be reached.
1734                !!!cp (114);
1735            }            }
1736          } else {          } else {
1737            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 1743  sub _get_next_token ($) {
1743    
1744          redo A;          redo A;
1745        } else {        } else {
1746          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1747                 0x0022 => 1, # "
1748                 0x0027 => 1, # '
1749                 0x003D => 1, # =
1750                }->{$self->{next_char}}) {
1751              !!!cp (115);
1752              !!!parse-error (type => 'bad attribute value');
1753            } else {
1754              !!!cp (116);
1755            }
1756            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1757          ## Stay in the state          ## Stay in the state
1758          !!!next-input-character;          !!!next-input-character;
1759          redo A;          redo A;
1760        }        }
1761      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {
1762        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        my $token = $self->_tokenize_attempt_to_consume_an_entity
1763              (1,
1764               $self->{last_attribute_value_state}
1765                 == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "
1766               $self->{last_attribute_value_state}
1767                 == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '
1768               -1);
1769    
1770        unless (defined $token) {        unless (defined $token) {
1771            !!!cp (117);
1772          $self->{current_attribute}->{value} .= '&';          $self->{current_attribute}->{value} .= '&';
1773        } else {        } else {
1774            !!!cp (118);
1775          $self->{current_attribute}->{value} .= $token->{data};          $self->{current_attribute}->{value} .= $token->{data};
1776            $self->{current_attribute}->{has_reference} = $token->{has_reference};
1777          ## 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"
1778        }        }
1779    
1780        $self->{state} = $self->{last_attribute_value_state};        $self->{state} = $self->{last_attribute_value_state};
1781        # next-input-character is already done        # next-input-character is already done
1782        redo A;        redo A;
1783        } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1784          if ($self->{next_char} == 0x0009 or # HT
1785              $self->{next_char} == 0x000A or # LF
1786              $self->{next_char} == 0x000B or # VT
1787              $self->{next_char} == 0x000C or # FF
1788              $self->{next_char} == 0x0020) { # SP
1789            !!!cp (118);
1790            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1791            !!!next-input-character;
1792            redo A;
1793          } elsif ($self->{next_char} == 0x003E) { # >
1794            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1795              !!!cp (119);
1796              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1797            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1798              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1799              if ($self->{current_token}->{attributes}) {
1800                !!!cp (120);
1801                !!!parse-error (type => 'end tag attribute');
1802              } else {
1803                ## NOTE: This state should never be reached.
1804                !!!cp (121);
1805              }
1806            } else {
1807              die "$0: $self->{current_token}->{type}: Unknown token type";
1808            }
1809            $self->{state} = DATA_STATE;
1810            !!!next-input-character;
1811    
1812            !!!emit ($self->{current_token}); # start tag or end tag
1813    
1814            redo A;
1815          } elsif ($self->{next_char} == 0x002F) { # /
1816            !!!cp (122);
1817            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1818            !!!next-input-character;
1819            redo A;
1820          } else {
1821            !!!cp ('124.1');
1822            !!!parse-error (type => 'no space between attributes');
1823            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1824            ## reconsume
1825            redo A;
1826          }
1827        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1828          if ($self->{next_char} == 0x003E) { # >
1829            if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1830              !!!cp ('124.2');
1831              !!!parse-error (type => 'nestc', token => $self->{current_token});
1832              ## TODO: Different type than slash in start tag
1833              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1834              if ($self->{current_token}->{attributes}) {
1835                !!!cp ('124.4');
1836                !!!parse-error (type => 'end tag attribute');
1837              } else {
1838                !!!cp ('124.5');
1839              }
1840              ## TODO: Test |<title></title/>|
1841            } else {
1842              !!!cp ('124.3');
1843              $self->{self_closing} = 1;
1844            }
1845    
1846            $self->{state} = DATA_STATE;
1847            !!!next-input-character;
1848    
1849            !!!emit ($self->{current_token}); # start tag or end tag
1850    
1851            redo A;
1852          } else {
1853            !!!cp ('124.4');
1854            !!!parse-error (type => 'nestc');
1855            ## TODO: This error type is wrong.
1856            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1857            ## Reconsume.
1858            redo A;
1859          }
1860      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1861        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1862                
1863        my $token = {type => COMMENT_TOKEN, data => ''};        ## NOTE: Set by the previous state
1864          #my $token = {type => COMMENT_TOKEN, data => ''};
1865    
1866        BC: {        BC: {
1867          if ($self->{next_input_character} == 0x003E) { # >          if ($self->{next_char} == 0x003E) { # >
1868              !!!cp (124);
1869            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1870            !!!next-input-character;            !!!next-input-character;
1871    
1872            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1873    
1874            redo A;            redo A;
1875          } elsif ($self->{next_input_character} == -1) {          } elsif ($self->{next_char} == -1) {
1876              !!!cp (125);
1877            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1878            ## reconsume            ## reconsume
1879    
1880            !!!emit ($token);            !!!emit ($self->{current_token}); # comment
1881    
1882            redo A;            redo A;
1883          } else {          } else {
1884            $token->{data} .= chr ($self->{next_input_character});            !!!cp (126);
1885              $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1886            !!!next-input-character;            !!!next-input-character;
1887            redo BC;            redo BC;
1888          }          }
1889        } # BC        } # BC
1890    
1891          die "$0: _get_next_token: unexpected case [BC]";
1892      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
1893        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1894    
1895          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1);
1896    
1897        my @next_char;        my @next_char;
1898        push @next_char, $self->{next_input_character};        push @next_char, $self->{next_char};
1899                
1900        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
1901          !!!next-input-character;          !!!next-input-character;
1902          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1903          if ($self->{next_input_character} == 0x002D) { # -          if ($self->{next_char} == 0x002D) { # -
1904            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};            !!!cp (127);
1905              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1906                                        line => $l, column => $c,
1907                                       };
1908            $self->{state} = COMMENT_START_STATE;            $self->{state} = COMMENT_START_STATE;
1909            !!!next-input-character;            !!!next-input-character;
1910            redo A;            redo A;
1911            } else {
1912              !!!cp (128);
1913          }          }
1914        } elsif ($self->{next_input_character} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
1915                 $self->{next_input_character} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
1916          !!!next-input-character;          !!!next-input-character;
1917          push @next_char, $self->{next_input_character};          push @next_char, $self->{next_char};
1918          if ($self->{next_input_character} == 0x004F or # O          if ($self->{next_char} == 0x004F or # O
1919              $self->{next_input_character} == 0x006F) { # o              $self->{next_char} == 0x006F) { # o
1920            !!!next-input-character;            !!!next-input-character;
1921            push @next_char, $self->{next_input_character};            push @next_char, $self->{next_char};
1922            if ($self->{next_input_character} == 0x0043 or # C            if ($self->{next_char} == 0x0043 or # C
1923                $self->{next_input_character} == 0x0063) { # c                $self->{next_char} == 0x0063) { # c
1924              !!!next-input-character;              !!!next-input-character;
1925              push @next_char, $self->{next_input_character};              push @next_char, $self->{next_char};
1926              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
1927                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
1928                !!!next-input-character;                !!!next-input-character;
1929                push @next_char, $self->{next_input_character};                push @next_char, $self->{next_char};
1930                if ($self->{next_input_character} == 0x0059 or # Y                if ($self->{next_char} == 0x0059 or # Y
1931                    $self->{next_input_character} == 0x0079) { # y                    $self->{next_char} == 0x0079) { # y
1932                  !!!next-input-character;                  !!!next-input-character;
1933                  push @next_char, $self->{next_input_character};                  push @next_char, $self->{next_char};
1934                  if ($self->{next_input_character} == 0x0050 or # P                  if ($self->{next_char} == 0x0050 or # P
1935                      $self->{next_input_character} == 0x0070) { # p                      $self->{next_char} == 0x0070) { # p
1936                    !!!next-input-character;                    !!!next-input-character;
1937                    push @next_char, $self->{next_input_character};                    push @next_char, $self->{next_char};
1938                    if ($self->{next_input_character} == 0x0045 or # E                    if ($self->{next_char} == 0x0045 or # E
1939                        $self->{next_input_character} == 0x0065) { # e                        $self->{next_char} == 0x0065) { # e
1940                      ## ISSUE: What a stupid code this is!                      !!!cp (129);
1941                        ## TODO: What a stupid code this is!
1942                      $self->{state} = DOCTYPE_STATE;                      $self->{state} = DOCTYPE_STATE;
1943                        $self->{current_token} = {type => DOCTYPE_TOKEN,
1944                                                  quirks => 1,
1945                                                  line => $l, column => $c,
1946                                                 };
1947                        !!!next-input-character;
1948                        redo A;
1949                      } else {
1950                        !!!cp (130);
1951                      }
1952                    } else {
1953                      !!!cp (131);
1954                    }
1955                  } else {
1956                    !!!cp (132);
1957                  }
1958                } else {
1959                  !!!cp (133);
1960                }
1961              } else {
1962                !!!cp (134);
1963              }
1964            } else {
1965              !!!cp (135);
1966            }
1967          } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
1968                   $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
1969                   $self->{next_char} == 0x005B) { # [
1970            !!!next-input-character;
1971            push @next_char, $self->{next_char};
1972            if ($self->{next_char} == 0x0043) { # C
1973              !!!next-input-character;
1974              push @next_char, $self->{next_char};
1975              if ($self->{next_char} == 0x0044) { # D
1976                !!!next-input-character;
1977                push @next_char, $self->{next_char};
1978                if ($self->{next_char} == 0x0041) { # A
1979                  !!!next-input-character;
1980                  push @next_char, $self->{next_char};
1981                  if ($self->{next_char} == 0x0054) { # T
1982                    !!!next-input-character;
1983                    push @next_char, $self->{next_char};
1984                    if ($self->{next_char} == 0x0041) { # A
1985                      !!!next-input-character;
1986                      push @next_char, $self->{next_char};
1987                      if ($self->{next_char} == 0x005B) { # [
1988                        !!!cp (135.1);
1989                        $self->{state} = CDATA_BLOCK_STATE;
1990                      !!!next-input-character;                      !!!next-input-character;
1991                      redo A;                      redo A;
1992                      } else {
1993                        !!!cp (135.2);
1994                    }                    }
1995                    } else {
1996                      !!!cp (135.3);
1997                  }                  }
1998                  } else {
1999                    !!!cp (135.4);                
2000                }                }
2001                } else {
2002                  !!!cp (135.5);
2003              }              }
2004              } else {
2005                !!!cp (135.6);
2006            }            }
2007            } else {
2008              !!!cp (135.7);
2009          }          }
2010          } else {
2011            !!!cp (136);
2012        }        }
2013    
2014        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment');
2015        $self->{next_input_character} = shift @next_char;        $self->{next_char} = shift @next_char;
2016        !!!back-next-input-character (@next_char);        !!!back-next-input-character (@next_char);
2017        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2018          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2019                                    line => $l, column => $c,
2020                                   };
2021        redo A;        redo A;
2022                
2023        ## ISSUE: typos in spec: chacacters, is is a parse error        ## ISSUE: typos in spec: chacacters, is is a parse error
2024        ## 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?
2025      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2026        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2027            !!!cp (137);
2028          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2029          !!!next-input-character;          !!!next-input-character;
2030          redo A;          redo A;
2031        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2032            !!!cp (138);
2033          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2034          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2035          !!!next-input-character;          !!!next-input-character;
# Line 1137  sub _get_next_token ($) { Line 2037  sub _get_next_token ($) {
2037          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2038    
2039          redo A;          redo A;
2040        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2041            !!!cp (139);
2042          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2043          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2044          ## reconsume          ## reconsume
# Line 1146  sub _get_next_token ($) { Line 2047  sub _get_next_token ($) {
2047    
2048          redo A;          redo A;
2049        } else {        } else {
2050            !!!cp (140);
2051          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2052              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2053          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2054          !!!next-input-character;          !!!next-input-character;
2055          redo A;          redo A;
2056        }        }
2057      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2058        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2059            !!!cp (141);
2060          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2061          !!!next-input-character;          !!!next-input-character;
2062          redo A;          redo A;
2063        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2064            !!!cp (142);
2065          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2066          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2067          !!!next-input-character;          !!!next-input-character;
# Line 1165  sub _get_next_token ($) { Line 2069  sub _get_next_token ($) {
2069          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2070    
2071          redo A;          redo A;
2072        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2073            !!!cp (143);
2074          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2075          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2076          ## reconsume          ## reconsume
# Line 1174  sub _get_next_token ($) { Line 2079  sub _get_next_token ($) {
2079    
2080          redo A;          redo A;
2081        } else {        } else {
2082            !!!cp (144);
2083          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2084              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2085          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2086          !!!next-input-character;          !!!next-input-character;
2087          redo A;          redo A;
2088        }        }
2089      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2090        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2091            !!!cp (145);
2092          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2093          !!!next-input-character;          !!!next-input-character;
2094          redo A;          redo A;
2095        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2096            !!!cp (146);
2097          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2098          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2099          ## reconsume          ## reconsume
# Line 1194  sub _get_next_token ($) { Line 2102  sub _get_next_token ($) {
2102    
2103          redo A;          redo A;
2104        } else {        } else {
2105          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2106            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2107          ## Stay in the state          ## Stay in the state
2108          !!!next-input-character;          !!!next-input-character;
2109          redo A;          redo A;
2110        }        }
2111      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2112        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2113            !!!cp (148);
2114          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2115          !!!next-input-character;          !!!next-input-character;
2116          redo A;          redo A;
2117        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2118            !!!cp (149);
2119          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2120          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2121          ## reconsume          ## reconsume
# Line 1213  sub _get_next_token ($) { Line 2124  sub _get_next_token ($) {
2124    
2125          redo A;          redo A;
2126        } else {        } else {
2127          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2128            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2129          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2130          !!!next-input-character;          !!!next-input-character;
2131          redo A;          redo A;
2132        }        }
2133      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2134        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2135            !!!cp (151);
2136          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2137          !!!next-input-character;          !!!next-input-character;
2138    
2139          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2140    
2141          redo A;          redo A;
2142        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2143          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2144            !!!parse-error (type => 'dash in comment',
2145                            line => $self->{line_prev},
2146                            column => $self->{column_prev});
2147          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2148          ## Stay in the state          ## Stay in the state
2149          !!!next-input-character;          !!!next-input-character;
2150          redo A;          redo A;
2151        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2152            !!!cp (153);
2153          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2154          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2155          ## reconsume          ## reconsume
# Line 1241  sub _get_next_token ($) { Line 2158  sub _get_next_token ($) {
2158    
2159          redo A;          redo A;
2160        } else {        } else {
2161          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2162          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2163                            line => $self->{line_prev},
2164                            column => $self->{column_prev});
2165            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2166          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2167          !!!next-input-character;          !!!next-input-character;
2168          redo A;          redo A;
2169        }        }
2170      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2171        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2172            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2173            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2174            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2175            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2176            !!!cp (155);
2177          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2178          !!!next-input-character;          !!!next-input-character;
2179          redo A;          redo A;
2180        } else {        } else {
2181            !!!cp (156);
2182          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2183          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2184          ## reconsume          ## reconsume
2185          redo A;          redo A;
2186        }        }
2187      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2188        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2189            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2190            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2191            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2192            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2193            !!!cp (157);
2194          ## Stay in the state          ## Stay in the state
2195          !!!next-input-character;          !!!next-input-character;
2196          redo A;          redo A;
2197        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2198            !!!cp (158);
2199          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2200          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2201          !!!next-input-character;          !!!next-input-character;
2202    
2203          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2204    
2205          redo A;          redo A;
2206        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2207            !!!cp (159);
2208          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2209          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2210          ## reconsume          ## reconsume
2211    
2212          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2213    
2214          redo A;          redo A;
2215        } else {        } else {
2216          $self->{current_token}          !!!cp (160);
2217              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
2218                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2219  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2220          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2221          !!!next-input-character;          !!!next-input-character;
# Line 1299  sub _get_next_token ($) { Line 2223  sub _get_next_token ($) {
2223        }        }
2224      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2225  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2226        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2227            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2228            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2229            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2230            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2231            !!!cp (161);
2232          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2233          !!!next-input-character;          !!!next-input-character;
2234          redo A;          redo A;
2235        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2236            !!!cp (162);
2237          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2238          !!!next-input-character;          !!!next-input-character;
2239    
2240          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2241    
2242          redo A;          redo A;
2243        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2244            !!!cp (163);
2245          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2246          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2247          ## reconsume          ## reconsume
2248    
2249          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2250          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2251    
2252          redo A;          redo A;
2253        } else {        } else {
2254            !!!cp (164);
2255          $self->{current_token}->{name}          $self->{current_token}->{name}
2256            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2257          ## Stay in the state          ## Stay in the state
2258          !!!next-input-character;          !!!next-input-character;
2259          redo A;          redo A;
2260        }        }
2261      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2262        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2263            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2264            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2265            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2266            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2267            !!!cp (165);
2268          ## Stay in the state          ## Stay in the state
2269          !!!next-input-character;          !!!next-input-character;
2270          redo A;          redo A;
2271        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2272            !!!cp (166);
2273          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2274          !!!next-input-character;          !!!next-input-character;
2275    
2276          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2277    
2278          redo A;          redo A;
2279        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2280            !!!cp (167);
2281          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2282          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2283          ## reconsume          ## reconsume
2284    
2285          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2286          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2287    
2288          redo A;          redo A;
2289        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2290                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2291          !!!next-input-character;          !!!next-input-character;
2292          if ($self->{next_input_character} == 0x0055 or # U          if ($self->{next_char} == 0x0055 or # U
2293              $self->{next_input_character} == 0x0075) { # u              $self->{next_char} == 0x0075) { # u
2294            !!!next-input-character;            !!!next-input-character;
2295            if ($self->{next_input_character} == 0x0042 or # B            if ($self->{next_char} == 0x0042 or # B
2296                $self->{next_input_character} == 0x0062) { # b                $self->{next_char} == 0x0062) { # b
2297              !!!next-input-character;              !!!next-input-character;
2298              if ($self->{next_input_character} == 0x004C or # L              if ($self->{next_char} == 0x004C or # L
2299                  $self->{next_input_character} == 0x006C) { # l                  $self->{next_char} == 0x006C) { # l
2300                !!!next-input-character;                !!!next-input-character;
2301                if ($self->{next_input_character} == 0x0049 or # I                if ($self->{next_char} == 0x0049 or # I
2302                    $self->{next_input_character} == 0x0069) { # i                    $self->{next_char} == 0x0069) { # i
2303                  !!!next-input-character;                  !!!next-input-character;
2304                  if ($self->{next_input_character} == 0x0043 or # C                  if ($self->{next_char} == 0x0043 or # C
2305                      $self->{next_input_character} == 0x0063) { # c                      $self->{next_char} == 0x0063) { # c
2306                      !!!cp (168);
2307                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2308                    !!!next-input-character;                    !!!next-input-character;
2309                    redo A;                    redo A;
2310                    } else {
2311                      !!!cp (169);
2312                  }                  }
2313                  } else {
2314                    !!!cp (170);
2315                }                }
2316                } else {
2317                  !!!cp (171);
2318              }              }
2319              } else {
2320                !!!cp (172);
2321            }            }
2322            } else {
2323              !!!cp (173);
2324          }          }
2325    
2326          #          #
2327        } elsif ($self->{next_input_character} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2328                 $self->{next_input_character} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2329          !!!next-input-character;          !!!next-input-character;
2330          if ($self->{next_input_character} == 0x0059 or # Y          if ($self->{next_char} == 0x0059 or # Y
2331              $self->{next_input_character} == 0x0079) { # y              $self->{next_char} == 0x0079) { # y
2332            !!!next-input-character;            !!!next-input-character;
2333            if ($self->{next_input_character} == 0x0053 or # S            if ($self->{next_char} == 0x0053 or # S
2334                $self->{next_input_character} == 0x0073) { # s                $self->{next_char} == 0x0073) { # s
2335              !!!next-input-character;              !!!next-input-character;
2336              if ($self->{next_input_character} == 0x0054 or # T              if ($self->{next_char} == 0x0054 or # T
2337                  $self->{next_input_character} == 0x0074) { # t                  $self->{next_char} == 0x0074) { # t
2338                !!!next-input-character;                !!!next-input-character;
2339                if ($self->{next_input_character} == 0x0045 or # E                if ($self->{next_char} == 0x0045 or # E
2340                    $self->{next_input_character} == 0x0065) { # e                    $self->{next_char} == 0x0065) { # e
2341                  !!!next-input-character;                  !!!next-input-character;
2342                  if ($self->{next_input_character} == 0x004D or # M                  if ($self->{next_char} == 0x004D or # M
2343                      $self->{next_input_character} == 0x006D) { # m                      $self->{next_char} == 0x006D) { # m
2344                      !!!cp (174);
2345                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;                    $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2346                    !!!next-input-character;                    !!!next-input-character;
2347                    redo A;                    redo A;
2348                    } else {
2349                      !!!cp (175);
2350                  }                  }
2351                  } else {
2352                    !!!cp (176);
2353                }                }
2354                } else {
2355                  !!!cp (177);
2356              }              }
2357              } else {
2358                !!!cp (178);
2359            }            }
2360            } else {
2361              !!!cp (179);
2362          }          }
2363    
2364          #          #
2365        } else {        } else {
2366            !!!cp (180);
2367          !!!next-input-character;          !!!next-input-character;
2368          #          #
2369        }        }
2370    
2371        !!!parse-error (type => 'string after DOCTYPE name');        !!!parse-error (type => 'string after DOCTYPE name');
2372          $self->{current_token}->{quirks} = 1;
2373    
2374        $self->{state} = BOGUS_DOCTYPE_STATE;        $self->{state} = BOGUS_DOCTYPE_STATE;
2375        # next-input-character is already done        # next-input-character is already done
2376        redo A;        redo A;
# Line 1422  sub _get_next_token ($) { Line 2378  sub _get_next_token ($) {
2378        if ({        if ({
2379              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2380              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2381            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2382            !!!cp (181);
2383          ## Stay in the state          ## Stay in the state
2384          !!!next-input-character;          !!!next-input-character;
2385          redo A;          redo A;
2386        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2387            !!!cp (182);
2388          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2389          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2390          !!!next-input-character;          !!!next-input-character;
2391          redo A;          redo A;
2392        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2393            !!!cp (183);
2394          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2395          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2396          !!!next-input-character;          !!!next-input-character;
2397          redo A;          redo A;
2398        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2399            !!!cp (184);
2400          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2401    
2402          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2403          !!!next-input-character;          !!!next-input-character;
2404    
2405          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2406          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2407    
2408          redo A;          redo A;
2409        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2410            !!!cp (185);
2411          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2412    
2413          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2414          ## reconsume          ## reconsume
2415    
2416          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2417          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2418    
2419          redo A;          redo A;
2420        } else {        } else {
2421            !!!cp (186);
2422          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2423            $self->{current_token}->{quirks} = 1;
2424    
2425          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2426          !!!next-input-character;          !!!next-input-character;
2427          redo A;          redo A;
2428        }        }
2429      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2430        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2431            !!!cp (187);
2432          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2433          !!!next-input-character;          !!!next-input-character;
2434          redo A;          redo A;
2435        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2436            !!!cp (188);
2437            !!!parse-error (type => 'unclosed PUBLIC literal');
2438    
2439            $self->{state} = DATA_STATE;
2440            !!!next-input-character;
2441    
2442            $self->{current_token}->{quirks} = 1;
2443            !!!emit ($self->{current_token}); # DOCTYPE
2444    
2445            redo A;
2446          } elsif ($self->{next_char} == -1) {
2447            !!!cp (189);
2448          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2449    
2450          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2451          ## reconsume          ## reconsume
2452    
2453          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2454          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2455    
2456          redo A;          redo A;
2457        } else {        } else {
2458            !!!cp (190);
2459          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2460              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2461          ## Stay in the state          ## Stay in the state
2462          !!!next-input-character;          !!!next-input-character;
2463          redo A;          redo A;
2464        }        }
2465      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2466        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2467            !!!cp (191);
2468          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2469          !!!next-input-character;          !!!next-input-character;
2470          redo A;          redo A;
2471        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2472            !!!cp (192);
2473            !!!parse-error (type => 'unclosed PUBLIC literal');
2474    
2475            $self->{state} = DATA_STATE;
2476            !!!next-input-character;
2477    
2478            $self->{current_token}->{quirks} = 1;
2479            !!!emit ($self->{current_token}); # DOCTYPE
2480    
2481            redo A;
2482          } elsif ($self->{next_char} == -1) {
2483            !!!cp (193);
2484          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2485    
2486          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2487          ## reconsume          ## reconsume
2488    
2489          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2490          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2491    
2492          redo A;          redo A;
2493        } else {        } else {
2494            !!!cp (194);
2495          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2496              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2497          ## Stay in the state          ## Stay in the state
2498          !!!next-input-character;          !!!next-input-character;
2499          redo A;          redo A;
# Line 1510  sub _get_next_token ($) { Line 2502  sub _get_next_token ($) {
2502        if ({        if ({
2503              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2504              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2505            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2506            !!!cp (195);
2507          ## Stay in the state          ## Stay in the state
2508          !!!next-input-character;          !!!next-input-character;
2509          redo A;          redo A;
2510        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2511            !!!cp (196);
2512          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2513          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2514          !!!next-input-character;          !!!next-input-character;
2515          redo A;          redo A;
2516        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2517            !!!cp (197);
2518          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2519          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2520          !!!next-input-character;          !!!next-input-character;
2521          redo A;          redo A;
2522        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2523            !!!cp (198);
2524          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2525          !!!next-input-character;          !!!next-input-character;
2526    
2527          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2528    
2529          redo A;          redo A;
2530        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2531            !!!cp (199);
2532          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2533    
2534          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2535          ## reconsume          ## reconsume
2536    
2537          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2538          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2539    
2540          redo A;          redo A;
2541        } else {        } else {
2542            !!!cp (200);
2543          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2544            $self->{current_token}->{quirks} = 1;
2545    
2546          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2547          !!!next-input-character;          !!!next-input-character;
2548          redo A;          redo A;
# Line 1551  sub _get_next_token ($) { Line 2551  sub _get_next_token ($) {
2551        if ({        if ({
2552              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2553              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2554            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2555            !!!cp (201);
2556          ## Stay in the state          ## Stay in the state
2557          !!!next-input-character;          !!!next-input-character;
2558          redo A;          redo A;
2559        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2560            !!!cp (202);
2561          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2562          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2563          !!!next-input-character;          !!!next-input-character;
2564          redo A;          redo A;
2565        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2566            !!!cp (203);
2567          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2568          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2569          !!!next-input-character;          !!!next-input-character;
2570          redo A;          redo A;
2571        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2572            !!!cp (204);
2573          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2574          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2575          !!!next-input-character;          !!!next-input-character;
2576    
2577          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2578          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2579    
2580          redo A;          redo A;
2581        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2582            !!!cp (205);
2583          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2584    
2585          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2586          ## reconsume          ## reconsume
2587    
2588          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2589          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2590    
2591          redo A;          redo A;
2592        } else {        } else {
2593            !!!cp (206);
2594          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2595            $self->{current_token}->{quirks} = 1;
2596    
2597          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2598          !!!next-input-character;          !!!next-input-character;
2599          redo A;          redo A;
2600        }        }
2601      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2602        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2603            !!!cp (207);
2604          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2605          !!!next-input-character;          !!!next-input-character;
2606          redo A;          redo A;
2607        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2608            !!!cp (208);
2609            !!!parse-error (type => 'unclosed PUBLIC literal');
2610    
2611            $self->{state} = DATA_STATE;
2612            !!!next-input-character;
2613    
2614            $self->{current_token}->{quirks} = 1;
2615            !!!emit ($self->{current_token}); # DOCTYPE
2616    
2617            redo A;
2618          } elsif ($self->{next_char} == -1) {
2619            !!!cp (209);
2620          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2621    
2622          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2623          ## reconsume          ## reconsume
2624    
2625          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2626          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2627    
2628          redo A;          redo A;
2629        } else {        } else {
2630            !!!cp (210);
2631          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2632              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2633          ## Stay in the state          ## Stay in the state
2634          !!!next-input-character;          !!!next-input-character;
2635          redo A;          redo A;
2636        }        }
2637      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2638        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2639            !!!cp (211);
2640          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2641          !!!next-input-character;          !!!next-input-character;
2642          redo A;          redo A;
2643        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == 0x003E) { # >
2644            !!!cp (212);
2645            !!!parse-error (type => 'unclosed PUBLIC literal');
2646    
2647            $self->{state} = DATA_STATE;
2648            !!!next-input-character;
2649    
2650            $self->{current_token}->{quirks} = 1;
2651            !!!emit ($self->{current_token}); # DOCTYPE
2652    
2653            redo A;
2654          } elsif ($self->{next_char} == -1) {
2655            !!!cp (213);
2656          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2657    
2658          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2659          ## reconsume          ## reconsume
2660    
2661          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2662          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2663    
2664          redo A;          redo A;
2665        } else {        } else {
2666            !!!cp (214);
2667          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2668              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2669          ## Stay in the state          ## Stay in the state
2670          !!!next-input-character;          !!!next-input-character;
2671          redo A;          redo A;
# Line 1638  sub _get_next_token ($) { Line 2674  sub _get_next_token ($) {
2674        if ({        if ({
2675              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2676              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2677            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2678            !!!cp (215);
2679          ## Stay in the state          ## Stay in the state
2680          !!!next-input-character;          !!!next-input-character;
2681          redo A;          redo A;
2682        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2683            !!!cp (216);
2684          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2685          !!!next-input-character;          !!!next-input-character;
2686    
2687          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2688    
2689          redo A;          redo A;
2690        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2691            !!!cp (217);
2692          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2693    
2694          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2695          ## reconsume          ## reconsume
2696    
2697          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2698          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2699    
2700          redo A;          redo A;
2701        } else {        } else {
2702            !!!cp (218);
2703          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2704            #$self->{current_token}->{quirks} = 1;
2705    
2706          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2707          !!!next-input-character;          !!!next-input-character;
2708          redo A;          redo A;
2709        }        }
2710      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2711        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2712            !!!cp (219);
2713          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2714          !!!next-input-character;          !!!next-input-character;
2715    
         delete $self->{current_token}->{correct};  
2716          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2717    
2718          redo A;          redo A;
2719        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2720            !!!cp (220);
2721          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2722          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2723          ## reconsume          ## reconsume
2724    
         delete $self->{current_token}->{correct};  
2725          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2726    
2727          redo A;          redo A;
2728        } else {        } else {
2729            !!!cp (221);
2730          ## Stay in the state          ## Stay in the state
2731          !!!next-input-character;          !!!next-input-character;
2732          redo A;          redo A;
2733        }        }
2734        } elsif ($self->{state} == CDATA_BLOCK_STATE) {
2735          my $s = '';
2736          
2737          my ($l, $c) = ($self->{line}, $self->{column});
2738    
2739          CS: while ($self->{next_char} != -1) {
2740            if ($self->{next_char} == 0x005D) { # ]
2741              !!!next-input-character;
2742              if ($self->{next_char} == 0x005D) { # ]
2743                !!!next-input-character;
2744                MDC: {
2745                  if ($self->{next_char} == 0x003E) { # >
2746                    !!!cp (221.1);
2747                    !!!next-input-character;
2748                    last CS;
2749                  } elsif ($self->{next_char} == 0x005D) { # ]
2750                    !!!cp (221.2);
2751                    $s .= ']';
2752                    !!!next-input-character;
2753                    redo MDC;
2754                  } else {
2755                    !!!cp (221.3);
2756                    $s .= ']]';
2757                    #
2758                  }
2759                } # MDC
2760              } else {
2761                !!!cp (221.4);
2762                $s .= ']';
2763                #
2764              }
2765            } else {
2766              !!!cp (221.5);
2767              #
2768            }
2769            $s .= chr $self->{next_char};
2770            !!!next-input-character;
2771          } # CS
2772    
2773          $self->{state} = DATA_STATE;
2774          ## next-input-character done or EOF, which is reconsumed.
2775    
2776          if (length $s) {
2777            !!!cp (221.6);
2778            !!!emit ({type => CHARACTER_TOKEN, data => $s,
2779                      line => $l, column => $c});
2780          } else {
2781            !!!cp (221.7);
2782          }
2783    
2784          redo A;
2785    
2786          ## ISSUE: "text tokens" in spec.
2787          ## TODO: Streaming support
2788      } else {      } else {
2789        die "$0: $self->{state}: Unknown state";        die "$0: $self->{state}: Unknown state";
2790      }      }
# Line 1696  sub _get_next_token ($) { Line 2793  sub _get_next_token ($) {
2793    die "$0: _get_next_token: unexpected case";    die "$0: _get_next_token: unexpected case";
2794  } # _get_next_token  } # _get_next_token
2795    
2796  sub _tokenize_attempt_to_consume_an_entity ($$) {  sub _tokenize_attempt_to_consume_an_entity ($$$) {
2797    my ($self, $in_attr) = @_;    my ($self, $in_attr, $additional) = @_;
2798    
2799      my ($l, $c) = ($self->{line_prev}, $self->{column_prev});
2800    
2801    if ({    if ({
2802         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2803         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR
2804        }->{$self->{next_input_character}}) {         $additional => 1,
2805          }->{$self->{next_char}}) {
2806        !!!cp (1001);
2807      ## Don't consume      ## Don't consume
2808      ## No error      ## No error
2809      return undef;      return undef;
2810    } elsif ($self->{next_input_character} == 0x0023) { # #    } elsif ($self->{next_char} == 0x0023) { # #
2811      !!!next-input-character;      !!!next-input-character;
2812      if ($self->{next_input_character} == 0x0078 or # x      if ($self->{next_char} == 0x0078 or # x
2813          $self->{next_input_character} == 0x0058) { # X          $self->{next_char} == 0x0058) { # X
2814        my $code;        my $code;
2815        X: {        X: {
2816          my $x_char = $self->{next_input_character};          my $x_char = $self->{next_char};
2817          !!!next-input-character;          !!!next-input-character;
2818          if (0x0030 <= $self->{next_input_character} and          if (0x0030 <= $self->{next_char} and
2819              $self->{next_input_character} <= 0x0039) { # 0..9              $self->{next_char} <= 0x0039) { # 0..9
2820              !!!cp (1002);
2821            $code ||= 0;            $code ||= 0;
2822            $code *= 0x10;            $code *= 0x10;
2823            $code += $self->{next_input_character} - 0x0030;            $code += $self->{next_char} - 0x0030;
2824            redo X;            redo X;
2825          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
2826                   $self->{next_input_character} <= 0x0066) { # a..f                   $self->{next_char} <= 0x0066) { # a..f
2827              !!!cp (1003);
2828            $code ||= 0;            $code ||= 0;
2829            $code *= 0x10;            $code *= 0x10;
2830            $code += $self->{next_input_character} - 0x0060 + 9;            $code += $self->{next_char} - 0x0060 + 9;
2831            redo X;            redo X;
2832          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
2833                   $self->{next_input_character} <= 0x0046) { # A..F                   $self->{next_char} <= 0x0046) { # A..F
2834              !!!cp (1004);
2835            $code ||= 0;            $code ||= 0;
2836            $code *= 0x10;            $code *= 0x10;
2837            $code += $self->{next_input_character} - 0x0040 + 9;            $code += $self->{next_char} - 0x0040 + 9;
2838            redo X;            redo X;
2839          } elsif (not defined $code) { # no hexadecimal digit          } elsif (not defined $code) { # no hexadecimal digit
2840            !!!parse-error (type => 'bare hcro');            !!!cp (1005);
2841            !!!back-next-input-character ($x_char, $self->{next_input_character});            !!!parse-error (type => 'bare hcro', line => $l, column => $c);
2842            $self->{next_input_character} = 0x0023; # #            !!!back-next-input-character ($x_char, $self->{next_char});
2843              $self->{next_char} = 0x0023; # #
2844            return undef;            return undef;
2845          } elsif ($self->{next_input_character} == 0x003B) { # ;          } elsif ($self->{next_char} == 0x003B) { # ;
2846              !!!cp (1006);
2847            !!!next-input-character;            !!!next-input-character;
2848          } else {          } else {
2849            !!!parse-error (type => 'no refc');            !!!cp (1007);
2850              !!!parse-error (type => 'no refc', line => $l, column => $c);
2851          }          }
2852    
2853          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2854            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);            !!!cp (1008);
2855              !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2856            $code = 0xFFFD;            $code = 0xFFFD;
2857          } elsif ($code > 0x10FFFF) {          } elsif ($code > 0x10FFFF) {
2858            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);            !!!cp (1009);
2859              !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2860            $code = 0xFFFD;            $code = 0xFFFD;
2861          } elsif ($code == 0x000D) {          } elsif ($code == 0x000D) {
2862            !!!parse-error (type => 'CR character reference');            !!!cp (1010);
2863              !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2864            $code = 0x000A;            $code = 0x000A;
2865          } elsif (0x80 <= $code and $code <= 0x9F) {          } elsif (0x80 <= $code and $code <= 0x9F) {
2866            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);            !!!cp (1011);
2867              !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2868            $code = $c1_entity_char->{$code};            $code = $c1_entity_char->{$code};
2869          }          }
2870    
2871          return {type => CHARACTER_TOKEN, data => chr $code};          return {type => CHARACTER_TOKEN, data => chr $code,
2872                    has_reference => 1,
2873                    line => $l, column => $c,
2874                   };
2875        } # X        } # X
2876      } elsif (0x0030 <= $self->{next_input_character} and      } elsif (0x0030 <= $self->{next_char} and
2877               $self->{next_input_character} <= 0x0039) { # 0..9               $self->{next_char} <= 0x0039) { # 0..9
2878        my $code = $self->{next_input_character} - 0x0030;        my $code = $self->{next_char} - 0x0030;
2879        !!!next-input-character;        !!!next-input-character;
2880                
2881        while (0x0030 <= $self->{next_input_character} and        while (0x0030 <= $self->{next_char} and
2882                  $self->{next_input_character} <= 0x0039) { # 0..9                  $self->{next_char} <= 0x0039) { # 0..9
2883            !!!cp (1012);
2884          $code *= 10;          $code *= 10;
2885          $code += $self->{next_input_character} - 0x0030;          $code += $self->{next_char} - 0x0030;
2886                    
2887          !!!next-input-character;          !!!next-input-character;
2888        }        }
2889    
2890        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{next_char} == 0x003B) { # ;
2891            !!!cp (1013);
2892          !!!next-input-character;          !!!next-input-character;
2893        } else {        } else {
2894          !!!parse-error (type => 'no refc');          !!!cp (1014);
2895            !!!parse-error (type => 'no refc', line => $l, column => $c);
2896        }        }
2897    
2898        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
2899          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1015);
2900            !!!parse-error (type => (sprintf 'invalid character reference:U+%04X', $code), line => $l, column => $c);
2901          $code = 0xFFFD;          $code = 0xFFFD;
2902        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
2903          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1016);
2904            !!!parse-error (type => (sprintf 'invalid character reference:U-%08X', $code), line => $l, column => $c);
2905          $code = 0xFFFD;          $code = 0xFFFD;
2906        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
2907          !!!parse-error (type => 'CR character reference');          !!!cp (1017);
2908            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
2909          $code = 0x000A;          $code = 0x000A;
2910        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
2911          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1018);
2912            !!!parse-error (type => (sprintf 'C1 character reference:U+%04X', $code), line => $l, column => $c);
2913          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
2914        }        }
2915                
2916        return {type => CHARACTER_TOKEN, data => chr $code};        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1,
2917                  line => $l, column => $c,
2918                 };
2919      } else {      } else {
2920        !!!parse-error (type => 'bare nero');        !!!cp (1019);
2921        !!!back-next-input-character ($self->{next_input_character});        !!!parse-error (type => 'bare nero', line => $l, column => $c);
2922        $self->{next_input_character} = 0x0023; # #        !!!back-next-input-character ($self->{next_char});
2923          $self->{next_char} = 0x0023; # #
2924        return undef;        return undef;
2925      }      }
2926    } elsif ((0x0041 <= $self->{next_input_character} and    } elsif ((0x0041 <= $self->{next_char} and
2927              $self->{next_input_character} <= 0x005A) or              $self->{next_char} <= 0x005A) or
2928             (0x0061 <= $self->{next_input_character} and             (0x0061 <= $self->{next_char} and
2929              $self->{next_input_character} <= 0x007A)) {              $self->{next_char} <= 0x007A)) {
2930      my $entity_name = chr $self->{next_input_character};      my $entity_name = chr $self->{next_char};
2931      !!!next-input-character;      !!!next-input-character;
2932    
2933      my $value = $entity_name;      my $value = $entity_name;
# Line 1811  sub _tokenize_attempt_to_consume_an_enti Line 2935  sub _tokenize_attempt_to_consume_an_enti
2935      require Whatpm::_NamedEntityList;      require Whatpm::_NamedEntityList;
2936      our $EntityChar;      our $EntityChar;
2937    
2938      while (length $entity_name < 10 and      while (length $entity_name < 30 and
2939             ## NOTE: Some number greater than the maximum length of entity name             ## NOTE: Some number greater than the maximum length of entity name
2940             ((0x0041 <= $self->{next_input_character} and # a             ((0x0041 <= $self->{next_char} and # a
2941               $self->{next_input_character} <= 0x005A) or # x               $self->{next_char} <= 0x005A) or # x
2942              (0x0061 <= $self->{next_input_character} and # a              (0x0061 <= $self->{next_char} and # a
2943               $self->{next_input_character} <= 0x007A) or # z               $self->{next_char} <= 0x007A) or # z
2944              (0x0030 <= $self->{next_input_character} and # 0              (0x0030 <= $self->{next_char} and # 0
2945               $self->{next_input_character} <= 0x0039) or # 9               $self->{next_char} <= 0x0039) or # 9
2946              $self->{next_input_character} == 0x003B)) { # ;              $self->{next_char} == 0x003B)) { # ;
2947        $entity_name .= chr $self->{next_input_character};        $entity_name .= chr $self->{next_char};
2948        if (defined $EntityChar->{$entity_name}) {        if (defined $EntityChar->{$entity_name}) {
2949          if ($self->{next_input_character} == 0x003B) { # ;          if ($self->{next_char} == 0x003B) { # ;
2950              !!!cp (1020);
2951            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2952            $match = 1;            $match = 1;
2953            !!!next-input-character;            !!!next-input-character;
2954            last;            last;
2955          } else {          } else {
2956              !!!cp (1021);
2957            $value = $EntityChar->{$entity_name};            $value = $EntityChar->{$entity_name};
2958            $match = -1;            $match = -1;
2959            !!!next-input-character;            !!!next-input-character;
2960          }          }
2961        } else {        } else {
2962          $value .= chr $self->{next_input_character};          !!!cp (1022);
2963            $value .= chr $self->{next_char};
2964          $match *= 2;          $match *= 2;
2965          !!!next-input-character;          !!!next-input-character;
2966        }        }
2967      }      }
2968            
2969      if ($match > 0) {      if ($match > 0) {
2970        return {type => CHARACTER_TOKEN, data => $value};        !!!cp (1023);
2971          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2972                  line => $l, column => $c,
2973                 };
2974      } elsif ($match < 0) {      } elsif ($match < 0) {
2975        !!!parse-error (type => 'no refc');        !!!parse-error (type => 'no refc', line => $l, column => $c);
2976        if ($in_attr and $match < -1) {        if ($in_attr and $match < -1) {
2977          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          !!!cp (1024);
2978        } else {          return {type => CHARACTER_TOKEN, data => '&'.$entity_name,
2979          return {type => CHARACTER_TOKEN, data => $value};                  line => $l, column => $c,
2980                   };
2981          } else {
2982            !!!cp (1025);
2983            return {type => CHARACTER_TOKEN, data => $value, has_reference => 1,
2984                    line => $l, column => $c,
2985                   };
2986        }        }
2987      } else {      } else {
2988        !!!parse-error (type => 'bare ero');        !!!cp (1026);
2989        ## NOTE: No characters are consumed in the spec.        !!!parse-error (type => 'bare ero', line => $l, column => $c);
2990        return {type => CHARACTER_TOKEN, data => '&'.$value};        ## NOTE: "No characters are consumed" in the spec.
2991          return {type => CHARACTER_TOKEN, data => '&'.$value,
2992                  line => $l, column => $c,
2993                 };
2994      }      }
2995    } else {    } else {
2996        !!!cp (1027);
2997      ## no characters are consumed      ## no characters are consumed
2998      !!!parse-error (type => 'bare ero');      !!!parse-error (type => 'bare ero', line => $l, column => $c);
2999      return undef;      return undef;
3000    }    }
3001  } # _tokenize_attempt_to_consume_an_entity  } # _tokenize_attempt_to_consume_an_entity
# Line 1893  sub _construct_tree ($) { Line 3033  sub _construct_tree ($) {
3033        
3034    !!!next-token;    !!!next-token;
3035    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3036    undef $self->{form_element};    undef $self->{form_element};
3037    undef $self->{head_element};    undef $self->{head_element};
3038    $self->{open_elements} = [];    $self->{open_elements} = [];
3039    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3040    
3041      ## NOTE: The "initial" insertion mode.
3042    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3043    
3044      ## NOTE: The "before html" insertion mode.
3045    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3046      $self->{insertion_mode} = BEFORE_HEAD_IM;
3047    
3048      ## NOTE: The "before head" insertion mode and so on.
3049    $self->_tree_construction_main;    $self->_tree_construction_main;
3050  } # _construct_tree  } # _construct_tree
3051    
3052  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3053    my $self = shift;    my $self = shift;
3054    
3055      ## NOTE: "initial" insertion mode
3056    
3057    INITIAL: {    INITIAL: {
3058      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3059        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 1917  sub _tree_construction_initial ($) { Line 3065  sub _tree_construction_initial ($) {
3065        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3066            defined $token->{public_identifier} or            defined $token->{public_identifier} or
3067            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3068          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3069            !!!parse-error (type => 'not HTML5', token => $token);
3070        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3071            !!!cp ('t2');
3072          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)
3073          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3074          } else {
3075            !!!cp ('t3');
3076        }        }
3077                
3078        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3079          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3080          ## NOTE: Default value for both |public_id| and |system_id| attributes
3081          ## are empty strings, so that we don't set any value in missing cases.
3082        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3083            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3084        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 1933  sub _tree_construction_initial ($) { Line 3087  sub _tree_construction_initial ($) {
3087        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3088        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3089                
3090        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3091            !!!cp ('t4');
3092          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3093        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3094          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
# Line 1987  sub _tree_construction_initial ($) { Line 3142  sub _tree_construction_initial ($) {
3142            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,
3143            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,
3144            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,
3145              "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,
3146              "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,
3147              "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,
3148            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,
3149            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,
3150            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,
# Line 2009  sub _tree_construction_initial ($) { Line 3167  sub _tree_construction_initial ($) {
3167            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,
3168            "HTML" => 1,            "HTML" => 1,
3169          }->{$pubid}) {          }->{$pubid}) {
3170              !!!cp ('t5');
3171            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3172          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or
3173                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {
3174            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3175                !!!cp ('t6');
3176              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3177            } else {            } else {
3178                !!!cp ('t7');
3179              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3180            }            }
3181          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or
3182                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {
3183              !!!cp ('t8');
3184            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3185            } else {
3186              !!!cp ('t9');
3187          }          }
3188          } else {
3189            !!!cp ('t10');
3190        }        }
3191        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3192          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3193          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3194          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") {
3195              ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"
3196            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3197              !!!cp ('t11');
3198            } else {
3199              !!!cp ('t12');
3200          }          }
3201          } else {
3202            !!!cp ('t13');
3203        }        }
3204                
3205        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3206        !!!next-token;        !!!next-token;
3207        return;        return;
3208      } elsif ({      } elsif ({
# Line 2038  sub _tree_construction_initial ($) { Line 3210  sub _tree_construction_initial ($) {
3210                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3211                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3212               }->{$token->{type}}) {               }->{$token->{type}}) {
3213        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3214          !!!parse-error (type => 'no DOCTYPE', token => $token);
3215        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3216        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3217        ## reprocess        ## reprocess
3218          !!!ack-later;
3219        return;        return;
3220      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3221        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3222          ## Ignore the token          ## Ignore the token
3223    
3224          unless (length $token->{data}) {          unless (length $token->{data}) {
3225            ## Stay in the phase            !!!cp ('t15');
3226              ## Stay in the insertion mode.
3227            !!!next-token;            !!!next-token;
3228            redo INITIAL;            redo INITIAL;
3229            } else {
3230              !!!cp ('t16');
3231          }          }
3232          } else {
3233            !!!cp ('t17');
3234        }        }
3235    
3236        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3237        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3238        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3239        ## reprocess        ## reprocess
3240        return;        return;
3241      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3242          !!!cp ('t18');
3243        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3244        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3245                
3246        ## Stay in the phase.        ## Stay in the insertion mode.
3247        !!!next-token;        !!!next-token;
3248        redo INITIAL;        redo INITIAL;
3249      } else {      } else {
3250        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3251      }      }
3252    } # INITIAL    } # INITIAL
3253    
3254      die "$0: _tree_construction_initial: This should be never reached";
3255  } # _tree_construction_initial  } # _tree_construction_initial
3256    
3257  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3258    my $self = shift;    my $self = shift;
3259    
3260      ## NOTE: "before html" insertion mode.
3261        
3262    B: {    B: {
3263        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3264          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3265            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3266          ## Ignore the token          ## Ignore the token
3267          ## Stay in the phase          ## Stay in the insertion mode.
3268          !!!next-token;          !!!next-token;
3269          redo B;          redo B;
3270        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3271            !!!cp ('t20');
3272          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3273          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3274          ## Stay in the phase          ## Stay in the insertion mode.
3275          !!!next-token;          !!!next-token;
3276          redo B;          redo B;
3277        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2093  sub _tree_construction_root_element ($) Line 3279  sub _tree_construction_root_element ($)
3279            ## Ignore the token.            ## Ignore the token.
3280    
3281            unless (length $token->{data}) {            unless (length $token->{data}) {
3282              ## Stay in the phase              !!!cp ('t21');
3283                ## Stay in the insertion mode.
3284              !!!next-token;              !!!next-token;
3285              redo B;              redo B;
3286              } else {
3287                !!!cp ('t22');
3288            }            }
3289            } else {
3290              !!!cp ('t23');
3291          }          }
3292    
3293          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3294    
3295          #          #
3296        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3297          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3298              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
3299            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3300                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3301            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3302                  [$root_element, $el_category->{html}];
3303    
3304              if ($token->{attributes}->{manifest}) {
3305                !!!cp ('t24');
3306                $self->{application_cache_selection}
3307                    ->($token->{attributes}->{manifest}->{value});
3308                ## ISSUE: Spec is unclear on relative references.
3309                ## According to Hixie (#whatwg 2008-03-19), it should be
3310                ## resolved against the base URI of the document in HTML
3311                ## or xml:base of the element in XHTML.
3312              } else {
3313                !!!cp ('t25');
3314                $self->{application_cache_selection}->(undef);
3315              }
3316    
3317              !!!nack ('t25c');
3318    
3319              !!!next-token;
3320              return; ## Go to the "before head" insertion mode.
3321          } else {          } else {
3322            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3323              #
3324          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3325        } elsif ({        } elsif ({
3326                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3327                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3328                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3329          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3330          #          #
3331        } else {        } else {
3332          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3333        }        }
3334    
3335        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3336        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3337        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3338        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3339        #redo B;  
3340        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3341    
3342        ## NOTE: Reprocess the token.
3343        !!!ack-later;
3344        return; ## Go to the "before head" insertion mode.
3345    
3346        ## ISSUE: There is an issue in the spec
3347    } # B    } # B
3348    
3349      die "$0: _tree_construction_root_element: This should never be reached";
3350  } # _tree_construction_root_element  } # _tree_construction_root_element
3351    
3352  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2147  sub _reset_insertion_mode ($) { Line 3361  sub _reset_insertion_mode ($) {
3361            
3362      ## Step 3      ## Step 3
3363      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"!?  
3364        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3365          $last = 1;          $last = 1;
3366          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3367            if ($self->{inner_html_node}->[1] eq 'td' or            if ($self->{inner_html_node}->[1] & TABLE_CELL_EL) {
3368                $self->{inner_html_node}->[1] eq 'th') {              !!!cp ('t27');
3369              #              #
3370            } else {            } else {
3371                !!!cp ('t28');
3372              $node = $self->{inner_html_node};              $node = $self->{inner_html_node};
3373            }            }
3374          }          }
3375        }        }
3376            
3377        ## Step 4..13      ## Step 4..14
3378        my $new_mode = {      my $new_mode;
3379        if ($node->[1] & FOREIGN_EL) {
3380          ## NOTE: Strictly spaking, the line below only applies to MathML and
3381          ## SVG elements.  Currently the HTML syntax supports only MathML and
3382          ## SVG elements as foreigners.
3383          $new_mode = $self->{insertion_mode} | IN_FOREIGN_CONTENT_IM;
3384          ## ISSUE: What is set as the secondary insertion mode?
3385        } else {
3386          $new_mode = {
3387                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3388                          ## NOTE: |option| and |optgroup| do not set
3389                          ## insertion mode to "in select" by themselves.
3390                        td => IN_CELL_IM,                        td => IN_CELL_IM,
3391                        th => IN_CELL_IM,                        th => IN_CELL_IM,
3392                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
# Line 2179  sub _reset_insertion_mode ($) { Line 3399  sub _reset_insertion_mode ($) {
3399                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3400                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3401                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3402                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3403        $self->{insertion_mode} = $new_mode and return if defined $new_mode;      }
3404        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3405                
3406        ## Step 14        ## Step 15
3407        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3408          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3409              !!!cp ('t29');
3410            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3411          } else {          } else {
3412              ## ISSUE: Can this state be reached?
3413              !!!cp ('t30');
3414            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3415          }          }
3416          return;          return;
3417          } else {
3418            !!!cp ('t31');
3419        }        }
3420                
3421        ## Step 15        ## Step 16
3422        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3423                
3424        ## Step 16        ## Step 17
3425        $i--;        $i--;
3426        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3427                
3428        ## Step 17        ## Step 18
3429        redo S3;        redo S3;
3430      } # S3      } # S3
3431    
3432      die "$0: _reset_insertion_mode: This line should never be reached";
3433  } # _reset_insertion_mode  } # _reset_insertion_mode
3434    
3435  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2223  sub _tree_construction_main ($) { Line 3451  sub _tree_construction_main ($) {
3451      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3452      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3453        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3454            !!!cp ('t32');
3455          return;          return;
3456        }        }
3457      }      }
# Line 2237  sub _tree_construction_main ($) { Line 3466  sub _tree_construction_main ($) {
3466    
3467        ## Step 6        ## Step 6
3468        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3469            !!!cp ('t33_1');
3470          #          #
3471        } else {        } else {
3472          my $in_open_elements;          my $in_open_elements;
3473          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3474            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3475                !!!cp ('t33');
3476              $in_open_elements = 1;              $in_open_elements = 1;
3477              last OE;              last OE;
3478            }            }
3479          }          }
3480          if ($in_open_elements) {          if ($in_open_elements) {
3481              !!!cp ('t34');
3482            #            #
3483          } else {          } else {
3484              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3485              !!!cp ('t35');
3486            redo S4;            redo S4;
3487          }          }
3488        }        }
# Line 2271  sub _tree_construction_main ($) { Line 3505  sub _tree_construction_main ($) {
3505    
3506        ## Step 11        ## Step 11
3507        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3508            !!!cp ('t36');
3509          ## Step 7'          ## Step 7'
3510          $i++;          $i++;
3511          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3512                    
3513          redo S7;          redo S7;
3514        }        }
3515    
3516          !!!cp ('t37');
3517      } # S7      } # S7
3518    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3519    
3520    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3521      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3522        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3523            !!!cp ('t38');
3524          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3525          return;          return;
3526        }        }
3527      }      }
3528    
3529        !!!cp ('t39');
3530    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3531    
3532    my $parse_rcdata = sub ($$) {    my $insert;
3533      my ($content_model_flag, $insert) = @_;  
3534      my $parse_rcdata = sub ($) {
3535        my ($content_model_flag) = @_;
3536    
3537      ## Step 1      ## Step 1
3538      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3539      my $el;      my $el;
3540      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3541    
3542      ## Step 2      ## Step 2
3543      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3544    
3545      ## Step 3      ## Step 3
3546      $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 3548  sub _tree_construction_main ($) {
3548    
3549      ## Step 4      ## Step 4
3550      my $text = '';      my $text = '';
3551        !!!nack ('t40.1');
3552      !!!next-token;      !!!next-token;
3553      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3554          !!!cp ('t40');
3555        $text .= $token->{data};        $text .= $token->{data};
3556        !!!next-token;        !!!next-token;
3557      }      }
3558    
3559      ## Step 5      ## Step 5
3560      if (length $text) {      if (length $text) {
3561          !!!cp ('t41');
3562        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3563        $el->append_child ($text);        $el->append_child ($text);
3564      }      }
# Line 2322  sub _tree_construction_main ($) { Line 3567  sub _tree_construction_main ($) {
3567      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3568    
3569      ## Step 7      ## Step 7
3570      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3571            $token->{tag_name} eq $start_tag_name) {
3572          !!!cp ('t42');
3573        ## 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});  
3574      } else {      } else {
3575        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3576          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3577            !!!cp ('t43');
3578            !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3579          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3580            !!!cp ('t44');
3581            !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
3582          } else {
3583            die "$0: $content_model_flag in parse_rcdata";
3584          }
3585      }      }
3586      !!!next-token;      !!!next-token;
3587    }; # $parse_rcdata    }; # $parse_rcdata
3588    
3589    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3590      my $script_el;      my $script_el;
3591      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3592      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3593    
3594      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3595      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3596            
3597      my $text = '';      my $text = '';
3598        !!!nack ('t45.1');
3599      !!!next-token;      !!!next-token;
3600      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3601          !!!cp ('t45');
3602        $text .= $token->{data};        $text .= $token->{data};
3603        !!!next-token;        !!!next-token;
3604      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3605      if (length $text) {      if (length $text) {
3606          !!!cp ('t46');
3607        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3608      }      }
3609                                
# Line 2357  sub _tree_construction_main ($) { Line 3611  sub _tree_construction_main ($) {
3611    
3612      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3613          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3614          !!!cp ('t47');
3615        ## Ignore the token        ## Ignore the token
3616      } else {      } else {
3617        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3618          !!!parse-error (type => 'in CDATA:#'.$token->{type}, token => $token);
3619        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3620        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3621      }      }
3622            
3623      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3624          !!!cp ('t49');
3625        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3626      } else {      } else {
3627          !!!cp ('t50');
3628        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3629        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3630    
# Line 2380  sub _tree_construction_main ($) { Line 3638  sub _tree_construction_main ($) {
3638      !!!next-token;      !!!next-token;
3639    }; # $script_start_tag    }; # $script_start_tag
3640    
3641      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3642      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3643      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3644    
3645    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3646      my $tag_name = shift;      my $end_tag_token = shift;
3647        my $tag_name = $end_tag_token->{tag_name};
3648    
3649        ## NOTE: The adoption agency algorithm (AAA).
3650    
3651      FET: {      FET: {
3652        ## Step 1        ## Step 1
3653        my $formatting_element;        my $formatting_element;
3654        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3655        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3656          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3657              !!!cp ('t52');
3658              last AFE;
3659            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3660                         eq $tag_name) {
3661              !!!cp ('t51');
3662            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3663            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3664            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
3665          }          }
3666        } # AFE        } # AFE
3667        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3668          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
3669            !!!parse-error (type => 'unmatched end tag:'.$tag_name, token => $end_tag_token);
3670          ## Ignore the token          ## Ignore the token
3671          !!!next-token;          !!!next-token;
3672          return;          return;
# Line 2409  sub _tree_construction_main ($) { Line 3678  sub _tree_construction_main ($) {
3678          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3679          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
3680            if ($in_scope) {            if ($in_scope) {
3681                !!!cp ('t54');
3682              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
3683              last INSCOPE;              last INSCOPE;
3684            } else { # in open elements but not in scope            } else { # in open elements but not in scope
3685              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
3686                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3687                                token => $end_tag_token);
3688              ## Ignore the token              ## Ignore the token
3689              !!!next-token;              !!!next-token;
3690              return;              return;
3691            }            }
3692          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
3693                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
3694            $in_scope = 0;            $in_scope = 0;
3695          }          }
3696        } # INSCOPE        } # INSCOPE
3697        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
3698          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
3699            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name},
3700                            token => $end_tag_token);
3701          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
3702          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
3703          return;          return;
3704        }        }
3705        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
3706          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
3707            !!!parse-error (type => 'not closed',
3708                            value => $self->{open_elements}->[-1]->[0]
3709                                ->manakai_local_name,
3710                            token => $end_tag_token);
3711        }        }
3712                
3713        ## Step 2        ## Step 2
# Line 2439  sub _tree_construction_main ($) { Line 3715  sub _tree_construction_main ($) {
3715        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
3716        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3717          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
3718          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
3719              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
3720              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
3721               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
3722              !!!cp ('t59');
3723            $furthest_block = $node;            $furthest_block = $node;
3724            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
3725          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
3726              !!!cp ('t60');
3727            last OE;            last OE;
3728          }          }
3729        } # OE        } # OE
3730                
3731        ## Step 3        ## Step 3
3732        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
3733            !!!cp ('t61');
3734          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
3735          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
3736          !!!next-token;          !!!next-token;
# Line 2464  sub _tree_construction_main ($) { Line 3743  sub _tree_construction_main ($) {
3743        ## Step 5        ## Step 5
3744        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
3745        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
3746            !!!cp ('t62');
3747          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
3748        }        }
3749                
# Line 2486  sub _tree_construction_main ($) { Line 3766  sub _tree_construction_main ($) {
3766          S7S2: {          S7S2: {
3767            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
3768              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
3769                  !!!cp ('t63');
3770                $node_i_in_active = $_;                $node_i_in_active = $_;
3771                last S7S2;                last S7S2;
3772              }              }
# Line 2499  sub _tree_construction_main ($) { Line 3780  sub _tree_construction_main ($) {
3780                    
3781          ## Step 4          ## Step 4
3782          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
3783              !!!cp ('t64');
3784            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
3785          }          }
3786                    
3787          ## Step 5          ## Step 5
3788          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
3789              !!!cp ('t65');
3790            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
3791            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
3792            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2521  sub _tree_construction_main ($) { Line 3804  sub _tree_construction_main ($) {
3804        } # S7          } # S7  
3805                
3806        ## Step 8        ## Step 8
3807        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
3808            my $foster_parent_element;
3809            my $next_sibling;
3810            OE: for (reverse 0..$#{$self->{open_elements}}) {
3811              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
3812                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3813                                 if (defined $parent and $parent->node_type == 1) {
3814                                   !!!cp ('t65.1');
3815                                   $foster_parent_element = $parent;
3816                                   $next_sibling = $self->{open_elements}->[$_]->[0];
3817                                 } else {
3818                                   !!!cp ('t65.2');
3819                                   $foster_parent_element
3820                                     = $self->{open_elements}->[$_ - 1]->[0];
3821                                 }
3822                                 last OE;
3823                               }
3824                             } # OE
3825                             $foster_parent_element = $self->{open_elements}->[0]->[0]
3826                               unless defined $foster_parent_element;
3827            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
3828            $open_tables->[-1]->[1] = 1; # tainted
3829          } else {
3830            !!!cp ('t65.3');
3831            $common_ancestor_node->[0]->append_child ($last_node->[0]);
3832          }
3833                
3834        ## Step 9        ## Step 9
3835        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2538  sub _tree_construction_main ($) { Line 3846  sub _tree_construction_main ($) {
3846        my $i;        my $i;
3847        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3848          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
3849              !!!cp ('t66');
3850            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
3851            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
3852          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
3853              !!!cp ('t67');
3854            $i = $_;            $i = $_;
3855          }          }
3856        } # AFE        } # AFE
# Line 2550  sub _tree_construction_main ($) { Line 3860  sub _tree_construction_main ($) {
3860        undef $i;        undef $i;
3861        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
3862          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
3863              !!!cp ('t68');
3864            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
3865            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
3866          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
3867              !!!cp ('t69');
3868            $i = $_;            $i = $_;
3869          }          }
3870        } # OE        } # OE
# Line 2563  sub _tree_construction_main ($) { Line 3875  sub _tree_construction_main ($) {
3875      } # FET      } # FET
3876    }; # $formatting_end_tag    }; # $formatting_end_tag
3877    
3878    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
3879      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
3880    }; # $insert_to_current    }; # $insert_to_current
3881    
3882    my $insert_to_foster = sub {    my $insert_to_foster = sub {
3883                         my $child = shift;      my $child = shift;
3884                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
3885                              table => 1, tbody => 1, tfoot => 1,        # MUST
3886                              thead => 1, tr => 1,        my $foster_parent_element;
3887                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
3888                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
3889                           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') {  
3890                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
3891                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
3892                                   !!!cp ('t70');
3893                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
3894                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
3895                               } else {                               } else {
3896                                   !!!cp ('t71');
3897                                 $foster_parent_element                                 $foster_parent_element
3898                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
3899                               }                               }
# Line 2593  sub _tree_construction_main ($) { Line 3904  sub _tree_construction_main ($) {
3904                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
3905                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
3906                             ($child, $next_sibling);                             ($child, $next_sibling);
3907                         } else {        $open_tables->[-1]->[1] = 1; # tainted
3908                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
3909                         }        !!!cp ('t72');
3910          $self->{open_elements}->[-1]->[0]->append_child ($child);
3911        }
3912    }; # $insert_to_foster    }; # $insert_to_foster
3913    
3914    my $insert;    B: while (1) {
   
   B: {  
3915      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3916        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
3917          !!!parse-error (type => 'DOCTYPE in the middle', token => $token);
3918        ## Ignore the token        ## Ignore the token
3919        ## Stay in the phase        ## Stay in the phase
3920        !!!next-token;        !!!next-token;
3921        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;  
3922      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
3923               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
3924        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
3925          ## Turn into the main phase          !!!cp ('t79');
3926          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3927          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
3928        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
3929          ## Turn into the main phase          !!!cp ('t80');
3930          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html:html', token => $token);
3931          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
3932          } else {
3933            !!!cp ('t81');
3934        }        }
3935    
3936  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
3937  ## 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');  
       }  
3938        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
3939        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
3940          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
3941              !!!cp ('t84');
3942            $top_el->set_attribute_ns            $top_el->set_attribute_ns
3943              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
3944               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
3945          }          }
3946        }        }
3947          !!!nack ('t84.1');
3948        !!!next-token;        !!!next-token;
3949        redo B;        next B;
3950      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3951        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3952        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
3953            !!!cp ('t85');
3954          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3955        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
3956            !!!cp ('t86');
3957          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
3958        } else {        } else {
3959            !!!cp ('t87');
3960          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
3961        }        }
3962        !!!next-token;        !!!next-token;
3963        redo B;        next B;
3964      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
3965          if ($token->{type} == CHARACTER_TOKEN) {
3966            !!!cp ('t87.1');
3967            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
3968            !!!next-token;
3969            next B;
3970          } elsif ($token->{type} == START_TAG_TOKEN) {
3971            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
3972                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
3973                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
3974                ($token->{tag_name} eq 'svg' and
3975                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
3976              ## NOTE: "using the rules for secondary insertion mode"then"continue"
3977              !!!cp ('t87.2');
3978              #
3979            } elsif ({
3980                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
3981                      center => 1, code => 1, dd => 1, div => 1, dl => 1, em => 1,
3982                      embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1, ## No h4!
3983                      h5 => 1, h6 => 1, head => 1, hr => 1, i => 1, img => 1,
3984                      li => 1, menu => 1, meta => 1, nobr => 1, p => 1, pre => 1,
3985                      ruby => 1, s => 1, small => 1, span => 1, strong => 1,
3986                      sub => 1, sup => 1, table => 1, tt => 1, u => 1, ul => 1,
3987                      var => 1,
3988                     }->{$token->{tag_name}}) {
3989              !!!cp ('t87.2');
3990              !!!parse-error (type => 'not closed',
3991                              value => $self->{open_elements}->[-1]->[0]
3992                                  ->manakai_local_name,
3993                              token => $token);
3994    
3995              pop @{$self->{open_elements}}
3996                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
3997    
3998              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
3999              ## Reprocess.
4000              next B;
4001            } else {
4002              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4003              my $tag_name = $token->{tag_name};
4004              if ($nsuri eq $SVG_NS) {
4005                $tag_name = {
4006                   altglyph => 'altGlyph',
4007                   altglyphdef => 'altGlyphDef',
4008                   altglyphitem => 'altGlyphItem',
4009                   animatecolor => 'animateColor',
4010                   animatemotion => 'animateMotion',
4011                   animatetransform => 'animateTransform',
4012                   clippath => 'clipPath',
4013                   feblend => 'feBlend',
4014                   fecolormatrix => 'feColorMatrix',
4015                   fecomponenttransfer => 'feComponentTransfer',
4016                   fecomposite => 'feComposite',
4017                   feconvolvematrix => 'feConvolveMatrix',
4018                   fediffuselighting => 'feDiffuseLighting',
4019                   fedisplacementmap => 'feDisplacementMap',
4020                   fedistantlight => 'feDistantLight',
4021                   feflood => 'feFlood',
4022                   fefunca => 'feFuncA',
4023                   fefuncb => 'feFuncB',
4024                   fefuncg => 'feFuncG',
4025                   fefuncr => 'feFuncR',
4026                   fegaussianblur => 'feGaussianBlur',
4027                   feimage => 'feImage',
4028                   femerge => 'feMerge',
4029                   femergenode => 'feMergeNode',
4030                   femorphology => 'feMorphology',
4031                   feoffset => 'feOffset',
4032                   fepointlight => 'fePointLight',
4033                   fespecularlighting => 'feSpecularLighting',
4034                   fespotlight => 'feSpotLight',
4035                   fetile => 'feTile',
4036                   feturbulence => 'feTurbulence',
4037                   foreignobject => 'foreignObject',
4038                   glyphref => 'glyphRef',
4039                   lineargradient => 'linearGradient',
4040                   radialgradient => 'radialGradient',
4041                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4042                   textpath => 'textPath',  
4043                }->{$tag_name} || $tag_name;
4044              }
4045    
4046              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4047    
4048              ## "adjust foreign attributes" - done in insert-element-f
4049    
4050              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4051    
4052              if ($self->{self_closing}) {
4053                pop @{$self->{open_elements}};
4054                !!!ack ('t87.3');
4055              } else {
4056                !!!cp ('t87.4');
4057              }
4058    
4059              !!!next-token;
4060              next B;
4061            }
4062          } elsif ($token->{type} == END_TAG_TOKEN) {
4063            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4064            !!!cp ('t87.5');
4065            #
4066          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4067            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4068            !!!cp ('t87.6');
4069            #
4070            ## TODO: ...
4071          } else {
4072            die "$0: $token->{type}: Unknown token type";        
4073          }
4074        }
4075    
4076        if ($self->{insertion_mode} & HEAD_IMS) {
4077        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4078          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4079            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4080                !!!cp ('t88.2');
4081                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4082              } else {
4083                !!!cp ('t88.1');
4084                ## Ignore the token.
4085                !!!next-token;
4086                next B;
4087              }
4088            unless (length $token->{data}) {            unless (length $token->{data}) {
4089                !!!cp ('t88');
4090              !!!next-token;              !!!next-token;
4091              redo B;              next B;
4092            }            }
4093          }          }
4094    
4095          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4096              !!!cp ('t89');
4097            ## As if <head>            ## As if <head>
4098            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4099            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4100            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4101                  [$self->{head_element}, $el_category->{head}];
4102    
4103            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4104            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4105    
4106            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4107          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4108              !!!cp ('t90');
4109            ## As if </noscript>            ## As if </noscript>
4110            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4111            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#character', token => $token);
4112                        
4113            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4114            ## As if </head>            ## As if </head>
# Line 2704  sub _tree_construction_main ($) { Line 4116  sub _tree_construction_main ($) {
4116    
4117            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4118          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4119              !!!cp ('t91');
4120            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4121    
4122            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4123            } else {
4124              !!!cp ('t92');
4125          }          }
4126    
4127              ## "after head" insertion mode          ## "after head" insertion mode
4128              ## As if <body>          ## As if <body>
4129              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4130              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4131              ## reprocess          ## reprocess
4132              redo B;          next B;
4133            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4134              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4135                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4136                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4137                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4138                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4139                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4140                  !!!next-token;              push @{$self->{open_elements}},
4141                  redo B;                  [$self->{head_element}, $el_category->{head}];
4142                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4143                  #              !!!nack ('t93.1');
4144                } else {              !!!next-token;
4145                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4146                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4147                  !!!next-token;              !!!cp ('t94');
4148                  redo B;              #
4149                }            } else {
4150              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              !!!cp ('t95');
4151                ## As if <head>              !!!parse-error (type => 'in head:head', token => $token); # or in head noscript
4152                !!!create-element ($self->{head_element}, 'head');              ## Ignore the token
4153                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!nack ('t95.1');
4154                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              !!!next-token;
4155                next B;
4156              }
4157            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4158              !!!cp ('t96');
4159              ## As if <head>
4160              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4161              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4162              push @{$self->{open_elements}},
4163                  [$self->{head_element}, $el_category->{head}];
4164    
4165                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4166                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4167              }          } else {
4168              !!!cp ('t97');
4169            }
4170    
4171              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4172                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4173                    !!!cp ('t98');
4174                  ## As if </noscript>                  ## As if </noscript>
4175                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4176                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript:base', token => $token);
4177                                
4178                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4179                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4180                  } else {
4181                    !!!cp ('t99');
4182                }                }
4183    
4184                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4185                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4186                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4187                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4188                    push @{$self->{open_elements}},
4189                        [$self->{head_element}, $el_category->{head}];
4190                  } else {
4191                    !!!cp ('t101');
4192                }                }
4193                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4194                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4195                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4196                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4197                  !!!nack ('t101.1');
4198                !!!next-token;                !!!next-token;
4199                redo B;                next B;
4200              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4201                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4202                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4203                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4204                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4205                    push @{$self->{open_elements}},
4206                        [$self->{head_element}, $el_category->{head}];
4207                  } else {
4208                    !!!cp ('t103');
4209                }                }
4210                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4211                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4212                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4213                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4214                  !!!ack ('t103.1');
4215                !!!next-token;                !!!next-token;
4216                redo B;                next B;
4217              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4218                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4219                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4220                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4221                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4222                    push @{$self->{open_elements}},
4223                        [$self->{head_element}, $el_category->{head}];
4224                  } else {
4225                    !!!cp ('t105');
4226                }                }
4227                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4228                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.
4229    
4230                unless ($self->{confident}) {                unless ($self->{confident}) {
4231                  my $charset;                  if ($token->{attributes}->{charset}) {
4232                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                    !!!cp ('t106');
4233                    $charset = $token->{attributes}->{charset}->{value};                    ## NOTE: Whether the encoding is supported or not is handled
4234                  }                    ## in the {change_encoding} callback.
4235                  if ($token->{attributes}->{'http-equiv'}) {                    $self->{change_encoding}
4236                    ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                        ->($self, $token->{attributes}->{charset}->{value},
4237                    if ($token->{attributes}->{'http-equiv'}->{value}                           $token);
4238                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                    
4239                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4240                          ->set_user_data (manakai_has_reference =>
4241                                               $token->{attributes}->{charset}
4242                                                   ->{has_reference});
4243                    } elsif ($token->{attributes}->{content}) {
4244                      if ($token->{attributes}->{content}->{value}
4245                          =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4246                              [\x09-\x0D\x20]*=
4247                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4248                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
4249                      $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                      !!!cp ('t107');
4250                    } ## TODO: And if supported                      ## NOTE: Whether the encoding is supported or not is handled
4251                        ## in the {change_encoding} callback.
4252                        $self->{change_encoding}
4253                            ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4254                               $token);
4255                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4256                            ->set_user_data (manakai_has_reference =>
4257                                                 $token->{attributes}->{content}
4258                                                       ->{has_reference});
4259                      } else {
4260                        !!!cp ('t108');
4261                      }
4262                    }
4263                  } else {
4264                    if ($token->{attributes}->{charset}) {
4265                      !!!cp ('t109');
4266                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4267                          ->set_user_data (manakai_has_reference =>
4268                                               $token->{attributes}->{charset}
4269                                                   ->{has_reference});
4270                    }
4271                    if ($token->{attributes}->{content}) {
4272                      !!!cp ('t110');
4273                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4274                          ->set_user_data (manakai_has_reference =>
4275                                               $token->{attributes}->{content}
4276                                                   ->{has_reference});
4277                  }                  }
                 ## TODO: Change the encoding  
4278                }                }
4279    
4280                ## TODO: Extracting |charset| from |meta|.                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4281                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4282                  !!!ack ('t110.1');
4283                !!!next-token;                !!!next-token;
4284                redo B;                next B;
4285              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4286                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4287                    !!!cp ('t111');
4288                  ## As if </noscript>                  ## As if </noscript>
4289                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4290                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript:title', token => $token);
4291                                
4292                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4293                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4294                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4295                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4296                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4297                    push @{$self->{open_elements}},
4298                        [$self->{head_element}, $el_category->{head}];
4299                  } else {
4300                    !!!cp ('t113');
4301                }                }
4302    
4303                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4304                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4305                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4306                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4307                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4308                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4309                redo B;                next B;
4310              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style') {
4311                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4312                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4313                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4314                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4315                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4316                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4317                    push @{$self->{open_elements}},
4318                        [$self->{head_element}, $el_category->{head}];
4319                  } else {
4320                    !!!cp ('t115');
4321                }                }
4322                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4323                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4324                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4325                redo B;                next B;
4326              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4327                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4328                    !!!cp ('t116');
4329                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4330                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4331                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4332                    !!!nack ('t116.1');
4333                  !!!next-token;                  !!!next-token;
4334                  redo B;                  next B;
4335                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4336                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4337                    !!!parse-error (type => 'in noscript:noscript', token => $token);
4338                  ## Ignore the token                  ## Ignore the token
4339                    !!!nack ('t117.1');
4340                  !!!next-token;                  !!!next-token;
4341                  redo B;                  next B;
4342                } else {                } else {
4343                    !!!cp ('t118');
4344                  #                  #
4345                }                }
4346              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4347                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4348                    !!!cp ('t119');
4349                  ## As if </noscript>                  ## As if </noscript>
4350                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4351                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:script', token => $token);
4352                                
4353                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4354                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4355                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4356                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4357                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head:'.$token->{tag_name}, token => $token);
4358                    push @{$self->{open_elements}},
4359                        [$self->{head_element}, $el_category->{head}];
4360                  } else {
4361                    !!!cp ('t121');
4362                }                }
4363    
4364                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4365                $script_start_tag->($insert_to_current);                $script_start_tag->();
4366                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4367                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4368                redo B;                next B;
4369              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4370                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4371                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4372                    !!!cp ('t122');
4373                  ## As if </noscript>                  ## As if </noscript>
4374                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4375                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript:'.$token->{tag_name}, token => $token);
4376                                    
4377                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4378                  ## As if </head>                  ## As if </head>
# Line 2885  sub _tree_construction_main ($) { Line 4380  sub _tree_construction_main ($) {
4380                                    
4381                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4382                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4383                    !!!cp ('t124');
4384                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4385                                    
4386                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4387                  } else {
4388                    !!!cp ('t125');
4389                }                }
4390    
4391                ## "after head" insertion mode                ## "after head" insertion mode
4392                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4393                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4394                    !!!cp ('t126');
4395                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4396                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4397                    !!!cp ('t127');
4398                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4399                } else {                } else {
4400                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4401                }                }
4402                  !!!nack ('t127.1');
4403                !!!next-token;                !!!next-token;
4404                redo B;                next B;
4405              } else {              } else {
4406                  !!!cp ('t128');
4407                #                #
4408              }              }
4409    
4410              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4411                  !!!cp ('t129');
4412                ## As if </noscript>                ## As if </noscript>
4413                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4414                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4415                                
4416                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4417                ## As if </head>                ## As if </head>
# Line 2916  sub _tree_construction_main ($) { Line 4419  sub _tree_construction_main ($) {
4419    
4420                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4421              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4422                  !!!cp ('t130');
4423                ## As if </head>                ## As if </head>
4424                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4425    
4426                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4427                } else {
4428                  !!!cp ('t131');
4429              }              }
4430    
4431              ## "after head" insertion mode              ## "after head" insertion mode
4432              ## As if <body>              ## As if <body>
4433              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4434              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4435              ## reprocess              ## reprocess
4436              redo B;              !!!ack-later;
4437                next B;
4438            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4439              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4440                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4441                    !!!cp ('t132');
4442                  ## As if <head>                  ## As if <head>
4443                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4444                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4445                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4446                        [$self->{head_element}, $el_category->{head}];
4447    
4448                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4449                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4450                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4451                  !!!next-token;                  !!!next-token;
4452                  redo B;                  next B;
4453                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4454                    !!!cp ('t133');
4455                  ## As if </noscript>                  ## As if </noscript>
4456                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4457                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/head', token => $token);
4458                                    
4459                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4460                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4461                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4462                  !!!next-token;                  !!!next-token;
4463                  redo B;                  next B;
4464                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4465                    !!!cp ('t134');
4466                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4467                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4468                  !!!next-token;                  !!!next-token;
4469                  redo B;                  next B;
4470                } else {                } else {
4471                    !!!cp ('t135');
4472                  #                  #
4473                }                }
4474              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4475                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4476                    !!!cp ('t136');
4477                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4478                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4479                  !!!next-token;                  !!!next-token;
4480                  redo B;                  next B;
4481                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4482                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!cp ('t137');
4483                    !!!parse-error (type => 'unmatched end tag:noscript', token => $token);
4484                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4485                  !!!next-token;                  !!!next-token;
4486                  redo B;                  next B;
4487                } else {                } else {
4488                    !!!cp ('t138');
4489                  #                  #
4490                }                }
4491              } elsif ({              } elsif ({
4492                        body => 1, html => 1,                        body => 1, html => 1,
4493                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4494                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4495                    !!!cp ('t139');
4496                  ## As if <head>                  ## As if <head>
4497                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4498                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4499                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4500                        [$self->{head_element}, $el_category->{head}];
4501    
4502                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4503                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4504                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4505                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t140');
4506                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4507                  ## Ignore the token                  ## Ignore the token
4508                  !!!next-token;                  !!!next-token;
4509                  redo B;                  next B;
4510                  } else {
4511                    !!!cp ('t141');
4512                }                }
4513                                
4514                #                #
# Line 2996  sub _tree_construction_main ($) { Line 4516  sub _tree_construction_main ($) {
4516                        p => 1, br => 1,                        p => 1, br => 1,
4517                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4518                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4519                    !!!cp ('t142');
4520                  ## As if <head>                  ## As if <head>
4521                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4522                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4523                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4524                        [$self->{head_element}, $el_category->{head}];
4525    
4526                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4527                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4528                  } else {
4529                    !!!cp ('t143');
4530                }                }
4531    
4532                #                #
4533              } else {              } else {
4534                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4535                    !!!cp ('t144');
4536                  #                  #
4537                } else {                } else {
4538                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t145');
4539                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4540                  ## Ignore the token                  ## Ignore the token
4541                  !!!next-token;                  !!!next-token;
4542                  redo B;                  next B;
4543                }                }
4544              }              }
4545    
4546              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4547                  !!!cp ('t146');
4548                ## As if </noscript>                ## As if </noscript>
4549                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4550                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/'.$token->{tag_name}, token => $token);
4551                                
4552                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4553                ## As if </head>                ## As if </head>
# Line 3028  sub _tree_construction_main ($) { Line 4555  sub _tree_construction_main ($) {
4555    
4556                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4557              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4558                  !!!cp ('t147');
4559                ## As if </head>                ## As if </head>
4560                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4561    
4562                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4563              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4564                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4565                  !!!cp ('t148');
4566                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4567                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4568                !!!next-token;                !!!next-token;
4569                redo B;                next B;
4570                } else {
4571                  !!!cp ('t149');
4572              }              }
4573    
4574              ## "after head" insertion mode              ## "after head" insertion mode
4575              ## As if <body>              ## As if <body>
4576              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4577              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4578              ## reprocess              ## reprocess
4579              redo B;              next B;
4580            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4581              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4582            }            !!!cp ('t149.1');
4583    
4584              ## NOTE: As if <head>
4585              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4586              $self->{open_elements}->[-1]->[0]->append_child
4587                  ($self->{head_element});
4588              #push @{$self->{open_elements}},
4589              #    [$self->{head_element}, $el_category->{head}];
4590              #$self->{insertion_mode} = IN_HEAD_IM;
4591              ## NOTE: Reprocess.
4592    
4593              ## NOTE: As if </head>
4594              #pop @{$self->{open_elements}};
4595              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4596              ## NOTE: Reprocess.
4597              
4598              #
4599            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4600              !!!cp ('t149.2');
4601    
4602              ## NOTE: As if </head>
4603              pop @{$self->{open_elements}};
4604              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4605              ## NOTE: Reprocess.
4606    
4607              #
4608            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4609              !!!cp ('t149.3');
4610    
4611              !!!parse-error (type => 'in noscript:#eof', token => $token);
4612    
4613              ## As if </noscript>
4614              pop @{$self->{open_elements}};
4615              #$self->{insertion_mode} = IN_HEAD_IM;
4616              ## NOTE: Reprocess.
4617    
4618              ## NOTE: As if </head>
4619              pop @{$self->{open_elements}};
4620              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
4621              ## NOTE: Reprocess.
4622    
4623              #
4624            } else {
4625              !!!cp ('t149.4');
4626              #
4627            }
4628    
4629            ## NOTE: As if <body>
4630            !!!insert-element ('body',, $token);
4631            $self->{insertion_mode} = IN_BODY_IM;
4632            ## NOTE: Reprocess.
4633            next B;
4634          } else {
4635            die "$0: $token->{type}: Unknown token type";
4636          }
4637    
4638            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
4639      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
4640            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
4641                !!!cp ('t150');
4642              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
4643              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
4644                            
4645              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4646    
4647              !!!next-token;              !!!next-token;
4648              redo B;              next B;
4649            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
4650              if ({              if ({
4651                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3066  sub _tree_construction_main ($) { Line 4653  sub _tree_construction_main ($) {
4653                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
4654                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
4655                  ## have an element in table scope                  ## have an element in table scope
4656                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
4657                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4658                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
4659                      $tn = $node->[1];                      !!!cp ('t151');
4660                      last INSCOPE;  
4661                    } elsif ({                      ## Close the cell
4662                              table => 1, html => 1,                      !!!back-token; # <x>
4663                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
4664                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
4665                    }                                line => $token->{line},
4666                  } # INSCOPE                                column => $token->{column}};
4667                    unless (defined $tn) {                      next B;
4668                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4669                      ## Ignore the token                      !!!cp ('t152');
4670                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
4671                      redo B;                      last;
4672                    }                    }
4673                                    }
4674                  ## Close the cell  
4675                  !!!back-token; # <?>                  !!!cp ('t153');
4676                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
4677                  redo B;                      value => $token->{tag_name}, token => $token);
4678                    ## Ignore the token
4679                    !!!nack ('t153.1');
4680                    !!!next-token;
4681                    next B;
4682                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4683                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed:caption', token => $token);
4684                                    
4685                  ## As if </caption>                  ## NOTE: As if </caption>.
4686                  ## have a table element in table scope                  ## have a table element in table scope
4687                  my $i;                  my $i;
4688                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4689                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4690                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
4691                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4692                      last INSCOPE;                        !!!cp ('t155');
4693                    } elsif ({                        $i = $_;
4694                              table => 1, html => 1,                        last INSCOPE;
4695                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4696                      last INSCOPE;                        !!!cp ('t156');
4697                          last;
4698                        }
4699                    }                    }
4700    
4701                      !!!cp ('t157');
4702                      !!!parse-error (type => 'start tag not allowed',
4703                                      value => $token->{tag_name}, token => $token);
4704                      ## Ignore the token
4705                      !!!nack ('t157.1');
4706                      !!!next-token;
4707                      next B;
4708                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4709                                    
4710                  ## generate implied end tags                  ## generate implied end tags
4711                  if ({                  while ($self->{open_elements}->[-1]->[1]
4712                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4713                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
4714                       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;  
4715                  }                  }
4716    
4717                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4718                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
4719                      !!!parse-error (type => 'not closed',
4720                                      value => $self->{open_elements}->[-1]->[0]
4721                                          ->manakai_local_name,
4722                                      token => $token);
4723                    } else {
4724                      !!!cp ('t160');
4725                  }                  }
4726                                    
4727                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3138  sub _tree_construction_main ($) { Line 4731  sub _tree_construction_main ($) {
4731                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4732                                    
4733                  ## reprocess                  ## reprocess
4734                  redo B;                  !!!ack-later;
4735                    next B;
4736                } else {                } else {
4737                    !!!cp ('t161');
4738                  #                  #
4739                }                }
4740              } else {              } else {
4741                  !!!cp ('t162');
4742                #                #
4743              }              }
4744            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3152  sub _tree_construction_main ($) { Line 4748  sub _tree_construction_main ($) {
4748                  my $i;                  my $i;
4749                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4750                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
4751                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4752                        !!!cp ('t163');
4753                      $i = $_;                      $i = $_;
4754                      last INSCOPE;                      last INSCOPE;
4755                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
4756                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
4757                      last INSCOPE;                      last INSCOPE;
4758                    }                    }
4759                  } # INSCOPE                  } # INSCOPE
4760                    unless (defined $i) {                    unless (defined $i) {
4761                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
4762                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4763                      ## Ignore the token                      ## Ignore the token
4764                      !!!next-token;                      !!!next-token;
4765                      redo B;                      next B;
4766                    }                    }
4767                                    
4768                  ## generate implied end tags                  ## generate implied end tags
4769                  if ({                  while ($self->{open_elements}->[-1]->[1]
4770                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4771                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
4772                       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;  
4773                  }                  }
4774                    
4775                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
4776                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
4777                      !!!cp ('t167');
4778                      !!!parse-error (type => 'not closed',
4779                                      value => $self->{open_elements}->[-1]->[0]
4780                                          ->manakai_local_name,
4781                                      token => $token);
4782                    } else {
4783                      !!!cp ('t168');
4784                  }                  }
4785                                    
4786                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3193  sub _tree_construction_main ($) { Line 4790  sub _tree_construction_main ($) {
4790                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
4791                                    
4792                  !!!next-token;                  !!!next-token;
4793                  redo B;                  next B;
4794                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
4795                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
4796                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4797                  ## Ignore the token                  ## Ignore the token
4798                  !!!next-token;                  !!!next-token;
4799                  redo B;                  next B;
4800                } else {                } else {
4801                    !!!cp ('t170');
4802                  #                  #
4803                }                }
4804              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
4805                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
4806                  ## have a table element in table scope                  ## have a table element in table scope
4807                  my $i;                  my $i;
4808                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
4809                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
4810                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
4811                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
4812                      last INSCOPE;                        !!!cp ('t171');
4813                    } elsif ({                        $i = $_;
4814                              table => 1, html => 1,                        last INSCOPE;
4815                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4816                      last INSCOPE;                        !!!cp ('t172');
4817                          last;
4818                        }
4819                    }                    }
4820    
4821                      !!!cp ('t173');
4822                      !!!parse-error (type => 'unmatched end tag',
4823                                      value => $token->{tag_name}, token => $token);
4824                      ## Ignore the token
4825                      !!!next-token;
4826                      next B;
4827                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
4828                                    
4829                  ## generate implied end tags                  ## generate implied end tags
4830                  if ({                  while ($self->{open_elements}->[-1]->[1]
4831                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
4832                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
4833                       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;  
4834                  }                  }
4835                                    
4836                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4837                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
4838                      !!!parse-error (type => 'not closed',
4839                                      value => $self->{open_elements}->[-1]->[0]
4840                                          ->manakai_local_name,
4841                                      token => $token);
4842                    } else {
4843                      !!!cp ('t176');
4844                  }                  }
4845                                    
4846                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3247  sub _tree_construction_main ($) { Line 4850  sub _tree_construction_main ($) {
4850                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
4851                                    
4852                  !!!next-token;                  !!!next-token;
4853                  redo B;                  next B;
4854                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
4855                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
4856                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4857                  ## Ignore the token                  ## Ignore the token
4858                  !!!next-token;                  !!!next-token;
4859                  redo B;                  next B;
4860                } else {                } else {
4861                    !!!cp ('t178');
4862                  #                  #
4863                }                }
4864              } elsif ({              } elsif ({
# Line 3264  sub _tree_construction_main ($) { Line 4869  sub _tree_construction_main ($) {
4869                ## have an element in table scope                ## have an element in table scope
4870                my $i;                my $i;
4871                my $tn;                my $tn;
4872                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
4873                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
4874                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
4875                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
4876                    last INSCOPE;                      !!!cp ('t179');
4877                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
4878                    $tn = $node->[1];  
4879                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
4880                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
4881                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
4882                            table => 1, html => 1,                                line => $token->{line},
4883                           }->{$node->[1]}) {                                column => $token->{column}};
4884                    last INSCOPE;                      next B;
4885                      } elsif ($node->[1] & TABLE_CELL_EL) {
4886                        !!!cp ('t180');
4887                        $tn = $node->[0]->manakai_local_name;
4888                        ## NOTE: There is exactly one |td| or |th| element
4889                        ## in scope in the stack of open elements by definition.
4890                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
4891                        ## ISSUE: Can this be reached?
4892                        !!!cp ('t181');
4893                        last;
4894                      }
4895                  }                  }
4896                } # INSCOPE  
4897                unless (defined $i) {                  !!!cp ('t182');
4898                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4899                        value => $token->{tag_name}, token => $token);
4900                  ## Ignore the token                  ## Ignore the token
4901                  !!!next-token;                  !!!next-token;
4902                  redo B;                  next B;
4903                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
4904              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
4905                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4906                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed:caption', token => $token);
4907    
4908                ## As if </caption>                ## As if </caption>
4909                ## have a table element in table scope                ## have a table element in table scope
4910                my $i;                my $i;
4911                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
4912                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
4913                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
4914                      !!!cp ('t184');
4915                    $i = $_;                    $i = $_;
4916                    last INSCOPE;                    last INSCOPE;
4917                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
4918                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
4919                    last INSCOPE;                    last INSCOPE;
4920                  }                  }
4921                } # INSCOPE                } # INSCOPE
4922                unless (defined $i) {                unless (defined $i) {
4923                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
4924                    !!!parse-error (type => 'unmatched end tag:caption', token => $token);
4925                  ## Ignore the token                  ## Ignore the token
4926                  !!!next-token;                  !!!next-token;
4927                  redo B;                  next B;
4928                }                }
4929                                
4930                ## generate implied end tags                ## generate implied end tags
4931                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
4932                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
4933                     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;  
4934                }                }
4935    
4936                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
4937                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
4938                    !!!parse-error (type => 'not closed',
4939                                    value => $self->{open_elements}->[-1]->[0]
4940                                        ->manakai_local_name,
4941                                    token => $token);
4942                  } else {
4943                    !!!cp ('t189');
4944                }                }
4945    
4946                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3340  sub _tree_construction_main ($) { Line 4950  sub _tree_construction_main ($) {
4950                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
4951    
4952                ## reprocess                ## reprocess
4953                redo B;                next B;
4954              } elsif ({              } elsif ({
4955                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
4956                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4957                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
4958                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
4959                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4960                  ## Ignore the token                  ## Ignore the token
4961                  !!!next-token;                  !!!next-token;
4962                  redo B;                  next B;
4963                } else {                } else {
4964                    !!!cp ('t191');
4965                  #                  #
4966                }                }
4967              } elsif ({              } elsif ({
# Line 3357  sub _tree_construction_main ($) { Line 4969  sub _tree_construction_main ($) {
4969                        thead => 1, tr => 1,                        thead => 1, tr => 1,
4970                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
4971                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
4972                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
4973                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
4974                ## Ignore the token                ## Ignore the token
4975                !!!next-token;                !!!next-token;
4976                redo B;                next B;
4977              } else {              } else {
4978                  !!!cp ('t193');
4979                #                #
4980              }              }
4981          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4982            for my $entry (@{$self->{open_elements}}) {
4983              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
4984                !!!cp ('t75');
4985                !!!parse-error (type => 'in body:#eof', token => $token);
4986                last;
4987              }
4988            }
4989    
4990            ## Stop parsing.
4991            last B;
4992        } else {        } else {
4993          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
4994        }        }
# Line 3372  sub _tree_construction_main ($) { Line 4997  sub _tree_construction_main ($) {
4997        #        #
4998      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
4999        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5000              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5001                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5002              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5003                                
5004                unless (length $token->{data}) {            unless (length $token->{data}) {
5005                  !!!next-token;              !!!cp ('t194');
5006                  redo B;              !!!next-token;
5007                }              next B;
5008              }            } else {
5009                !!!cp ('t195');
5010              }
5011            }
5012    
5013              !!!parse-error (type => 'in table:#character');              !!!parse-error (type => 'in table:#character', token => $token);
5014    
5015              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5016              ## 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 5018  sub _tree_construction_main ($) {
5018              ## result in a new Text node.              ## result in a new Text node.
5019              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5020                            
5021              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]}) {  
5022                # MUST                # MUST
5023                my $foster_parent_element;                my $foster_parent_element;
5024                my $next_sibling;                my $next_sibling;
5025                my $prev_sibling;                my $prev_sibling;
5026                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5027                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5028                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5029                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5030                        !!!cp ('t196');
5031                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5032                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5033                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5034                    } else {                    } else {
5035                        !!!cp ('t197');
5036                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5037                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5038                    }                    }
# Line 3416  sub _tree_construction_main ($) { Line 5044  sub _tree_construction_main ($) {
5044                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5045                if (defined $prev_sibling and                if (defined $prev_sibling and
5046                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5047                    !!!cp ('t198');
5048                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5049                } else {                } else {
5050                    !!!cp ('t199');
5051                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5052                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5053                     $next_sibling);                     $next_sibling);
5054                }                }
5055              } else {            $open_tables->[-1]->[1] = 1; # tainted
5056                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5057              }            !!!cp ('t200');
5058              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5059            }
5060                            
5061              !!!next-token;          !!!next-token;
5062              redo B;          next B;
5063        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5064              if ({              if ({
5065                   tr => ($self->{insertion_mode} != IN_ROW_IM),                   tr => ($self->{insertion_mode} != IN_ROW_IM),
# Line 3435  sub _tree_construction_main ($) { Line 5067  sub _tree_construction_main ($) {
5067                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5068                if ($self->{insertion_mode} == IN_TABLE_IM) {                if ($self->{insertion_mode} == IN_TABLE_IM) {
5069                  ## Clear back to table context                  ## Clear back to table context
5070                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5071                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5072                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t201');
5073                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5074                  }                  }
5075                                    
5076                  !!!insert-element ('tbody');                  !!!insert-element ('tbody',, $token);
5077                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5078                  ## reprocess in the "in table body" insertion mode...                  ## reprocess in the "in table body" insertion mode...
5079                }                }
5080    
5081                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5082                  unless ($token->{tag_name} eq 'tr') {                  unless ($token->{tag_name} eq 'tr') {
5083                    !!!parse-error (type => 'missing start tag:tr');                    !!!cp ('t202');
5084                      !!!parse-error (type => 'missing start tag:tr', token => $token);
5085                  }                  }
5086                                    
5087                  ## Clear back to table body context                  ## Clear back to table body context
5088                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5089                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5090                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t203');
5091                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5092                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5093                  }                  }
5094                                    
5095                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5096                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5097                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5098                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5099                      !!!nack ('t204');
5100                    !!!next-token;                    !!!next-token;
5101                    redo B;                    next B;
5102                  } else {                  } else {
5103                    !!!insert-element ('tr');                    !!!cp ('t205');
5104                      !!!insert-element ('tr',, $token);
5105                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5106                  }                  }
5107                  } else {
5108                    !!!cp ('t206');
5109                }                }
5110    
5111                ## Clear back to table row context                ## Clear back to table row context
5112                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5113                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5114                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5115                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5116                }                }
5117                                
5118                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5119                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5120    
5121                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5122                                
5123                  !!!nack ('t207.1');
5124                !!!next-token;                !!!next-token;
5125                redo B;                next B;
5126              } elsif ({              } elsif ({
5127                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5128                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3496  sub _tree_construction_main ($) { Line 5134  sub _tree_construction_main ($) {
5134                  my $i;                  my $i;
5135                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5136                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5137                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5138                        !!!cp ('t208');
5139                      $i = $_;                      $i = $_;
5140                      last INSCOPE;                      last INSCOPE;
5141                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5142                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5143                      last INSCOPE;                      last INSCOPE;
5144                    }                    }
5145                  } # INSCOPE                  } # INSCOPE
5146                  unless (defined $i) {                  unless (defined $i) {
5147                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5148    ## TODO: This type is wrong.
5149                      !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name}, token => $token);
5150                    ## Ignore the token                    ## Ignore the token
5151                      !!!nack ('t210.1');
5152                    !!!next-token;                    !!!next-token;
5153                    redo B;                    next B;
5154                  }                  }
5155                                    
5156                  ## Clear back to table row context                  ## Clear back to table row context
5157                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5158                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5159                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5160                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5161                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5162                  }                  }
5163                                    
5164                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5165                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5166                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5167                      !!!cp ('t212');
5168                    ## reprocess                    ## reprocess
5169                    redo B;                    !!!ack-later;
5170                      next B;
5171                  } else {                  } else {
5172                      !!!cp ('t213');
5173                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5174                  }                  }
5175                }                }
# Line 3535  sub _tree_construction_main ($) { Line 5179  sub _tree_construction_main ($) {
5179                  my $i;                  my $i;
5180                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5181                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5182                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5183                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5184                      $i = $_;                      $i = $_;
5185                      last INSCOPE;                      last INSCOPE;
5186                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5187                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5188                      last INSCOPE;                      last INSCOPE;
5189                    }                    }
5190                  } # INSCOPE                  } # INSCOPE
5191                  unless (defined $i) {                  unless (defined $i) {
5192                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5193    ## TODO: This erorr type ios wrong.
5194                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5195                    ## Ignore the token                    ## Ignore the token
5196                      !!!nack ('t216.1');
5197                    !!!next-token;                    !!!next-token;
5198                    redo B;                    next B;
5199                  }                  }
5200    
5201                  ## Clear back to table body context                  ## Clear back to table body context
5202                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5203                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5204                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5205                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5206                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5207                  }                  }
5208                                    
# Line 3571  sub _tree_construction_main ($) { Line 5216  sub _tree_construction_main ($) {
5216                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5217                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5218                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5219                  } else {
5220                    !!!cp ('t218');
5221                }                }
5222    
5223                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5224                  ## Clear back to table context                  ## Clear back to table context
5225                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5226                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5227                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5228                      ## ISSUE: Can this state be reached?
5229                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5230                  }                  }
5231                                    
5232                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5233                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5234                  ## reprocess                  ## reprocess
5235                  redo B;                  !!!ack-later;
5236                    next B;
5237                } elsif ({                } elsif ({
5238                          caption => 1,                          caption => 1,
5239                          colgroup => 1,                          colgroup => 1,
5240                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5241                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5242                  ## Clear back to table context                  ## Clear back to table context
5243                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5244                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5245                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5246                      ## ISSUE: Can this state be reached?
5247                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5248                  }                  }
5249                                    
5250                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5251                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5252                                    
5253                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5254                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5255                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5256                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3609  sub _tree_construction_main ($) { Line 5259  sub _tree_construction_main ($) {
5259                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5260                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5261                  !!!next-token;                  !!!next-token;
5262                  redo B;                  !!!nack ('t220.1');
5263                    next B;
5264                } else {                } else {
5265                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5266                }                }
5267              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5268                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5269                                  value => $self->{open_elements}->[-1]->[0]
5270                                      ->manakai_local_name,
5271                                  token => $token);
5272    
5273                ## As if </table>                ## As if </table>
5274                ## have a table element in table scope                ## have a table element in table scope
5275                my $i;                my $i;
5276                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5277                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5278                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5279                      !!!cp ('t221');
5280                    $i = $_;                    $i = $_;
5281                    last INSCOPE;                    last INSCOPE;
5282                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5283                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5284                    last INSCOPE;                    last INSCOPE;
5285                  }                  }
5286                } # INSCOPE                } # INSCOPE
5287                unless (defined $i) {                unless (defined $i) {
5288                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5289    ## TODO: The following is wrong, maybe.
5290                    !!!parse-error (type => 'unmatched end tag:table', token => $token);
5291                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5292                    !!!nack ('t223.1');
5293                  !!!next-token;                  !!!next-token;
5294                  redo B;                  next B;
5295                }                }
5296                                
5297    ## TODO: Followings are removed from the latest spec.
5298                ## generate implied end tags                ## generate implied end tags
5299                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5300                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5301                     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;  
5302                }                }
5303    
5304                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5305                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5306                    ## NOTE: |<table><tr><table>|
5307                    !!!parse-error (type => 'not closed',
5308                                    value => $self->{open_elements}->[-1]->[0]
5309                                        ->manakai_local_name,
5310                                    token => $token);
5311                  } else {
5312                    !!!cp ('t226');
5313                }                }
5314    
5315                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5316                  pop @{$open_tables};
5317    
5318                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5319    
5320                ## reprocess            ## reprocess
5321                redo B;            !!!ack-later;
5322          } else {            next B;
5323            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5324              if (not $open_tables->[-1]->[1]) { # tainted
5325                !!!cp ('t227.8');
5326                ## NOTE: This is a "as if in head" code clone.
5327                $parse_rcdata->(CDATA_CONTENT_MODEL);
5328                next B;
5329              } else {
5330                !!!cp ('t227.7');
5331                #
5332              }
5333            } elsif ($token->{tag_name} eq 'script') {
5334              if (not $open_tables->[-1]->[1]) { # tainted
5335                !!!cp ('t227.6');
5336                ## NOTE: This is a "as if in head" code clone.
5337                $script_start_tag->();
5338                next B;
5339              } else {
5340                !!!cp ('t227.5');
5341                #
5342              }
5343            } elsif ($token->{tag_name} eq 'input') {
5344              if (not $open_tables->[-1]->[1]) { # tainted
5345                if ($token->{attributes}->{type}) { ## TODO: case
5346                  my $type = lc $token->{attributes}->{type}->{value};
5347                  if ($type eq 'hidden') {
5348                    !!!cp ('t227.3');
5349                    !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5350    
5351            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5352    
5353                    ## TODO: form element pointer
5354    
5355                    pop @{$self->{open_elements}};
5356    
5357                    !!!next-token;
5358                    !!!ack ('t227.2.1');
5359                    next B;
5360                  } else {
5361                    !!!cp ('t227.2');
5362                    #
5363                  }
5364                } else {
5365                  !!!cp ('t227.1');
5366                  #
5367                }
5368              } else {
5369                !!!cp ('t227.4');
5370                #
5371              }
5372            } else {
5373              !!!cp ('t227');
5374            #            #
5375          }          }
5376    
5377            !!!parse-error (type => 'in table:'.$token->{tag_name}, token => $token);
5378    
5379            $insert = $insert_to_foster;
5380            #
5381        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5382              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5383                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3674  sub _tree_construction_main ($) { Line 5385  sub _tree_construction_main ($) {
5385                my $i;                my $i;
5386                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5387                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5388                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5389                      !!!cp ('t228');
5390                    $i = $_;                    $i = $_;
5391                    last INSCOPE;                    last INSCOPE;
5392                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5393                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5394                    last INSCOPE;                    last INSCOPE;
5395                  }                  }
5396                } # INSCOPE                } # INSCOPE
5397                unless (defined $i) {                unless (defined $i) {
5398                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5399                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5400                  ## Ignore the token                  ## Ignore the token
5401                    !!!nack ('t230.1');
5402                  !!!next-token;                  !!!next-token;
5403                  redo B;                  next B;
5404                  } else {
5405                    !!!cp ('t232');
5406                }                }
5407    
5408                ## Clear back to table row context                ## Clear back to table row context
5409                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5410                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5411                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5412                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5413                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5414                }                }
5415    
5416                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5417                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5418                !!!next-token;                !!!next-token;
5419                redo B;                !!!nack ('t231.1');
5420                  next B;
5421              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5422                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5423                  ## As if </tr>                  ## As if </tr>
# Line 3709  sub _tree_construction_main ($) { Line 5425  sub _tree_construction_main ($) {
5425                  my $i;                  my $i;
5426                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5427                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5428                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5429                        !!!cp ('t233');
5430                      $i = $_;                      $i = $_;
5431                      last INSCOPE;                      last INSCOPE;
5432                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5433                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5434                      last INSCOPE;                      last INSCOPE;
5435                    }                    }
5436                  } # INSCOPE                  } # INSCOPE
5437                  unless (defined $i) {                  unless (defined $i) {
5438                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5439    ## TODO: The following is wrong.
5440                      !!!parse-error (type => 'unmatched end tag:'.$token->{type}, token => $token);
5441                    ## Ignore the token                    ## Ignore the token
5442                      !!!nack ('t236.1');
5443                    !!!next-token;                    !!!next-token;
5444                    redo B;                    next B;
5445                  }                  }
5446                                    
5447                  ## Clear back to table row context                  ## Clear back to table row context
5448                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5449                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5450                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5451                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5452                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5453                  }                  }
5454                                    
# Line 3743  sub _tree_construction_main ($) { Line 5462  sub _tree_construction_main ($) {
5462                  my $i;                  my $i;
5463                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5464                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5465                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5466                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5467                      $i = $_;                      $i = $_;
5468                      last INSCOPE;                      last INSCOPE;
5469                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5470                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5471                      last INSCOPE;                      last INSCOPE;
5472                    }                    }
5473                  } # INSCOPE                  } # INSCOPE
5474                  unless (defined $i) {                  unless (defined $i) {
5475                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5476                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5477                    ## Ignore the token                    ## Ignore the token
5478                      !!!nack ('t239.1');
5479                    !!!next-token;                    !!!next-token;
5480                    redo B;                    next B;
5481                  }                  }
5482                                    
5483                  ## Clear back to table body context                  ## Clear back to table body context
5484                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5485                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5486                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5487                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5488                  }                  }
5489                                    
# Line 3781  sub _tree_construction_main ($) { Line 5499  sub _tree_construction_main ($) {
5499                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5500                }                }
5501    
5502                  ## NOTE: </table> in the "in table" insertion mode.
5503                  ## When you edit the code fragment below, please ensure that
5504                  ## the code for <table> in the "in table" insertion mode
5505                  ## is synced with it.
5506    
5507                ## have a table element in table scope                ## have a table element in table scope
5508                my $i;                my $i;
5509                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5510                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5511                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5512                      !!!cp ('t241');
5513                    $i = $_;                    $i = $_;
5514                    last INSCOPE;                    last INSCOPE;
5515                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5516                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5517                    last INSCOPE;                    last INSCOPE;
5518                  }                  }
5519                } # INSCOPE                } # INSCOPE
5520                unless (defined $i) {                unless (defined $i) {
5521                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5522                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5523                  ## Ignore the token                  ## Ignore the token
5524                    !!!nack ('t243.1');
5525                  !!!next-token;                  !!!next-token;
5526                  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]);  
5527                }                }
5528                                    
5529                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5530                  pop @{$open_tables};
5531                                
5532                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5533                                
5534                !!!next-token;                !!!next-token;
5535                redo B;                next B;
5536              } elsif ({              } elsif ({
5537                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5538                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3832  sub _tree_construction_main ($) { Line 5542  sub _tree_construction_main ($) {
5542                  my $i;                  my $i;
5543                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5544                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5545                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5546                        !!!cp ('t247');
5547                      $i = $_;                      $i = $_;
5548                      last INSCOPE;                      last INSCOPE;
5549                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5550                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5551                      last INSCOPE;                      last INSCOPE;
5552                    }                    }
5553                  } # INSCOPE                  } # INSCOPE
5554                    unless (defined $i) {                    unless (defined $i) {
5555                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5556                        !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5557                      ## Ignore the token                      ## Ignore the token
5558                        !!!nack ('t249.1');
5559                      !!!next-token;                      !!!next-token;
5560                      redo B;                      next B;
5561                    }                    }
5562                                    
5563                  ## As if </tr>                  ## As if </tr>
# Line 3853  sub _tree_construction_main ($) { Line 5565  sub _tree_construction_main ($) {
5565                  my $i;                  my $i;
5566                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5567                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5568                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5569                        !!!cp ('t250');
5570                      $i = $_;                      $i = $_;
5571                      last INSCOPE;                      last INSCOPE;
5572                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5573                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
5574                      last INSCOPE;                      last INSCOPE;
5575                    }                    }
5576                  } # INSCOPE                  } # INSCOPE
5577                    unless (defined $i) {                    unless (defined $i) {
5578                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
5579                        !!!parse-error (type => 'unmatched end tag:tr', token => $token);
5580                      ## Ignore the token                      ## Ignore the token
5581                        !!!nack ('t252.1');
5582                      !!!next-token;                      !!!next-token;
5583                      redo B;                      next B;
5584                    }                    }
5585                                    
5586                  ## Clear back to table row context                  ## Clear back to table row context
5587                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5588                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5589                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
5590                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5591                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5592                  }                  }
5593                                    
# Line 3886  sub _tree_construction_main ($) { Line 5600  sub _tree_construction_main ($) {
5600                my $i;                my $i;
5601                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5602                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5603                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5604                      !!!cp ('t254');
5605                    $i = $_;                    $i = $_;
5606                    last INSCOPE;                    last INSCOPE;
5607                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5608                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
5609                    last INSCOPE;                    last INSCOPE;
5610                  }                  }
5611                } # INSCOPE                } # INSCOPE
5612                unless (defined $i) {                unless (defined $i) {
5613                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
5614                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5615                  ## Ignore the token                  ## Ignore the token
5616                    !!!nack ('t256.1');
5617                  !!!next-token;                  !!!next-token;
5618                  redo B;                  next B;
5619                }                }
5620    
5621                ## Clear back to table body context                ## Clear back to table body context
5622                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5623                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
5624                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
5625                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
5626                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5627                }                }
5628    
5629                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5630                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5631                  !!!nack ('t257.1');
5632                !!!next-token;                !!!next-token;
5633                redo B;                next B;
5634              } elsif ({              } elsif ({
5635                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
5636                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
5637                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
5638                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
5639                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5640                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
5641                ## Ignore the token            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5642                !!!next-token;            ## Ignore the token
5643                redo B;            !!!nack ('t258.1');
5644               !!!next-token;
5645              next B;
5646          } else {          } else {
5647            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!cp ('t259');
5648              !!!parse-error (type => 'in table:/'.$token->{tag_name}, token => $token);
5649    
5650            $insert = $insert_to_foster;            $insert = $insert_to_foster;
5651            #            #
5652          }          }
5653          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5654            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5655                    @{$self->{open_elements}} == 1) { # redundant, maybe
5656              !!!parse-error (type => 'in body:#eof', token => $token);
5657              !!!cp ('t259.1');
5658              #
5659            } else {
5660              !!!cp ('t259.2');
5661              #
5662            }
5663    
5664            ## Stop parsing
5665            last B;
5666        } else {        } else {
5667          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5668        }        }
# Line 3938  sub _tree_construction_main ($) { Line 5671  sub _tree_construction_main ($) {
5671              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5672                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5673                unless (length $token->{data}) {                unless (length $token->{data}) {
5674                    !!!cp ('t260');
5675                  !!!next-token;                  !!!next-token;
5676                  redo B;                  next B;
5677                }                }
5678              }              }
5679                            
5680                !!!cp ('t261');
5681              #              #
5682            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5683              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
5684                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
5685                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5686                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5687                  !!!ack ('t262.1');
5688                !!!next-token;                !!!next-token;
5689                redo B;                next B;
5690              } else {              } else {
5691                  !!!cp ('t263');
5692                #                #
5693              }              }
5694            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
5695              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
5696                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5697                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
5698                    !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5699                  ## Ignore the token                  ## Ignore the token
5700                  !!!next-token;                  !!!next-token;
5701                  redo B;                  next B;
5702                } else {                } else {
5703                    !!!cp ('t265');
5704                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
5705                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5706                  !!!next-token;                  !!!next-token;
5707                  redo B;                              next B;            
5708                }                }
5709              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
5710                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
5711                  !!!parse-error (type => 'unmatched end tag:col', token => $token);
5712                ## Ignore the token                ## Ignore the token
5713                !!!next-token;                !!!next-token;
5714                redo B;                next B;
5715              } else {              } else {
5716                  !!!cp ('t267');
5717                #                #
5718              }              }
5719            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5720              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
5721            }              @{$self->{open_elements}} == 1) { # redundant, maybe
5722              !!!cp ('t270.2');
5723              ## Stop parsing.
5724              last B;
5725            } else {
5726              ## NOTE: As if </colgroup>.
5727              !!!cp ('t270.1');
5728              pop @{$self->{open_elements}}; # colgroup
5729              $self->{insertion_mode} = IN_TABLE_IM;
5730              ## Reprocess.
5731              next B;
5732            }
5733          } else {
5734            die "$0: $token->{type}: Unknown token type";
5735          }
5736    
5737            ## As if </colgroup>            ## As if </colgroup>
5738            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
5739              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
5740    ## TODO: Wrong error type?
5741                !!!parse-error (type => 'unmatched end tag:colgroup', token => $token);
5742              ## Ignore the token              ## Ignore the token
5743                !!!nack ('t269.1');
5744              !!!next-token;              !!!next-token;
5745              redo B;              next B;
5746            } else {            } else {
5747                !!!cp ('t270');
5748              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
5749              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
5750                !!!ack-later;
5751              ## reprocess              ## reprocess
5752              redo B;              next B;
5753            }            }
5754      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
5755        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5756            !!!cp ('t271');
5757          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5758          !!!next-token;          !!!next-token;
5759          redo B;          next B;
5760        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5761              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
5762                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5763                  ## As if </option>              !!!cp ('t272');
5764                  pop @{$self->{open_elements}};              ## As if </option>
5765                }              pop @{$self->{open_elements}};
5766              } else {
5767                !!!cp ('t273');
5768              }
5769    
5770                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5771                !!!next-token;            !!!nack ('t273.1');
5772                redo B;            !!!next-token;
5773              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
5774                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
5775                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5776                  pop @{$self->{open_elements}};              !!!cp ('t274');
5777                }              ## As if </option>
5778                pop @{$self->{open_elements}};
5779              } else {
5780                !!!cp ('t275');
5781              }
5782    
5783                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5784                  ## As if </optgroup>              !!!cp ('t276');
5785                  pop @{$self->{open_elements}};              ## As if </optgroup>
5786                }              pop @{$self->{open_elements}};
5787              } else {
5788                !!!cp ('t277');
5789              }
5790    
5791                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5792                !!!next-token;            !!!nack ('t277.1');
5793                redo B;            !!!next-token;
5794              } elsif ($token->{tag_name} eq 'select') {            next B;
5795                !!!parse-error (type => 'not closed:select');          } elsif ($token->{tag_name} eq 'select' or
5796                ## As if </select> instead                   $token->{tag_name} eq 'input' or
5797                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5798                my $i;                    {
5799                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
5800                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
5801                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
5802                    $i = $_;                    }->{$token->{tag_name}})) {
5803                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
5804                  } elsif ({            !!!parse-error (type => 'not closed:select', token => $token);
5805                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
5806                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
5807                    last INSCOPE;            ## have an element in table scope
5808                  }            my $i;
5809                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5810                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
5811                  !!!parse-error (type => 'unmatched end tag:select');              if ($node->[1] & SELECT_EL) {
5812                  ## Ignore the token                !!!cp ('t278');
5813                  !!!next-token;                $i = $_;
5814                  redo B;                last INSCOPE;
5815                }              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5816                  !!!cp ('t279');
5817                  last INSCOPE;
5818                }
5819              } # INSCOPE
5820              unless (defined $i) {
5821                !!!cp ('t280');
5822                !!!parse-error (type => 'unmatched end tag:select', token => $token);
5823                ## Ignore the token
5824                !!!nack ('t280.1');
5825                !!!next-token;
5826                next B;
5827              }
5828                                
5829                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
5830              splice @{$self->{open_elements}}, $i;
5831    
5832                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5833    
5834                !!!next-token;            if ($token->{tag_name} eq 'select') {
5835                redo B;              !!!nack ('t281.2');
5836                !!!next-token;
5837                next B;
5838              } else {
5839                !!!cp ('t281.1');
5840                !!!ack-later;
5841                ## Reprocess the token.
5842                next B;
5843              }
5844          } else {          } else {
5845            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
5846              !!!parse-error (type => 'in select:'.$token->{tag_name}, token => $token);
5847            ## Ignore the token            ## Ignore the token
5848              !!!nack ('t282.1');
5849            !!!next-token;            !!!next-token;
5850            redo B;            next B;
5851          }          }
5852        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5853              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
5854                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
5855                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
5856                  ## As if </option>              !!!cp ('t283');
5857                  splice @{$self->{open_elements}}, -2;              ## As if </option>
5858                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
5859                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
5860                } else {              !!!cp ('t284');
5861                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
5862                  ## Ignore the token            } else {
5863                }              !!!cp ('t285');
5864                !!!next-token;              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5865                redo B;              ## Ignore the token
5866              } elsif ($token->{tag_name} eq 'option') {            }
5867                if ($self->{open_elements}->[-1]->[1] eq 'option') {            !!!nack ('t285.1');
5868                  pop @{$self->{open_elements}};            !!!next-token;
5869                } else {            next B;
5870                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'option') {
5871                  ## Ignore the token            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
5872                }              !!!cp ('t286');
5873                !!!next-token;              pop @{$self->{open_elements}};
5874                redo B;            } else {
5875              } elsif ($token->{tag_name} eq 'select') {              !!!cp ('t287');
5876                ## have an element in table scope              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5877                my $i;              ## Ignore the token
5878                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            }
5879                  my $node = $self->{open_elements}->[$_];            !!!nack ('t287.1');
5880                  if ($node->[1] eq $token->{tag_name}) {            !!!next-token;
5881                    $i = $_;            next B;
5882                    last INSCOPE;          } elsif ($token->{tag_name} eq 'select') {
5883                  } elsif ({            ## have an element in table scope
5884                            table => 1, html => 1,            my $i;
5885                           }->{$node->[1]}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5886                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
5887                  }              if ($node->[1] & SELECT_EL) {
5888                } # INSCOPE                !!!cp ('t288');
5889                unless (defined $i) {                $i = $_;
5890                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                last INSCOPE;
5891                  ## Ignore the token              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5892                  !!!next-token;                !!!cp ('t289');
5893                  redo B;                last INSCOPE;
5894                }              }
5895              } # INSCOPE
5896              unless (defined $i) {
5897                !!!cp ('t290');
5898                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5899                ## Ignore the token
5900                !!!nack ('t290.1');
5901                !!!next-token;
5902                next B;
5903              }
5904                                
5905                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
5906              splice @{$self->{open_elements}}, $i;
5907    
5908                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5909    
5910                !!!next-token;            !!!nack ('t291.1');
5911                redo B;            !!!next-token;
5912              } elsif ({            next B;
5913                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
5914                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
5915                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
5916                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
5917                     }->{$token->{tag_name}}) {
5918    ## TODO: The following is wrong?
5919              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
5920                                
5921                ## have an element in table scope            ## have an element in table scope
5922                my $i;            my $i;
5923                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5924                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5925                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5926                    $i = $_;                !!!cp ('t292');
5927                    last INSCOPE;                $i = $_;
5928                  } elsif ({                last INSCOPE;
5929                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5930                           }->{$node->[1]}) {                !!!cp ('t293');
5931                    last INSCOPE;                last INSCOPE;
5932                  }              }
5933                } # INSCOPE            } # INSCOPE
5934                unless (defined $i) {            unless (defined $i) {
5935                  ## Ignore the token              !!!cp ('t294');
5936                  !!!next-token;              ## Ignore the token
5937                  redo B;              !!!nack ('t294.1');
5938                }              !!!next-token;
5939                next B;
5940              }
5941                                
5942                ## As if </select>            ## As if </select>
5943                ## have an element in table scope            ## have an element in table scope
5944                undef $i;            undef $i;
5945                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5946                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
5947                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
5948                    $i = $_;                !!!cp ('t295');
5949                    last INSCOPE;                $i = $_;
5950                  } elsif ({                last INSCOPE;
5951                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
5952                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
5953                    last INSCOPE;                !!!cp ('t296');
5954                  }                last INSCOPE;
5955                } # INSCOPE              }
5956                unless (defined $i) {            } # INSCOPE
5957                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
5958                  ## Ignore the </select> token              !!!cp ('t297');
5959                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
5960                  redo B;              !!!parse-error (type => 'unmatched end tag:select', token => $token);
5961                }              ## Ignore the </select> token
5962                !!!nack ('t297.1');
5963                !!!next-token; ## TODO: ok?
5964                next B;
5965              }
5966                                
5967                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
5968              splice @{$self->{open_elements}}, $i;
5969    
5970                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
5971    
5972                ## reprocess            !!!ack-later;
5973                redo B;            ## reprocess
5974              next B;
5975          } else {          } else {
5976            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
5977              !!!parse-error (type => 'in select:/'.$token->{tag_name}, token => $token);
5978            ## Ignore the token            ## Ignore the token
5979              !!!nack ('t299.3');
5980            !!!next-token;            !!!next-token;
5981            redo B;            next B;
5982          }          }
5983          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5984            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
5985                    @{$self->{open_elements}} == 1) { # redundant, maybe
5986              !!!cp ('t299.1');
5987              !!!parse-error (type => 'in body:#eof', token => $token);
5988            } else {
5989              !!!cp ('t299.2');
5990            }
5991    
5992            ## Stop parsing.
5993            last B;
5994        } else {        } else {
5995          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5996        }        }
# Line 4175  sub _tree_construction_main ($) { Line 6004  sub _tree_construction_main ($) {
6004            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6005                        
6006            unless (length $token->{data}) {            unless (length $token->{data}) {
6007                !!!cp ('t300');
6008              !!!next-token;              !!!next-token;
6009              redo B;              next B;
6010            }            }
6011          }          }
6012                    
6013          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6014            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6015              !!!parse-error (type => 'after html:#character', token => $token);
6016    
6017            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6018            } else {
6019              !!!cp ('t302');
6020          }          }
6021                    
6022          ## "after body" insertion mode          ## "after body" insertion mode
6023          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#character', token => $token);
6024    
6025          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6026          ## reprocess          ## reprocess
6027          redo B;          next B;
6028        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6029          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6030            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6031              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6032                        
6033            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6034            } else {
6035              !!!cp ('t304');
6036          }          }
6037    
6038          ## "after body" insertion mode          ## "after body" insertion mode
6039          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body:'.$token->{tag_name}, token => $token);
6040    
6041          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6042            !!!ack-later;
6043          ## reprocess          ## reprocess
6044          redo B;          next B;
6045        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6046          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6047            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6048              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6049                        
6050            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6051            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6052            } else {
6053              !!!cp ('t306');
6054          }          }
6055    
6056          ## "after body" insertion mode          ## "after body" insertion mode
6057          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6058            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6059              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6060                !!!parse-error (type => 'unmatched end tag:html', token => $token);
6061              ## Ignore the token              ## Ignore the token
6062              !!!next-token;              !!!next-token;
6063              redo B;              next B;
6064            } else {            } else {
6065                !!!cp ('t308');
6066              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6067              !!!next-token;              !!!next-token;
6068              redo B;              next B;
6069            }            }
6070          } else {          } else {
6071            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6072              !!!parse-error (type => 'after body:/'.$token->{tag_name}, token => $token);
6073    
6074            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6075            ## reprocess            ## reprocess
6076            redo B;            next B;
6077          }          }
6078          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6079            !!!cp ('t309.2');
6080            ## Stop parsing
6081            last B;
6082        } else {        } else {
6083          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6084        }        }
# Line 4241  sub _tree_construction_main ($) { Line 6088  sub _tree_construction_main ($) {
6088            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6089                        
6090            unless (length $token->{data}) {            unless (length $token->{data}) {
6091                !!!cp ('t310');
6092              !!!next-token;              !!!next-token;
6093              redo B;              next B;
6094            }            }
6095          }          }
6096                    
6097          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6098            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6099              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6100                !!!parse-error (type => 'in frameset:#character', token => $token);
6101            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6102              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6103                !!!parse-error (type => 'after frameset:#character', token => $token);
6104            } else { # "after html frameset"            } else { # "after html frameset"
6105              !!!parse-error (type => 'after html:#character');              !!!cp ('t313');
6106                !!!parse-error (type => 'after html:#character', token => $token);
6107    
6108              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6109              ## Reprocess in the "main" phase, "after frameset"...              ## Reprocess in the "after frameset" insertion mode.
6110              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#character', token => $token);
6111            }            }
6112                        
6113            ## Ignore the token.            ## Ignore the token.
6114            if (length $token->{data}) {            if (length $token->{data}) {
6115                !!!cp ('t314');
6116              ## reprocess the rest of characters              ## reprocess the rest of characters
6117            } else {            } else {
6118                !!!cp ('t315');
6119              !!!next-token;              !!!next-token;
6120            }            }
6121            redo B;            next B;
6122          }          }
6123                    
6124          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6125        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6126          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6127            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t316');
6128              !!!parse-error (type => 'after html:'.$token->{tag_name}, token => $token);
6129    
6130            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6131            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6132          }          } else {
6133              !!!cp ('t317');
6134            }
6135    
6136          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6137              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6138            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6139              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6140              !!!nack ('t318.1');
6141            !!!next-token;            !!!next-token;
6142            redo B;            next B;
6143          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6144                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6145            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6146              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6147            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6148              !!!ack ('t319.1');
6149            !!!next-token;            !!!next-token;
6150            redo B;            next B;
6151          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6152              !!!cp ('t320');
6153            ## NOTE: As if in body.            ## NOTE: As if in body.
6154            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6155            redo B;            next B;
6156          } else {          } else {
6157            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6158              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6159                !!!parse-error (type => 'in frameset:'.$token->{tag_name}, token => $token);
6160            } else {            } else {
6161              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!cp ('t322');
6162                !!!parse-error (type => 'after frameset:'.$token->{tag_name}, token => $token);
6163            }            }
6164            ## Ignore the token            ## Ignore the token
6165              !!!nack ('t322.1');
6166            !!!next-token;            !!!next-token;
6167            redo B;            next B;
6168          }          }
6169        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6170          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {          if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
6171            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t323');
6172              !!!parse-error (type => 'after html:/'.$token->{tag_name}, token => $token);
6173    
6174            $self->{insertion_mode} = AFTER_FRAMESET_IM;            $self->{insertion_mode} = AFTER_FRAMESET_IM;
6175            ## Process in the "main" phase, "after frameset" insertion mode...            ## Process in the "after frameset" insertion mode.
6176            } else {
6177              !!!cp ('t324');
6178          }          }
6179    
6180          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6181              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6182            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6183                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6184              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6185                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6186              ## Ignore the token              ## Ignore the token
6187              !!!next-token;              !!!next-token;
6188            } else {            } else {
6189                !!!cp ('t326');
6190              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6191              !!!next-token;              !!!next-token;
6192            }            }
6193    
6194            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6195                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6196                !!!cp ('t327');
6197              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6198              } else {
6199                !!!cp ('t328');
6200            }            }
6201            redo B;            next B;
6202          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6203                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6204              !!!cp ('t329');
6205            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6206            !!!next-token;            !!!next-token;
6207            redo B;            next B;
6208          } else {          } else {
6209            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6210              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6211                !!!parse-error (type => 'in frameset:/'.$token->{tag_name}, token => $token);
6212            } else {            } else {
6213              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!cp ('t331');
6214                !!!parse-error (type => 'after frameset:/'.$token->{tag_name}, token => $token);
6215            }            }
6216            ## Ignore the token            ## Ignore the token
6217            !!!next-token;            !!!next-token;
6218            redo B;            next B;
6219            }
6220          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6221            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6222                    @{$self->{open_elements}} == 1) { # redundant, maybe
6223              !!!cp ('t331.1');
6224              !!!parse-error (type => 'in body:#eof', token => $token);
6225            } else {
6226              !!!cp ('t331.2');
6227          }          }
6228            
6229            ## Stop parsing
6230            last B;
6231        } else {        } else {
6232          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6233        }        }
# Line 4354  sub _tree_construction_main ($) { Line 6240  sub _tree_construction_main ($) {
6240      ## "in body" insertion mode      ## "in body" insertion mode
6241      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6242        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6243            !!!cp ('t332');
6244          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6245          $script_start_tag->($insert);          $script_start_tag->();
6246          redo B;          next B;
6247        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6248            !!!cp ('t333');
6249          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6250          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6251          redo B;          next B;
6252        } elsif ({        } elsif ({
6253                  base => 1, link => 1,                  base => 1, link => 1,
6254                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6255            !!!cp ('t334');
6256          ## 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
6257          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6258          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6259            !!!ack ('t334.1');
6260          !!!next-token;          !!!next-token;
6261          redo B;          next B;
6262        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6263          ## 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
6264          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6265          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.
6266    
6267          unless ($self->{confident}) {          unless ($self->{confident}) {
6268            my $charset;            if ($token->{attributes}->{charset}) {
6269            if ($token->{attributes}->{charset}) { ## TODO: And if supported              !!!cp ('t335');
6270              $charset = $token->{attributes}->{charset}->{value};              ## NOTE: Whether the encoding is supported or not is handled
6271            }              ## in the {change_encoding} callback.
6272            if ($token->{attributes}->{'http-equiv'}) {              $self->{change_encoding}
6273              ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6274              if ($token->{attributes}->{'http-equiv'}->{value}              
6275                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6276                    ->set_user_data (manakai_has_reference =>
6277                                         $token->{attributes}->{charset}
6278                                             ->{has_reference});
6279              } elsif ($token->{attributes}->{content}) {
6280                if ($token->{attributes}->{content}->{value}
6281                    =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6282                        [\x09-\x0D\x20]*=
6283                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6284                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {
6285                $charset = defined $1 ? $1 : defined $2 ? $2 : $3;                !!!cp ('t336');
6286              } ## TODO: And if supported                ## NOTE: Whether the encoding is supported or not is handled
6287                  ## in the {change_encoding} callback.
6288                  $self->{change_encoding}
6289                      ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6290                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6291                      ->set_user_data (manakai_has_reference =>
6292                                           $token->{attributes}->{content}
6293                                                 ->{has_reference});
6294                }
6295              }
6296            } else {
6297              if ($token->{attributes}->{charset}) {
6298                !!!cp ('t337');
6299                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6300                    ->set_user_data (manakai_has_reference =>
6301                                         $token->{attributes}->{charset}
6302                                             ->{has_reference});
6303              }
6304              if ($token->{attributes}->{content}) {
6305                !!!cp ('t338');
6306                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6307                    ->set_user_data (manakai_has_reference =>
6308                                         $token->{attributes}->{content}
6309                                             ->{has_reference});
6310            }            }
           ## TODO: Change the encoding  
6311          }          }
6312    
6313            !!!ack ('t338.1');
6314          !!!next-token;          !!!next-token;
6315          redo B;          next B;
6316        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6317          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6318          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6319          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6320            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6321        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6322          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body:body', token => $token);
6323                                
6324          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6325              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6326              !!!cp ('t342');
6327            ## Ignore the token            ## Ignore the token
6328          } else {          } else {
6329            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6330            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6331              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6332                  !!!cp ('t343');
6333                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6334                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6335                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6336              }              }
6337            }            }
6338          }          }
6339            !!!nack ('t343.1');
6340          !!!next-token;          !!!next-token;
6341          redo B;          next B;
6342        } elsif ({        } elsif ({
6343                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6344                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6345                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6346                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6347                  pre => 1,                  pre => 1, listing => 1,
6348                    form => 1,
6349                    table => 1,
6350                    hr => 1,
6351                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6352            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6353              !!!cp ('t350');
6354              !!!parse-error (type => 'in form:form', token => $token);
6355              ## Ignore the token
6356              !!!nack ('t350.1');
6357              !!!next-token;
6358              next B;
6359            }
6360    
6361          ## has a p element in scope          ## has a p element in scope
6362          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6363            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6364              !!!back-token;              !!!cp ('t344');
6365              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6366              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6367            } elsif ({                        line => $token->{line}, column => $token->{column}};
6368                      table => 1, caption => 1, td => 1, th => 1,              next B;
6369                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6370                     }->{$_->[1]}) {              !!!cp ('t345');
6371              last INSCOPE;              last INSCOPE;
6372            }            }
6373          } # INSCOPE          } # INSCOPE
6374                        
6375          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6376          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6377              !!!nack ('t346.1');
6378            !!!next-token;            !!!next-token;
6379            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6380              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6381              unless (length $token->{data}) {              unless (length $token->{data}) {
6382                  !!!cp ('t346');
6383                !!!next-token;                !!!next-token;
6384                } else {
6385                  !!!cp ('t349');
6386              }              }
6387              } else {
6388                !!!cp ('t348');
6389            }            }
6390          } else {          } elsif ($token->{tag_name} eq 'form') {
6391              !!!cp ('t347.1');
6392              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6393    
6394              !!!nack ('t347.2');
6395            !!!next-token;            !!!next-token;
6396          }          } elsif ($token->{tag_name} eq 'table') {
6397          redo B;            !!!cp ('t382');
6398        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6399          if (defined $self->{form_element}) {            
6400            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6401            ## Ignore the token  
6402              !!!nack ('t382.1');
6403              !!!next-token;
6404            } elsif ($token->{tag_name} eq 'hr') {
6405              !!!cp ('t386');
6406              pop @{$self->{open_elements}};
6407            
6408              !!!nack ('t386.1');
6409            !!!next-token;            !!!next-token;
           redo B;  
6410          } else {          } else {
6411            ## 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];  
6412            !!!next-token;            !!!next-token;
           redo B;  
6413          }          }
6414        } elsif ($token->{tag_name} eq 'li') {          next B;
6415          ## 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') {  
6416          ## has a p element in scope          ## has a p element in scope
6417          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6418            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6419              !!!back-token;              !!!cp ('t353');
6420              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6421              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6422            } elsif ({                        line => $token->{line}, column => $token->{column}};
6423                      table => 1, caption => 1, td => 1, th => 1,              next B;
6424                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6425                     }->{$_->[1]}) {              !!!cp ('t354');
6426              last INSCOPE;              last INSCOPE;
6427            }            }
6428          } # INSCOPE          } # INSCOPE
# Line 4546  sub _tree_construction_main ($) { Line 6430  sub _tree_construction_main ($) {
6430          ## Step 1          ## Step 1
6431          my $i = -1;          my $i = -1;
6432          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6433            my $li_or_dtdd = {li => {li => 1},
6434                              dt => {dt => 1, dd => 1},
6435                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6436          LI: {          LI: {
6437            ## Step 2            ## Step 2
6438            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6439              if ($i != -1) {              if ($i != -1) {
6440                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6441                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6442                                  value => $self->{open_elements}->[-1]->[0]
6443                                      ->manakai_local_name,
6444                                  token => $token);
6445                } else {
6446                  !!!cp ('t356');
6447              }              }
6448              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6449              last LI;              last LI;
6450              } else {
6451                !!!cp ('t357');
6452            }            }
6453                        
6454            ## Step 3            ## Step 3
6455            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6456                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6457                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6458                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6459                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6460                  not ($node->[1] & DIV_EL)) {
6461                !!!cp ('t358');
6462              last LI;              last LI;
6463            }            }
6464                        
6465              !!!cp ('t359');
6466            ## Step 4            ## Step 4
6467            $i--;            $i--;
6468            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6469            redo LI;            redo LI;
6470          } # LI          } # LI
6471                        
6472          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6473            !!!nack ('t359.1');
6474          !!!next-token;          !!!next-token;
6475          redo B;          next B;
6476        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6477          ## has a p element in scope          ## has a p element in scope
6478          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6479            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6480              !!!back-token;              !!!cp ('t367');
6481              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6482              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6483            } elsif ({                        line => $token->{line}, column => $token->{column}};
6484                      table => 1, caption => 1, td => 1, th => 1,              next B;
6485                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6486                     }->{$_->[1]}) {              !!!cp ('t368');
6487              last INSCOPE;              last INSCOPE;
6488            }            }
6489          } # INSCOPE          } # INSCOPE
6490                        
6491          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6492                        
6493          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6494                        
6495            !!!nack ('t368.1');
6496          !!!next-token;          !!!next-token;
6497          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;  
6498        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6499          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6500            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6501            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6502              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6503                !!!parse-error (type => 'in a:a', token => $token);
6504                            
6505              !!!back-token;              !!!back-token; # <a>
6506              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6507              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6508                $formatting_end_tag->($token);
6509                            
6510              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6511                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6512                    !!!cp ('t372');
6513                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6514                  last AFE2;                  last AFE2;
6515                }                }
6516              } # AFE2              } # AFE2
6517              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6518                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6519                    !!!cp ('t373');
6520                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6521                  last OE;                  last OE;
6522                }                }
6523              } # OE              } # OE
6524              last AFE;              last AFE;
6525            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6526                !!!cp ('t374');
6527              last AFE;              last AFE;
6528            }            }
6529          } # AFE          } # AFE
6530                        
6531          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6532    
6533          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6534          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6535    
6536            !!!nack ('t374.1');
6537          !!!next-token;          !!!next-token;
6538          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;  
6539        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6540          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6541    
6542          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6543          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6544            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6545            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6546              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
6547              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6548              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6549              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6550            } elsif ({                        line => $token->{line}, column => $token->{column}};
6551                      table => 1, caption => 1, td => 1, th => 1,              next B;
6552                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6553                     }->{$node->[1]}) {              !!!cp ('t377');
6554              last INSCOPE;              last INSCOPE;
6555            }            }
6556          } # INSCOPE          } # INSCOPE
6557                    
6558          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6559          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6560                    
6561            !!!nack ('t377.1');
6562          !!!next-token;          !!!next-token;
6563          redo B;          next B;
6564        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6565          ## has a button element in scope          ## has a button element in scope
6566          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6567            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6568            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6569              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
6570              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
6571              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
6572              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6573            } elsif ({                        line => $token->{line}, column => $token->{column}};
6574                      table => 1, caption => 1, td => 1, th => 1,              next B;
6575                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6576                     }->{$node->[1]}) {              !!!cp ('t379');
6577              last INSCOPE;              last INSCOPE;
6578            }            }
6579          } # INSCOPE          } # INSCOPE
6580                        
6581          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6582                        
6583          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6584          push @$active_formatting_elements, ['#marker', ''];  
6585            ## TODO: associate with $self->{form_element} if defined
6586    
         !!!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});  
6587          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
6588            
6589          !!!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;  
             
6590          !!!next-token;          !!!next-token;
6591          redo B;          next B;
6592        } elsif ({        } elsif ({
6593                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
6594                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
6595                  image => 1,                  noembed => 1,
6596                    noframes => 1,
6597                    noscript => 0, ## TODO: 1 if scripting is enabled
6598                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6599          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
6600            !!!parse-error (type => 'image');            !!!cp ('t381');
6601            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
6602            } else {
6603              !!!cp ('t399');
6604          }          }
6605            ## NOTE: There is an "as if in body" code clone.
6606          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
6607          $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;  
6608        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
6609          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
6610                    
6611          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
6612              !!!cp ('t389');
6613            ## Ignore the token            ## Ignore the token
6614              !!!nack ('t389'); ## NOTE: Not acknowledged.
6615            !!!next-token;            !!!next-token;
6616            redo B;            next B;
6617          } else {          } else {
6618            my $at = $token->{attributes};            my $at = $token->{attributes};
6619            my $form_attrs;            my $form_attrs;
# Line 4834  sub _tree_construction_main ($) { Line 6624  sub _tree_construction_main ($) {
6624            delete $at->{prompt};            delete $at->{prompt};
6625            my @tokens = (            my @tokens = (
6626                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
6627                           attributes => $form_attrs},                           attributes => $form_attrs,
6628                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
6629                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
6630                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
6631                            {type => START_TAG_TOKEN, tag_name => 'p',
6632                             line => $token->{line}, column => $token->{column}},
6633                            {type => START_TAG_TOKEN, tag_name => 'label',
6634                             line => $token->{line}, column => $token->{column}},
6635                         );                         );
6636            if ($prompt_attr) {            if ($prompt_attr) {
6637              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
6638                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
6639                               #line => $token->{line}, column => $token->{column},
6640                              };
6641            } else {            } else {
6642                !!!cp ('t391');
6643              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
6644                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
6645                               #line => $token->{line}, column => $token->{column},
6646                              }; # SHOULD
6647              ## TODO: make this configurable              ## TODO: make this configurable
6648            }            }
6649            push @tokens,            push @tokens,
6650                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
6651                             line => $token->{line}, column => $token->{column}},
6652                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
6653                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
6654                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
6655                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
6656                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
6657            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
6658                             line => $token->{line}, column => $token->{column}},
6659                            {type => END_TAG_TOKEN, tag_name => 'form',
6660                             line => $token->{line}, column => $token->{column}};
6661              !!!nack ('t391.1'); ## NOTE: Not acknowledged.
6662            !!!back-token (@tokens);            !!!back-token (@tokens);
6663            redo B;            !!!next-token;
6664              next B;
6665          }          }
6666        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
6667          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
6668          my $el;          my $el;
6669          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
6670                    
6671          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
6672          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4869  sub _tree_construction_main ($) { Line 6675  sub _tree_construction_main ($) {
6675          $insert->($el);          $insert->($el);
6676                    
6677          my $text = '';          my $text = '';
6678            !!!nack ('t392.1');
6679          !!!next-token;          !!!next-token;
6680          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
6681            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
6682            unless (length $token->{data}) {            unless (length $token->{data}) {
6683                !!!cp ('t392');
6684              !!!next-token;              !!!next-token;
6685              } else {
6686                !!!cp ('t393');
6687            }            }
6688            } else {
6689              !!!cp ('t394');
6690          }          }
6691          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
6692              !!!cp ('t395');
6693            $text .= $token->{data};            $text .= $token->{data};
6694            !!!next-token;            !!!next-token;
6695          }          }
6696          if (length $text) {          if (length $text) {
6697              !!!cp ('t396');
6698            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
6699          }          }
6700                    
# Line 4888  sub _tree_construction_main ($) { Line 6702  sub _tree_construction_main ($) {
6702                    
6703          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
6704              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
6705              !!!cp ('t397');
6706            ## Ignore the token            ## Ignore the token
6707          } else {          } else {
6708            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
6709              !!!parse-error (type => 'in RCDATA:#'.$token->{type}, token => $token);
6710          }          }
6711          !!!next-token;          !!!next-token;
6712          redo B;          next B;
6713        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
6714                  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') {  
6715          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6716    
6717            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
6718    
6719            ## "adjust foreign attributes" - done in insert-element-f
6720                    
6721          !!!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);
6722                    
6723          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
6724              pop @{$self->{open_elements}};
6725              !!!ack ('t398.1');
6726            } else {
6727              !!!cp ('t398.2');
6728              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
6729              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
6730              ## mode, "in body" (not "in foreign content") secondary insertion
6731              ## mode, maybe.
6732            }
6733    
6734          !!!next-token;          !!!next-token;
6735          redo B;          next B;
6736        } elsif ({        } elsif ({
6737                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
6738                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
6739                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
6740                  thead => 1, tr => 1,                  thead => 1, tr => 1,
6741                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6742          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
6743            !!!parse-error (type => 'in body:'.$token->{tag_name}, token => $token);
6744          ## Ignore the token          ## Ignore the token
6745            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
6746          !!!next-token;          !!!next-token;
6747          redo B;          next B;
6748                    
6749          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
6750        } else {        } else {
6751            if ($token->{tag_name} eq 'image') {
6752              !!!cp ('t384');
6753              !!!parse-error (type => 'image', token => $token);
6754              $token->{tag_name} = 'img';
6755            } else {
6756              !!!cp ('t385');
6757            }
6758    
6759            ## NOTE: There is an "as if <br>" code clone.
6760          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6761                    
6762          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6763    
6764            if ({
6765                 applet => 1, marquee => 1, object => 1,
6766                }->{$token->{tag_name}}) {
6767              !!!cp ('t380');
6768              push @$active_formatting_elements, ['#marker', ''];
6769              !!!nack ('t380.1');
6770            } elsif ({
6771                      b => 1, big => 1, em => 1, font => 1, i => 1,
6772                      s => 1, small => 1, strile => 1,
6773                      strong => 1, tt => 1, u => 1,
6774                     }->{$token->{tag_name}}) {
6775              !!!cp ('t375');
6776              push @$active_formatting_elements, $self->{open_elements}->[-1];
6777              !!!nack ('t375.1');
6778            } elsif ($token->{tag_name} eq 'input') {
6779              !!!cp ('t388');
6780              ## TODO: associate with $self->{form_element} if defined
6781              pop @{$self->{open_elements}};
6782              !!!ack ('t388.2');
6783            } elsif ({
6784                      area => 1, basefont => 1, bgsound => 1, br => 1,
6785                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
6786                      #image => 1,
6787                     }->{$token->{tag_name}}) {
6788              !!!cp ('t388.1');
6789              pop @{$self->{open_elements}};
6790              !!!ack ('t388.3');
6791            } elsif ($token->{tag_name} eq 'select') {
6792              ## TODO: associate with $self->{form_element} if defined
6793            
6794              if ($self->{insertion_mode} & TABLE_IMS or
6795                  $self->{insertion_mode} & BODY_TABLE_IMS or
6796                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6797                !!!cp ('t400.1');
6798                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
6799              } else {
6800                !!!cp ('t400.2');
6801                $self->{insertion_mode} = IN_SELECT_IM;
6802              }
6803              !!!nack ('t400.3');
6804            } else {
6805              !!!nack ('t402');
6806            }
6807                    
6808          !!!next-token;          !!!next-token;
6809          redo B;          next B;
6810        }        }
6811      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
6812        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
6813          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
6814              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
6815            for (@{$self->{open_elements}}) {          INSCOPE: {
6816              unless ({            for (reverse @{$self->{open_elements}}) {
6817                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
6818                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
6819                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
6820                      }->{$_->[1]}) {                last INSCOPE;
6821                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
6822                  !!!cp ('t405.1');
6823                  last;
6824              }              }
6825            }            }
6826    
6827            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
6828            !!!next-token;                            value => $token->{tag_name}, token => $token);
6829            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
6830            !!!next-token;            !!!next-token;
6831            redo B;            next B;
6832            } # INSCOPE
6833    
6834            for (@{$self->{open_elements}}) {
6835              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
6836                !!!cp ('t403');
6837                !!!parse-error (type => 'not closed',
6838                                value => $_->[0]->manakai_local_name,
6839                                token => $token);
6840                last;
6841              } else {
6842                !!!cp ('t404');
6843              }
6844          }          }
6845    
6846            $self->{insertion_mode} = AFTER_BODY_IM;
6847            !!!next-token;
6848            next B;
6849        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
6850          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
6851            ## up-to-date, though it has same effect as speced.
6852            if (@{$self->{open_elements}} > 1 and
6853                $self->{open_elements}->[1]->[1] & BODY_EL) {
6854            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
6855            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
6856              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
6857                !!!parse-error (type => 'not closed',
6858                                value => $self->{open_elements}->[1]->[0]
6859                                    ->manakai_local_name,
6860                                token => $token);
6861              } else {
6862                !!!cp ('t407');
6863            }            }
6864            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6865            ## reprocess            ## reprocess
6866            redo B;            next B;
6867          } else {          } else {
6868            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
6869              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6870            ## Ignore the token            ## Ignore the token
6871            !!!next-token;            !!!next-token;
6872            redo B;            next B;
6873          }          }
6874        } elsif ({        } elsif ({
6875                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6876                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
6877                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
6878                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
6879                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
6880                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6881          ## has an element in scope          ## has an element in scope
6882          my $i;          my $i;
6883          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6884            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6885            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6886              ## 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;  
             }  
6887              $i = $_;              $i = $_;
6888              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
6889            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6890                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6891              last INSCOPE;              last INSCOPE;
6892            }            }
6893          } # INSCOPE          } # INSCOPE
6894            
6895          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6896            if (defined $i) {            !!!cp ('t413');
6897              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6898            } else {
6899              ## Step 1. generate implied end tags
6900              while ({
6901                      dd => ($token->{tag_name} ne 'dd'),
6902                      dt => ($token->{tag_name} ne 'dt'),
6903                      li => ($token->{tag_name} ne 'li'),
6904                      p => 1,
6905                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
6906                !!!cp ('t409');
6907                pop @{$self->{open_elements}};
6908              }
6909    
6910              ## Step 2.
6911              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6912                      ne $token->{tag_name}) {
6913                !!!cp ('t412');
6914                !!!parse-error (type => 'not closed',
6915                                value => $self->{open_elements}->[-1]->[0]
6916                                    ->manakai_local_name,
6917                                token => $token);
6918            } else {            } else {
6919              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
6920            }            }
6921          }  
6922                      ## Step 3.
         if (defined $i) {  
6923            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6924          } elsif ($token->{tag_name} eq 'p') {  
6925            ## As if <p>, then reprocess the current token            ## Step 4.
6926            my $el;            $clear_up_to_marker->()
6927            !!!create-element ($el, 'p');                if {
6928            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
6929                  }->{$token->{tag_name}};
6930          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
6931          !!!next-token;          !!!next-token;
6932          redo B;          next B;
6933        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
6934            undef $self->{form_element};
6935    
6936          ## has an element in scope          ## has an element in scope
6937            my $i;
6938          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6939            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6940            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
6941              ## generate implied end tags              !!!cp ('t418');
6942              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;  
             }  
6943              last INSCOPE;              last INSCOPE;
6944            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6945                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6946              last INSCOPE;              last INSCOPE;
6947            }            }
6948          } # INSCOPE          } # INSCOPE
6949            
6950          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6951            pop @{$self->{open_elements}};            !!!cp ('t421');
6952          } else {            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6953            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          } else {
6954              ## Step 1. generate implied end tags
6955              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
6956                !!!cp ('t417');
6957                pop @{$self->{open_elements}};
6958              }
6959              
6960              ## Step 2.
6961              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
6962                      ne $token->{tag_name}) {
6963                !!!cp ('t417.1');
6964                !!!parse-error (type => 'not closed',
6965                                value => $self->{open_elements}->[-1]->[0]
6966                                    ->manakai_local_name,
6967                                token => $token);
6968              } else {
6969                !!!cp ('t420');
6970              }  
6971              
6972              ## Step 3.
6973              splice @{$self->{open_elements}}, $i;
6974          }          }
6975    
         undef $self->{form_element};  
6976          !!!next-token;          !!!next-token;
6977          redo B;          next B;
6978        } elsif ({        } elsif ({
6979                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6980                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5069  sub _tree_construction_main ($) { Line 6982  sub _tree_construction_main ($) {
6982          my $i;          my $i;
6983          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6984            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6985            if ({            if ($node->[1] & HEADING_EL) {
6986                 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;  
             }  
6987              $i = $_;              $i = $_;
6988              last INSCOPE;              last INSCOPE;
6989            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
6990                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6991              last INSCOPE;              last INSCOPE;
6992            }            }
6993          } # INSCOPE          } # INSCOPE
6994            
6995          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
6996            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
6997              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
6998            } else {
6999              ## Step 1. generate implied end tags
7000              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7001                !!!cp ('t422');
7002                pop @{$self->{open_elements}};
7003              }
7004              
7005              ## Step 2.
7006              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7007                      ne $token->{tag_name}) {
7008                !!!cp ('t425');
7009                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7010              } else {
7011                !!!cp ('t426');
7012              }
7013    
7014              ## Step 3.
7015              splice @{$self->{open_elements}}, $i;
7016          }          }
7017                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7018          !!!next-token;          !!!next-token;
7019          redo B;          next B;
7020          } elsif ($token->{tag_name} eq 'p') {
7021            ## has an element in scope
7022            my $i;
7023            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7024              my $node = $self->{open_elements}->[$_];
7025              if ($node->[1] & P_EL) {
7026                !!!cp ('t410.1');
7027                $i = $_;
7028                last INSCOPE;
7029              } elsif ($node->[1] & SCOPING_EL) {
7030                !!!cp ('t411.1');
7031                last INSCOPE;
7032              }
7033            } # INSCOPE
7034    
7035            if (defined $i) {
7036              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7037                      ne $token->{tag_name}) {
7038                !!!cp ('t412.1');
7039                !!!parse-error (type => 'not closed',
7040                                value => $self->{open_elements}->[-1]->[0]
7041                                    ->manakai_local_name,
7042                                token => $token);
7043              } else {
7044                !!!cp ('t414.1');
7045              }
7046    
7047              splice @{$self->{open_elements}}, $i;
7048            } else {
7049              !!!cp ('t413.1');
7050              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7051    
7052              !!!cp ('t415.1');
7053              ## As if <p>, then reprocess the current token
7054              my $el;
7055              !!!create-element ($el, $HTML_NS, 'p',, $token);
7056              $insert->($el);
7057              ## NOTE: Not inserted into |$self->{open_elements}|.
7058            }
7059    
7060            !!!next-token;
7061            next B;
7062        } elsif ({        } elsif ({
7063                  a => 1,                  a => 1,
7064                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7065                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7066                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7067                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7068          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7069          redo B;          $formatting_end_tag->($token);
7070            next B;
7071        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7072          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7073            !!!parse-error (type => 'unmatched end tag:br', token => $token);
7074    
7075          ## As if <br>          ## As if <br>
7076          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7077                    
7078          my $el;          my $el;
7079          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7080          $insert->($el);          $insert->($el);
7081                    
7082          ## Ignore the token.          ## Ignore the token.
7083          !!!next-token;          !!!next-token;
7084          redo B;          next B;
7085        } elsif ({        } elsif ({
7086                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7087                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5133  sub _tree_construction_main ($) { Line 7094  sub _tree_construction_main ($) {
7094                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7095                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7096                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7097          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7098            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7099          ## Ignore the token          ## Ignore the token
7100          !!!next-token;          !!!next-token;
7101          redo B;          next B;
7102                    
7103          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7104                    
# Line 5147  sub _tree_construction_main ($) { Line 7109  sub _tree_construction_main ($) {
7109    
7110          ## Step 2          ## Step 2
7111          S2: {          S2: {
7112            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7113              ## Step 1              ## Step 1
7114              ## generate implied end tags              ## generate implied end tags
7115              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7116                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7117                   td => 1, th => 1, tr => 1,                ## ISSUE: Can this case be reached?
7118                   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;  
7119              }              }
7120                    
7121              ## Step 2              ## Step 2
7122              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7123                        ne $token->{tag_name}) {
7124                  !!!cp ('t431');
7125                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7126                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7127                                  value => $self->{open_elements}->[-1]->[0]
7128                                      ->manakai_local_name,
7129                                  token => $token);
7130                } else {
7131                  !!!cp ('t432');
7132              }              }
7133                            
7134              ## Step 3              ## Step 3
# Line 5174  sub _tree_construction_main ($) { Line 7138  sub _tree_construction_main ($) {
7138              last S2;              last S2;
7139            } else {            } else {
7140              ## Step 3              ## Step 3
7141              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7142                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7143                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7144                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7145                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7146                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name}, token => $token);
7147                ## Ignore the token                ## Ignore the token
7148                !!!next-token;                !!!next-token;
7149                last S2;                last S2;
7150              }              }
7151    
7152                !!!cp ('t434');
7153            }            }
7154                        
7155            ## Step 4            ## Step 4
# Line 5192  sub _tree_construction_main ($) { Line 7159  sub _tree_construction_main ($) {
7159            ## Step 5;            ## Step 5;
7160            redo S2;            redo S2;
7161          } # S2          } # S2
7162          redo B;          next B;
7163        }        }
7164      }      }
7165      redo B;      next B;
7166      } continue { # B
7167        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7168          ## NOTE: The code below is executed in cases where it does not have
7169          ## to be, but it it is harmless even in those cases.
7170          ## has an element in scope
7171          INSCOPE: {
7172            for (reverse 0..$#{$self->{open_elements}}) {
7173              my $node = $self->{open_elements}->[$_];
7174              if ($node->[1] & FOREIGN_EL) {
7175                last INSCOPE;
7176              } elsif ($node->[1] & SCOPING_EL) {
7177                last;
7178              }
7179            }
7180            
7181            ## NOTE: No foreign element in scope.
7182            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7183          } # INSCOPE
7184        }
7185    } # B    } # B
7186    
   ## 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  
   
7187    ## Stop parsing # MUST    ## Stop parsing # MUST
7188        
7189    ## TODO: script stuffs    ## TODO: script stuffs
# Line 5214  sub set_inner_html ($$$) { Line 7195  sub set_inner_html ($$$) {
7195    my $s = \$_[0];    my $s = \$_[0];
7196    my $onerror = $_[1];    my $onerror = $_[1];
7197    
7198      ## ISSUE: Should {confident} be true?
7199    
7200    my $nt = $node->node_type;    my $nt = $node->node_type;
7201    if ($nt == 9) {    if ($nt == 9) {
7202      # MUST      # MUST
# Line 5242  sub set_inner_html ($$$) { Line 7225  sub set_inner_html ($$$) {
7225      my $p = $class->new;      my $p = $class->new;
7226      $p->{document} = $doc;      $p->{document} = $doc;
7227    
7228      ## Step 9 # MUST      ## Step 8 # MUST
7229      my $i = 0;      my $i = 0;
7230      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7231      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7232      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7233        my $self = shift;        my $self = shift;
7234    
7235        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7236        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7237    
7238          $self->{next_char} = -1 and return if $i >= length $$s;
7239          $self->{next_char} = ord substr $$s, $i++, 1;
7240    
7241        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7242        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7243        $column++;  
7244          if ($self->{next_char} == 0x000A) { # LF
7245        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7246          $line++;          $p->{column} = 0;
7247          $column = 0;          !!!cp ('i1');
7248        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7249          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7250          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7251          $line++;          $p->{line}++;
7252          $column = 0;          $p->{column} = 0;
7253        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7254          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7255        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7256            !!!cp ('i3');
7257          } elsif ($self->{next_char} == 0x0000) { # NULL
7258            !!!cp ('i4');
7259          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7260          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7261          } elsif ($self->{next_char} <= 0x0008 or
7262                   (0x000E <= $self->{next_char} and
7263                    $self->{next_char} <= 0x001F) or
7264                   (0x007F <= $self->{next_char} and
7265                    $self->{next_char} <= 0x009F) or
7266                   (0xD800 <= $self->{next_char} and
7267                    $self->{next_char} <= 0xDFFF) or
7268                   (0xFDD0 <= $self->{next_char} and
7269                    $self->{next_char} <= 0xFDDF) or
7270                   {
7271                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7272                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7273                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7274                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7275                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7276                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7277                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7278                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7279                    0x10FFFE => 1, 0x10FFFF => 1,
7280                   }->{$self->{next_char}}) {
7281            !!!cp ('i4.1');
7282            !!!parse-error (type => 'control char', level => $self->{must_level});
7283    ## TODO: error type documentation
7284        }        }
7285      };      };
7286      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7287      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7288            
7289      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7290        my (%opt) = @_;        my (%opt) = @_;
7291        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7292          my $column = $opt{column};
7293          if (defined $opt{token} and defined $opt{token}->{line}) {
7294            $line = $opt{token}->{line};
7295            $column = $opt{token}->{column};
7296          }
7297          warn "Parse error ($opt{type}) at line $line column $column\n";
7298      };      };
7299      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7300        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7301      };      };
7302            
7303      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7304      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7305    
7306      ## Step 2      ## Step 2
7307      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7308      $p->{content_model} = {      $p->{content_model} = {
7309        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7310        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5303  sub set_inner_html ($$$) { Line 7321  sub set_inner_html ($$$) {
7321          unless defined $p->{content_model};          unless defined $p->{content_model};
7322          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7323    
7324      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7325          ## TODO: Foreign element OK?
7326    
7327      ## Step 4      ## Step 3
7328      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7329        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7330    
7331      ## Step 5 # MUST      ## Step 4 # MUST
7332      $doc->append_child ($root);      $doc->append_child ($root);
7333    
7334      ## Step 6 # MUST      ## Step 5 # MUST
7335      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7336    
7337      undef $p->{head_element};      undef $p->{head_element};
7338    
7339      ## Step 7 # MUST      ## Step 6 # MUST
7340      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7341    
7342      ## Step 8 # MUST      ## Step 7 # MUST
7343      my $anode = $node;      my $anode = $node;
7344      AN: while (defined $anode) {      AN: while (defined $anode) {
7345        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7346          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7347          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7348            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7349                !!!cp ('i5');
7350              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7351              last AN;              last AN;
7352            }            }
# Line 5335  sub set_inner_html ($$$) { Line 7355  sub set_inner_html ($$$) {
7355        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7356      } # AN      } # AN
7357            
7358      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7359      {      {
7360        my $self = $p;        my $self = $p;
7361        !!!next-token;        !!!next-token;
7362      }      }
7363      $p->_tree_construction_main;      $p->_tree_construction_main;
7364    
7365      ## Step 11 # MUST      ## Step 10 # MUST
7366      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7367      for (@cn) {      for (@cn) {
7368        $node->remove_child ($_);        $node->remove_child ($_);
7369      }      }
7370      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7371    
7372      ## Step 12 # MUST      ## Step 11 # MUST
7373      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7374      for (@cn) {      for (@cn) {
7375        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5359  sub set_inner_html ($$$) { Line 7378  sub set_inner_html ($$$) {
7378      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7379    
7380      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7381    
7382        delete $p->{parse_error}; # delete loop
7383    } else {    } else {
7384      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";
7385    }    }
# Line 5366  sub set_inner_html ($$$) { Line 7387  sub set_inner_html ($$$) {
7387    
7388  } # tree construction stage  } # tree construction stage
7389    
7390  sub get_inner_html ($$$) {  package Whatpm::HTML::RestartParser;
7391    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  
7392    
7393  1;  1;
7394  # $Date$  # $Date$

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24