/[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.72 by wakaba, Sun Mar 2 14:32:26 2008 UTC revision 1.172 by wakaba, Sun Sep 14 03:07:58 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231  };  };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296    };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 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  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
360    my $charset = shift;    my $charset_name = shift;
361    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
362    
363    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
364      my $self = shift;      my (%opt) = @_;
365      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
366      ## TODO: if $charset is supported    };
367      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      ## "Change the encoding" algorithm:    my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      ## Step 1        };
372      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
373        $charset = 'utf-8';    ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403      }      }
404    
405      ## Step 2      ## Step 2
406      if (defined $self->{input_encoding} and      my $byte_buffer = '';
407          $self->{input_encoding} eq $charset) {      for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419        $self->{confident} = 1;        $self->{confident} = 1;
420        return;        last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435      }      }
436    
437      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
438          ':'.$charset, level => 'w');      ## TODO: <meta charset>
439    
440      ## Step 3      ## Step 5
441      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
442    
443      ## Step 4      ## Step 6
444      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559    }; # $self->{change_encoding}    }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
575    my $return;    my $return;
576    try {    try {
577      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
579      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
580      $s = \ (Encode::decode ($charset, $$bytes_s));      
581      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $return = $self->parse_char_string ($s, @args);  
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605    };    };
606    return $return;    return $return;
607  } # parse_byte_string  } # parse_byte_stream
608    
609  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 615  sub parse_byte_string ($$$$;$) {
615  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
616  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
617    
618  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      my $s = ref $_[0] ? $_[0] : \($_[0]);
622      require Whatpm::Charset::DecodeHandle;
623      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
633    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
636    
# Line 177  sub parse_string ($$$;$) { Line 641  sub parse_string ($$$;$) {
641        if defined $self->{input_encoding};        if defined $self->{input_encoding};
642    
643    my $i = 0;    my $i = 0;
644    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
645    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
646    $self->{set_next_input_character} = sub {    $self->{set_next_char} = sub {
647      my $self = shift;      my $self = shift;
648    
649      pop @{$self->{prev_input_character}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652      $self->{next_input_character} = -1 and return if $i >= length $$s;      my $char;
653      $self->{next_input_character} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
654      $column++;        $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662        ($self->{line_prev}, $self->{column_prev})
663            = ($self->{line}, $self->{column});
664        $self->{column}++;
665            
666      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667        $line++;        !!!cp ('j1');
668        $column = 0;        $self->{line}++;
669      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
670        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{next_char} == 0x000D) { # CR
671        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
672        $line++;  ## TODO: support for abort/streaming
673        $column = 0;        my $next = $input->getc;
674      } elsif ($self->{next_input_character} > 0x10FFFF) {        if (defined $next and $next ne "\x0A") {
675        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_next_char} = $next;
676      } elsif ($self->{next_input_character} == 0x0000) { # NULL        }
677          $self->{next_char} = 0x000A; # LF # MUST
678          $self->{line}++;
679          $self->{column} = 0;
680        } elsif ($self->{next_char} > 0x10FFFF) {
681          !!!cp ('j3');
682          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
683        } elsif ($self->{next_char} == 0x0000) { # NULL
684          !!!cp ('j4');
685        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
686        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
687        } elsif ($self->{next_char} <= 0x0008 or
688                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
689                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
690                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
691                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
692    ## ISSUE: U+FDE0-U+FDEF are not excluded
693                 {
694                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
695                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
696                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
697                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
698                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
699                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
700                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
701                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
702                  0x10FFFE => 1, 0x10FFFF => 1,
703                 }->{$self->{next_char}}) {
704          !!!cp ('j5');
705          if ($self->{next_char} < 0x10000) {
706            !!!parse-error (type => 'control char',
707                            text => (sprintf 'U+%04X', $self->{next_char}));
708          } else {
709            !!!parse-error (type => 'control char',
710                            text => (sprintf 'U-%08X', $self->{next_char}));
711          }
712      }      }
713    };    };
714    $self->{prev_input_character} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
715    $self->{next_input_character} = -1;    $self->{next_char} = -1;
716    
717      $self->{read_until} = sub {
718        #my ($scalar, $specials_range, $offset) = @_;
719        my $specials_range = $_[1];
720        return 0 if defined $self->{next_next_char};
721        my $count = $input->manakai_read_until
722           ($_[0],
723            qr/(?![$specials_range\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
724            $_[2]);
725        if ($count) {
726          $self->{column} += $count;
727          $self->{column_prev} += $count;
728          $self->{prev_char} = [-1, -1, -1];
729          $self->{next_char} = -1;
730        }
731        return $count;
732      }; # $self->{read_until}
733    
734    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
735      my (%opt) = @_;      my (%opt) = @_;
736      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
737        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
738        warn "Parse error ($opt{type}) at line $line column $column\n";
739    };    };
740    $self->{parse_error} = sub {    $self->{parse_error} = sub {
741      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
742    };    };
743    
744    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 746  sub parse_string ($$$;$) {
746    $self->_construct_tree;    $self->_construct_tree;
747    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
748    
749      delete $self->{parse_error}; # remove loop
750    
751    return $self->{document};    return $self->{document};
752  } # parse_string  } # parse_char_stream
753    
754  sub new ($) {  sub new ($) {
755    my $class = shift;    my $class = shift;
756    my $self = bless {}, $class;    my $self = bless {
757    $self->{set_next_input_character} = sub {      level => {must => 'm',
758      $self->{next_input_character} = -1;                should => 's',
759                  warn => 'w',
760                  info => 'i',
761                  uncertain => 'u'},
762      }, $class;
763      $self->{set_next_char} = sub {
764        $self->{next_char} = -1;
765    };    };
766    $self->{parse_error} = sub {    $self->{parse_error} = sub {
767      #      #
# Line 254  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 788  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
788  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
789    
790  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
791  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
792  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
793  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
794  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 265  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 799  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
799  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
800  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
801  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
802  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
803  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
804  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
805  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 821  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
821  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
822  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
823  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
824    sub SELF_CLOSING_START_TAG_STATE () { 34 }
825    sub CDATA_SECTION_STATE () { 35 }
826    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
827    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
828    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
829    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
830    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
831    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
832    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
833    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
834    ## NOTE: "Entity data state", "entity in attribute value state", and
835    ## "consume a character reference" algorithm are jointly implemented
836    ## using the following six states:
837    sub ENTITY_STATE () { 44 }
838    sub ENTITY_HASH_STATE () { 45 }
839    sub NCR_NUM_STATE () { 46 }
840    sub HEXREF_X_STATE () { 47 }
841    sub HEXREF_HEX_STATE () { 48 }
842    sub ENTITY_NAME_STATE () { 49 }
843    
844  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
845  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 856  sub TABLE_IMS ()      { 0b1000000 }
856  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
857  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
858  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
859    sub SELECT_IMS ()     { 0b10000000000 }
860    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
861        ## NOTE: "in foreign content" insertion mode is special; it is combined
862        ## with the secondary insertion mode.  In this parser, they are stored
863        ## together in the bit-or'ed form.
864    
865    ## NOTE: "initial" and "before html" insertion modes have no constants.
866    
867    ## NOTE: "after after body" insertion mode.
868  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
869    
870    ## NOTE: "after after frameset" insertion mode.
871  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
872    
873  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
874  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
875  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 319  sub IN_TABLE_IM () { TABLE_IMS } Line 883  sub IN_TABLE_IM () { TABLE_IMS }
883  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
884  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
885  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
886  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
887    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
888  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
889    
890  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 327  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 892  sub IN_COLUMN_GROUP_IM () { 0b10 }
892  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
893    my $self = shift;    my $self = shift;
894    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
895      #$self->{state_keyword}; # initialized when used
896      #$self->{entity__value}; # initialized when used
897      #$self->{entity__match}; # initialized when used
898    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
899    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
900    undef $self->{current_attribute};    undef $self->{current_attribute};
901    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
902    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
903    $self->{char} = [];    delete $self->{self_closing};
904    # $self->{next_input_character}    # $self->{next_char}
905    !!!next-input-character;    !!!next-input-character;
906    $self->{token} = [];    $self->{token} = [];
907    # $self->{escape}    # $self->{escape}
# Line 346  sub _initialize_tokenizer ($) { Line 914  sub _initialize_tokenizer ($) {
914  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
915  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{public_identifier} (DOCTYPE_TOKEN)
916  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{system_identifier} (DOCTYPE_TOKEN)
917  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
918  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
919  ##        ->{name}  ##        ->{name}
920  ##        ->{value}  ##        ->{value}
921  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
922  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
923    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
924    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
925    ##     while the token is pushed back to the stack.
926    
927  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
928    
# Line 361  sub _initialize_tokenizer ($) { Line 932  sub _initialize_tokenizer ($) {
932  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
933  ## and removed from the list.  ## and removed from the list.
934    
935  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
936  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
 ## contains some requirements that are not detected by the  
 ## parsing algorithm:  
 ## - Some requirements on character encoding declarations. ## TODO  
 ## - "Elements MUST NOT contain content that their content model disallows."  
 ##   ... Some are parse error, some are not (will be reported by c.c.).  
 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO  
 ## - Text (in elements, attributes, and comments) SHOULD NOT contain  
 ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)  
   
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
937    
938  sub _get_next_token ($) {  sub _get_next_token ($) {
939    my $self = shift;    my $self = shift;
940    
941      if ($self->{self_closing}) {
942        !!!parse-error (type => 'nestc', token => $self->{current_token});
943        ## NOTE: The |self_closing| flag is only set by start tag token.
944        ## In addition, when a start tag token is emitted, it is always set to
945        ## |current_token|.
946        delete $self->{self_closing};
947      }
948    
949    if (@{$self->{token}}) {    if (@{$self->{token}}) {
950        $self->{self_closing} = $self->{token}->[0]->{self_closing};
951      return shift @{$self->{token}};      return shift @{$self->{token}};
952    }    }
953    
954    A: {    A: {
955      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
956        if ($self->{next_input_character} == 0x0026) { # &        if ($self->{next_char} == 0x0026) { # &
957          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
958              not $self->{escape}) {              not $self->{escape}) {
959            $self->{state} = ENTITY_DATA_STATE;            !!!cp (1);
960              ## NOTE: In the spec, the tokenizer is switched to the
961              ## "entity data state".  In this implementation, the tokenizer
962              ## is switched to the |ENTITY_STATE|, which is an implementation
963              ## of the "consume a character reference" algorithm.
964              $self->{entity_additional} = -1;
965              $self->{prev_state} = DATA_STATE;
966              $self->{state} = ENTITY_STATE;
967            !!!next-input-character;            !!!next-input-character;
968            redo A;            redo A;
969          } else {          } else {
970              !!!cp (2);
971            #            #
972          }          }
973        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
974          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
975            unless ($self->{escape}) {            unless ($self->{escape}) {
976              if ($self->{prev_input_character}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
977                  $self->{prev_input_character}->[1] == 0x0021 and # !                  $self->{prev_char}->[1] == 0x0021 and # !
978                  $self->{prev_input_character}->[2] == 0x003C) { # <                  $self->{prev_char}->[2] == 0x003C) { # <
979                  !!!cp (3);
980                $self->{escape} = 1;                $self->{escape} = 1;
981                } else {
982                  !!!cp (4);
983              }              }
984              } else {
985                !!!cp (5);
986            }            }
987          }          }
988                    
989          #          #
990        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{next_char} == 0x003C) { # <
991          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
992              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
993               not $self->{escape})) {               not $self->{escape})) {
994              !!!cp (6);
995            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
996            !!!next-input-character;            !!!next-input-character;
997            redo A;            redo A;
998          } else {          } else {
999              !!!cp (7);
1000            #            #
1001          }          }
1002        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1003          if ($self->{escape} and          if ($self->{escape} and
1004              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1005            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
1006                $self->{prev_input_character}->[1] == 0x002D) { # -                $self->{prev_char}->[1] == 0x002D) { # -
1007                !!!cp (8);
1008              delete $self->{escape};              delete $self->{escape};
1009              } else {
1010                !!!cp (9);
1011            }            }
1012            } else {
1013              !!!cp (10);
1014          }          }
1015                    
1016          #          #
1017        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1018          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
1019            !!!emit ({type => END_OF_FILE_TOKEN,
1020                      line => $self->{line}, column => $self->{column}});
1021          last A; ## TODO: ok?          last A; ## TODO: ok?
1022          } else {
1023            !!!cp (12);
1024        }        }
1025        # Anything else        # Anything else
1026        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1027                     data => chr $self->{next_input_character}};                     data => chr $self->{next_char},
1028                       line => $self->{line}, column => $self->{column},
1029                      };
1030          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1031    
1032        ## Stay in the data state        ## Stay in the data state
1033        !!!next-input-character;        !!!next-input-character;
1034    
1035        !!!emit ($token);        !!!emit ($token);
1036    
1037        redo A;        redo A;
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!emit ({type => CHARACTER_TOKEN, data => '&'});  
       } else {  
         !!!emit ($token);  
       }  
   
       redo A;  
1038      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1039        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1040          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
1041              !!!cp (15);
1042            !!!next-input-character;            !!!next-input-character;
1043            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1044            redo A;            redo A;
1045          } else {          } else {
1046              !!!cp (16);
1047            ## reconsume            ## reconsume
1048            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1049    
1050            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1051                        line => $self->{line_prev},
1052                        column => $self->{column_prev},
1053                       });
1054    
1055            redo A;            redo A;
1056          }          }
1057        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1058          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{next_char} == 0x0021) { # !
1059              !!!cp (17);
1060            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1061            !!!next-input-character;            !!!next-input-character;
1062            redo A;            redo A;
1063          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{next_char} == 0x002F) { # /
1064              !!!cp (18);
1065            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1066            !!!next-input-character;            !!!next-input-character;
1067            redo A;            redo A;
1068          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{next_char} and
1069                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{next_char} <= 0x005A) { # A..Z
1070              !!!cp (19);
1071            $self->{current_token}            $self->{current_token}
1072              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1073                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1074                   line => $self->{line_prev},
1075                   column => $self->{column_prev}};
1076            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1077            !!!next-input-character;            !!!next-input-character;
1078            redo A;            redo A;
1079          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{next_char} and
1080                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1081              !!!cp (20);
1082            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1083                              tag_name => chr ($self->{next_input_character})};                                      tag_name => chr ($self->{next_char}),
1084                                        line => $self->{line_prev},
1085                                        column => $self->{column_prev}};
1086            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1087            !!!next-input-character;            !!!next-input-character;
1088            redo A;            redo A;
1089          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1090            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1091              !!!parse-error (type => 'empty start tag',
1092                              line => $self->{line_prev},
1093                              column => $self->{column_prev});
1094            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1095            !!!next-input-character;            !!!next-input-character;
1096    
1097            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1098                        line => $self->{line_prev},
1099                        column => $self->{column_prev},
1100                       });
1101    
1102            redo A;            redo A;
1103          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1104            !!!parse-error (type => 'pio');            !!!cp (22);
1105              !!!parse-error (type => 'pio',
1106                              line => $self->{line_prev},
1107                              column => $self->{column_prev});
1108            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1109            ## $self->{next_input_character} is intentionally left as is            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1110                                        line => $self->{line_prev},
1111                                        column => $self->{column_prev},
1112                                       };
1113              ## $self->{next_char} is intentionally left as is
1114            redo A;            redo A;
1115          } else {          } else {
1116            !!!parse-error (type => 'bare stago');            !!!cp (23);
1117              !!!parse-error (type => 'bare stago',
1118                              line => $self->{line_prev},
1119                              column => $self->{column_prev});
1120            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1121            ## reconsume            ## reconsume
1122    
1123            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1124                        line => $self->{line_prev},
1125                        column => $self->{column_prev},
1126                       });
1127    
1128            redo A;            redo A;
1129          }          }
# Line 517  sub _get_next_token ($) { Line 1131  sub _get_next_token ($) {
1131          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1132        }        }
1133      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1134          ## NOTE: The "close tag open state" in the spec is implemented as
1135          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1136    
1137          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1138        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1139          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1140            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1141            my @next_char;            $self->{state_keyword} = '';
1142            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            ## Reconsume.
1143              push @next_char, $self->{next_input_character};            redo A;
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               $self->{next_input_character} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
   
               !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_input_character};  
         
           unless ($self->{next_input_character} == 0x0009 or # HT  
                   $self->{next_input_character} == 0x000A or # LF  
                   $self->{next_input_character} == 0x000B or # VT  
                   $self->{next_input_character} == 0x000C or # FF  
                   $self->{next_input_character} == 0x0020 or # SP  
                   $self->{next_input_character} == 0x003E or # >  
                   $self->{next_input_character} == 0x002F or # /  
                   $self->{next_input_character} == -1) {  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
             redo A;  
           } else {  
             $self->{next_input_character} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1144          } else {          } else {
1145            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1146            # next-input-character is already done            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1147              !!!cp (28);
1148            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1149            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1150              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1151                        line => $l, column => $c,
1152                       });
1153            redo A;            redo A;
1154          }          }
1155        }        }
1156          
1157        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{next_char} and
1158            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1159          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1160                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{current_token}
1161                = {type => END_TAG_TOKEN,
1162                   tag_name => chr ($self->{next_char} + 0x0020),
1163                   line => $l, column => $c};
1164          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1165          !!!next-input-character;          !!!next-input-character;
1166          redo A;          redo A;
1167        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{next_char} and
1168                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1169            !!!cp (30);
1170          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1171                            tag_name => chr ($self->{next_input_character})};                                    tag_name => chr ($self->{next_char}),
1172                                      line => $l, column => $c};
1173          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1174          !!!next-input-character;          !!!next-input-character;
1175          redo A;          redo A;
1176        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1177          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1178            !!!parse-error (type => 'empty end tag',
1179                            line => $self->{line_prev}, ## "<" in "</>"
1180                            column => $self->{column_prev} - 1);
1181          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1182          !!!next-input-character;          !!!next-input-character;
1183          redo A;          redo A;
1184        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1185            !!!cp (32);
1186          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1187          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1188          # reconsume          # reconsume
1189    
1190          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1191                      line => $l, column => $c,
1192                     });
1193    
1194          redo A;          redo A;
1195        } else {        } else {
1196            !!!cp (33);
1197          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1198          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1199          ## $self->{next_input_character} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1200          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1201                                      column => $self->{column_prev} - 1,
1202                                     };
1203            ## NOTE: $self->{next_char} is intentionally left as is.
1204            ## Although the "anything else" case of the spec not explicitly
1205            ## states that the next input character is to be reconsumed,
1206            ## it will be included to the |data| of the comment token
1207            ## generated from the bogus end tag, as defined in the
1208            ## "bogus comment state" entry.
1209            redo A;
1210          }
1211        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1212          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1213          if (length $ch) {
1214            my $CH = $ch;
1215            $ch =~ tr/a-z/A-Z/;
1216            my $nch = chr $self->{next_char};
1217            if ($nch eq $ch or $nch eq $CH) {
1218              !!!cp (24);
1219              ## Stay in the state.
1220              $self->{state_keyword} .= $nch;
1221              !!!next-input-character;
1222              redo A;
1223            } else {
1224              !!!cp (25);
1225              $self->{state} = DATA_STATE;
1226              ## Reconsume.
1227              !!!emit ({type => CHARACTER_TOKEN,
1228                        data => '</' . $self->{state_keyword},
1229                        line => $self->{line_prev},
1230                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1231                       });
1232              redo A;
1233            }
1234          } else { # after "<{tag-name}"
1235            unless ({
1236                     0x0009 => 1, # HT
1237                     0x000A => 1, # LF
1238                     0x000B => 1, # VT
1239                     0x000C => 1, # FF
1240                     0x0020 => 1, # SP
1241                     0x003E => 1, # >
1242                     0x002F => 1, # /
1243                     -1 => 1, # EOF
1244                    }->{$self->{next_char}}) {
1245              !!!cp (26);
1246              ## Reconsume.
1247              $self->{state} = DATA_STATE;
1248              !!!emit ({type => CHARACTER_TOKEN,
1249                        data => '</' . $self->{state_keyword},
1250                        line => $self->{line_prev},
1251                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1252                       });
1253              redo A;
1254            } else {
1255              !!!cp (27);
1256              $self->{current_token}
1257                  = {type => END_TAG_TOKEN,
1258                     tag_name => $self->{last_emitted_start_tag_name},
1259                     line => $self->{line_prev},
1260                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1261              $self->{state} = TAG_NAME_STATE;
1262              ## Reconsume.
1263              redo A;
1264            }
1265        }        }
1266      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1267        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1268            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1269            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1270            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1271            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1272            !!!cp (34);
1273          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1274          !!!next-input-character;          !!!next-input-character;
1275          redo A;          redo A;
1276        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1277          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1278            $self->{current_token}->{first_start_tag}            !!!cp (35);
               = not defined $self->{last_emitted_start_tag_name};  
1279            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1280          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1281            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1282            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1283              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1284            }            #  !!! cp (36);
1285              #  !!! parse-error (type => 'end tag attribute');
1286              #} else {
1287                !!!cp (37);
1288              #}
1289          } else {          } else {
1290            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1291          }          }
# Line 628  sub _get_next_token ($) { Line 1295  sub _get_next_token ($) {
1295          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1296    
1297          redo A;          redo A;
1298        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1299                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1300          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1301            $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);
1302            # start tag or end tag            # start tag or end tag
1303          ## Stay in this state          ## Stay in this state
1304          !!!next-input-character;          !!!next-input-character;
1305          redo A;          redo A;
1306        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1307          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1308          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1309            $self->{current_token}->{first_start_tag}            !!!cp (39);
               = not defined $self->{last_emitted_start_tag_name};  
1310            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1311          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1312            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1313            if ($self->{current_token}->{attributes}) {            #if ($self->{current_token}->{attributes}) {
1314              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1315            }            #  !!! cp (40);
1316              #  !!! parse-error (type => 'end tag attribute');
1317              #} else {
1318                !!!cp (41);
1319              #}
1320          } else {          } else {
1321            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
1322          }          }
# Line 655  sub _get_next_token ($) { Line 1326  sub _get_next_token ($) {
1326          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1327    
1328          redo A;          redo A;
1329        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1330            !!!cp (42);
1331            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1332          !!!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  
1333          redo A;          redo A;
1334        } else {        } else {
1335          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1336            $self->{current_token}->{tag_name} .= chr $self->{next_char};
1337            # start tag or end tag            # start tag or end tag
1338          ## Stay in the state          ## Stay in the state
1339          !!!next-input-character;          !!!next-input-character;
1340          redo A;          redo A;
1341        }        }
1342      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1343        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1344            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1345            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1346            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1347            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1348            !!!cp (45);
1349          ## Stay in the state          ## Stay in the state
1350          !!!next-input-character;          !!!next-input-character;
1351          redo A;          redo A;
1352        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1353          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1354            $self->{current_token}->{first_start_tag}            !!!cp (46);
               = not defined $self->{last_emitted_start_tag_name};  
1355            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1356          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1357            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1358            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1359                !!!cp (47);
1360              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1361              } else {
1362                !!!cp (48);
1363            }            }
1364          } else {          } else {
1365            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 703  sub _get_next_token ($) { Line 1370  sub _get_next_token ($) {
1370          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1371    
1372          redo A;          redo A;
1373        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1374                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1375          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1376                                value => ''};          $self->{current_attribute}
1377                = {name => chr ($self->{next_char} + 0x0020),
1378                   value => '',
1379                   line => $self->{line}, column => $self->{column}};
1380          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1381          !!!next-input-character;          !!!next-input-character;
1382          redo A;          redo A;
1383        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1384            !!!cp (50);
1385            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1386          !!!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  
1387          redo A;          redo A;
1388        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1389          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1390          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1391            $self->{current_token}->{first_start_tag}            !!!cp (52);
               = not defined $self->{last_emitted_start_tag_name};  
1392            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1393          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1394            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1395            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1396                !!!cp (53);
1397              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1398              } else {
1399                !!!cp (54);
1400            }            }
1401          } else {          } else {
1402            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 748  sub _get_next_token ($) { Line 1412  sub _get_next_token ($) {
1412               0x0022 => 1, # "               0x0022 => 1, # "
1413               0x0027 => 1, # '               0x0027 => 1, # '
1414               0x003D => 1, # =               0x003D => 1, # =
1415              }->{$self->{next_input_character}}) {              }->{$self->{next_char}}) {
1416              !!!cp (55);
1417            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1418            } else {
1419              !!!cp (56);
1420          }          }
1421          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          $self->{current_attribute}
1422                                value => ''};              = {name => chr ($self->{next_char}),
1423                   value => '',
1424                   line => $self->{line}, column => $self->{column}};
1425          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1426          !!!next-input-character;          !!!next-input-character;
1427          redo A;          redo A;
# Line 761  sub _get_next_token ($) { Line 1430  sub _get_next_token ($) {
1430        my $before_leave = sub {        my $before_leave = sub {
1431          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1432              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1433            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1434              !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1435            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1436          } else {          } else {
1437              !!!cp (58);
1438            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}
1439              = $self->{current_attribute};              = $self->{current_attribute};
1440          }          }
1441        }; # $before_leave        }; # $before_leave
1442    
1443        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1444            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1445            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1446            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1447            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1448            !!!cp (59);
1449          $before_leave->();          $before_leave->();
1450          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1451          !!!next-input-character;          !!!next-input-character;
1452          redo A;          redo A;
1453        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1454            !!!cp (60);
1455          $before_leave->();          $before_leave->();
1456          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1457          !!!next-input-character;          !!!next-input-character;
1458          redo A;          redo A;
1459        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1460          $before_leave->();          $before_leave->();
1461          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1462            $self->{current_token}->{first_start_tag}            !!!cp (61);
               = not defined $self->{last_emitted_start_tag_name};  
1463            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1464          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1465              !!!cp (62);
1466            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1467            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1468              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
# Line 803  sub _get_next_token ($) { Line 1476  sub _get_next_token ($) {
1476          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1477    
1478          redo A;          redo A;
1479        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1480                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1481          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1482            $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);
1483          ## Stay in the state          ## Stay in the state
1484          !!!next-input-character;          !!!next-input-character;
1485          redo A;          redo A;
1486        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1487            !!!cp (64);
1488          $before_leave->();          $before_leave->();
1489            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1490          !!!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  
1491          redo A;          redo A;
1492        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1494          $before_leave->();          $before_leave->();
1495          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1496            $self->{current_token}->{first_start_tag}            !!!cp (66);
               = not defined $self->{last_emitted_start_tag_name};  
1497            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1498          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1499            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1500            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1501                !!!cp (67);
1502              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1503              } else {
1504                ## NOTE: This state should never be reached.
1505                !!!cp (68);
1506            }            }
1507          } else {          } else {
1508            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 845  sub _get_next_token ($) { Line 1514  sub _get_next_token ($) {
1514    
1515          redo A;          redo A;
1516        } else {        } else {
1517          if ($self->{next_input_character} == 0x0022 or # "          if ($self->{next_char} == 0x0022 or # "
1518              $self->{next_input_character} == 0x0027) { # '              $self->{next_char} == 0x0027) { # '
1519              !!!cp (69);
1520            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1521            } else {
1522              !!!cp (70);
1523          }          }
1524          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          $self->{current_attribute}->{name} .= chr ($self->{next_char});
1525          ## Stay in the state          ## Stay in the state
1526          !!!next-input-character;          !!!next-input-character;
1527          redo A;          redo A;
1528        }        }
1529      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1530        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1531            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1532            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1533            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1534            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1535            !!!cp (71);
1536          ## Stay in the state          ## Stay in the state
1537          !!!next-input-character;          !!!next-input-character;
1538          redo A;          redo A;
1539        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{next_char} == 0x003D) { # =
1540            !!!cp (72);
1541          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1542          !!!next-input-character;          !!!next-input-character;
1543          redo A;          redo A;
1544        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1545          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1546            $self->{current_token}->{first_start_tag}            !!!cp (73);
               = not defined $self->{last_emitted_start_tag_name};  
1547            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1548          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1549            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1550            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1551                !!!cp (74);
1552              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1553              } else {
1554                ## NOTE: This state should never be reached.
1555                !!!cp (75);
1556            }            }
1557          } else {          } else {
1558            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 886  sub _get_next_token ($) { Line 1563  sub _get_next_token ($) {
1563          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1564    
1565          redo A;          redo A;
1566        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{next_char} and
1567                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1568          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1569                                value => ''};          $self->{current_attribute}
1570                = {name => chr ($self->{next_char} + 0x0020),
1571                   value => '',
1572                   line => $self->{line}, column => $self->{column}};
1573          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1574          !!!next-input-character;          !!!next-input-character;
1575          redo A;          redo A;
1576        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1577            !!!cp (77);
1578            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1579          !!!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  
1580          redo A;          redo A;
1581        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1582          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1583          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1584            $self->{current_token}->{first_start_tag}            !!!cp (79);
               = not defined $self->{last_emitted_start_tag_name};  
1585            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1586          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1587            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1588            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1589                !!!cp (80);
1590              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1591              } else {
1592                ## NOTE: This state should never be reached.
1593                !!!cp (81);
1594            }            }
1595          } else {          } else {
1596            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 928  sub _get_next_token ($) { Line 1602  sub _get_next_token ($) {
1602    
1603          redo A;          redo A;
1604        } else {        } else {
1605          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{next_char} == 0x0022 or # "
1606                                value => ''};              $self->{next_char} == 0x0027) { # '
1607              !!!cp (78);
1608              !!!parse-error (type => 'bad attribute name');
1609            } else {
1610              !!!cp (82);
1611            }
1612            $self->{current_attribute}
1613                = {name => chr ($self->{next_char}),
1614                   value => '',
1615                   line => $self->{line}, column => $self->{column}};
1616          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1617          !!!next-input-character;          !!!next-input-character;
1618          redo A;                  redo A;        
1619        }        }
1620      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1621        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1622            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1623            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1624            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1625            $self->{next_input_character} == 0x0020) { # SP                  $self->{next_char} == 0x0020) { # SP      
1626            !!!cp (83);
1627          ## Stay in the state          ## Stay in the state
1628          !!!next-input-character;          !!!next-input-character;
1629          redo A;          redo A;
1630        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
1631            !!!cp (84);
1632          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1633          !!!next-input-character;          !!!next-input-character;
1634          redo A;          redo A;
1635        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1636            !!!cp (85);
1637          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1638          ## reconsume          ## reconsume
1639          redo A;          redo A;
1640        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
1641            !!!cp (86);
1642          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1643          !!!next-input-character;          !!!next-input-character;
1644          redo A;          redo A;
1645        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1646            !!!parse-error (type => 'empty unquoted attribute value');
1647          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1648            $self->{current_token}->{first_start_tag}            !!!cp (87);
               = not defined $self->{last_emitted_start_tag_name};  
1649            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1650          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1651            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1652            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1653                !!!cp (88);
1654              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1655              } else {
1656                ## NOTE: This state should never be reached.
1657                !!!cp (89);
1658            }            }
1659          } else {          } else {
1660            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 974  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1666    
1667          redo A;          redo A;
1668        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1669          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1670          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1671            $self->{current_token}->{first_start_tag}            !!!cp (90);
               = not defined $self->{last_emitted_start_tag_name};  
1672            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1673          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1674            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1675            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1676                !!!cp (91);
1677              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1678              } else {
1679                ## NOTE: This state should never be reached.
1680                !!!cp (92);
1681            }            }
1682          } else {          } else {
1683            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 995  sub _get_next_token ($) { Line 1689  sub _get_next_token ($) {
1689    
1690          redo A;          redo A;
1691        } else {        } else {
1692          if ($self->{next_input_character} == 0x003D) { # =          if ($self->{next_char} == 0x003D) { # =
1693              !!!cp (93);
1694            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1695            } else {
1696              !!!cp (94);
1697          }          }
1698          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1699          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1700          !!!next-input-character;          !!!next-input-character;
1701          redo A;          redo A;
1702        }        }
1703      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1704        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
1705            !!!cp (95);
1706          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1707          !!!next-input-character;          !!!next-input-character;
1708          redo A;          redo A;
1709        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1710          $self->{last_attribute_value_state} = $self->{state};          !!!cp (96);
1711          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## NOTE: In the spec, the tokenizer is switched to the
1712            ## "entity in attribute value state".  In this implementation, the
1713            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1714            ## implementation of the "consume a character reference" algorithm.
1715            $self->{prev_state} = $self->{state};
1716            $self->{entity_additional} = 0x0022; # "
1717            $self->{state} = ENTITY_STATE;
1718          !!!next-input-character;          !!!next-input-character;
1719          redo A;          redo A;
1720        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1721          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1722          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1723            $self->{current_token}->{first_start_tag}            !!!cp (97);
               = not defined $self->{last_emitted_start_tag_name};  
1724            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1725          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1726            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1727            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1728                !!!cp (98);
1729              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1730              } else {
1731                ## NOTE: This state should never be reached.
1732                !!!cp (99);
1733            }            }
1734          } else {          } else {
1735            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1034  sub _get_next_token ($) { Line 1741  sub _get_next_token ($) {
1741    
1742          redo A;          redo A;
1743        } else {        } else {
1744          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1745            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1746          ## Stay in the state          ## Stay in the state
1747          !!!next-input-character;          !!!next-input-character;
1748          redo A;          redo A;
1749        }        }
1750      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1751        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
1752            !!!cp (101);
1753          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1754          !!!next-input-character;          !!!next-input-character;
1755          redo A;          redo A;
1756        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1757          $self->{last_attribute_value_state} = $self->{state};          !!!cp (102);
1758          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## NOTE: In the spec, the tokenizer is switched to the
1759            ## "entity in attribute value state".  In this implementation, the
1760            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1761            ## implementation of the "consume a character reference" algorithm.
1762            $self->{entity_additional} = 0x0027; # '
1763            $self->{prev_state} = $self->{state};
1764            $self->{state} = ENTITY_STATE;
1765          !!!next-input-character;          !!!next-input-character;
1766          redo A;          redo A;
1767        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1768          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1769          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1770            $self->{current_token}->{first_start_tag}            !!!cp (103);
               = not defined $self->{last_emitted_start_tag_name};  
1771            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1772          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1773            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1774            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1775                !!!cp (104);
1776              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1777              } else {
1778                ## NOTE: This state should never be reached.
1779                !!!cp (105);
1780            }            }
1781          } else {          } else {
1782            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1070  sub _get_next_token ($) { Line 1788  sub _get_next_token ($) {
1788    
1789          redo A;          redo A;
1790        } else {        } else {
1791          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1792            $self->{current_attribute}->{value} .= chr ($self->{next_char});
1793          ## Stay in the state          ## Stay in the state
1794          !!!next-input-character;          !!!next-input-character;
1795          redo A;          redo A;
1796        }        }
1797      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1798        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1799            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1800            $self->{next_input_character} == 0x000B or # HT            $self->{next_char} == 0x000B or # HT
1801            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1802            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1803            !!!cp (107);
1804          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1805          !!!next-input-character;          !!!next-input-character;
1806          redo A;          redo A;
1807        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1808          $self->{last_attribute_value_state} = $self->{state};          !!!cp (108);
1809          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## NOTE: In the spec, the tokenizer is switched to the
1810            ## "entity in attribute value state".  In this implementation, the
1811            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1812            ## implementation of the "consume a character reference" algorithm.
1813            $self->{entity_additional} = -1;
1814            $self->{prev_state} = $self->{state};
1815            $self->{state} = ENTITY_STATE;
1816          !!!next-input-character;          !!!next-input-character;
1817          redo A;          redo A;
1818        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1819          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1820            $self->{current_token}->{first_start_tag}            !!!cp (109);
               = not defined $self->{last_emitted_start_tag_name};  
1821            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1822          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1823            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1824            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1825                !!!cp (110);
1826              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1827              } else {
1828                ## NOTE: This state should never be reached.
1829                !!!cp (111);
1830            }            }
1831          } else {          } else {
1832            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1108  sub _get_next_token ($) { Line 1837  sub _get_next_token ($) {
1837          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1838    
1839          redo A;          redo A;
1840        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
1841          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1842          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1843            $self->{current_token}->{first_start_tag}            !!!cp (112);
               = not defined $self->{last_emitted_start_tag_name};  
1844            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1845          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1846            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1847            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1848                !!!cp (113);
1849              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1850              } else {
1851                ## NOTE: This state should never be reached.
1852                !!!cp (114);
1853            }            }
1854          } else {          } else {
1855            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1133  sub _get_next_token ($) { Line 1865  sub _get_next_token ($) {
1865               0x0022 => 1, # "               0x0022 => 1, # "
1866               0x0027 => 1, # '               0x0027 => 1, # '
1867               0x003D => 1, # =               0x003D => 1, # =
1868              }->{$self->{next_input_character}}) {              }->{$self->{next_char}}) {
1869              !!!cp (115);
1870            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1871            } else {
1872              !!!cp (116);
1873          }          }
1874          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1875          ## Stay in the state          ## Stay in the state
1876          !!!next-input-character;          !!!next-input-character;
1877          redo A;          redo A;
1878        }        }
     } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {  
       my $token = $self->_tokenize_attempt_to_consume_an_entity  
           (1,  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '  
            -1);  
   
       unless (defined $token) {  
         $self->{current_attribute}->{value} .= '&';  
       } else {  
         $self->{current_attribute}->{value} .= $token->{data};  
         $self->{current_attribute}->{has_reference} = $token->{has_reference};  
         ## ISSUE: spec says "append the returned character token to the current attribute's value"  
       }  
   
       $self->{state} = $self->{last_attribute_value_state};  
       # next-input-character is already done  
       redo A;  
1879      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1880        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1881            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
1882            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
1883            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
1884            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
1885            !!!cp (118);
1886          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1887          !!!next-input-character;          !!!next-input-character;
1888          redo A;          redo A;
1889        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1890          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1891            $self->{current_token}->{first_start_tag}            !!!cp (119);
               = not defined $self->{last_emitted_start_tag_name};  
1892            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1893          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1894            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1895            if ($self->{current_token}->{attributes}) {            if ($self->{current_token}->{attributes}) {
1896                !!!cp (120);
1897              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1898              } else {
1899                ## NOTE: This state should never be reached.
1900                !!!cp (121);
1901            }            }
1902          } else {          } else {
1903            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{current_token}->{type}: Unknown token type";
# Line 1189  sub _get_next_token ($) { Line 1908  sub _get_next_token ($) {
1908          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{current_token}); # start tag or end tag
1909    
1910          redo A;          redo A;
1911        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1912            !!!cp (122);
1913            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1914          !!!next-input-character;          !!!next-input-character;
1915          if ($self->{next_input_character} == 0x003E and # >          redo A;
1916              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1917              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1918            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1919            #            !!!cp (122.3);
1920              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1921            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1922              if ($self->{current_token}->{attributes}) {
1923                !!!cp (122.1);
1924                !!!parse-error (type => 'end tag attribute');
1925              } else {
1926                ## NOTE: This state should never be reached.
1927                !!!cp (122.2);
1928              }
1929          } else {          } else {
1930            !!!parse-error (type => 'nestc');            die "$0: $self->{current_token}->{type}: Unknown token type";
1931          }          }
1932          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1933          # next-input-character is already done          ## Reconsume.
1934            !!!emit ($self->{current_token}); # start tag or end tag
1935          redo A;          redo A;
1936        } else {        } else {
1937            !!!cp ('124.1');
1938          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1939          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1940          ## reconsume          ## reconsume
1941          redo A;          redo A;
1942        }        }
1943      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1944        ## (only happen if PCDATA state)        if ($self->{next_char} == 0x003E) { # >
1945                  if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1946        my $token = {type => COMMENT_TOKEN, data => ''};            !!!cp ('124.2');
1947              !!!parse-error (type => 'nestc', token => $self->{current_token});
1948        BC: {            ## TODO: Different type than slash in start tag
1949          if ($self->{next_input_character} == 0x003E) { # >            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1950            $self->{state} = DATA_STATE;            if ($self->{current_token}->{attributes}) {
1951            !!!next-input-character;              !!!cp ('124.4');
1952                !!!parse-error (type => 'end tag attribute');
1953            !!!emit ($token);            } else {
1954                !!!cp ('124.5');
1955              }
1956              ## TODO: Test |<title></title/>|
1957            } else {
1958              !!!cp ('124.3');
1959              $self->{self_closing} = 1;
1960            }
1961    
1962            redo A;          $self->{state} = DATA_STATE;
1963          } elsif ($self->{next_input_character} == -1) {          !!!next-input-character;
           $self->{state} = DATA_STATE;  
           ## reconsume  
1964    
1965            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
1966    
1967            redo A;          redo A;
1968          } elsif ($self->{next_char} == -1) {
1969            !!!parse-error (type => 'unclosed tag');
1970            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1971              !!!cp (124.7);
1972              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1973            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1974              if ($self->{current_token}->{attributes}) {
1975                !!!cp (124.5);
1976                !!!parse-error (type => 'end tag attribute');
1977              } else {
1978                ## NOTE: This state should never be reached.
1979                !!!cp (124.6);
1980              }
1981          } else {          } else {
1982            $token->{data} .= chr ($self->{next_input_character});            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!next-input-character;  
           redo BC;  
1983          }          }
1984        } # BC          $self->{state} = DATA_STATE;
1985      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {          ## Reconsume.
1986            !!!emit ($self->{current_token}); # start tag or end tag
1987            redo A;
1988          } else {
1989            !!!cp ('124.4');
1990            !!!parse-error (type => 'nestc');
1991            ## TODO: This error type is wrong.
1992            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1993            ## Reconsume.
1994            redo A;
1995          }
1996        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1997        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
1998    
1999        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2000        push @next_char, $self->{next_input_character};        ## consumes characters one-by-one basis.
2001                
2002        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x003E) { # >
2003            !!!cp (124);
2004            $self->{state} = DATA_STATE;
2005          !!!next-input-character;          !!!next-input-character;
2006          push @next_char, $self->{next_input_character};  
2007          if ($self->{next_input_character} == 0x002D) { # -          !!!emit ($self->{current_token}); # comment
2008            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};          redo A;
2009            $self->{state} = COMMENT_START_STATE;        } elsif ($self->{next_char} == -1) {
2010            !!!next-input-character;          !!!cp (125);
2011            redo A;          $self->{state} = DATA_STATE;
2012          }          ## reconsume
2013        } elsif ($self->{next_input_character} == 0x0044 or # D  
2014                 $self->{next_input_character} == 0x0064) { # d          !!!emit ($self->{current_token}); # comment
2015            redo A;
2016          } else {
2017            !!!cp (126);
2018            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2019            ## Stay in the state.
2020          !!!next-input-character;          !!!next-input-character;
2021          push @next_char, $self->{next_input_character};          redo A;
2022          if ($self->{next_input_character} == 0x004F or # O        }
2023              $self->{next_input_character} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2024            !!!next-input-character;        ## (only happen if PCDATA state)
2025            push @next_char, $self->{next_input_character};        
2026            if ($self->{next_input_character} == 0x0043 or # C        if ($self->{next_char} == 0x002D) { # -
2027                $self->{next_input_character} == 0x0063) { # c          !!!cp (133);
2028              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2029              push @next_char, $self->{next_input_character};          !!!next-input-character;
2030              if ($self->{next_input_character} == 0x0054 or # T          redo A;
2031                  $self->{next_input_character} == 0x0074) { # t        } elsif ($self->{next_char} == 0x0044 or # D
2032                !!!next-input-character;                 $self->{next_char} == 0x0064) { # d
2033                push @next_char, $self->{next_input_character};          ## ASCII case-insensitive.
2034                if ($self->{next_input_character} == 0x0059 or # Y          !!!cp (130);
2035                    $self->{next_input_character} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2036                  !!!next-input-character;          $self->{state_keyword} = chr $self->{next_char};
2037                  push @next_char, $self->{next_input_character};          !!!next-input-character;
2038                  if ($self->{next_input_character} == 0x0050 or # P          redo A;
2039                      $self->{next_input_character} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2040                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2041                    push @next_char, $self->{next_input_character};                 $self->{next_char} == 0x005B) { # [
2042                    if ($self->{next_input_character} == 0x0045 or # E          !!!cp (135.4);                
2043                        $self->{next_input_character} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2044                      ## ISSUE: What a stupid code this is!          $self->{state_keyword} = '[';
2045                      $self->{state} = DOCTYPE_STATE;          !!!next-input-character;
2046                      !!!next-input-character;          redo A;
2047                      redo A;        } else {
2048                    }          !!!cp (136);
                 }  
               }  
             }  
           }  
         }  
2049        }        }
2050    
2051        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2052        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2053        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2054          ## Reconsume.
2055        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2056          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2057                                    line => $self->{line_prev},
2058                                    column => $self->{column_prev} - 1,
2059                                   };
2060        redo A;        redo A;
2061              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2062        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2063        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?          !!!cp (127);
2064            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2065                                      line => $self->{line_prev},
2066                                      column => $self->{column_prev} - 2,
2067                                     };
2068            $self->{state} = COMMENT_START_STATE;
2069            !!!next-input-character;
2070            redo A;
2071          } else {
2072            !!!cp (128);
2073            !!!parse-error (type => 'bogus comment',
2074                            line => $self->{line_prev},
2075                            column => $self->{column_prev} - 2);
2076            $self->{state} = BOGUS_COMMENT_STATE;
2077            ## Reconsume.
2078            $self->{current_token} = {type => COMMENT_TOKEN,
2079                                      data => '-',
2080                                      line => $self->{line_prev},
2081                                      column => $self->{column_prev} - 2,
2082                                     };
2083            redo A;
2084          }
2085        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2086          ## ASCII case-insensitive.
2087          if ($self->{next_char} == [
2088                undef,
2089                0x004F, # O
2090                0x0043, # C
2091                0x0054, # T
2092                0x0059, # Y
2093                0x0050, # P
2094              ]->[length $self->{state_keyword}] or
2095              $self->{next_char} == [
2096                undef,
2097                0x006F, # o
2098                0x0063, # c
2099                0x0074, # t
2100                0x0079, # y
2101                0x0070, # p
2102              ]->[length $self->{state_keyword}]) {
2103            !!!cp (131);
2104            ## Stay in the state.
2105            $self->{state_keyword} .= chr $self->{next_char};
2106            !!!next-input-character;
2107            redo A;
2108          } elsif ((length $self->{state_keyword}) == 6 and
2109                   ($self->{next_char} == 0x0045 or # E
2110                    $self->{next_char} == 0x0065)) { # e
2111            !!!cp (129);
2112            $self->{state} = DOCTYPE_STATE;
2113            $self->{current_token} = {type => DOCTYPE_TOKEN,
2114                                      quirks => 1,
2115                                      line => $self->{line_prev},
2116                                      column => $self->{column_prev} - 7,
2117                                     };
2118            !!!next-input-character;
2119            redo A;
2120          } else {
2121            !!!cp (132);        
2122            !!!parse-error (type => 'bogus comment',
2123                            line => $self->{line_prev},
2124                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2125            $self->{state} = BOGUS_COMMENT_STATE;
2126            ## Reconsume.
2127            $self->{current_token} = {type => COMMENT_TOKEN,
2128                                      data => $self->{state_keyword},
2129                                      line => $self->{line_prev},
2130                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2131                                     };
2132            redo A;
2133          }
2134        } elsif ($self->{state} == MD_CDATA_STATE) {
2135          if ($self->{next_char} == {
2136                '[' => 0x0043, # C
2137                '[C' => 0x0044, # D
2138                '[CD' => 0x0041, # A
2139                '[CDA' => 0x0054, # T
2140                '[CDAT' => 0x0041, # A
2141              }->{$self->{state_keyword}}) {
2142            !!!cp (135.1);
2143            ## Stay in the state.
2144            $self->{state_keyword} .= chr $self->{next_char};
2145            !!!next-input-character;
2146            redo A;
2147          } elsif ($self->{state_keyword} eq '[CDATA' and
2148                   $self->{next_char} == 0x005B) { # [
2149            !!!cp (135.2);
2150            $self->{current_token} = {type => CHARACTER_TOKEN,
2151                                      data => '',
2152                                      line => $self->{line_prev},
2153                                      column => $self->{column_prev} - 7};
2154            $self->{state} = CDATA_SECTION_STATE;
2155            !!!next-input-character;
2156            redo A;
2157          } else {
2158            !!!cp (135.3);
2159            !!!parse-error (type => 'bogus comment',
2160                            line => $self->{line_prev},
2161                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2162            $self->{state} = BOGUS_COMMENT_STATE;
2163            ## Reconsume.
2164            $self->{current_token} = {type => COMMENT_TOKEN,
2165                                      data => $self->{state_keyword},
2166                                      line => $self->{line_prev},
2167                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2168                                     };
2169            redo A;
2170          }
2171      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2172        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2173            !!!cp (137);
2174          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2175          !!!next-input-character;          !!!next-input-character;
2176          redo A;          redo A;
2177        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2178            !!!cp (138);
2179          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2180          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2181          !!!next-input-character;          !!!next-input-character;
# Line 1308  sub _get_next_token ($) { Line 2183  sub _get_next_token ($) {
2183          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2184    
2185          redo A;          redo A;
2186        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2187            !!!cp (139);
2188          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2189          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2190          ## reconsume          ## reconsume
# Line 1317  sub _get_next_token ($) { Line 2193  sub _get_next_token ($) {
2193    
2194          redo A;          redo A;
2195        } else {        } else {
2196            !!!cp (140);
2197          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2198              .= chr ($self->{next_input_character});              .= chr ($self->{next_char});
2199          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2200          !!!next-input-character;          !!!next-input-character;
2201          redo A;          redo A;
2202        }        }
2203      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2204        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2205            !!!cp (141);
2206          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2207          !!!next-input-character;          !!!next-input-character;
2208          redo A;          redo A;
2209        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2210            !!!cp (142);
2211          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2212          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2213          !!!next-input-character;          !!!next-input-character;
# Line 1336  sub _get_next_token ($) { Line 2215  sub _get_next_token ($) {
2215          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2216    
2217          redo A;          redo A;
2218        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2219            !!!cp (143);
2220          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2221          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2222          ## reconsume          ## reconsume
# Line 1345  sub _get_next_token ($) { Line 2225  sub _get_next_token ($) {
2225    
2226          redo A;          redo A;
2227        } else {        } else {
2228            !!!cp (144);
2229          $self->{current_token}->{data} # comment          $self->{current_token}->{data} # comment
2230              .= '-' . chr ($self->{next_input_character});              .= '-' . chr ($self->{next_char});
2231          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2232          !!!next-input-character;          !!!next-input-character;
2233          redo A;          redo A;
2234        }        }
2235      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2236        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2237            !!!cp (145);
2238          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2239          !!!next-input-character;          !!!next-input-character;
2240          redo A;          redo A;
2241        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2242            !!!cp (146);
2243          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2244          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2245          ## reconsume          ## reconsume
# Line 1365  sub _get_next_token ($) { Line 2248  sub _get_next_token ($) {
2248    
2249          redo A;          redo A;
2250        } else {        } else {
2251          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2252            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2253          ## Stay in the state          ## Stay in the state
2254          !!!next-input-character;          !!!next-input-character;
2255          redo A;          redo A;
2256        }        }
2257      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2258        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2259            !!!cp (148);
2260          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2261          !!!next-input-character;          !!!next-input-character;
2262          redo A;          redo A;
2263        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2264            !!!cp (149);
2265          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2266          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2267          ## reconsume          ## reconsume
# Line 1384  sub _get_next_token ($) { Line 2270  sub _get_next_token ($) {
2270    
2271          redo A;          redo A;
2272        } else {        } else {
2273          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2274            $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment
2275          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2276          !!!next-input-character;          !!!next-input-character;
2277          redo A;          redo A;
2278        }        }
2279      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2280        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2281            !!!cp (151);
2282          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2283          !!!next-input-character;          !!!next-input-character;
2284    
2285          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{current_token}); # comment
2286    
2287          redo A;          redo A;
2288        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2289          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2290            !!!parse-error (type => 'dash in comment',
2291                            line => $self->{line_prev},
2292                            column => $self->{column_prev});
2293          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2294          ## Stay in the state          ## Stay in the state
2295          !!!next-input-character;          !!!next-input-character;
2296          redo A;          redo A;
2297        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2298            !!!cp (153);
2299          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2300          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2301          ## reconsume          ## reconsume
# Line 1412  sub _get_next_token ($) { Line 2304  sub _get_next_token ($) {
2304    
2305          redo A;          redo A;
2306        } else {        } else {
2307          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2308          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2309                            line => $self->{line_prev},
2310                            column => $self->{column_prev});
2311            $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2312          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2313          !!!next-input-character;          !!!next-input-character;
2314          redo A;          redo A;
2315        }        }
2316      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2317        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2318            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2319            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2320            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2321            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2322            !!!cp (155);
2323          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2324          !!!next-input-character;          !!!next-input-character;
2325          redo A;          redo A;
2326        } else {        } else {
2327            !!!cp (156);
2328          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2329          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2330          ## reconsume          ## reconsume
2331          redo A;          redo A;
2332        }        }
2333      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2334        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2335            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2336            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2337            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2338            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2339            !!!cp (157);
2340          ## Stay in the state          ## Stay in the state
2341          !!!next-input-character;          !!!next-input-character;
2342          redo A;          redo A;
2343        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2344            !!!cp (158);
2345          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2346          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2347          !!!next-input-character;          !!!next-input-character;
2348    
2349          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2350    
2351          redo A;          redo A;
2352        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2353            !!!cp (159);
2354          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2355          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2356          ## reconsume          ## reconsume
2357    
2358          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2359    
2360          redo A;          redo A;
2361        } else {        } else {
2362          $self->{current_token}          !!!cp (160);
2363              = {type => DOCTYPE_TOKEN,          $self->{current_token}->{name} = chr $self->{next_char};
2364                 name => chr ($self->{next_input_character}),          delete $self->{current_token}->{quirks};
                correct => 1};  
2365  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2366          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2367          !!!next-input-character;          !!!next-input-character;
# Line 1470  sub _get_next_token ($) { Line 2369  sub _get_next_token ($) {
2369        }        }
2370      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2371  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2372        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2373            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2374            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2375            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2376            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2377            !!!cp (161);
2378          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2379          !!!next-input-character;          !!!next-input-character;
2380          redo A;          redo A;
2381        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2382            !!!cp (162);
2383          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2384          !!!next-input-character;          !!!next-input-character;
2385    
2386          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2387    
2388          redo A;          redo A;
2389        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2390            !!!cp (163);
2391          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2392          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2393          ## reconsume          ## reconsume
2394    
2395          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2396          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2397    
2398          redo A;          redo A;
2399        } else {        } else {
2400            !!!cp (164);
2401          $self->{current_token}->{name}          $self->{current_token}->{name}
2402            .= chr ($self->{next_input_character}); # DOCTYPE            .= chr ($self->{next_char}); # DOCTYPE
2403          ## Stay in the state          ## Stay in the state
2404          !!!next-input-character;          !!!next-input-character;
2405          redo A;          redo A;
2406        }        }
2407      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2408        if ($self->{next_input_character} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
2409            $self->{next_input_character} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
2410            $self->{next_input_character} == 0x000B or # VT            $self->{next_char} == 0x000B or # VT
2411            $self->{next_input_character} == 0x000C or # FF            $self->{next_char} == 0x000C or # FF
2412            $self->{next_input_character} == 0x0020) { # SP            $self->{next_char} == 0x0020) { # SP
2413            !!!cp (165);
2414          ## Stay in the state          ## Stay in the state
2415          !!!next-input-character;          !!!next-input-character;
2416          redo A;          redo A;
2417        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2418            !!!cp (166);
2419          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2420          !!!next-input-character;          !!!next-input-character;
2421    
2422          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2423    
2424          redo A;          redo A;
2425        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2426            !!!cp (167);
2427          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2428          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2429          ## reconsume          ## reconsume
2430    
2431          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2432          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2433    
2434          redo A;          redo A;
2435        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2436                 $self->{next_input_character} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2437            $self->{state} = PUBLIC_STATE;
2438            $self->{state_keyword} = chr $self->{next_char};
2439          !!!next-input-character;          !!!next-input-character;
2440          if ($self->{next_input_character} == 0x0055 or # U          redo A;
2441              $self->{next_input_character} == 0x0075) { # u        } elsif ($self->{next_char} == 0x0053 or # S
2442            !!!next-input-character;                 $self->{next_char} == 0x0073) { # s
2443            if ($self->{next_input_character} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2444                $self->{next_input_character} == 0x0062) { # b          $self->{state_keyword} = chr $self->{next_char};
             !!!next-input-character;  
             if ($self->{next_input_character} == 0x004C or # L  
                 $self->{next_input_character} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_input_character} == 0x0049 or # I  
                   $self->{next_input_character} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_input_character} == 0x0043 or # C  
                     $self->{next_input_character} == 0x0063) { # c  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
       } elsif ($self->{next_input_character} == 0x0053 or # S  
                $self->{next_input_character} == 0x0073) { # s  
2445          !!!next-input-character;          !!!next-input-character;
2446          if ($self->{next_input_character} == 0x0059 or # Y          redo A;
             $self->{next_input_character} == 0x0079) { # y  
           !!!next-input-character;  
           if ($self->{next_input_character} == 0x0053 or # S  
               $self->{next_input_character} == 0x0073) { # s  
             !!!next-input-character;  
             if ($self->{next_input_character} == 0x0054 or # T  
                 $self->{next_input_character} == 0x0074) { # t  
               !!!next-input-character;  
               if ($self->{next_input_character} == 0x0045 or # E  
                   $self->{next_input_character} == 0x0065) { # e  
                 !!!next-input-character;  
                 if ($self->{next_input_character} == 0x004D or # M  
                     $self->{next_input_character} == 0x006D) { # m  
                   $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
2447        } else {        } else {
2448            !!!cp (180);
2449            !!!parse-error (type => 'string after DOCTYPE name');
2450            $self->{current_token}->{quirks} = 1;
2451    
2452            $self->{state} = BOGUS_DOCTYPE_STATE;
2453          !!!next-input-character;          !!!next-input-character;
2454          #          redo A;
2455          }
2456        } elsif ($self->{state} == PUBLIC_STATE) {
2457          ## ASCII case-insensitive
2458          if ($self->{next_char} == [
2459                undef,
2460                0x0055, # U
2461                0x0042, # B
2462                0x004C, # L
2463                0x0049, # I
2464              ]->[length $self->{state_keyword}] or
2465              $self->{next_char} == [
2466                undef,
2467                0x0075, # u
2468                0x0062, # b
2469                0x006C, # l
2470                0x0069, # i
2471              ]->[length $self->{state_keyword}]) {
2472            !!!cp (175);
2473            ## Stay in the state.
2474            $self->{state_keyword} .= chr $self->{next_char};
2475            !!!next-input-character;
2476            redo A;
2477          } elsif ((length $self->{state_keyword}) == 5 and
2478                   ($self->{next_char} == 0x0043 or # C
2479                    $self->{next_char} == 0x0063)) { # c
2480            !!!cp (168);
2481            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2482            !!!next-input-character;
2483            redo A;
2484          } else {
2485            !!!cp (169);
2486            !!!parse-error (type => 'string after DOCTYPE name',
2487                            line => $self->{line_prev},
2488                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2489            $self->{current_token}->{quirks} = 1;
2490    
2491            $self->{state} = BOGUS_DOCTYPE_STATE;
2492            ## Reconsume.
2493            redo A;
2494        }        }
2495        } elsif ($self->{state} == SYSTEM_STATE) {
2496          ## ASCII case-insensitive
2497          if ($self->{next_char} == [
2498                undef,
2499                0x0059, # Y
2500                0x0053, # S
2501                0x0054, # T
2502                0x0045, # E
2503              ]->[length $self->{state_keyword}] or
2504              $self->{next_char} == [
2505                undef,
2506                0x0079, # y
2507                0x0073, # s
2508                0x0074, # t
2509                0x0065, # e
2510              ]->[length $self->{state_keyword}]) {
2511            !!!cp (170);
2512            ## Stay in the state.
2513            $self->{state_keyword} .= chr $self->{next_char};
2514            !!!next-input-character;
2515            redo A;
2516          } elsif ((length $self->{state_keyword}) == 5 and
2517                   ($self->{next_char} == 0x004D or # M
2518                    $self->{next_char} == 0x006D)) { # m
2519            !!!cp (171);
2520            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2521            !!!next-input-character;
2522            redo A;
2523          } else {
2524            !!!cp (172);
2525            !!!parse-error (type => 'string after DOCTYPE name',
2526                            line => $self->{line_prev},
2527                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2528            $self->{current_token}->{quirks} = 1;
2529    
2530        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2531        $self->{state} = BOGUS_DOCTYPE_STATE;          ## Reconsume.
2532        # next-input-character is already done          redo A;
2533        redo A;        }
2534      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2535        if ({        if ({
2536              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2537              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2538            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2539            !!!cp (181);
2540          ## Stay in the state          ## Stay in the state
2541          !!!next-input-character;          !!!next-input-character;
2542          redo A;          redo A;
2543        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{next_char} eq 0x0022) { # "
2544            !!!cp (182);
2545          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2546          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2547          !!!next-input-character;          !!!next-input-character;
2548          redo A;          redo A;
2549        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{next_char} eq 0x0027) { # '
2550            !!!cp (183);
2551          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{current_token}->{public_identifier} = ''; # DOCTYPE
2552          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2553          !!!next-input-character;          !!!next-input-character;
2554          redo A;          redo A;
2555        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{next_char} eq 0x003E) { # >
2556            !!!cp (184);
2557          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2558    
2559          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2560          !!!next-input-character;          !!!next-input-character;
2561    
2562          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2563          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2564    
2565          redo A;          redo A;
2566        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2567            !!!cp (185);
2568          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2569    
2570          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2571          ## reconsume          ## reconsume
2572    
2573          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2574          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2575    
2576          redo A;          redo A;
2577        } else {        } else {
2578            !!!cp (186);
2579          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2580            $self->{current_token}->{quirks} = 1;
2581    
2582          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2583          !!!next-input-character;          !!!next-input-character;
2584          redo A;          redo A;
2585        }        }
2586      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2587        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2588            !!!cp (187);
2589          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2590          !!!next-input-character;          !!!next-input-character;
2591          redo A;          redo A;
2592        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2593            !!!cp (188);
2594          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2595    
2596          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2597          !!!next-input-character;          !!!next-input-character;
2598    
2599          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2600          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2601    
2602          redo A;          redo A;
2603        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2604            !!!cp (189);
2605          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2606    
2607          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2608          ## reconsume          ## reconsume
2609    
2610          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2611          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2612    
2613          redo A;          redo A;
2614        } else {        } else {
2615            !!!cp (190);
2616          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2617              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2618          ## Stay in the state          ## Stay in the state
2619          !!!next-input-character;          !!!next-input-character;
2620          redo A;          redo A;
2621        }        }
2622      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2623        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2624            !!!cp (191);
2625          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2626          !!!next-input-character;          !!!next-input-character;
2627          redo A;          redo A;
2628        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2629            !!!cp (192);
2630          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2631    
2632          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2633          !!!next-input-character;          !!!next-input-character;
2634    
2635          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2636          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2637    
2638          redo A;          redo A;
2639        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2640            !!!cp (193);
2641          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2642    
2643          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2644          ## reconsume          ## reconsume
2645    
2646          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2647          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2648    
2649          redo A;          redo A;
2650        } else {        } else {
2651            !!!cp (194);
2652          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2653              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2654          ## Stay in the state          ## Stay in the state
2655          !!!next-input-character;          !!!next-input-character;
2656          redo A;          redo A;
# Line 1701  sub _get_next_token ($) { Line 2659  sub _get_next_token ($) {
2659        if ({        if ({
2660              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2661              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2662            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2663            !!!cp (195);
2664          ## Stay in the state          ## Stay in the state
2665          !!!next-input-character;          !!!next-input-character;
2666          redo A;          redo A;
2667        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2668            !!!cp (196);
2669          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2670          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2671          !!!next-input-character;          !!!next-input-character;
2672          redo A;          redo A;
2673        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2674            !!!cp (197);
2675          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2676          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2677          !!!next-input-character;          !!!next-input-character;
2678          redo A;          redo A;
2679        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2680            !!!cp (198);
2681          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2682          !!!next-input-character;          !!!next-input-character;
2683    
2684          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2685    
2686          redo A;          redo A;
2687        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2688            !!!cp (199);
2689          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2690    
2691          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2692          ## reconsume          ## reconsume
2693    
2694          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2695          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2696    
2697          redo A;          redo A;
2698        } else {        } else {
2699            !!!cp (200);
2700          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2701            $self->{current_token}->{quirks} = 1;
2702    
2703          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2704          !!!next-input-character;          !!!next-input-character;
2705          redo A;          redo A;
# Line 1742  sub _get_next_token ($) { Line 2708  sub _get_next_token ($) {
2708        if ({        if ({
2709              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2710              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2711            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2712            !!!cp (201);
2713          ## Stay in the state          ## Stay in the state
2714          !!!next-input-character;          !!!next-input-character;
2715          redo A;          redo A;
2716        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{next_char} == 0x0022) { # "
2717            !!!cp (202);
2718          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2719          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2720          !!!next-input-character;          !!!next-input-character;
2721          redo A;          redo A;
2722        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{next_char} == 0x0027) { # '
2723            !!!cp (203);
2724          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{current_token}->{system_identifier} = ''; # DOCTYPE
2725          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2726          !!!next-input-character;          !!!next-input-character;
2727          redo A;          redo A;
2728        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2729            !!!cp (204);
2730          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2731          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2732          !!!next-input-character;          !!!next-input-character;
2733    
2734          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2735          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2736    
2737          redo A;          redo A;
2738        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2739            !!!cp (205);
2740          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2741    
2742          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2743          ## reconsume          ## reconsume
2744    
2745          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2746          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2747    
2748          redo A;          redo A;
2749        } else {        } else {
2750            !!!cp (206);
2751          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2752            $self->{current_token}->{quirks} = 1;
2753    
2754          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2755          !!!next-input-character;          !!!next-input-character;
2756          redo A;          redo A;
2757        }        }
2758      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2759        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{next_char} == 0x0022) { # "
2760            !!!cp (207);
2761          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2762          !!!next-input-character;          !!!next-input-character;
2763          redo A;          redo A;
2764        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2765          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!cp (208);
2766            !!!parse-error (type => 'unclosed SYSTEM literal');
2767    
2768          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2769          !!!next-input-character;          !!!next-input-character;
2770    
2771          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2772          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2773    
2774          redo A;          redo A;
2775        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2776            !!!cp (209);
2777          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2778    
2779          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2780          ## reconsume          ## reconsume
2781    
2782          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2783          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2784    
2785          redo A;          redo A;
2786        } else {        } else {
2787            !!!cp (210);
2788          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2789              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2790          ## Stay in the state          ## Stay in the state
2791          !!!next-input-character;          !!!next-input-character;
2792          redo A;          redo A;
2793        }        }
2794      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2795        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{next_char} == 0x0027) { # '
2796            !!!cp (211);
2797          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2798          !!!next-input-character;          !!!next-input-character;
2799          redo A;          redo A;
2800        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2801          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!cp (212);
2802            !!!parse-error (type => 'unclosed SYSTEM literal');
2803    
2804          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2805          !!!next-input-character;          !!!next-input-character;
2806    
2807          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2808          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2809    
2810          redo A;          redo A;
2811        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2812            !!!cp (213);
2813          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2814    
2815          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2816          ## reconsume          ## reconsume
2817    
2818          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2819          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2820    
2821          redo A;          redo A;
2822        } else {        } else {
2823            !!!cp (214);
2824          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2825              .= chr $self->{next_input_character};              .= chr $self->{next_char};
2826          ## Stay in the state          ## Stay in the state
2827          !!!next-input-character;          !!!next-input-character;
2828          redo A;          redo A;
# Line 1849  sub _get_next_token ($) { Line 2831  sub _get_next_token ($) {
2831        if ({        if ({
2832              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2833              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2834            }->{$self->{next_input_character}}) {            }->{$self->{next_char}}) {
2835            !!!cp (215);
2836          ## Stay in the state          ## Stay in the state
2837          !!!next-input-character;          !!!next-input-character;
2838          redo A;          redo A;
2839        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2840            !!!cp (216);
2841          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2842          !!!next-input-character;          !!!next-input-character;
2843    
2844          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2845    
2846          redo A;          redo A;
2847        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2848            !!!cp (217);
2849          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2850          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2851          ## reconsume          ## reconsume
2852    
2853          delete $self->{current_token}->{correct};          $self->{current_token}->{quirks} = 1;
2854          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2855    
2856          redo A;          redo A;
2857        } else {        } else {
2858            !!!cp (218);
2859          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2860            #$self->{current_token}->{quirks} = 1;
2861    
2862          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2863          !!!next-input-character;          !!!next-input-character;
2864          redo A;          redo A;
2865        }        }
2866      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2867        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{next_char} == 0x003E) { # >
2868            !!!cp (219);
2869          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2870          !!!next-input-character;          !!!next-input-character;
2871    
         delete $self->{current_token}->{correct};  
2872          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2873    
2874          redo A;          redo A;
2875        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{next_char} == -1) {
2876            !!!cp (220);
2877          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2878          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2879          ## reconsume          ## reconsume
2880    
         delete $self->{current_token}->{correct};  
2881          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{current_token}); # DOCTYPE
2882    
2883          redo A;          redo A;
2884        } else {        } else {
2885            !!!cp (221);
2886          ## Stay in the state          ## Stay in the state
2887          !!!next-input-character;          !!!next-input-character;
2888          redo A;          redo A;
2889        }        }
2890      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2891        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2892      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2893    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2894          
2895    die "$0: _get_next_token: unexpected case";        if ($self->{next_char} == 0x005D) { # ]
2896  } # _get_next_token          !!!cp (221.1);
2897            $self->{state} = CDATA_SECTION_MSE1_STATE;
2898  sub _tokenize_attempt_to_consume_an_entity ($$$) {          !!!next-input-character;
2899    my ($self, $in_attr, $additional) = @_;          redo A;
2900          } elsif ($self->{next_char} == -1) {
2901            $self->{state} = DATA_STATE;
2902            !!!next-input-character;
2903            if (length $self->{current_token}->{data}) { # character
2904              !!!cp (221.2);
2905              !!!emit ($self->{current_token}); # character
2906            } else {
2907              !!!cp (221.3);
2908              ## No token to emit. $self->{current_token} is discarded.
2909            }        
2910            redo A;
2911          } else {
2912            !!!cp (221.4);
2913            $self->{current_token}->{data} .= chr $self->{next_char};
2914            ## Stay in the state.
2915            !!!next-input-character;
2916            redo A;
2917          }
2918    
2919    if ({        ## ISSUE: "text tokens" in spec.
2920         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2921         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{next_char} == 0x005D) { # ]
2922         $additional => 1,          !!!cp (221.5);
2923        }->{$self->{next_input_character}}) {          $self->{state} = CDATA_SECTION_MSE2_STATE;
2924      ## Don't consume          !!!next-input-character;
2925      ## No error          redo A;
2926      return undef;        } else {
2927    } elsif ($self->{next_input_character} == 0x0023) { # #          !!!cp (221.6);
2928      !!!next-input-character;          $self->{current_token}->{data} .= ']';
2929      if ($self->{next_input_character} == 0x0078 or # x          $self->{state} = CDATA_SECTION_STATE;
2930          $self->{next_input_character} == 0x0058) { # X          ## Reconsume.
2931        my $code;          redo A;
2932        X: {        }
2933          my $x_char = $self->{next_input_character};      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2934          !!!next-input-character;        if ($self->{next_char} == 0x003E) { # >
2935          if (0x0030 <= $self->{next_input_character} and          $self->{state} = DATA_STATE;
2936              $self->{next_input_character} <= 0x0039) { # 0..9          !!!next-input-character;
2937            $code ||= 0;          if (length $self->{current_token}->{data}) { # character
2938            $code *= 0x10;            !!!cp (221.7);
2939            $code += $self->{next_input_character} - 0x0030;            !!!emit ($self->{current_token}); # character
           redo X;  
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0066) { # a..f  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0046) { # A..F  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!parse-error (type => 'bare hcro');  
           !!!back-next-input-character ($x_char, $self->{next_input_character});  
           $self->{next_input_character} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_input_character} == 0x003B) { # ;  
           !!!next-input-character;  
2940          } else {          } else {
2941            !!!parse-error (type => 'no refc');            !!!cp (221.8);
2942              ## No token to emit. $self->{current_token} is discarded.
2943          }          }
2944            redo A;
2945          } elsif ($self->{next_char} == 0x005D) { # ]
2946            !!!cp (221.9); # character
2947            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2948            ## Stay in the state.
2949            !!!next-input-character;
2950            redo A;
2951          } else {
2952            !!!cp (221.11);
2953            $self->{current_token}->{data} .= ']]'; # character
2954            $self->{state} = CDATA_SECTION_STATE;
2955            ## Reconsume.
2956            redo A;
2957          }
2958        } elsif ($self->{state} == ENTITY_STATE) {
2959          if ({
2960            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2961            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
2962            $self->{entity_additional} => 1,
2963          }->{$self->{next_char}}) {
2964            !!!cp (1001);
2965            ## Don't consume
2966            ## No error
2967            ## Return nothing.
2968            #
2969          } elsif ($self->{next_char} == 0x0023) { # #
2970            !!!cp (999);
2971            $self->{state} = ENTITY_HASH_STATE;
2972            $self->{state_keyword} = '#';
2973            !!!next-input-character;
2974            redo A;
2975          } elsif ((0x0041 <= $self->{next_char} and
2976                    $self->{next_char} <= 0x005A) or # A..Z
2977                   (0x0061 <= $self->{next_char} and
2978                    $self->{next_char} <= 0x007A)) { # a..z
2979            !!!cp (998);
2980            require Whatpm::_NamedEntityList;
2981            $self->{state} = ENTITY_NAME_STATE;
2982            $self->{state_keyword} = chr $self->{next_char};
2983            $self->{entity__value} = $self->{state_keyword};
2984            $self->{entity__match} = 0;
2985            !!!next-input-character;
2986            redo A;
2987          } else {
2988            !!!cp (1027);
2989            !!!parse-error (type => 'bare ero');
2990            ## Return nothing.
2991            #
2992          }
2993    
2994          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
2995            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## reference" algorithm.  In other word, there is an "&" character
2996            $code = 0xFFFD;        ## that does not introduce a character reference, which would be
2997          } elsif ($code > 0x10FFFF) {        ## appended to the parent element or the attribute value in later
2998            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        ## process of the tokenizer.
2999            $code = 0xFFFD;  
3000          } elsif ($code == 0x000D) {        if ($self->{prev_state} == DATA_STATE) {
3001            !!!parse-error (type => 'CR character reference');          !!!cp (997);
3002            $code = 0x000A;          $self->{state} = $self->{prev_state};
3003          } elsif (0x80 <= $code and $code <= 0x9F) {          ## Reconsume.
3004            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3005            $code = $c1_entity_char->{$code};                    line => $self->{line_prev},
3006          }                    column => $self->{column_prev},
3007                     });
3008          return {type => CHARACTER_TOKEN, data => chr $code,          redo A;
3009                  has_reference => 1};        } else {
3010        } # X          !!!cp (996);
3011      } elsif (0x0030 <= $self->{next_input_character} and          $self->{current_attribute}->{value} .= '&';
3012               $self->{next_input_character} <= 0x0039) { # 0..9          $self->{state} = $self->{prev_state};
3013        my $code = $self->{next_input_character} - 0x0030;          ## Reconsume.
3014        !!!next-input-character;          redo A;
3015                }
3016        while (0x0030 <= $self->{next_input_character} and      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3017                  $self->{next_input_character} <= 0x0039) { # 0..9        if ($self->{next_char} == 0x0078 or # x
3018          $code *= 10;            $self->{next_char} == 0x0058) { # X
3019          $code += $self->{next_input_character} - 0x0030;          !!!cp (995);
3020            $self->{state} = HEXREF_X_STATE;
3021            $self->{state_keyword} .= chr $self->{next_char};
3022            !!!next-input-character;
3023            redo A;
3024          } elsif (0x0030 <= $self->{next_char} and
3025                   $self->{next_char} <= 0x0039) { # 0..9
3026            !!!cp (994);
3027            $self->{state} = NCR_NUM_STATE;
3028            $self->{state_keyword} = $self->{next_char} - 0x0030;
3029            !!!next-input-character;
3030            redo A;
3031          } else {
3032            !!!parse-error (type => 'bare nero',
3033                            line => $self->{line_prev},
3034                            column => $self->{column_prev} - 1);
3035    
3036            ## NOTE: According to the spec algorithm, nothing is returned,
3037            ## and then "&#" is appended to the parent element or the attribute
3038            ## value in the later processing.
3039    
3040            if ($self->{prev_state} == DATA_STATE) {
3041              !!!cp (1019);
3042              $self->{state} = $self->{prev_state};
3043              ## Reconsume.
3044              !!!emit ({type => CHARACTER_TOKEN,
3045                        data => '&#',
3046                        line => $self->{line_prev},
3047                        column => $self->{column_prev} - 1,
3048                       });
3049              redo A;
3050            } else {
3051              !!!cp (993);
3052              $self->{current_attribute}->{value} .= '&#';
3053              $self->{state} = $self->{prev_state};
3054              ## Reconsume.
3055              redo A;
3056            }
3057          }
3058        } elsif ($self->{state} == NCR_NUM_STATE) {
3059          if (0x0030 <= $self->{next_char} and
3060              $self->{next_char} <= 0x0039) { # 0..9
3061            !!!cp (1012);
3062            $self->{state_keyword} *= 10;
3063            $self->{state_keyword} += $self->{next_char} - 0x0030;
3064                    
3065            ## Stay in the state.
3066          !!!next-input-character;          !!!next-input-character;
3067            redo A;
3068          } elsif ($self->{next_char} == 0x003B) { # ;
3069            !!!cp (1013);
3070            !!!next-input-character;
3071            #
3072          } else {
3073            !!!cp (1014);
3074            !!!parse-error (type => 'no refc');
3075            ## Reconsume.
3076            #
3077          }
3078    
3079          my $code = $self->{state_keyword};
3080          my $l = $self->{line_prev};
3081          my $c = $self->{column_prev};
3082          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3083            !!!cp (1015);
3084            !!!parse-error (type => 'invalid character reference',
3085                            text => (sprintf 'U+%04X', $code),
3086                            line => $l, column => $c);
3087            $code = 0xFFFD;
3088          } elsif ($code > 0x10FFFF) {
3089            !!!cp (1016);
3090            !!!parse-error (type => 'invalid character reference',
3091                            text => (sprintf 'U-%08X', $code),
3092                            line => $l, column => $c);
3093            $code = 0xFFFD;
3094          } elsif ($code == 0x000D) {
3095            !!!cp (1017);
3096            !!!parse-error (type => 'CR character reference',
3097                            line => $l, column => $c);
3098            $code = 0x000A;
3099          } elsif (0x80 <= $code and $code <= 0x9F) {
3100            !!!cp (1018);
3101            !!!parse-error (type => 'C1 character reference',
3102                            text => (sprintf 'U+%04X', $code),
3103                            line => $l, column => $c);
3104            $code = $c1_entity_char->{$code};
3105        }        }
3106    
3107        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{prev_state} == DATA_STATE) {
3108            !!!cp (992);
3109            $self->{state} = $self->{prev_state};
3110            ## Reconsume.
3111            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3112                      line => $l, column => $c,
3113                     });
3114            redo A;
3115          } else {
3116            !!!cp (991);
3117            $self->{current_attribute}->{value} .= chr $code;
3118            $self->{current_attribute}->{has_reference} = 1;
3119            $self->{state} = $self->{prev_state};
3120            ## Reconsume.
3121            redo A;
3122          }
3123        } elsif ($self->{state} == HEXREF_X_STATE) {
3124          if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3125              (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3126              (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3127            # 0..9, A..F, a..f
3128            !!!cp (990);
3129            $self->{state} = HEXREF_HEX_STATE;
3130            $self->{state_keyword} = 0;
3131            ## Reconsume.
3132            redo A;
3133          } else {
3134            !!!parse-error (type => 'bare hcro',
3135                            line => $self->{line_prev},
3136                            column => $self->{column_prev} - 2);
3137    
3138            ## NOTE: According to the spec algorithm, nothing is returned,
3139            ## and then "&#" followed by "X" or "x" is appended to the parent
3140            ## element or the attribute value in the later processing.
3141    
3142            if ($self->{prev_state} == DATA_STATE) {
3143              !!!cp (1005);
3144              $self->{state} = $self->{prev_state};
3145              ## Reconsume.
3146              !!!emit ({type => CHARACTER_TOKEN,
3147                        data => '&' . $self->{state_keyword},
3148                        line => $self->{line_prev},
3149                        column => $self->{column_prev} - length $self->{state_keyword},
3150                       });
3151              redo A;
3152            } else {
3153              !!!cp (989);
3154              $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3155              $self->{state} = $self->{prev_state};
3156              ## Reconsume.
3157              redo A;
3158            }
3159          }
3160        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3161          if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3162            # 0..9
3163            !!!cp (1002);
3164            $self->{state_keyword} *= 0x10;
3165            $self->{state_keyword} += $self->{next_char} - 0x0030;
3166            ## Stay in the state.
3167            !!!next-input-character;
3168            redo A;
3169          } elsif (0x0061 <= $self->{next_char} and
3170                   $self->{next_char} <= 0x0066) { # a..f
3171            !!!cp (1003);
3172            $self->{state_keyword} *= 0x10;
3173            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3174            ## Stay in the state.
3175            !!!next-input-character;
3176            redo A;
3177          } elsif (0x0041 <= $self->{next_char} and
3178                   $self->{next_char} <= 0x0046) { # A..F
3179            !!!cp (1004);
3180            $self->{state_keyword} *= 0x10;
3181            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3182            ## Stay in the state.
3183            !!!next-input-character;
3184            redo A;
3185          } elsif ($self->{next_char} == 0x003B) { # ;
3186            !!!cp (1006);
3187          !!!next-input-character;          !!!next-input-character;
3188            #
3189        } else {        } else {
3190          !!!parse-error (type => 'no refc');          !!!cp (1007);
3191            !!!parse-error (type => 'no refc',
3192                            line => $self->{line},
3193                            column => $self->{column});
3194            ## Reconsume.
3195            #
3196        }        }
3197    
3198          my $code = $self->{state_keyword};
3199          my $l = $self->{line_prev};
3200          my $c = $self->{column_prev};
3201        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3202          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1008);
3203            !!!parse-error (type => 'invalid character reference',
3204                            text => (sprintf 'U+%04X', $code),
3205                            line => $l, column => $c);
3206          $code = 0xFFFD;          $code = 0xFFFD;
3207        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3208          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1009);
3209            !!!parse-error (type => 'invalid character reference',
3210                            text => (sprintf 'U-%08X', $code),
3211                            line => $l, column => $c);
3212          $code = 0xFFFD;          $code = 0xFFFD;
3213        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3214          !!!parse-error (type => 'CR character reference');          !!!cp (1010);
3215            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3216          $code = 0x000A;          $code = 0x000A;
3217        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3218          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1011);
3219            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3220          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3221        }        }
3222          
3223        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        if ($self->{prev_state} == DATA_STATE) {
3224      } else {          !!!cp (988);
3225        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3226        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3227        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3228        return undef;                    line => $l, column => $c,
3229      }                   });
3230    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
3231              $self->{next_input_character} <= 0x005A) or        } else {
3232             (0x0061 <= $self->{next_input_character} and          !!!cp (987);
3233              $self->{next_input_character} <= 0x007A)) {          $self->{current_attribute}->{value} .= chr $code;
3234      my $entity_name = chr $self->{next_input_character};          $self->{current_attribute}->{has_reference} = 1;
3235      !!!next-input-character;          $self->{state} = $self->{prev_state};
3236            ## Reconsume.
3237      my $value = $entity_name;          redo A;
3238      my $match = 0;        }
3239      require Whatpm::_NamedEntityList;      } elsif ($self->{state} == ENTITY_NAME_STATE) {
3240      our $EntityChar;        if (length $self->{state_keyword} < 30 and
3241              ## NOTE: Some number greater than the maximum length of entity name
3242      while (length $entity_name < 10 and            ((0x0041 <= $self->{next_char} and # a
3243             ## NOTE: Some number greater than the maximum length of entity name              $self->{next_char} <= 0x005A) or # x
3244             ((0x0041 <= $self->{next_input_character} and # a             (0x0061 <= $self->{next_char} and # a
3245               $self->{next_input_character} <= 0x005A) or # x              $self->{next_char} <= 0x007A) or # z
3246              (0x0061 <= $self->{next_input_character} and # a             (0x0030 <= $self->{next_char} and # 0
3247               $self->{next_input_character} <= 0x007A) or # z              $self->{next_char} <= 0x0039) or # 9
3248              (0x0030 <= $self->{next_input_character} and # 0             $self->{next_char} == 0x003B)) { # ;
3249               $self->{next_input_character} <= 0x0039) or # 9          our $EntityChar;
3250              $self->{next_input_character} == 0x003B)) { # ;          $self->{state_keyword} .= chr $self->{next_char};
3251        $entity_name .= chr $self->{next_input_character};          if (defined $EntityChar->{$self->{state_keyword}}) {
3252        if (defined $EntityChar->{$entity_name}) {            if ($self->{next_char} == 0x003B) { # ;
3253          if ($self->{next_input_character} == 0x003B) { # ;              !!!cp (1020);
3254            $value = $EntityChar->{$entity_name};              $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3255            $match = 1;              $self->{entity__match} = 1;
3256            !!!next-input-character;              !!!next-input-character;
3257            last;              #
3258              } else {
3259                !!!cp (1021);
3260                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3261                $self->{entity__match} = -1;
3262                ## Stay in the state.
3263                !!!next-input-character;
3264                redo A;
3265              }
3266          } else {          } else {
3267            $value = $EntityChar->{$entity_name};            !!!cp (1022);
3268            $match = -1;            $self->{entity__value} .= chr $self->{next_char};
3269              $self->{entity__match} *= 2;
3270              ## Stay in the state.
3271            !!!next-input-character;            !!!next-input-character;
3272              redo A;
3273          }          }
       } else {  
         $value .= chr $self->{next_input_character};  
         $match *= 2;  
         !!!next-input-character;  
3274        }        }
3275      }  
3276              my $data;
3277      if ($match > 0) {        my $has_ref;
3278        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        if ($self->{entity__match} > 0) {
3279      } elsif ($match < 0) {          !!!cp (1023);
3280        !!!parse-error (type => 'no refc');          $data = $self->{entity__value};
3281        if ($in_attr and $match < -1) {          $has_ref = 1;
3282          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          #
3283          } elsif ($self->{entity__match} < 0) {
3284            !!!parse-error (type => 'no refc');
3285            if ($self->{prev_state} != DATA_STATE and # in attribute
3286                $self->{entity__match} < -1) {
3287              !!!cp (1024);
3288              $data = '&' . $self->{state_keyword};
3289              #
3290            } else {
3291              !!!cp (1025);
3292              $data = $self->{entity__value};
3293              $has_ref = 1;
3294              #
3295            }
3296        } else {        } else {
3297          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          !!!cp (1026);
3298            !!!parse-error (type => 'bare ero',
3299                            line => $self->{line_prev},
3300                            column => $self->{column_prev});
3301            $data = '&' . $self->{state_keyword};
3302            #
3303          }
3304      
3305          ## NOTE: In these cases, when a character reference is found,
3306          ## it is consumed and a character token is returned, or, otherwise,
3307          ## nothing is consumed and returned, according to the spec algorithm.
3308          ## In this implementation, anything that has been examined by the
3309          ## tokenizer is appended to the parent element or the attribute value
3310          ## as string, either literal string when no character reference or
3311          ## entity-replaced string otherwise, in this stage, since any characters
3312          ## that would not be consumed are appended in the data state or in an
3313          ## appropriate attribute value state anyway.
3314    
3315          if ($self->{prev_state} == DATA_STATE) {
3316            !!!cp (986);
3317            $self->{state} = $self->{prev_state};
3318            ## Reconsume.
3319            !!!emit ({type => CHARACTER_TOKEN,
3320                      data => $data,
3321                      line => $self->{line_prev},
3322                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3323                     });
3324            redo A;
3325          } else {
3326            !!!cp (985);
3327            $self->{current_attribute}->{value} .= $data;
3328            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3329            $self->{state} = $self->{prev_state};
3330            ## Reconsume.
3331            redo A;
3332        }        }
3333      } else {      } else {
3334        !!!parse-error (type => 'bare ero');        die "$0: $self->{state}: Unknown state";
       ## NOTE: "No characters are consumed" in the spec.  
       return {type => CHARACTER_TOKEN, data => '&'.$value};  
3335      }      }
3336    } else {    } # A  
3337      ## no characters are consumed  
3338      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3339      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3340    
3341  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3342    my $self = shift;    my $self = shift;
# Line 2080  sub _initialize_tree_constructor ($) { Line 3345  sub _initialize_tree_constructor ($) {
3345    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3346    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3347    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3348      $self->{document}->set_user_data (manakai_source_line => 1);
3349      $self->{document}->set_user_data (manakai_source_column => 1);
3350  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3351    
3352  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2106  sub _construct_tree ($) { Line 3373  sub _construct_tree ($) {
3373        
3374    !!!next-token;    !!!next-token;
3375    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3376    undef $self->{form_element};    undef $self->{form_element};
3377    undef $self->{head_element};    undef $self->{head_element};
3378    $self->{open_elements} = [];    $self->{open_elements} = [];
3379    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3380    
3381      ## NOTE: The "initial" insertion mode.
3382    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3383    
3384      ## NOTE: The "before html" insertion mode.
3385    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3386      $self->{insertion_mode} = BEFORE_HEAD_IM;
3387    
3388      ## NOTE: The "before head" insertion mode and so on.
3389    $self->_tree_construction_main;    $self->_tree_construction_main;
3390  } # _construct_tree  } # _construct_tree
3391    
3392  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3393    my $self = shift;    my $self = shift;
3394    
3395      ## NOTE: "initial" insertion mode
3396    
3397    INITIAL: {    INITIAL: {
3398      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3399        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2126  sub _tree_construction_initial ($) { Line 3401  sub _tree_construction_initial ($) {
3401        ## language.        ## language.
3402        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3403        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3404        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3405        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3406            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3407          !!!parse-error (type => 'not HTML5');          !!!cp ('t1');
3408            !!!parse-error (type => 'not HTML5', token => $token);
3409        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3410          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3411          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3412          } elsif (defined $token->{public_identifier}) {
3413            if ($token->{public_identifier} eq 'XSLT-compat') {
3414              !!!cp ('t1.2');
3415              !!!parse-error (type => 'XSLT-compat', token => $token,
3416                              level => $self->{level}->{should});
3417            } else {
3418              !!!parse-error (type => 'not HTML5', token => $token);
3419            }
3420          } else {
3421            !!!cp ('t3');
3422            #
3423        }        }
3424                
3425        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3426          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3427          ## NOTE: Default value for both |public_id| and |system_id| attributes
3428          ## are empty strings, so that we don't set any value in missing cases.
3429        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3430            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3431        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2146  sub _tree_construction_initial ($) { Line 3434  sub _tree_construction_initial ($) {
3434        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3435        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3436                
3437        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3438            !!!cp ('t4');
3439          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3440        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3441          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3442          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3443          if ({          my $prefix = [
3444            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3445            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3446            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3447            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3448            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3449            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3450            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3451            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3452            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3453            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3454            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3455            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3456            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3457            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3458            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3459            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3460            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3461            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3462            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3463            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3464            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3465            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3466            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3467            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3468            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3469            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3470            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3471            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3472            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3473            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3474            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3475            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3476            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3477            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3478            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3479            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3480            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3481            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3482            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3483            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3484            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3485            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3486            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3487            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3488            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3489            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3490            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3491            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3492            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3493            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3494            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3495            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3496            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3497            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3498            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3499            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3500            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3501            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3502            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3503            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3504            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3505            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3506            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3507            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3508            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3509            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3510            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,              $pubid eq "HTML") {
3511            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,            !!!cp ('t5');
           "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,  
           "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,  
           "HTML" => 1,  
         }->{$pubid}) {  
3512            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3513          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3514                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3515            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3516                !!!cp ('t6');
3517              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3518            } else {            } else {
3519                !!!cp ('t7');
3520              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3521            }            }
3522          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3523                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3524              !!!cp ('t8');
3525            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3526            } else {
3527              !!!cp ('t9');
3528          }          }
3529          } else {
3530            !!!cp ('t10');
3531        }        }
3532        if (defined $token->{system_identifier}) {        if (defined $token->{system_identifier}) {
3533          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3534          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3535          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") {
3536              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3537              ## marked as quirks.
3538            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3539              !!!cp ('t11');
3540            } else {
3541              !!!cp ('t12');
3542          }          }
3543          } else {
3544            !!!cp ('t13');
3545        }        }
3546                
3547        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3548        !!!next-token;        !!!next-token;
3549        return;        return;
3550      } elsif ({      } elsif ({
# Line 2254  sub _tree_construction_initial ($) { Line 3552  sub _tree_construction_initial ($) {
3552                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3553                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3554               }->{$token->{type}}) {               }->{$token->{type}}) {
3555        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3556          !!!parse-error (type => 'no DOCTYPE', token => $token);
3557        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3558        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3559        ## reprocess        ## reprocess
3560          !!!ack-later;
3561        return;        return;
3562      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3563        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3564          ## Ignore the token          ## Ignore the token
3565    
3566          unless (length $token->{data}) {          unless (length $token->{data}) {
3567            ## Stay in the phase            !!!cp ('t15');
3568              ## Stay in the insertion mode.
3569            !!!next-token;            !!!next-token;
3570            redo INITIAL;            redo INITIAL;
3571            } else {
3572              !!!cp ('t16');
3573          }          }
3574          } else {
3575            !!!cp ('t17');
3576        }        }
3577    
3578        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3579        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3580        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3581        ## reprocess        ## reprocess
3582        return;        return;
3583      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3584          !!!cp ('t18');
3585        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3586        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3587                
3588        ## Stay in the phase.        ## Stay in the insertion mode.
3589        !!!next-token;        !!!next-token;
3590        redo INITIAL;        redo INITIAL;
3591      } else {      } else {
3592        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3593      }      }
3594    } # INITIAL    } # INITIAL
3595    
3596      die "$0: _tree_construction_initial: This should be never reached";
3597  } # _tree_construction_initial  } # _tree_construction_initial
3598    
3599  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3600    my $self = shift;    my $self = shift;
3601    
3602      ## NOTE: "before html" insertion mode.
3603        
3604    B: {    B: {
3605        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3606          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3607            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3608          ## Ignore the token          ## Ignore the token
3609          ## Stay in the phase          ## Stay in the insertion mode.
3610          !!!next-token;          !!!next-token;
3611          redo B;          redo B;
3612        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3613            !!!cp ('t20');
3614          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3615          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3616          ## Stay in the phase          ## Stay in the insertion mode.
3617          !!!next-token;          !!!next-token;
3618          redo B;          redo B;
3619        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2309  sub _tree_construction_root_element ($) Line 3621  sub _tree_construction_root_element ($)
3621            ## Ignore the token.            ## Ignore the token.
3622    
3623            unless (length $token->{data}) {            unless (length $token->{data}) {
3624              ## Stay in the phase              !!!cp ('t21');
3625                ## Stay in the insertion mode.
3626              !!!next-token;              !!!next-token;
3627              redo B;              redo B;
3628              } else {
3629                !!!cp ('t22');
3630            }            }
3631            } else {
3632              !!!cp ('t23');
3633          }          }
3634    
3635          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3636    
3637          #          #
3638        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3639          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3640              $token->{attributes}->{manifest}) {            my $root_element;
3641            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3642                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3643            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3644                  [$root_element, $el_category->{html}];
3645    
3646              if ($token->{attributes}->{manifest}) {
3647                !!!cp ('t24');
3648                $self->{application_cache_selection}
3649                    ->($token->{attributes}->{manifest}->{value});
3650                ## ISSUE: Spec is unclear on relative references.
3651                ## According to Hixie (#whatwg 2008-03-19), it should be
3652                ## resolved against the base URI of the document in HTML
3653                ## or xml:base of the element in XHTML.
3654              } else {
3655                !!!cp ('t25');
3656                $self->{application_cache_selection}->(undef);
3657              }
3658    
3659              !!!nack ('t25c');
3660    
3661              !!!next-token;
3662              return; ## Go to the "before head" insertion mode.
3663          } else {          } else {
3664            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3665              #
3666          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3667        } elsif ({        } elsif ({
3668                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3669                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3670                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3671          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3672          #          #
3673        } else {        } else {
3674          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3675        }        }
3676    
3677        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3678        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3679        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3680        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3681        #redo B;  
3682        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3683    
3684        ## NOTE: Reprocess the token.
3685        !!!ack-later;
3686        return; ## Go to the "before head" insertion mode.
3687    
3688        ## ISSUE: There is an issue in the spec
3689    } # B    } # B
3690    
3691      die "$0: _tree_construction_root_element: This should never be reached";
3692  } # _tree_construction_root_element  } # _tree_construction_root_element
3693    
3694  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2363  sub _reset_insertion_mode ($) { Line 3703  sub _reset_insertion_mode ($) {
3703            
3704      ## Step 3      ## Step 3
3705      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"!?  
3706        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3707          $last = 1;          $last = 1;
3708          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3709            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3710                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3711              #          } else {
3712            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3713          }          }
3714        }        }
3715              
3716        ## Step 4..13        ## Step 4..14
3717        my $new_mode = {        my $new_mode;
3718          if ($node->[1] & FOREIGN_EL) {
3719            !!!cp ('t28.1');
3720            ## NOTE: Strictly spaking, the line below only applies to MathML and
3721            ## SVG elements.  Currently the HTML syntax supports only MathML and
3722            ## SVG elements as foreigners.
3723            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3724          } elsif ($node->[1] & TABLE_CELL_EL) {
3725            if ($last) {
3726              !!!cp ('t28.2');
3727              #
3728            } else {
3729              !!!cp ('t28.3');
3730              $new_mode = IN_CELL_IM;
3731            }
3732          } else {
3733            !!!cp ('t28.4');
3734            $new_mode = {
3735                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3736                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3737                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3738                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3739                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3740                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2395  sub _reset_insertion_mode ($) { Line 3745  sub _reset_insertion_mode ($) {
3745                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3746                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3747                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3748                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3749          }
3750        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3751                
3752        ## Step 14        ## Step 15
3753        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3754          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3755              !!!cp ('t29');
3756            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3757          } else {          } else {
3758              ## ISSUE: Can this state be reached?
3759              !!!cp ('t30');
3760            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3761          }          }
3762          return;          return;
3763          } else {
3764            !!!cp ('t31');
3765        }        }
3766                
3767        ## Step 15        ## Step 16
3768        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3769                
3770        ## Step 16        ## Step 17
3771        $i--;        $i--;
3772        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3773                
3774        ## Step 17        ## Step 18
3775        redo S3;        redo S3;
3776      } # S3      } # S3
3777    
3778      die "$0: _reset_insertion_mode: This line should never be reached";
3779  } # _reset_insertion_mode  } # _reset_insertion_mode
3780    
3781  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2439  sub _tree_construction_main ($) { Line 3797  sub _tree_construction_main ($) {
3797      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3798      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3799        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3800            !!!cp ('t32');
3801          return;          return;
3802        }        }
3803      }      }
# Line 2453  sub _tree_construction_main ($) { Line 3812  sub _tree_construction_main ($) {
3812    
3813        ## Step 6        ## Step 6
3814        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3815            !!!cp ('t33_1');
3816          #          #
3817        } else {        } else {
3818          my $in_open_elements;          my $in_open_elements;
3819          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3820            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3821                !!!cp ('t33');
3822              $in_open_elements = 1;              $in_open_elements = 1;
3823              last OE;              last OE;
3824            }            }
3825          }          }
3826          if ($in_open_elements) {          if ($in_open_elements) {
3827              !!!cp ('t34');
3828            #            #
3829          } else {          } else {
3830              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3831              !!!cp ('t35');
3832            redo S4;            redo S4;
3833          }          }
3834        }        }
# Line 2487  sub _tree_construction_main ($) { Line 3851  sub _tree_construction_main ($) {
3851    
3852        ## Step 11        ## Step 11
3853        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3854            !!!cp ('t36');
3855          ## Step 7'          ## Step 7'
3856          $i++;          $i++;
3857          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3858                    
3859          redo S7;          redo S7;
3860        }        }
3861    
3862          !!!cp ('t37');
3863      } # S7      } # S7
3864    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3865    
3866    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3867      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3868        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3869            !!!cp ('t38');
3870          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3871          return;          return;
3872        }        }
3873      }      }
3874    
3875        !!!cp ('t39');
3876    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3877    
3878    my $parse_rcdata = sub ($$) {    my $insert;
3879      my ($content_model_flag, $insert) = @_;  
3880      my $parse_rcdata = sub ($) {
3881        my ($content_model_flag) = @_;
3882    
3883      ## Step 1      ## Step 1
3884      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3885      my $el;      my $el;
3886      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3887    
3888      ## Step 2      ## Step 2
3889      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3890    
3891      ## Step 3      ## Step 3
3892      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2522  sub _tree_construction_main ($) { Line 3894  sub _tree_construction_main ($) {
3894    
3895      ## Step 4      ## Step 4
3896      my $text = '';      my $text = '';
3897        !!!nack ('t40.1');
3898      !!!next-token;      !!!next-token;
3899      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3900          !!!cp ('t40');
3901        $text .= $token->{data};        $text .= $token->{data};
3902        !!!next-token;        !!!next-token;
3903      }      }
3904    
3905      ## Step 5      ## Step 5
3906      if (length $text) {      if (length $text) {
3907          !!!cp ('t41');
3908        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
3909        $el->append_child ($text);        $el->append_child ($text);
3910      }      }
# Line 2538  sub _tree_construction_main ($) { Line 3913  sub _tree_construction_main ($) {
3913      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
3914    
3915      ## Step 7      ## Step 7
3916      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
3917            $token->{tag_name} eq $start_tag_name) {
3918          !!!cp ('t42');
3919        ## 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});  
3920      } else {      } else {
3921        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3922          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3923            !!!cp ('t43');
3924            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3925          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3926            !!!cp ('t44');
3927            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3928          } else {
3929            die "$0: $content_model_flag in parse_rcdata";
3930          }
3931      }      }
3932      !!!next-token;      !!!next-token;
3933    }; # $parse_rcdata    }; # $parse_rcdata
3934    
3935    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3936      my $script_el;      my $script_el;
3937      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3938      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3939    
3940      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3941      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3942            
3943      my $text = '';      my $text = '';
3944        !!!nack ('t45.1');
3945      !!!next-token;      !!!next-token;
3946      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3947          !!!cp ('t45');
3948        $text .= $token->{data};        $text .= $token->{data};
3949        !!!next-token;        !!!next-token;
3950      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
3951      if (length $text) {      if (length $text) {
3952          !!!cp ('t46');
3953        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
3954      }      }
3955                                
# Line 2573  sub _tree_construction_main ($) { Line 3957  sub _tree_construction_main ($) {
3957    
3958      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
3959          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
3960          !!!cp ('t47');
3961        ## Ignore the token        ## Ignore the token
3962      } else {      } else {
3963        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
3964          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3965        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3966        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3967      }      }
3968            
3969      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
3970          !!!cp ('t49');
3971        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3972      } else {      } else {
3973          !!!cp ('t50');
3974        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
3975        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
3976    
# Line 2596  sub _tree_construction_main ($) { Line 3984  sub _tree_construction_main ($) {
3984      !!!next-token;      !!!next-token;
3985    }; # $script_start_tag    }; # $script_start_tag
3986    
3987      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3988      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3989      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3990    
3991    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3992      my $tag_name = shift;      my $end_tag_token = shift;
3993        my $tag_name = $end_tag_token->{tag_name};
3994    
3995        ## NOTE: The adoption agency algorithm (AAA).
3996    
3997      FET: {      FET: {
3998        ## Step 1        ## Step 1
3999        my $formatting_element;        my $formatting_element;
4000        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4001        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4002          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4003              !!!cp ('t52');
4004              last AFE;
4005            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4006                         eq $tag_name) {
4007              !!!cp ('t51');
4008            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4009            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4010            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4011          }          }
4012        } # AFE        } # AFE
4013        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4014          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4015            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4016          ## Ignore the token          ## Ignore the token
4017          !!!next-token;          !!!next-token;
4018          return;          return;
# Line 2625  sub _tree_construction_main ($) { Line 4024  sub _tree_construction_main ($) {
4024          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4025          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4026            if ($in_scope) {            if ($in_scope) {
4027                !!!cp ('t54');
4028              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4029              last INSCOPE;              last INSCOPE;
4030            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4031              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4032                !!!parse-error (type => 'unmatched end tag',
4033                                text => $token->{tag_name},
4034                                token => $end_tag_token);
4035              ## Ignore the token              ## Ignore the token
4036              !!!next-token;              !!!next-token;
4037              return;              return;
4038            }            }
4039          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4040                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4041            $in_scope = 0;            $in_scope = 0;
4042          }          }
4043        } # INSCOPE        } # INSCOPE
4044        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4045          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4046            !!!parse-error (type => 'unmatched end tag',
4047                            text => $token->{tag_name},
4048                            token => $end_tag_token);
4049          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4050          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4051          return;          return;
4052        }        }
4053        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4054          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4055            !!!parse-error (type => 'not closed',
4056                            text => $self->{open_elements}->[-1]->[0]
4057                                ->manakai_local_name,
4058                            token => $end_tag_token);
4059        }        }
4060                
4061        ## Step 2        ## Step 2
# Line 2655  sub _tree_construction_main ($) { Line 4063  sub _tree_construction_main ($) {
4063        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4064        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4065          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4066          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4067              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4068              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4069               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4070              !!!cp ('t59');
4071            $furthest_block = $node;            $furthest_block = $node;
4072            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4073          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4074              !!!cp ('t60');
4075            last OE;            last OE;
4076          }          }
4077        } # OE        } # OE
4078                
4079        ## Step 3        ## Step 3
4080        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4081            !!!cp ('t61');
4082          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4083          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4084          !!!next-token;          !!!next-token;
# Line 2680  sub _tree_construction_main ($) { Line 4091  sub _tree_construction_main ($) {
4091        ## Step 5        ## Step 5
4092        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4093        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4094            !!!cp ('t62');
4095          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4096        }        }
4097                
# Line 2702  sub _tree_construction_main ($) { Line 4114  sub _tree_construction_main ($) {
4114          S7S2: {          S7S2: {
4115            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4116              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4117                  !!!cp ('t63');
4118                $node_i_in_active = $_;                $node_i_in_active = $_;
4119                last S7S2;                last S7S2;
4120              }              }
# Line 2715  sub _tree_construction_main ($) { Line 4128  sub _tree_construction_main ($) {
4128                    
4129          ## Step 4          ## Step 4
4130          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4131              !!!cp ('t64');
4132            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4133          }          }
4134                    
4135          ## Step 5          ## Step 5
4136          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4137              !!!cp ('t65');
4138            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4139            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4140            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2737  sub _tree_construction_main ($) { Line 4152  sub _tree_construction_main ($) {
4152        } # S7          } # S7  
4153                
4154        ## Step 8        ## Step 8
4155        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4156            my $foster_parent_element;
4157            my $next_sibling;
4158            OE: for (reverse 0..$#{$self->{open_elements}}) {
4159              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4160                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4161                                 if (defined $parent and $parent->node_type == 1) {
4162                                   !!!cp ('t65.1');
4163                                   $foster_parent_element = $parent;
4164                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4165                                 } else {
4166                                   !!!cp ('t65.2');
4167                                   $foster_parent_element
4168                                     = $self->{open_elements}->[$_ - 1]->[0];
4169                                 }
4170                                 last OE;
4171                               }
4172                             } # OE
4173                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4174                               unless defined $foster_parent_element;
4175            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4176            $open_tables->[-1]->[1] = 1; # tainted
4177          } else {
4178            !!!cp ('t65.3');
4179            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4180          }
4181                
4182        ## Step 9        ## Step 9
4183        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2754  sub _tree_construction_main ($) { Line 4194  sub _tree_construction_main ($) {
4194        my $i;        my $i;
4195        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4196          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4197              !!!cp ('t66');
4198            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4199            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4200          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4201              !!!cp ('t67');
4202            $i = $_;            $i = $_;
4203          }          }
4204        } # AFE        } # AFE
# Line 2766  sub _tree_construction_main ($) { Line 4208  sub _tree_construction_main ($) {
4208        undef $i;        undef $i;
4209        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4210          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4211              !!!cp ('t68');
4212            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4213            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4214          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4215              !!!cp ('t69');
4216            $i = $_;            $i = $_;
4217          }          }
4218        } # OE        } # OE
# Line 2779  sub _tree_construction_main ($) { Line 4223  sub _tree_construction_main ($) {
4223      } # FET      } # FET
4224    }; # $formatting_end_tag    }; # $formatting_end_tag
4225    
4226    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4227      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4228    }; # $insert_to_current    }; # $insert_to_current
4229    
4230    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4231                         my $child = shift;      my $child = shift;
4232                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4233                              table => 1, tbody => 1, tfoot => 1,        # MUST
4234                              thead => 1, tr => 1,        my $foster_parent_element;
4235                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4236                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4237                           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') {  
4238                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4239                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4240                                   !!!cp ('t70');
4241                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4242                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4243                               } else {                               } else {
4244                                   !!!cp ('t71');
4245                                 $foster_parent_element                                 $foster_parent_element
4246                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4247                               }                               }
# Line 2809  sub _tree_construction_main ($) { Line 4252  sub _tree_construction_main ($) {
4252                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4253                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4254                             ($child, $next_sibling);                             ($child, $next_sibling);
4255                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4256                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4257                         }        !!!cp ('t72');
4258          $self->{open_elements}->[-1]->[0]->append_child ($child);
4259        }
4260    }; # $insert_to_foster    }; # $insert_to_foster
4261    
4262    my $insert;    B: while (1) {
   
   B: {  
4263      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4264        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
4265          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4266        ## Ignore the token        ## Ignore the token
4267        ## Stay in the phase        ## Stay in the phase
4268        !!!next-token;        !!!next-token;
4269        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;  
4270      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4271               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4272        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4273          ## Turn into the main phase          !!!cp ('t79');
4274          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4275          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4276        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4277          ## Turn into the main phase          !!!cp ('t80');
4278          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4279          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4280          } else {
4281            !!!cp ('t81');
4282        }        }
4283    
4284  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4285  ## 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');  
       }  
4286        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4287        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4288          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4289              !!!cp ('t84');
4290            $top_el->set_attribute_ns            $top_el->set_attribute_ns
4291              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
4292               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4293          }          }
4294        }        }
4295          !!!nack ('t84.1');
4296        !!!next-token;        !!!next-token;
4297        redo B;        next B;
4298      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4299        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4300        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4301            !!!cp ('t85');
4302          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
4303        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4304            !!!cp ('t86');
4305          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
4306        } else {        } else {
4307            !!!cp ('t87');
4308          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4309        }        }
4310        !!!next-token;        !!!next-token;
4311        redo B;        next B;
4312      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4313          if ($token->{type} == CHARACTER_TOKEN) {
4314            !!!cp ('t87.1');
4315            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4316            !!!next-token;
4317            next B;
4318          } elsif ($token->{type} == START_TAG_TOKEN) {
4319            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4320                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4321                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4322                ($token->{tag_name} eq 'svg' and
4323                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4324              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4325              !!!cp ('t87.2');
4326              #
4327            } elsif ({
4328                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4329                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4330                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4331                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4332                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4333                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4334                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4335                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4336                     }->{$token->{tag_name}}) {
4337              !!!cp ('t87.2');
4338              !!!parse-error (type => 'not closed',
4339                              text => $self->{open_elements}->[-1]->[0]
4340                                  ->manakai_local_name,
4341                              token => $token);
4342    
4343              pop @{$self->{open_elements}}
4344                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4345    
4346              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4347              ## Reprocess.
4348              next B;
4349            } else {
4350              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4351              my $tag_name = $token->{tag_name};
4352              if ($nsuri eq $SVG_NS) {
4353                $tag_name = {
4354                   altglyph => 'altGlyph',
4355                   altglyphdef => 'altGlyphDef',
4356                   altglyphitem => 'altGlyphItem',
4357                   animatecolor => 'animateColor',
4358                   animatemotion => 'animateMotion',
4359                   animatetransform => 'animateTransform',
4360                   clippath => 'clipPath',
4361                   feblend => 'feBlend',
4362                   fecolormatrix => 'feColorMatrix',
4363                   fecomponenttransfer => 'feComponentTransfer',
4364                   fecomposite => 'feComposite',
4365                   feconvolvematrix => 'feConvolveMatrix',
4366                   fediffuselighting => 'feDiffuseLighting',
4367                   fedisplacementmap => 'feDisplacementMap',
4368                   fedistantlight => 'feDistantLight',
4369                   feflood => 'feFlood',
4370                   fefunca => 'feFuncA',
4371                   fefuncb => 'feFuncB',
4372                   fefuncg => 'feFuncG',
4373                   fefuncr => 'feFuncR',
4374                   fegaussianblur => 'feGaussianBlur',
4375                   feimage => 'feImage',
4376                   femerge => 'feMerge',
4377                   femergenode => 'feMergeNode',
4378                   femorphology => 'feMorphology',
4379                   feoffset => 'feOffset',
4380                   fepointlight => 'fePointLight',
4381                   fespecularlighting => 'feSpecularLighting',
4382                   fespotlight => 'feSpotLight',
4383                   fetile => 'feTile',
4384                   feturbulence => 'feTurbulence',
4385                   foreignobject => 'foreignObject',
4386                   glyphref => 'glyphRef',
4387                   lineargradient => 'linearGradient',
4388                   radialgradient => 'radialGradient',
4389                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4390                   textpath => 'textPath',  
4391                }->{$tag_name} || $tag_name;
4392              }
4393    
4394              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4395    
4396              ## "adjust foreign attributes" - done in insert-element-f
4397    
4398              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4399    
4400              if ($self->{self_closing}) {
4401                pop @{$self->{open_elements}};
4402                !!!ack ('t87.3');
4403              } else {
4404                !!!cp ('t87.4');
4405              }
4406    
4407              !!!next-token;
4408              next B;
4409            }
4410          } elsif ($token->{type} == END_TAG_TOKEN) {
4411            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4412            !!!cp ('t87.5');
4413            #
4414          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4415            !!!cp ('t87.6');
4416            !!!parse-error (type => 'not closed',
4417                            text => $self->{open_elements}->[-1]->[0]
4418                                ->manakai_local_name,
4419                            token => $token);
4420    
4421            pop @{$self->{open_elements}}
4422                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4423    
4424            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4425            ## Reprocess.
4426            next B;
4427          } else {
4428            die "$0: $token->{type}: Unknown token type";        
4429          }
4430        }
4431    
4432        if ($self->{insertion_mode} & HEAD_IMS) {
4433        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4434          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4435            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4436                !!!cp ('t88.2');
4437                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4438              } else {
4439                !!!cp ('t88.1');
4440                ## Ignore the token.
4441                !!!next-token;
4442                next B;
4443              }
4444            unless (length $token->{data}) {            unless (length $token->{data}) {
4445                !!!cp ('t88');
4446              !!!next-token;              !!!next-token;
4447              redo B;              next B;
4448            }            }
4449          }          }
4450    
4451          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4452              !!!cp ('t89');
4453            ## As if <head>            ## As if <head>
4454            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4455            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4456            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4457                  [$self->{head_element}, $el_category->{head}];
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    
4462            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4463          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4464              !!!cp ('t90');
4465            ## As if </noscript>            ## As if </noscript>
4466            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4467            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4468                        
4469            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4470            ## As if </head>            ## As if </head>
# Line 2920  sub _tree_construction_main ($) { Line 4472  sub _tree_construction_main ($) {
4472    
4473            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4474          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4475              !!!cp ('t91');
4476            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4477    
4478            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4479            } else {
4480              !!!cp ('t92');
4481          }          }
4482    
4483              ## "after head" insertion mode          ## "after head" insertion mode
4484              ## As if <body>          ## As if <body>
4485              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4486              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4487              ## reprocess          ## reprocess
4488              redo B;          next B;
4489            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4490              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4491                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4492                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4493                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4494                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4495                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4496                  !!!next-token;              push @{$self->{open_elements}},
4497                  redo B;                  [$self->{head_element}, $el_category->{head}];
4498                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4499                  #              !!!nack ('t93.1');
4500                } else {              !!!next-token;
4501                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4502                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4503                  !!!next-token;              !!!cp ('t93.2');
4504                  redo B;              !!!parse-error (type => 'after head', text => 'head',
4505                }                              token => $token);
4506              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              ## Ignore the token
4507                ## As if <head>              !!!nack ('t93.3');
4508                !!!create-element ($self->{head_element}, 'head');              !!!next-token;
4509                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              next B;
4510                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
4511                !!!cp ('t95');
4512                !!!parse-error (type => 'in head:head',
4513                                token => $token); # or in head noscript
4514                ## Ignore the token
4515                !!!nack ('t95.1');
4516                !!!next-token;
4517                next B;
4518              }
4519            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4520              !!!cp ('t96');
4521              ## As if <head>
4522              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4523              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4524              push @{$self->{open_elements}},
4525                  [$self->{head_element}, $el_category->{head}];
4526    
4527                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4528                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4529              }          } else {
4530              !!!cp ('t97');
4531            }
4532    
4533              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4534                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4535                    !!!cp ('t98');
4536                  ## As if </noscript>                  ## As if </noscript>
4537                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4538                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4539                                    token => $token);
4540                                
4541                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4542                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4543                  } else {
4544                    !!!cp ('t99');
4545                }                }
4546    
4547                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4548                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4549                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4550                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4551                                    text => $token->{tag_name}, token => $token);
4552                    push @{$self->{open_elements}},
4553                        [$self->{head_element}, $el_category->{head}];
4554                  } else {
4555                    !!!cp ('t101');
4556                }                }
4557                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4558                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4559                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4560                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4561                  !!!nack ('t101.1');
4562                !!!next-token;                !!!next-token;
4563                redo B;                next B;
4564              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4565                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4566                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4567                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4568                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4569                                    text => $token->{tag_name}, token => $token);
4570                    push @{$self->{open_elements}},
4571                        [$self->{head_element}, $el_category->{head}];
4572                  } else {
4573                    !!!cp ('t103');
4574                }                }
4575                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4576                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4577                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4578                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4579                  !!!ack ('t103.1');
4580                !!!next-token;                !!!next-token;
4581                redo B;                next B;
4582              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4583                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4584                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4585                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4586                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4587                                    text => $token->{tag_name}, token => $token);
4588                    push @{$self->{open_elements}},
4589                        [$self->{head_element}, $el_category->{head}];
4590                  } else {
4591                    !!!cp ('t105');
4592                }                }
4593                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4594                my $meta_el = 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.
4595    
4596                unless ($self->{confident}) {                unless ($self->{confident}) {
4597                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4598                      !!!cp ('t106');
4599                      ## NOTE: Whether the encoding is supported or not is handled
4600                      ## in the {change_encoding} callback.
4601                    $self->{change_encoding}                    $self->{change_encoding}
4602                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4603                             $token);
4604                                        
4605                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4606                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4607                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4608                                                 ->{has_reference});                                                 ->{has_reference});
4609                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4610                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4611                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4612                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4613                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4614                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4615                        !!!cp ('t107');
4616                        ## NOTE: Whether the encoding is supported or not is handled
4617                        ## in the {change_encoding} callback.
4618                      $self->{change_encoding}                      $self->{change_encoding}
4619                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4620                               $token);
4621                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4622                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4623                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
4624                                                     ->{has_reference});                                                     ->{has_reference});
4625                      } else {
4626                        !!!cp ('t108');
4627                    }                    }
4628                  }                  }
4629                } else {                } else {
4630                  if ($token->{attributes}->{charset}) {                  if ($token->{attributes}->{charset}) {
4631                      !!!cp ('t109');
4632                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4633                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4634                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4635                                                 ->{has_reference});                                                 ->{has_reference});
4636                  }                  }
4637                  if ($token->{attributes}->{content}) {                  if ($token->{attributes}->{content}) {
4638                      !!!cp ('t110');
4639                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4640                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4641                                             $token->{attributes}->{content}                                             $token->{attributes}->{content}
# Line 3039  sub _tree_construction_main ($) { Line 4643  sub _tree_construction_main ($) {
4643                  }                  }
4644                }                }
4645    
4646                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4647                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4648                  !!!ack ('t110.1');
4649                !!!next-token;                !!!next-token;
4650                redo B;                next B;
4651              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4652                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4653                    !!!cp ('t111');
4654                  ## As if </noscript>                  ## As if </noscript>
4655                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4656                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4657                                    token => $token);
4658                                
4659                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4660                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4661                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4662                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4663                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4664                                    text => $token->{tag_name}, token => $token);
4665                    push @{$self->{open_elements}},
4666                        [$self->{head_element}, $el_category->{head}];
4667                  } else {
4668                    !!!cp ('t113');
4669                }                }
4670    
4671                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4672                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4673                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4674                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4675                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4676                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4677                redo B;                next B;
4678              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4679                         $token->{tag_name} eq 'noframes') {
4680                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4681                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4682                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4683                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4684                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4685                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4686                                    text => $token->{tag_name}, token => $token);
4687                    push @{$self->{open_elements}},
4688                        [$self->{head_element}, $el_category->{head}];
4689                  } else {
4690                    !!!cp ('t115');
4691                }                }
4692                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4693                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4694                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4695                redo B;                next B;
4696              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4697                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4698                    !!!cp ('t116');
4699                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4700                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4701                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4702                    !!!nack ('t116.1');
4703                  !!!next-token;                  !!!next-token;
4704                  redo B;                  next B;
4705                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4706                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4707                    !!!parse-error (type => 'in noscript', text => 'noscript',
4708                                    token => $token);
4709                  ## Ignore the token                  ## Ignore the token
4710                    !!!nack ('t117.1');
4711                  !!!next-token;                  !!!next-token;
4712                  redo B;                  next B;
4713                } else {                } else {
4714                    !!!cp ('t118');
4715                  #                  #
4716                }                }
4717              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4718                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4719                    !!!cp ('t119');
4720                  ## As if </noscript>                  ## As if </noscript>
4721                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4722                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4723                                    token => $token);
4724                                
4725                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4726                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4727                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4728                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4729                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4730                                    text => $token->{tag_name}, token => $token);
4731                    push @{$self->{open_elements}},
4732                        [$self->{head_element}, $el_category->{head}];
4733                  } else {
4734                    !!!cp ('t121');
4735                }                }
4736    
4737                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4738                $script_start_tag->($insert_to_current);                $script_start_tag->();
4739                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4740                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4741                redo B;                next B;
4742              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4743                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4744                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4745                    !!!cp ('t122');
4746                  ## As if </noscript>                  ## As if </noscript>
4747                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4748                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4749                                    text => $token->{tag_name}, token => $token);
4750                                    
4751                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4752                  ## As if </head>                  ## As if </head>
# Line 3122  sub _tree_construction_main ($) { Line 4754  sub _tree_construction_main ($) {
4754                                    
4755                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4756                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4757                    !!!cp ('t124');
4758                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4759                                    
4760                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4761                  } else {
4762                    !!!cp ('t125');
4763                }                }
4764    
4765                ## "after head" insertion mode                ## "after head" insertion mode
4766                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4767                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4768                    !!!cp ('t126');
4769                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4770                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4771                    !!!cp ('t127');
4772                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4773                } else {                } else {
4774                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4775                }                }
4776                  !!!nack ('t127.1');
4777                !!!next-token;                !!!next-token;
4778                redo B;                next B;
4779              } else {              } else {
4780                  !!!cp ('t128');
4781                #                #
4782              }              }
4783    
4784              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4785                  !!!cp ('t129');
4786                ## As if </noscript>                ## As if </noscript>
4787                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4788                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4789                                  text => $token->{tag_name}, token => $token);
4790                                
4791                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4792                ## As if </head>                ## As if </head>
# Line 3153  sub _tree_construction_main ($) { Line 4794  sub _tree_construction_main ($) {
4794    
4795                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4796              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4797                  !!!cp ('t130');
4798                ## As if </head>                ## As if </head>
4799                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4800    
4801                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4802                } else {
4803                  !!!cp ('t131');
4804              }              }
4805    
4806              ## "after head" insertion mode              ## "after head" insertion mode
4807              ## As if <body>              ## As if <body>
4808              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4809              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4810              ## reprocess              ## reprocess
4811              redo B;              !!!ack-later;
4812                next B;
4813            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4814              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4815                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4816                    !!!cp ('t132');
4817                  ## As if <head>                  ## As if <head>
4818                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4819                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4820                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4821                        [$self->{head_element}, $el_category->{head}];
4822    
4823                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4824                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4825                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4826                  !!!next-token;                  !!!next-token;
4827                  redo B;                  next B;
4828                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4829                    !!!cp ('t133');
4830                  ## As if </noscript>                  ## As if </noscript>
4831                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4832                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
4833                                    text => 'head', token => $token);
4834                                    
4835                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4836                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4837                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4838                  !!!next-token;                  !!!next-token;
4839                  redo B;                  next B;
4840                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4841                    !!!cp ('t134');
4842                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4843                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4844                  !!!next-token;                  !!!next-token;
4845                  redo B;                  next B;
4846                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4847                    !!!cp ('t134.1');
4848                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4849                                    token => $token);
4850                    ## Ignore the token
4851                    !!!next-token;
4852                    next B;
4853                } else {                } else {
4854                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4855                }                }
4856              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4857                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4858                    !!!cp ('t136');
4859                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4860                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4861                  !!!next-token;                  !!!next-token;
4862                  redo B;                  next B;
4863                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4864                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4865                    !!!cp ('t137');
4866                    !!!parse-error (type => 'unmatched end tag',
4867                                    text => 'noscript', token => $token);
4868                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4869                  !!!next-token;                  !!!next-token;
4870                  redo B;                  next B;
4871                } else {                } else {
4872                    !!!cp ('t138');
4873                  #                  #
4874                }                }
4875              } elsif ({              } elsif ({
4876                        body => 1, html => 1,                        body => 1, html => 1,
4877                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4878                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4879                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4880                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4881                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4882                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag',
4883                                    text => $token->{tag_name}, token => $token);
4884                  $self->{insertion_mode} = IN_HEAD_IM;                  ## Ignore the token
4885                  ## Reprocess in the "in head" insertion mode...                  !!!next-token;
4886                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                  next B;
4887                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4888                    !!!cp ('t140.1');
4889                    !!!parse-error (type => 'unmatched end tag',
4890                                    text => $token->{tag_name}, token => $token);
4891                  ## Ignore the token                  ## Ignore the token
4892                  !!!next-token;                  !!!next-token;
4893                  redo B;                  next B;
4894                  } else {
4895                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4896                }                }
4897                              } elsif ($token->{tag_name} eq 'p') {
4898                #                !!!cp ('t142');
4899              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4900                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4901                       }->{$token->{tag_name}}) {                ## Ignore the token
4902                  !!!next-token;
4903                  next B;
4904                } elsif ($token->{tag_name} eq 'br') {
4905                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4906                  ## As if <head>                  !!!cp ('t142.2');
4907                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
4908                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4909                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4910                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4911      
4912                    ## Reprocess in the "after head" insertion mode...
4913                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4914                    !!!cp ('t143.2');
4915                    ## As if </head>
4916                    pop @{$self->{open_elements}};
4917                    $self->{insertion_mode} = AFTER_HEAD_IM;
4918      
4919                    ## Reprocess in the "after head" insertion mode...
4920                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4921                    !!!cp ('t143.3');
4922                    ## ISSUE: Two parse errors for <head><noscript></br>
4923                    !!!parse-error (type => 'unmatched end tag',
4924                                    text => 'br', token => $token);
4925                    ## As if </noscript>
4926                    pop @{$self->{open_elements}};
4927                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4928    
4929                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4930                }                  ## As if </head>
4931                    pop @{$self->{open_elements}};
4932                    $self->{insertion_mode} = AFTER_HEAD_IM;
4933    
4934                #                  ## Reprocess in the "after head" insertion mode...
4935              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4936                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
4937                  #                  #
4938                } else {                } else {
4939                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
4940                }                }
4941    
4942                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4943                  !!!parse-error (type => 'unmatched end tag',
4944                                  text => 'br', token => $token);
4945                  ## Ignore the token
4946                  !!!next-token;
4947                  next B;
4948                } else {
4949                  !!!cp ('t145');
4950                  !!!parse-error (type => 'unmatched end tag',
4951                                  text => $token->{tag_name}, token => $token);
4952                  ## Ignore the token
4953                  !!!next-token;
4954                  next B;
4955              }              }
4956    
4957              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4958                  !!!cp ('t146');
4959                ## As if </noscript>                ## As if </noscript>
4960                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4961                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4962                                  text => $token->{tag_name}, token => $token);
4963                                
4964                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4965                ## As if </head>                ## As if </head>
# Line 3265  sub _tree_construction_main ($) { Line 4967  sub _tree_construction_main ($) {
4967    
4968                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4969              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4970                  !!!cp ('t147');
4971                ## As if </head>                ## As if </head>
4972                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4973    
4974                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4975              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4976                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
4977                  !!!cp ('t148');
4978                  !!!parse-error (type => 'unmatched end tag',
4979                                  text => $token->{tag_name}, token => $token);
4980                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4981                !!!next-token;                !!!next-token;
4982                redo B;                next B;
4983                } else {
4984                  !!!cp ('t149');
4985              }              }
4986    
4987              ## "after head" insertion mode              ## "after head" insertion mode
4988              ## As if <body>              ## As if <body>
4989              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4990              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4991              ## reprocess              ## reprocess
4992              redo B;              next B;
4993            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4994              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4995            }            !!!cp ('t149.1');
4996    
4997              ## NOTE: As if <head>
4998              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4999              $self->{open_elements}->[-1]->[0]->append_child
5000                  ($self->{head_element});
5001              #push @{$self->{open_elements}},
5002              #    [$self->{head_element}, $el_category->{head}];
5003              #$self->{insertion_mode} = IN_HEAD_IM;
5004              ## NOTE: Reprocess.
5005    
5006              ## NOTE: As if </head>
5007              #pop @{$self->{open_elements}};
5008              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5009              ## NOTE: Reprocess.
5010              
5011              #
5012            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5013              !!!cp ('t149.2');
5014    
5015              ## NOTE: As if </head>
5016              pop @{$self->{open_elements}};
5017              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5018              ## NOTE: Reprocess.
5019    
5020              #
5021            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5022              !!!cp ('t149.3');
5023    
5024              !!!parse-error (type => 'in noscript:#eof', token => $token);
5025    
5026              ## As if </noscript>
5027              pop @{$self->{open_elements}};
5028              #$self->{insertion_mode} = IN_HEAD_IM;
5029              ## NOTE: Reprocess.
5030    
5031              ## NOTE: As if </head>
5032              pop @{$self->{open_elements}};
5033              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5034              ## NOTE: Reprocess.
5035    
5036              #
5037            } else {
5038              !!!cp ('t149.4');
5039              #
5040            }
5041    
5042            ## NOTE: As if <body>
5043            !!!insert-element ('body',, $token);
5044            $self->{insertion_mode} = IN_BODY_IM;
5045            ## NOTE: Reprocess.
5046            next B;
5047          } else {
5048            die "$0: $token->{type}: Unknown token type";
5049          }
5050    
5051            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5052      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
5053            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5054                !!!cp ('t150');
5055              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
5056              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5057                            
5058              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5059    
5060              !!!next-token;              !!!next-token;
5061              redo B;              next B;
5062            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5063              if ({              if ({
5064                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3303  sub _tree_construction_main ($) { Line 5066  sub _tree_construction_main ($) {
5066                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5067                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5068                  ## have an element in table scope                  ## have an element in table scope
5069                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5070                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5071                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5072                      $tn = $node->[1];                      !!!cp ('t151');
5073                      last INSCOPE;  
5074                    } elsif ({                      ## Close the cell
5075                              table => 1, html => 1,                      !!!back-token; # <x>
5076                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
5077                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
5078                    }                                line => $token->{line},
5079                  } # INSCOPE                                column => $token->{column}};
5080                    unless (defined $tn) {                      next B;
5081                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5082                      ## Ignore the token                      !!!cp ('t152');
5083                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
5084                      redo B;                      last;
5085                    }                    }
5086                                    }
5087                  ## Close the cell  
5088                  !!!back-token; # <?>                  !!!cp ('t153');
5089                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
5090                  redo B;                      text => $token->{tag_name}, token => $token);
5091                    ## Ignore the token
5092                    !!!nack ('t153.1');
5093                    !!!next-token;
5094                    next B;
5095                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5096                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5097                                    token => $token);
5098                                    
5099                  ## As if </caption>                  ## NOTE: As if </caption>.
5100                  ## have a table element in table scope                  ## have a table element in table scope
5101                  my $i;                  my $i;
5102                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5103                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5104                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5105                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5106                      last INSCOPE;                        !!!cp ('t155');
5107                    } elsif ({                        $i = $_;
5108                              table => 1, html => 1,                        last INSCOPE;
5109                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5110                      last INSCOPE;                        !!!cp ('t156');
5111                          last;
5112                        }
5113                    }                    }
5114    
5115                      !!!cp ('t157');
5116                      !!!parse-error (type => 'start tag not allowed',
5117                                      text => $token->{tag_name}, token => $token);
5118                      ## Ignore the token
5119                      !!!nack ('t157.1');
5120                      !!!next-token;
5121                      next B;
5122                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5123                                    
5124                  ## generate implied end tags                  ## generate implied end tags
5125                  if ({                  while ($self->{open_elements}->[-1]->[1]
5126                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5127                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
5128                       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;  
5129                  }                  }
5130    
5131                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5132                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
5133                      !!!parse-error (type => 'not closed',
5134                                      text => $self->{open_elements}->[-1]->[0]
5135                                          ->manakai_local_name,
5136                                      token => $token);
5137                    } else {
5138                      !!!cp ('t160');
5139                  }                  }
5140                                    
5141                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3375  sub _tree_construction_main ($) { Line 5145  sub _tree_construction_main ($) {
5145                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5146                                    
5147                  ## reprocess                  ## reprocess
5148                  redo B;                  !!!ack-later;
5149                    next B;
5150                } else {                } else {
5151                    !!!cp ('t161');
5152                  #                  #
5153                }                }
5154              } else {              } else {
5155                  !!!cp ('t162');
5156                #                #
5157              }              }
5158            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3389  sub _tree_construction_main ($) { Line 5162  sub _tree_construction_main ($) {
5162                  my $i;                  my $i;
5163                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5164                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5165                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5166                        !!!cp ('t163');
5167                      $i = $_;                      $i = $_;
5168                      last INSCOPE;                      last INSCOPE;
5169                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5170                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
5171                      last INSCOPE;                      last INSCOPE;
5172                    }                    }
5173                  } # INSCOPE                  } # INSCOPE
5174                    unless (defined $i) {                    unless (defined $i) {
5175                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
5176                        !!!parse-error (type => 'unmatched end tag',
5177                                        text => $token->{tag_name},
5178                                        token => $token);
5179                      ## Ignore the token                      ## Ignore the token
5180                      !!!next-token;                      !!!next-token;
5181                      redo B;                      next B;
5182                    }                    }
5183                                    
5184                  ## generate implied end tags                  ## generate implied end tags
5185                  if ({                  while ($self->{open_elements}->[-1]->[1]
5186                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5187                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
5188                       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;  
5189                  }                  }
5190                    
5191                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5192                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
5193                      !!!cp ('t167');
5194                      !!!parse-error (type => 'not closed',
5195                                      text => $self->{open_elements}->[-1]->[0]
5196                                          ->manakai_local_name,
5197                                      token => $token);
5198                    } else {
5199                      !!!cp ('t168');
5200                  }                  }
5201                                    
5202                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3430  sub _tree_construction_main ($) { Line 5206  sub _tree_construction_main ($) {
5206                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5207                                    
5208                  !!!next-token;                  !!!next-token;
5209                  redo B;                  next B;
5210                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5211                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
5212                    !!!parse-error (type => 'unmatched end tag',
5213                                    text => $token->{tag_name}, token => $token);
5214                  ## Ignore the token                  ## Ignore the token
5215                  !!!next-token;                  !!!next-token;
5216                  redo B;                  next B;
5217                } else {                } else {
5218                    !!!cp ('t170');
5219                  #                  #
5220                }                }
5221              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
5222                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5223                  ## have a table element in table scope                  ## have a table element in table scope
5224                  my $i;                  my $i;
5225                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5226                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5227                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5228                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5229                      last INSCOPE;                        !!!cp ('t171');
5230                    } elsif ({                        $i = $_;
5231                              table => 1, html => 1,                        last INSCOPE;
5232                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5233                      last INSCOPE;                        !!!cp ('t172');
5234                          last;
5235                        }
5236                    }                    }
5237    
5238                      !!!cp ('t173');
5239                      !!!parse-error (type => 'unmatched end tag',
5240                                      text => $token->{tag_name}, token => $token);
5241                      ## Ignore the token
5242                      !!!next-token;
5243                      next B;
5244                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5245                                    
5246                  ## generate implied end tags                  ## generate implied end tags
5247                  if ({                  while ($self->{open_elements}->[-1]->[1]
5248                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5249                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
5250                       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;  
5251                  }                  }
5252                                    
5253                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5254                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
5255                      !!!parse-error (type => 'not closed',
5256                                      text => $self->{open_elements}->[-1]->[0]
5257                                          ->manakai_local_name,
5258                                      token => $token);
5259                    } else {
5260                      !!!cp ('t176');
5261                  }                  }
5262                                    
5263                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3484  sub _tree_construction_main ($) { Line 5267  sub _tree_construction_main ($) {
5267                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5268                                    
5269                  !!!next-token;                  !!!next-token;
5270                  redo B;                  next B;
5271                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5272                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5273                    !!!parse-error (type => 'unmatched end tag',
5274                                    text => $token->{tag_name}, token => $token);
5275                  ## Ignore the token                  ## Ignore the token
5276                  !!!next-token;                  !!!next-token;
5277                  redo B;                  next B;
5278                } else {                } else {
5279                    !!!cp ('t178');
5280                  #                  #
5281                }                }
5282              } elsif ({              } elsif ({
# Line 3501  sub _tree_construction_main ($) { Line 5287  sub _tree_construction_main ($) {
5287                ## have an element in table scope                ## have an element in table scope
5288                my $i;                my $i;
5289                my $tn;                my $tn;
5290                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5291                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5292                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5293                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5294                    last INSCOPE;                      !!!cp ('t179');
5295                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5296                    $tn = $node->[1];  
5297                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5298                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5299                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5300                            table => 1, html => 1,                                line => $token->{line},
5301                           }->{$node->[1]}) {                                column => $token->{column}};
5302                    last INSCOPE;                      next B;
5303                      } elsif ($node->[1] & TABLE_CELL_EL) {
5304                        !!!cp ('t180');
5305                        $tn = $node->[0]->manakai_local_name;
5306                        ## NOTE: There is exactly one |td| or |th| element
5307                        ## in scope in the stack of open elements by definition.
5308                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5309                        ## ISSUE: Can this be reached?
5310                        !!!cp ('t181');
5311                        last;
5312                      }
5313                  }                  }
5314                } # INSCOPE  
5315                unless (defined $i) {                  !!!cp ('t182');
5316                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5317                        text => $token->{tag_name}, token => $token);
5318                  ## Ignore the token                  ## Ignore the token
5319                  !!!next-token;                  !!!next-token;
5320                  redo B;                  next B;
5321                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5322              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5323                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5324                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5325                                  token => $token);
5326    
5327                ## As if </caption>                ## As if </caption>
5328                ## have a table element in table scope                ## have a table element in table scope
5329                my $i;                my $i;
5330                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5331                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5332                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5333                      !!!cp ('t184');
5334                    $i = $_;                    $i = $_;
5335                    last INSCOPE;                    last INSCOPE;
5336                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5337                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5338                    last INSCOPE;                    last INSCOPE;
5339                  }                  }
5340                } # INSCOPE                } # INSCOPE
5341                unless (defined $i) {                unless (defined $i) {
5342                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5343                    !!!parse-error (type => 'unmatched end tag',
5344                                    text => 'caption', token => $token);
5345                  ## Ignore the token                  ## Ignore the token
5346                  !!!next-token;                  !!!next-token;
5347                  redo B;                  next B;
5348                }                }
5349                                
5350                ## generate implied end tags                ## generate implied end tags
5351                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5352                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5353                     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;  
5354                }                }
5355    
5356                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5357                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5358                    !!!parse-error (type => 'not closed',
5359                                    text => $self->{open_elements}->[-1]->[0]
5360                                        ->manakai_local_name,
5361                                    token => $token);
5362                  } else {
5363                    !!!cp ('t189');
5364                }                }
5365    
5366                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3577  sub _tree_construction_main ($) { Line 5370  sub _tree_construction_main ($) {
5370                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5371    
5372                ## reprocess                ## reprocess
5373                redo B;                next B;
5374              } elsif ({              } elsif ({
5375                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5376                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5377                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5378                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
5379                    !!!parse-error (type => 'unmatched end tag',
5380                                    text => $token->{tag_name}, token => $token);
5381                  ## Ignore the token                  ## Ignore the token
5382                  !!!next-token;                  !!!next-token;
5383                  redo B;                  next B;
5384                } else {                } else {
5385                    !!!cp ('t191');
5386                  #                  #
5387                }                }
5388              } elsif ({              } elsif ({
# Line 3594  sub _tree_construction_main ($) { Line 5390  sub _tree_construction_main ($) {
5390                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5391                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5392                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5393                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5394                  !!!parse-error (type => 'unmatched end tag',
5395                                  text => $token->{tag_name}, token => $token);
5396                ## Ignore the token                ## Ignore the token
5397                !!!next-token;                !!!next-token;
5398                redo B;                next B;
5399              } else {              } else {
5400                  !!!cp ('t193');
5401                #                #
5402              }              }
5403          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5404            for my $entry (@{$self->{open_elements}}) {
5405              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5406                !!!cp ('t75');
5407                !!!parse-error (type => 'in body:#eof', token => $token);
5408                last;
5409              }
5410            }
5411    
5412            ## Stop parsing.
5413            last B;
5414        } else {        } else {
5415          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5416        }        }
# Line 3609  sub _tree_construction_main ($) { Line 5419  sub _tree_construction_main ($) {
5419        #        #
5420      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5421        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5422              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5423                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5424              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5425                                
5426                unless (length $token->{data}) {            unless (length $token->{data}) {
5427                  !!!next-token;              !!!cp ('t194');
5428                  redo B;              !!!next-token;
5429                }              next B;
5430              }            } else {
5431                !!!cp ('t195');
5432              }
5433            }
5434    
5435              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5436    
5437              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5438              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3626  sub _tree_construction_main ($) { Line 5440  sub _tree_construction_main ($) {
5440              ## result in a new Text node.              ## result in a new Text node.
5441              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5442                            
5443              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]}) {  
5444                # MUST                # MUST
5445                my $foster_parent_element;                my $foster_parent_element;
5446                my $next_sibling;                my $next_sibling;
5447                my $prev_sibling;                my $prev_sibling;
5448                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5449                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5450                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5451                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5452                        !!!cp ('t196');
5453                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5454                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5455                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5456                    } else {                    } else {
5457                        !!!cp ('t197');
5458                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5459                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5460                    }                    }
# Line 3653  sub _tree_construction_main ($) { Line 5466  sub _tree_construction_main ($) {
5466                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5467                if (defined $prev_sibling and                if (defined $prev_sibling and
5468                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5469                    !!!cp ('t198');
5470                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5471                } else {                } else {
5472                    !!!cp ('t199');
5473                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5474                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5475                     $next_sibling);                     $next_sibling);
5476                }                }
5477              } else {            $open_tables->[-1]->[1] = 1; # tainted
5478                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5479              }            !!!cp ('t200');
5480              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5481            }
5482                            
5483              !!!next-token;          !!!next-token;
5484              redo B;          next B;
5485        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5486              if ({          if ({
5487                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5488                   th => 1, td => 1,               th => 1, td => 1,
5489                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5490                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5491                  ## Clear back to table context              ## Clear back to table context
5492                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5493                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5494                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!cp ('t201');
5495                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5496                  }              }
5497                                
5498                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5499                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5500                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5501                }            }
5502              
5503                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5504                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5505                    !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t202');
5506                  }                !!!parse-error (type => 'missing start tag:tr', token => $token);
5507                }
5508                                    
5509                  ## Clear back to table body context              ## Clear back to table body context
5510                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5511                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5512                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5513                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## ISSUE: Can this case be reached?
5514                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5515                  }              }
5516                                    
5517                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5518                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5519                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5520                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5521                      !!!nack ('t204');
5522                    !!!next-token;                    !!!next-token;
5523                    redo B;                    next B;
5524                  } else {                  } else {
5525                    !!!insert-element ('tr');                    !!!cp ('t205');
5526                      !!!insert-element ('tr',, $token);
5527                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5528                  }                  }
5529                  } else {
5530                    !!!cp ('t206');
5531                }                }
5532    
5533                ## Clear back to table row context                ## Clear back to table row context
5534                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5535                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5536                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5537                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5538                }                }
5539                                
5540                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5541                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5542    
5543                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5544                                
5545                  !!!nack ('t207.1');
5546                !!!next-token;                !!!next-token;
5547                redo B;                next B;
5548              } elsif ({              } elsif ({
5549                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5550                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3733  sub _tree_construction_main ($) { Line 5556  sub _tree_construction_main ($) {
5556                  my $i;                  my $i;
5557                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5558                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5559                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5560                        !!!cp ('t208');
5561                      $i = $_;                      $i = $_;
5562                      last INSCOPE;                      last INSCOPE;
5563                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5564                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5565                      last INSCOPE;                      last INSCOPE;
5566                    }                    }
5567                  } # INSCOPE                  } # INSCOPE
5568                  unless (defined $i) {                  unless (defined $i) {
5569                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5570    ## TODO: This type is wrong.
5571                      !!!parse-error (type => 'unmacthed end tag',
5572                                      text => $token->{tag_name}, token => $token);
5573                    ## Ignore the token                    ## Ignore the token
5574                      !!!nack ('t210.1');
5575                    !!!next-token;                    !!!next-token;
5576                    redo B;                    next B;
5577                  }                  }
5578                                    
5579                  ## Clear back to table row context                  ## Clear back to table row context
5580                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5581                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5582                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5583                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5584                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5585                  }                  }
5586                                    
5587                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5588                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5589                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5590                      !!!cp ('t212');
5591                    ## reprocess                    ## reprocess
5592                    redo B;                    !!!ack-later;
5593                      next B;
5594                  } else {                  } else {
5595                      !!!cp ('t213');
5596                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5597                  }                  }
5598                }                }
# Line 3772  sub _tree_construction_main ($) { Line 5602  sub _tree_construction_main ($) {
5602                  my $i;                  my $i;
5603                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5604                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5605                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5606                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5607                      $i = $_;                      $i = $_;
5608                      last INSCOPE;                      last INSCOPE;
5609                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5610                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5611                      last INSCOPE;                      last INSCOPE;
5612                    }                    }
5613                  } # INSCOPE                  } # INSCOPE
5614                  unless (defined $i) {                  unless (defined $i) {
5615                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5616    ## TODO: This erorr type is wrong.
5617                      !!!parse-error (type => 'unmatched end tag',
5618                                      text => $token->{tag_name}, token => $token);
5619                    ## Ignore the token                    ## Ignore the token
5620                      !!!nack ('t216.1');
5621                    !!!next-token;                    !!!next-token;
5622                    redo B;                    next B;
5623                  }                  }
5624    
5625                  ## Clear back to table body context                  ## Clear back to table body context
5626                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5627                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5628                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5629                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5630                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5631                  }                  }
5632                                    
# Line 3808  sub _tree_construction_main ($) { Line 5640  sub _tree_construction_main ($) {
5640                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5641                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5642                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5643                  } else {
5644                    !!!cp ('t218');
5645                }                }
5646    
5647                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5648                  ## Clear back to table context                  ## Clear back to table context
5649                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5650                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5651                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5652                      ## ISSUE: Can this state be reached?
5653                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5654                  }                  }
5655                                    
5656                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5657                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5658                  ## reprocess                  ## reprocess
5659                  redo B;                  !!!ack-later;
5660                    next B;
5661                } elsif ({                } elsif ({
5662                          caption => 1,                          caption => 1,
5663                          colgroup => 1,                          colgroup => 1,
5664                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5665                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5666                  ## Clear back to table context                  ## Clear back to table context
5667                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5668                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5669                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5670                      ## ISSUE: Can this state be reached?
5671                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5672                  }                  }
5673                                    
5674                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5675                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5676                                    
5677                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5678                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5679                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5680                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3846  sub _tree_construction_main ($) { Line 5683  sub _tree_construction_main ($) {
5683                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5684                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5685                  !!!next-token;                  !!!next-token;
5686                  redo B;                  !!!nack ('t220.1');
5687                    next B;
5688                } else {                } else {
5689                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5690                }                }
5691              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5692                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5693                                  text => $self->{open_elements}->[-1]->[0]
5694                                      ->manakai_local_name,
5695                                  token => $token);
5696    
5697                ## As if </table>                ## As if </table>
5698                ## have a table element in table scope                ## have a table element in table scope
5699                my $i;                my $i;
5700                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5701                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5702                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5703                      !!!cp ('t221');
5704                    $i = $_;                    $i = $_;
5705                    last INSCOPE;                    last INSCOPE;
5706                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5707                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5708                    last INSCOPE;                    last INSCOPE;
5709                  }                  }
5710                } # INSCOPE                } # INSCOPE
5711                unless (defined $i) {                unless (defined $i) {
5712                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5713    ## TODO: The following is wrong, maybe.
5714                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5715                                    token => $token);
5716                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5717                    !!!nack ('t223.1');
5718                  !!!next-token;                  !!!next-token;
5719                  redo B;                  next B;
5720                }                }
5721                                
5722    ## TODO: Followings are removed from the latest spec.
5723                ## generate implied end tags                ## generate implied end tags
5724                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5725                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5726                     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;  
5727                }                }
5728    
5729                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5730                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5731                    ## NOTE: |<table><tr><table>|
5732                    !!!parse-error (type => 'not closed',
5733                                    text => $self->{open_elements}->[-1]->[0]
5734                                        ->manakai_local_name,
5735                                    token => $token);
5736                  } else {
5737                    !!!cp ('t226');
5738                }                }
5739    
5740                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5741                  pop @{$open_tables};
5742    
5743                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5744    
5745                ## reprocess            ## reprocess
5746                redo B;            !!!ack-later;
5747          } else {            next B;
5748            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5749              if (not $open_tables->[-1]->[1]) { # tainted
5750                !!!cp ('t227.8');
5751                ## NOTE: This is a "as if in head" code clone.
5752                $parse_rcdata->(CDATA_CONTENT_MODEL);
5753                next B;
5754              } else {
5755                !!!cp ('t227.7');
5756                #
5757              }
5758            } elsif ($token->{tag_name} eq 'script') {
5759              if (not $open_tables->[-1]->[1]) { # tainted
5760                !!!cp ('t227.6');
5761                ## NOTE: This is a "as if in head" code clone.
5762                $script_start_tag->();
5763                next B;
5764              } else {
5765                !!!cp ('t227.5');
5766                #
5767              }
5768            } elsif ($token->{tag_name} eq 'input') {
5769              if (not $open_tables->[-1]->[1]) { # tainted
5770                if ($token->{attributes}->{type}) { ## TODO: case
5771                  my $type = lc $token->{attributes}->{type}->{value};
5772                  if ($type eq 'hidden') {
5773                    !!!cp ('t227.3');
5774                    !!!parse-error (type => 'in table',
5775                                    text => $token->{tag_name}, token => $token);
5776    
5777            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5778    
5779                    ## TODO: form element pointer
5780    
5781                    pop @{$self->{open_elements}};
5782    
5783                    !!!next-token;
5784                    !!!ack ('t227.2.1');
5785                    next B;
5786                  } else {
5787                    !!!cp ('t227.2');
5788                    #
5789                  }
5790                } else {
5791                  !!!cp ('t227.1');
5792                  #
5793                }
5794              } else {
5795                !!!cp ('t227.4');
5796                #
5797              }
5798            } else {
5799              !!!cp ('t227');
5800            #            #
5801          }          }
5802    
5803            !!!parse-error (type => 'in table', text => $token->{tag_name},
5804                            token => $token);
5805    
5806            $insert = $insert_to_foster;
5807            #
5808        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5809              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5810                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3911  sub _tree_construction_main ($) { Line 5812  sub _tree_construction_main ($) {
5812                my $i;                my $i;
5813                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5814                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5815                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5816                      !!!cp ('t228');
5817                    $i = $_;                    $i = $_;
5818                    last INSCOPE;                    last INSCOPE;
5819                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5820                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5821                    last INSCOPE;                    last INSCOPE;
5822                  }                  }
5823                } # INSCOPE                } # INSCOPE
5824                unless (defined $i) {                unless (defined $i) {
5825                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5826                    !!!parse-error (type => 'unmatched end tag',
5827                                    text => $token->{tag_name}, token => $token);
5828                  ## Ignore the token                  ## Ignore the token
5829                    !!!nack ('t230.1');
5830                  !!!next-token;                  !!!next-token;
5831                  redo B;                  next B;
5832                  } else {
5833                    !!!cp ('t232');
5834                }                }
5835    
5836                ## Clear back to table row context                ## Clear back to table row context
5837                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5838                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5839                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5840                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5841                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5842                }                }
5843    
5844                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5845                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5846                !!!next-token;                !!!next-token;
5847                redo B;                !!!nack ('t231.1');
5848                  next B;
5849              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5850                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5851                  ## As if </tr>                  ## As if </tr>
# Line 3946  sub _tree_construction_main ($) { Line 5853  sub _tree_construction_main ($) {
5853                  my $i;                  my $i;
5854                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5855                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5856                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5857                        !!!cp ('t233');
5858                      $i = $_;                      $i = $_;
5859                      last INSCOPE;                      last INSCOPE;
5860                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5861                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5862                      last INSCOPE;                      last INSCOPE;
5863                    }                    }
5864                  } # INSCOPE                  } # INSCOPE
5865                  unless (defined $i) {                  unless (defined $i) {
5866                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5867    ## TODO: The following is wrong.
5868                      !!!parse-error (type => 'unmatched end tag',
5869                                      text => $token->{type}, token => $token);
5870                    ## Ignore the token                    ## Ignore the token
5871                      !!!nack ('t236.1');
5872                    !!!next-token;                    !!!next-token;
5873                    redo B;                    next B;
5874                  }                  }
5875                                    
5876                  ## Clear back to table row context                  ## Clear back to table row context
5877                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5878                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5879                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5880                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5881                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5882                  }                  }
5883                                    
# Line 3980  sub _tree_construction_main ($) { Line 5891  sub _tree_construction_main ($) {
5891                  my $i;                  my $i;
5892                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5893                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5894                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5895                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5896                      $i = $_;                      $i = $_;
5897                      last INSCOPE;                      last INSCOPE;
5898                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5899                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5900                      last INSCOPE;                      last INSCOPE;
5901                    }                    }
5902                  } # INSCOPE                  } # INSCOPE
5903                  unless (defined $i) {                  unless (defined $i) {
5904                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
5905                      !!!parse-error (type => 'unmatched end tag',
5906                                      text => $token->{tag_name}, token => $token);
5907                    ## Ignore the token                    ## Ignore the token
5908                      !!!nack ('t239.1');
5909                    !!!next-token;                    !!!next-token;
5910                    redo B;                    next B;
5911                  }                  }
5912                                    
5913                  ## Clear back to table body context                  ## Clear back to table body context
5914                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5915                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5916                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5917                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5918                  }                  }
5919                                    
# Line 4018  sub _tree_construction_main ($) { Line 5929  sub _tree_construction_main ($) {
5929                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5930                }                }
5931    
5932                  ## NOTE: </table> in the "in table" insertion mode.
5933                  ## When you edit the code fragment below, please ensure that
5934                  ## the code for <table> in the "in table" insertion mode
5935                  ## is synced with it.
5936    
5937                ## have a table element in table scope                ## have a table element in table scope
5938                my $i;                my $i;
5939                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5940                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5941                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5942                      !!!cp ('t241');
5943                    $i = $_;                    $i = $_;
5944                    last INSCOPE;                    last INSCOPE;
5945                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5946                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
5947                    last INSCOPE;                    last INSCOPE;
5948                  }                  }
5949                } # INSCOPE                } # INSCOPE
5950                unless (defined $i) {                unless (defined $i) {
5951                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
5952                    !!!parse-error (type => 'unmatched end tag',
5953                                    text => $token->{tag_name}, token => $token);
5954                  ## Ignore the token                  ## Ignore the token
5955                    !!!nack ('t243.1');
5956                  !!!next-token;                  !!!next-token;
5957                  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]);  
5958                }                }
5959                                    
5960                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5961                  pop @{$open_tables};
5962                                
5963                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5964                                
5965                !!!next-token;                !!!next-token;
5966                redo B;                next B;
5967              } elsif ({              } elsif ({
5968                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5969                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4069  sub _tree_construction_main ($) { Line 5973  sub _tree_construction_main ($) {
5973                  my $i;                  my $i;
5974                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5975                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5976                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5977                        !!!cp ('t247');
5978                      $i = $_;                      $i = $_;
5979                      last INSCOPE;                      last INSCOPE;
5980                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5981                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
5982                      last INSCOPE;                      last INSCOPE;
5983                    }                    }
5984                  } # INSCOPE                  } # INSCOPE
5985                    unless (defined $i) {                    unless (defined $i) {
5986                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
5987                        !!!parse-error (type => 'unmatched end tag',
5988                                        text => $token->{tag_name}, token => $token);
5989                      ## Ignore the token                      ## Ignore the token
5990                        !!!nack ('t249.1');
5991                      !!!next-token;                      !!!next-token;
5992                      redo B;                      next B;
5993                    }                    }
5994                                    
5995                  ## As if </tr>                  ## As if </tr>
# Line 4090  sub _tree_construction_main ($) { Line 5997  sub _tree_construction_main ($) {
5997                  my $i;                  my $i;
5998                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5999                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6000                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6001                        !!!cp ('t250');
6002                      $i = $_;                      $i = $_;
6003                      last INSCOPE;                      last INSCOPE;
6004                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6005                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
6006                      last INSCOPE;                      last INSCOPE;
6007                    }                    }
6008                  } # INSCOPE                  } # INSCOPE
6009                    unless (defined $i) {                    unless (defined $i) {
6010                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
6011                        !!!parse-error (type => 'unmatched end tag',
6012                                        text => 'tr', token => $token);
6013                      ## Ignore the token                      ## Ignore the token
6014                        !!!nack ('t252.1');
6015                      !!!next-token;                      !!!next-token;
6016                      redo B;                      next B;
6017                    }                    }
6018                                    
6019                  ## Clear back to table row context                  ## Clear back to table row context
6020                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6021                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
6022                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
6023                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6024                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6025                  }                  }
6026                                    
# Line 4123  sub _tree_construction_main ($) { Line 6033  sub _tree_construction_main ($) {
6033                my $i;                my $i;
6034                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6035                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6036                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6037                      !!!cp ('t254');
6038                    $i = $_;                    $i = $_;
6039                    last INSCOPE;                    last INSCOPE;
6040                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6041                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6042                    last INSCOPE;                    last INSCOPE;
6043                  }                  }
6044                } # INSCOPE                } # INSCOPE
6045                unless (defined $i) {                unless (defined $i) {
6046                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
6047                    !!!parse-error (type => 'unmatched end tag',
6048                                    text => $token->{tag_name}, token => $token);
6049                  ## Ignore the token                  ## Ignore the token
6050                    !!!nack ('t256.1');
6051                  !!!next-token;                  !!!next-token;
6052                  redo B;                  next B;
6053                }                }
6054    
6055                ## Clear back to table body context                ## Clear back to table body context
6056                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6057                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6058                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6059                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6060                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6061                }                }
6062    
6063                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6064                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6065                  !!!nack ('t257.1');
6066                !!!next-token;                !!!next-token;
6067                redo B;                next B;
6068              } elsif ({              } elsif ({
6069                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6070                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6071                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6072                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6073                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6074                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6075                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6076                !!!next-token;                            text => $token->{tag_name}, token => $token);
6077                redo B;            ## Ignore the token
6078          } else {            !!!nack ('t258.1');
6079            !!!parse-error (type => 'in table:/'.$token->{tag_name});             !!!next-token;
6080              next B;
6081            } else {
6082              !!!cp ('t259');
6083              !!!parse-error (type => 'in table:/',
6084                              text => $token->{tag_name}, token => $token);
6085    
6086            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6087            #            #
6088          }          }
6089          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6090            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6091                    @{$self->{open_elements}} == 1) { # redundant, maybe
6092              !!!parse-error (type => 'in body:#eof', token => $token);
6093              !!!cp ('t259.1');
6094              #
6095            } else {
6096              !!!cp ('t259.2');
6097              #
6098            }
6099    
6100            ## Stop parsing
6101            last B;
6102        } else {        } else {
6103          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6104        }        }
# Line 4175  sub _tree_construction_main ($) { Line 6107  sub _tree_construction_main ($) {
6107              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6108                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6109                unless (length $token->{data}) {                unless (length $token->{data}) {
6110                    !!!cp ('t260');
6111                  !!!next-token;                  !!!next-token;
6112                  redo B;                  next B;
6113                }                }
6114              }              }
6115                            
6116                !!!cp ('t261');
6117              #              #
6118            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6119              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6120                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
6121                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6122                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6123                  !!!ack ('t262.1');
6124                !!!next-token;                !!!next-token;
6125                redo B;                next B;
6126              } else {              } else {
6127                  !!!cp ('t263');
6128                #                #
6129              }              }
6130            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6131              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6132                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6133                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
6134                    !!!parse-error (type => 'unmatched end tag',
6135                                    text => 'colgroup', token => $token);
6136                  ## Ignore the token                  ## Ignore the token
6137                  !!!next-token;                  !!!next-token;
6138                  redo B;                  next B;
6139                } else {                } else {
6140                    !!!cp ('t265');
6141                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6142                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6143                  !!!next-token;                  !!!next-token;
6144                  redo B;                              next B;            
6145                }                }
6146              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6147                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
6148                  !!!parse-error (type => 'unmatched end tag',
6149                                  text => 'col', token => $token);
6150                ## Ignore the token                ## Ignore the token
6151                !!!next-token;                !!!next-token;
6152                redo B;                next B;
6153              } else {              } else {
6154                  !!!cp ('t267');
6155                #                #
6156              }              }
6157            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6158              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6159            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6160              !!!cp ('t270.2');
6161              ## Stop parsing.
6162              last B;
6163            } else {
6164              ## NOTE: As if </colgroup>.
6165              !!!cp ('t270.1');
6166              pop @{$self->{open_elements}}; # colgroup
6167              $self->{insertion_mode} = IN_TABLE_IM;
6168              ## Reprocess.
6169              next B;
6170            }
6171          } else {
6172            die "$0: $token->{type}: Unknown token type";
6173          }
6174    
6175            ## As if </colgroup>            ## As if </colgroup>
6176            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6177              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
6178    ## TODO: Wrong error type?
6179                !!!parse-error (type => 'unmatched end tag',
6180                                text => 'colgroup', token => $token);
6181              ## Ignore the token              ## Ignore the token
6182                !!!nack ('t269.1');
6183              !!!next-token;              !!!next-token;
6184              redo B;              next B;
6185            } else {            } else {
6186                !!!cp ('t270');
6187              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6188              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6189                !!!ack-later;
6190              ## reprocess              ## reprocess
6191              redo B;              next B;
6192            }            }
6193      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6194        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6195            !!!cp ('t271');
6196          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6197          !!!next-token;          !!!next-token;
6198          redo B;          next B;
6199        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6200              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6201                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6202                  ## As if </option>              !!!cp ('t272');
6203                  pop @{$self->{open_elements}};              ## As if </option>
6204                }              pop @{$self->{open_elements}};
6205              } else {
6206                !!!cp ('t273');
6207              }
6208    
6209                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6210                !!!next-token;            !!!nack ('t273.1');
6211                redo B;            !!!next-token;
6212              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6213                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6214                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6215                  pop @{$self->{open_elements}};              !!!cp ('t274');
6216                }              ## As if </option>
6217                pop @{$self->{open_elements}};
6218              } else {
6219                !!!cp ('t275');
6220              }
6221    
6222                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6223                  ## As if </optgroup>              !!!cp ('t276');
6224                  pop @{$self->{open_elements}};              ## As if </optgroup>
6225                }              pop @{$self->{open_elements}};
6226              } else {
6227                !!!cp ('t277');
6228              }
6229    
6230                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6231                !!!next-token;            !!!nack ('t277.1');
6232                redo B;            !!!next-token;
6233              } elsif ($token->{tag_name} eq 'select') {            next B;
6234                !!!parse-error (type => 'not closed:select');          } elsif ({
6235                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6236                ## have an element in table scope                   }->{$token->{tag_name}} or
6237                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6238                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6239                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6240                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6241                    $i = $_;                     tr => 1, td => 1, th => 1,
6242                    last INSCOPE;                    }->{$token->{tag_name}})) {
6243                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6244                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6245                           }->{$node->[1]}) {                            token => $token);
6246                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6247                  }            ## as if there were </select> (otherwise).
6248                } # INSCOPE            ## have an element in table scope
6249                unless (defined $i) {            my $i;
6250                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6251                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6252                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6253                  redo B;                !!!cp ('t278');
6254                }                $i = $_;
6255                  last INSCOPE;
6256                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6257                  !!!cp ('t279');
6258                  last INSCOPE;
6259                }
6260              } # INSCOPE
6261              unless (defined $i) {
6262                !!!cp ('t280');
6263                !!!parse-error (type => 'unmatched end tag',
6264                                text => 'select', token => $token);
6265                ## Ignore the token
6266                !!!nack ('t280.1');
6267                !!!next-token;
6268                next B;
6269              }
6270                                
6271                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6272              splice @{$self->{open_elements}}, $i;
6273    
6274                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6275    
6276                !!!next-token;            if ($token->{tag_name} eq 'select') {
6277                redo B;              !!!nack ('t281.2');
6278                !!!next-token;
6279                next B;
6280              } else {
6281                !!!cp ('t281.1');
6282                !!!ack-later;
6283                ## Reprocess the token.
6284                next B;
6285              }
6286          } else {          } else {
6287            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
6288              !!!parse-error (type => 'in select',
6289                              text => $token->{tag_name}, token => $token);
6290            ## Ignore the token            ## Ignore the token
6291              !!!nack ('t282.1');
6292            !!!next-token;            !!!next-token;
6293            redo B;            next B;
6294          }          }
6295        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6296              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6297                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6298                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6299                  ## As if </option>              !!!cp ('t283');
6300                  splice @{$self->{open_elements}}, -2;              ## As if </option>
6301                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
6302                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6303                } else {              !!!cp ('t284');
6304                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
6305                  ## Ignore the token            } else {
6306                }              !!!cp ('t285');
6307                !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6308                redo B;                              text => $token->{tag_name}, token => $token);
6309              } elsif ($token->{tag_name} eq 'option') {              ## Ignore the token
6310                if ($self->{open_elements}->[-1]->[1] eq 'option') {            }
6311                  pop @{$self->{open_elements}};            !!!nack ('t285.1');
6312                } else {            !!!next-token;
6313                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            next B;
6314                  ## Ignore the token          } elsif ($token->{tag_name} eq 'option') {
6315                }            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6316                !!!next-token;              !!!cp ('t286');
6317                redo B;              pop @{$self->{open_elements}};
6318              } elsif ($token->{tag_name} eq 'select') {            } else {
6319                ## have an element in table scope              !!!cp ('t287');
6320                my $i;              !!!parse-error (type => 'unmatched end tag',
6321                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                              text => $token->{tag_name}, token => $token);
6322                  my $node = $self->{open_elements}->[$_];              ## Ignore the token
6323                  if ($node->[1] eq $token->{tag_name}) {            }
6324                    $i = $_;            !!!nack ('t287.1');
6325                    last INSCOPE;            !!!next-token;
6326                  } elsif ({            next B;
6327                            table => 1, html => 1,          } elsif ($token->{tag_name} eq 'select') {
6328                           }->{$node->[1]}) {            ## have an element in table scope
6329                    last INSCOPE;            my $i;
6330                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6331                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6332                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6333                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t288');
6334                  ## Ignore the token                $i = $_;
6335                  !!!next-token;                last INSCOPE;
6336                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6337                }                !!!cp ('t289');
6338                  last INSCOPE;
6339                }
6340              } # INSCOPE
6341              unless (defined $i) {
6342                !!!cp ('t290');
6343                !!!parse-error (type => 'unmatched end tag',
6344                                text => $token->{tag_name}, token => $token);
6345                ## Ignore the token
6346                !!!nack ('t290.1');
6347                !!!next-token;
6348                next B;
6349              }
6350                                
6351                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6352              splice @{$self->{open_elements}}, $i;
6353    
6354                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6355    
6356                !!!next-token;            !!!nack ('t291.1');
6357                redo B;            !!!next-token;
6358              } elsif ({            next B;
6359                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6360                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6361                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6362                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6363                     }->{$token->{tag_name}}) {
6364    ## TODO: The following is wrong?
6365              !!!parse-error (type => 'unmatched end tag',
6366                              text => $token->{tag_name}, token => $token);
6367                                
6368                ## have an element in table scope            ## have an element in table scope
6369                my $i;            my $i;
6370                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6371                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6372                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6373                    $i = $_;                !!!cp ('t292');
6374                    last INSCOPE;                $i = $_;
6375                  } elsif ({                last INSCOPE;
6376                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6377                           }->{$node->[1]}) {                !!!cp ('t293');
6378                    last INSCOPE;                last INSCOPE;
6379                  }              }
6380                } # INSCOPE            } # INSCOPE
6381                unless (defined $i) {            unless (defined $i) {
6382                  ## Ignore the token              !!!cp ('t294');
6383                  !!!next-token;              ## Ignore the token
6384                  redo B;              !!!nack ('t294.1');
6385                }              !!!next-token;
6386                next B;
6387              }
6388                                
6389                ## As if </select>            ## As if </select>
6390                ## have an element in table scope            ## have an element in table scope
6391                undef $i;            undef $i;
6392                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6393                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6394                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6395                    $i = $_;                !!!cp ('t295');
6396                    last INSCOPE;                $i = $_;
6397                  } elsif ({                last INSCOPE;
6398                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6399                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
6400                    last INSCOPE;                !!!cp ('t296');
6401                  }                last INSCOPE;
6402                } # INSCOPE              }
6403                unless (defined $i) {            } # INSCOPE
6404                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
6405                  ## Ignore the </select> token              !!!cp ('t297');
6406                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
6407                  redo B;              !!!parse-error (type => 'unmatched end tag',
6408                }                              text => 'select', token => $token);
6409                ## Ignore the </select> token
6410                !!!nack ('t297.1');
6411                !!!next-token; ## TODO: ok?
6412                next B;
6413              }
6414                                
6415                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
6416              splice @{$self->{open_elements}}, $i;
6417    
6418                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6419    
6420                ## reprocess            !!!ack-later;
6421                redo B;            ## reprocess
6422              next B;
6423          } else {          } else {
6424            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
6425              !!!parse-error (type => 'in select:/',
6426                              text => $token->{tag_name}, token => $token);
6427            ## Ignore the token            ## Ignore the token
6428              !!!nack ('t299.3');
6429            !!!next-token;            !!!next-token;
6430            redo B;            next B;
6431          }          }
6432          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6433            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6434                    @{$self->{open_elements}} == 1) { # redundant, maybe
6435              !!!cp ('t299.1');
6436              !!!parse-error (type => 'in body:#eof', token => $token);
6437            } else {
6438              !!!cp ('t299.2');
6439            }
6440    
6441            ## Stop parsing.
6442            last B;
6443        } else {        } else {
6444          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6445        }        }
# Line 4412  sub _tree_construction_main ($) { Line 6453  sub _tree_construction_main ($) {
6453            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6454                        
6455            unless (length $token->{data}) {            unless (length $token->{data}) {
6456                !!!cp ('t300');
6457              !!!next-token;              !!!next-token;
6458              redo B;              next B;
6459            }            }
6460          }          }
6461                    
6462          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6463            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6464              !!!parse-error (type => 'after html:#text', token => $token);
6465    
6466            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6467            } else {
6468              !!!cp ('t302');
6469          }          }
6470                    
6471          ## "after body" insertion mode          ## "after body" insertion mode
6472          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6473    
6474          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6475          ## reprocess          ## reprocess
6476          redo B;          next B;
6477        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6478          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6479            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6480              !!!parse-error (type => 'after html',
6481                              text => $token->{tag_name}, token => $token);
6482                        
6483            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6484            } else {
6485              !!!cp ('t304');
6486          }          }
6487    
6488          ## "after body" insertion mode          ## "after body" insertion mode
6489          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6490                            text => $token->{tag_name}, token => $token);
6491    
6492          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6493            !!!ack-later;
6494          ## reprocess          ## reprocess
6495          redo B;          next B;
6496        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6497          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6498            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6499              !!!parse-error (type => 'after html:/',
6500                              text => $token->{tag_name}, token => $token);
6501                        
6502            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6503            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6504            } else {
6505              !!!cp ('t306');
6506          }          }
6507    
6508          ## "after body" insertion mode          ## "after body" insertion mode
6509          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6510            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6511              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6512                !!!parse-error (type => 'unmatched end tag',
6513                                text => 'html', token => $token);
6514              ## Ignore the token              ## Ignore the token
6515              !!!next-token;              !!!next-token;
6516              redo B;              next B;
6517            } else {            } else {
6518                !!!cp ('t308');
6519              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6520              !!!next-token;              !!!next-token;
6521              redo B;              next B;
6522            }            }
6523          } else {          } else {
6524            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6525              !!!parse-error (type => 'after body:/',
6526                              text => $token->{tag_name}, token => $token);
6527    
6528            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6529            ## reprocess            ## reprocess
6530            redo B;            next B;
6531          }          }
6532          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6533            !!!cp ('t309.2');
6534            ## Stop parsing
6535            last B;
6536        } else {        } else {
6537          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6538        }        }
# Line 4478  sub _tree_construction_main ($) { Line 6542  sub _tree_construction_main ($) {
6542            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6543                        
6544            unless (length $token->{data}) {            unless (length $token->{data}) {
6545                !!!cp ('t310');
6546              !!!next-token;              !!!next-token;
6547              redo B;              next B;
6548            }            }
6549          }          }
6550                    
6551          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6552            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6553              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6554                !!!parse-error (type => 'in frameset:#text', token => $token);
6555            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6556              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6557            } else { # "after html frameset"              !!!parse-error (type => 'after frameset:#text', token => $token);
6558              !!!parse-error (type => 'after html:#character');            } else { # "after after frameset"
6559                !!!cp ('t313');
6560              $self->{insertion_mode} = AFTER_FRAMESET_IM;              !!!parse-error (type => 'after html:#text', token => $token);
             ## Reprocess in the "main" phase, "after frameset"...  
             !!!parse-error (type => 'after frameset:#character');  
6561            }            }
6562                        
6563            ## Ignore the token.            ## Ignore the token.
6564            if (length $token->{data}) {            if (length $token->{data}) {
6565                !!!cp ('t314');
6566              ## reprocess the rest of characters              ## reprocess the rest of characters
6567            } else {            } else {
6568                !!!cp ('t315');
6569              !!!next-token;              !!!next-token;
6570            }            }
6571            redo B;            next B;
6572          }          }
6573                    
6574          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6575        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!parse-error (type => 'after html:'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "main" phase, "after frameset" insertion mode...  
         }  
   
6576          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6577              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6578            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6579              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6580              !!!nack ('t318.1');
6581            !!!next-token;            !!!next-token;
6582            redo B;            next B;
6583          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6584                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6585            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6586              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6587            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6588              !!!ack ('t319.1');
6589            !!!next-token;            !!!next-token;
6590            redo B;            next B;
6591          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6592            ## NOTE: As if in body.            !!!cp ('t320');
6593            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6594            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6595              next B;
6596    
6597              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6598              ## has no parse error.
6599          } else {          } else {
6600            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6601              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6602            } else {              !!!parse-error (type => 'in frameset',
6603              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6604              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6605                !!!cp ('t322');
6606                !!!parse-error (type => 'after frameset',
6607                                text => $token->{tag_name}, token => $token);
6608              } else { # "after after frameset"
6609                !!!cp ('t322.2');
6610                !!!parse-error (type => 'after after frameset',
6611                                text => $token->{tag_name}, token => $token);
6612            }            }
6613            ## Ignore the token            ## Ignore the token
6614              !!!nack ('t322.1');
6615            !!!next-token;            !!!next-token;
6616            redo B;            next B;
6617          }          }
6618        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!parse-error (type => 'after html:/'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "main" phase, "after frameset" insertion mode...  
         }  
   
6619          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6620              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6621            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6622                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6623              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6624                !!!parse-error (type => 'unmatched end tag',
6625                                text => $token->{tag_name}, token => $token);
6626              ## Ignore the token              ## Ignore the token
6627              !!!next-token;              !!!next-token;
6628            } else {            } else {
6629                !!!cp ('t326');
6630              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6631              !!!next-token;              !!!next-token;
6632            }            }
6633    
6634            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6635                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6636                !!!cp ('t327');
6637              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6638              } else {
6639                !!!cp ('t328');
6640            }            }
6641            redo B;            next B;
6642          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6643                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6644              !!!cp ('t329');
6645            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6646            !!!next-token;            !!!next-token;
6647            redo B;            next B;
6648          } else {          } else {
6649            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6650              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6651            } else {              !!!parse-error (type => 'in frameset:/',
6652              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6653              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6654                !!!cp ('t330.1');
6655                !!!parse-error (type => 'after frameset:/',
6656                                text => $token->{tag_name}, token => $token);
6657              } else { # "after after html"
6658                !!!cp ('t331');
6659                !!!parse-error (type => 'after after frameset:/',
6660                                text => $token->{tag_name}, token => $token);
6661            }            }
6662            ## Ignore the token            ## Ignore the token
6663            !!!next-token;            !!!next-token;
6664            redo B;            next B;
6665            }
6666          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6667            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6668                    @{$self->{open_elements}} == 1) { # redundant, maybe
6669              !!!cp ('t331.1');
6670              !!!parse-error (type => 'in body:#eof', token => $token);
6671            } else {
6672              !!!cp ('t331.2');
6673          }          }
6674            
6675            ## Stop parsing
6676            last B;
6677        } else {        } else {
6678          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6679        }        }
# Line 4591  sub _tree_construction_main ($) { Line 6686  sub _tree_construction_main ($) {
6686      ## "in body" insertion mode      ## "in body" insertion mode
6687      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6688        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6689            !!!cp ('t332');
6690          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6691          $script_start_tag->($insert);          $script_start_tag->();
6692          redo B;          next B;
6693        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6694            !!!cp ('t333');
6695          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6696          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6697          redo B;          next B;
6698        } elsif ({        } elsif ({
6699                  base => 1, link => 1,                  base => 1, link => 1,
6700                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6701            !!!cp ('t334');
6702          ## 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
6703          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6704          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6705            !!!ack ('t334.1');
6706          !!!next-token;          !!!next-token;
6707          redo B;          next B;
6708        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6709          ## 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
6710          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6711          my $meta_el = 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.
6712    
6713          unless ($self->{confident}) {          unless ($self->{confident}) {
6714            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6715                !!!cp ('t335');
6716                ## NOTE: Whether the encoding is supported or not is handled
6717                ## in the {change_encoding} callback.
6718              $self->{change_encoding}              $self->{change_encoding}
6719                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6720                            
6721              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6722                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6723                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6724                                           ->{has_reference});                                           ->{has_reference});
6725            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6726              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6727                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6728                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6729                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6730                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6731                  !!!cp ('t336');
6732                  ## NOTE: Whether the encoding is supported or not is handled
6733                  ## in the {change_encoding} callback.
6734                $self->{change_encoding}                $self->{change_encoding}
6735                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6736                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6737                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6738                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 4637  sub _tree_construction_main ($) { Line 6741  sub _tree_construction_main ($) {
6741            }            }
6742          } else {          } else {
6743            if ($token->{attributes}->{charset}) {            if ($token->{attributes}->{charset}) {
6744                !!!cp ('t337');
6745              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6746                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6747                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6748                                           ->{has_reference});                                           ->{has_reference});
6749            }            }
6750            if ($token->{attributes}->{content}) {            if ($token->{attributes}->{content}) {
6751                !!!cp ('t338');
6752              $meta_el->[0]->get_attribute_node_ns (undef, 'content')              $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6753                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6754                                       $token->{attributes}->{content}                                       $token->{attributes}->{content}
# Line 4650  sub _tree_construction_main ($) { Line 6756  sub _tree_construction_main ($) {
6756            }            }
6757          }          }
6758    
6759            !!!ack ('t338.1');
6760          !!!next-token;          !!!next-token;
6761          redo B;          next B;
6762        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6763          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6764          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6765          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6766            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6767        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6768          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6769                                
6770          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6771              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6772              !!!cp ('t342');
6773            ## Ignore the token            ## Ignore the token
6774          } else {          } else {
6775            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6776            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6777              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6778                  !!!cp ('t343');
6779                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6780                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6781                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6782              }              }
6783            }            }
6784          }          }
6785            !!!nack ('t343.1');
6786          !!!next-token;          !!!next-token;
6787          redo B;          next B;
6788        } elsif ({        } elsif ({
6789                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6790                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6791                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6792                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6793                  pre => 1,                  pre => 1, listing => 1,
6794                    form => 1,
6795                    table => 1,
6796                    hr => 1,
6797                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6798            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6799              !!!cp ('t350');
6800              !!!parse-error (type => 'in form:form', token => $token);
6801              ## Ignore the token
6802              !!!nack ('t350.1');
6803              !!!next-token;
6804              next B;
6805            }
6806    
6807          ## has a p element in scope          ## has a p element in scope
6808          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6809            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6810              !!!back-token;              !!!cp ('t344');
6811              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6812              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6813            } elsif ({                        line => $token->{line}, column => $token->{column}};
6814                      table => 1, caption => 1, td => 1, th => 1,              next B;
6815                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6816                     }->{$_->[1]}) {              !!!cp ('t345');
6817              last INSCOPE;              last INSCOPE;
6818            }            }
6819          } # INSCOPE          } # INSCOPE
6820                        
6821          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6822          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6823              !!!nack ('t346.1');
6824            !!!next-token;            !!!next-token;
6825            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6826              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6827              unless (length $token->{data}) {              unless (length $token->{data}) {
6828                  !!!cp ('t346');
6829                !!!next-token;                !!!next-token;
6830                } else {
6831                  !!!cp ('t349');
6832              }              }
6833              } else {
6834                !!!cp ('t348');
6835            }            }
6836          } else {          } elsif ($token->{tag_name} eq 'form') {
6837              !!!cp ('t347.1');
6838              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6839    
6840              !!!nack ('t347.2');
6841            !!!next-token;            !!!next-token;
6842          }          } elsif ($token->{tag_name} eq 'table') {
6843          redo B;            !!!cp ('t382');
6844        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6845          if (defined $self->{form_element}) {            
6846            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6847            ## Ignore the token  
6848              !!!nack ('t382.1');
6849              !!!next-token;
6850            } elsif ($token->{tag_name} eq 'hr') {
6851              !!!cp ('t386');
6852              pop @{$self->{open_elements}};
6853            
6854              !!!nack ('t386.1');
6855            !!!next-token;            !!!next-token;
           redo B;  
6856          } else {          } else {
6857            ## 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];  
6858            !!!next-token;            !!!next-token;
           redo B;  
6859          }          }
6860        } elsif ($token->{tag_name} eq 'li') {          next B;
6861          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6862          ## has a p element in scope          ## has a p element in scope
6863          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6864            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6865              !!!back-token;              !!!cp ('t353');
6866              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6867              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6868            } elsif ({                        line => $token->{line}, column => $token->{column}};
6869                      table => 1, caption => 1, td => 1, th => 1,              next B;
6870                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6871                     }->{$_->[1]}) {              !!!cp ('t354');
6872              last INSCOPE;              last INSCOPE;
6873            }            }
6874          } # INSCOPE          } # INSCOPE
# Line 4758  sub _tree_construction_main ($) { Line 6876  sub _tree_construction_main ($) {
6876          ## Step 1          ## Step 1
6877          my $i = -1;          my $i = -1;
6878          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6879            my $li_or_dtdd = {li => {li => 1},
6880                              dt => {dt => 1, dd => 1},
6881                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6882          LI: {          LI: {
6883            ## Step 2            ## Step 2
6884            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6885              if ($i != -1) {              if ($i != -1) {
6886                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6887                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6888              }                                text => $self->{open_elements}->[-1]->[0]
6889              splice @{$self->{open_elements}}, $i;                                    ->manakai_local_name,
6890              last LI;                                token => $token);
6891            }              } else {
6892                            !!!cp ('t356');
           ## 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') {  
         ## 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  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
6893              }              }
6894              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6895              last LI;              last LI;
6896              } else {
6897                !!!cp ('t357');
6898            }            }
6899                        
6900            ## Step 3            ## Step 3
6901            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6902                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6903                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6904                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6905                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6906                  not ($node->[1] & DIV_EL)) {
6907                !!!cp ('t358');
6908              last LI;              last LI;
6909            }            }
6910                        
6911              !!!cp ('t359');
6912            ## Step 4            ## Step 4
6913            $i--;            $i--;
6914            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
6915            redo LI;            redo LI;
6916          } # LI          } # LI
6917                        
6918          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6919            !!!nack ('t359.1');
6920          !!!next-token;          !!!next-token;
6921          redo B;          next B;
6922        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6923          ## has a p element in scope          ## has a p element in scope
6924          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6925            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6926              !!!back-token;              !!!cp ('t367');
6927              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
6928              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6929            } elsif ({                        line => $token->{line}, column => $token->{column}};
6930                      table => 1, caption => 1, td => 1, th => 1,              next B;
6931                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6932                     }->{$_->[1]}) {              !!!cp ('t368');
6933              last INSCOPE;              last INSCOPE;
6934            }            }
6935          } # INSCOPE          } # INSCOPE
6936                        
6937          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6938                        
6939          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6940                        
6941            !!!nack ('t368.1');
6942          !!!next-token;          !!!next-token;
6943          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;  
6944        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6945          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6946            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6947            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6948              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
6949                !!!parse-error (type => 'in a:a', token => $token);
6950                            
6951              !!!back-token;              !!!back-token; # <a>
6952              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6953              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6954                $formatting_end_tag->($token);
6955                            
6956              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6957                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
6958                    !!!cp ('t372');
6959                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
6960                  last AFE2;                  last AFE2;
6961                }                }
6962              } # AFE2              } # AFE2
6963              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
6964                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
6965                    !!!cp ('t373');
6966                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
6967                  last OE;                  last OE;
6968                }                }
6969              } # OE              } # OE
6970              last AFE;              last AFE;
6971            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
6972                !!!cp ('t374');
6973              last AFE;              last AFE;
6974            }            }
6975          } # AFE          } # AFE
6976                        
6977          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6978    
6979          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6980          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6981    
6982            !!!nack ('t374.1');
6983          !!!next-token;          !!!next-token;
6984          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;  
6985        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6986          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6987    
6988          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6989          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6990            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6991            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6992              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
6993              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
6994              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
6995              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6996            } elsif ({                        line => $token->{line}, column => $token->{column}};
6997                      table => 1, caption => 1, td => 1, th => 1,              next B;
6998                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
6999                     }->{$node->[1]}) {              !!!cp ('t377');
7000              last INSCOPE;              last INSCOPE;
7001            }            }
7002          } # INSCOPE          } # INSCOPE
7003                    
7004          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7005          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7006                    
7007            !!!nack ('t377.1');
7008          !!!next-token;          !!!next-token;
7009          redo B;          next B;
7010        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7011          ## has a button element in scope          ## has a button element in scope
7012          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7013            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7014            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7015              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
7016              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
7017              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
7018              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7019            } elsif ({                        line => $token->{line}, column => $token->{column}};
7020                      table => 1, caption => 1, td => 1, th => 1,              next B;
7021                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
7022                     }->{$node->[1]}) {              !!!cp ('t379');
7023              last INSCOPE;              last INSCOPE;
7024            }            }
7025          } # INSCOPE          } # INSCOPE
7026                        
7027          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7028                        
7029          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7030          push @$active_formatting_elements, ['#marker', ''];  
7031            ## TODO: associate with $self->{form_element} if defined
7032    
         !!!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});  
7033          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7034            
7035          !!!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;  
             
7036          !!!next-token;          !!!next-token;
7037          redo B;          next B;
7038        } elsif ({        } elsif ({
7039                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7040                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7041                  image => 1,                  noembed => 1,
7042                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7043                    noscript => 0, ## TODO: 1 if scripting is enabled
7044                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7045          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7046            !!!parse-error (type => 'image');            !!!cp ('t381');
7047            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
7048            } else {
7049              !!!cp ('t399');
7050          }          }
7051            ## NOTE: There is an "as if in body" code clone.
7052          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7053          $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;  
7054        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7055          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7056                    
7057          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7058              !!!cp ('t389');
7059            ## Ignore the token            ## Ignore the token
7060              !!!nack ('t389'); ## NOTE: Not acknowledged.
7061            !!!next-token;            !!!next-token;
7062            redo B;            next B;
7063          } else {          } else {
7064              !!!ack ('t391.1');
7065    
7066            my $at = $token->{attributes};            my $at = $token->{attributes};
7067            my $form_attrs;            my $form_attrs;
7068            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5093  sub _tree_construction_main ($) { Line 7072  sub _tree_construction_main ($) {
7072            delete $at->{prompt};            delete $at->{prompt};
7073            my @tokens = (            my @tokens = (
7074                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7075                           attributes => $form_attrs},                           attributes => $form_attrs,
7076                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7077                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7078                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7079                            {type => START_TAG_TOKEN, tag_name => 'p',
7080                             line => $token->{line}, column => $token->{column}},
7081                            {type => START_TAG_TOKEN, tag_name => 'label',
7082                             line => $token->{line}, column => $token->{column}},
7083                         );                         );
7084            if ($prompt_attr) {            if ($prompt_attr) {
7085              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
7086                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7087                               #line => $token->{line}, column => $token->{column},
7088                              };
7089            } else {            } else {
7090                !!!cp ('t391');
7091              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7092                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7093                               #line => $token->{line}, column => $token->{column},
7094                              }; # SHOULD
7095              ## TODO: make this configurable              ## TODO: make this configurable
7096            }            }
7097            push @tokens,            push @tokens,
7098                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7099                             line => $token->{line}, column => $token->{column}},
7100                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7101                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7102                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7103                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7104                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7105            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7106                             line => $token->{line}, column => $token->{column}},
7107                            {type => END_TAG_TOKEN, tag_name => 'form',
7108                             line => $token->{line}, column => $token->{column}};
7109            !!!back-token (@tokens);            !!!back-token (@tokens);
7110            redo B;            !!!next-token;
7111              next B;
7112          }          }
7113        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7114          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7115          my $el;          my $el;
7116          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7117                    
7118          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7119          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5128  sub _tree_construction_main ($) { Line 7122  sub _tree_construction_main ($) {
7122          $insert->($el);          $insert->($el);
7123                    
7124          my $text = '';          my $text = '';
7125            !!!nack ('t392.1');
7126          !!!next-token;          !!!next-token;
7127          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7128            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
7129            unless (length $token->{data}) {            unless (length $token->{data}) {
7130                !!!cp ('t392');
7131              !!!next-token;              !!!next-token;
7132              } else {
7133                !!!cp ('t393');
7134            }            }
7135            } else {
7136              !!!cp ('t394');
7137          }          }
7138          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
7139              !!!cp ('t395');
7140            $text .= $token->{data};            $text .= $token->{data};
7141            !!!next-token;            !!!next-token;
7142          }          }
7143          if (length $text) {          if (length $text) {
7144              !!!cp ('t396');
7145            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
7146          }          }
7147                    
# Line 5147  sub _tree_construction_main ($) { Line 7149  sub _tree_construction_main ($) {
7149                    
7150          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
7151              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
7152              !!!cp ('t397');
7153            ## Ignore the token            ## Ignore the token
7154          } else {          } else {
7155            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
7156              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7157          }          }
7158          !!!next-token;          !!!next-token;
7159            next B;
7160          } elsif ($token->{tag_name} eq 'rt' or
7161                   $token->{tag_name} eq 'rp') {
7162            ## has a |ruby| element in scope
7163            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7164              my $node = $self->{open_elements}->[$_];
7165              if ($node->[1] & RUBY_EL) {
7166                !!!cp ('t398.1');
7167                ## generate implied end tags
7168                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7169                  !!!cp ('t398.2');
7170                  pop @{$self->{open_elements}};
7171                }
7172                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7173                  !!!cp ('t398.3');
7174                  !!!parse-error (type => 'not closed',
7175                                  text => $self->{open_elements}->[-1]->[0]
7176                                      ->manakai_local_name,
7177                                  token => $token);
7178                  pop @{$self->{open_elements}}
7179                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7180                }
7181                last INSCOPE;
7182              } elsif ($node->[1] & SCOPING_EL) {
7183                !!!cp ('t398.4');
7184                last INSCOPE;
7185              }
7186            } # INSCOPE
7187    
7188            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7189    
7190            !!!nack ('t398.5');
7191            !!!next-token;
7192          redo B;          redo B;
7193        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7194                  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') {  
7195          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7196    
7197            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7198    
7199            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7200    
7201            ## "adjust foreign attributes" - done in insert-element-f
7202                    
7203          !!!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);
7204                    
7205          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
7206              pop @{$self->{open_elements}};
7207              !!!ack ('t398.1');
7208            } else {
7209              !!!cp ('t398.2');
7210              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7211              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7212              ## mode, "in body" (not "in foreign content") secondary insertion
7213              ## mode, maybe.
7214            }
7215    
7216          !!!next-token;          !!!next-token;
7217          redo B;          next B;
7218        } elsif ({        } elsif ({
7219                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7220                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
7221                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
7222                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7223                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7224          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
7225            !!!parse-error (type => 'in body',
7226                            text => $token->{tag_name}, token => $token);
7227          ## Ignore the token          ## Ignore the token
7228            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7229          !!!next-token;          !!!next-token;
7230          redo B;          next B;
7231                    
7232          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7233        } else {        } else {
7234            if ($token->{tag_name} eq 'image') {
7235              !!!cp ('t384');
7236              !!!parse-error (type => 'image', token => $token);
7237              $token->{tag_name} = 'img';
7238            } else {
7239              !!!cp ('t385');
7240            }
7241    
7242            ## NOTE: There is an "as if <br>" code clone.
7243          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7244                    
7245          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7246    
7247            if ({
7248                 applet => 1, marquee => 1, object => 1,
7249                }->{$token->{tag_name}}) {
7250              !!!cp ('t380');
7251              push @$active_formatting_elements, ['#marker', ''];
7252              !!!nack ('t380.1');
7253            } elsif ({
7254                      b => 1, big => 1, em => 1, font => 1, i => 1,
7255                      s => 1, small => 1, strile => 1,
7256                      strong => 1, tt => 1, u => 1,
7257                     }->{$token->{tag_name}}) {
7258              !!!cp ('t375');
7259              push @$active_formatting_elements, $self->{open_elements}->[-1];
7260              !!!nack ('t375.1');
7261            } elsif ($token->{tag_name} eq 'input') {
7262              !!!cp ('t388');
7263              ## TODO: associate with $self->{form_element} if defined
7264              pop @{$self->{open_elements}};
7265              !!!ack ('t388.2');
7266            } elsif ({
7267                      area => 1, basefont => 1, bgsound => 1, br => 1,
7268                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7269                      #image => 1,
7270                     }->{$token->{tag_name}}) {
7271              !!!cp ('t388.1');
7272              pop @{$self->{open_elements}};
7273              !!!ack ('t388.3');
7274            } elsif ($token->{tag_name} eq 'select') {
7275              ## TODO: associate with $self->{form_element} if defined
7276            
7277              if ($self->{insertion_mode} & TABLE_IMS or
7278                  $self->{insertion_mode} & BODY_TABLE_IMS or
7279                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7280                !!!cp ('t400.1');
7281                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7282              } else {
7283                !!!cp ('t400.2');
7284                $self->{insertion_mode} = IN_SELECT_IM;
7285              }
7286              !!!nack ('t400.3');
7287            } else {
7288              !!!nack ('t402');
7289            }
7290                    
7291          !!!next-token;          !!!next-token;
7292          redo B;          next B;
7293        }        }
7294      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7295        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7296          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7297              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7298            for (@{$self->{open_elements}}) {          INSCOPE: {
7299              unless ({            for (reverse @{$self->{open_elements}}) {
7300                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7301                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7302                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7303                      }->{$_->[1]}) {                last INSCOPE;
7304                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
7305                  !!!cp ('t405.1');
7306                  last;
7307              }              }
7308            }            }
7309    
7310            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7311            !!!next-token;                            text => $token->{tag_name}, token => $token);
7312            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
7313            !!!next-token;            !!!next-token;
7314            redo B;            next B;
7315            } # INSCOPE
7316    
7317            for (@{$self->{open_elements}}) {
7318              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7319                !!!cp ('t403');
7320                !!!parse-error (type => 'not closed',
7321                                text => $_->[0]->manakai_local_name,
7322                                token => $token);
7323                last;
7324              } else {
7325                !!!cp ('t404');
7326              }
7327          }          }
7328    
7329            $self->{insertion_mode} = AFTER_BODY_IM;
7330            !!!next-token;
7331            next B;
7332        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7333          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
7334            ## up-to-date, though it has same effect as speced.
7335            if (@{$self->{open_elements}} > 1 and
7336                $self->{open_elements}->[1]->[1] & BODY_EL) {
7337            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7338            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7339              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
7340                !!!parse-error (type => 'not closed',
7341                                text => $self->{open_elements}->[1]->[0]
7342                                    ->manakai_local_name,
7343                                token => $token);
7344              } else {
7345                !!!cp ('t407');
7346            }            }
7347            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7348            ## reprocess            ## reprocess
7349            redo B;            next B;
7350          } else {          } else {
7351            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
7352              !!!parse-error (type => 'unmatched end tag',
7353                              text => $token->{tag_name}, token => $token);
7354            ## Ignore the token            ## Ignore the token
7355            !!!next-token;            !!!next-token;
7356            redo B;            next B;
7357          }          }
7358        } elsif ({        } elsif ({
7359                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7360                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7361                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7362                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7363                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7364                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7365          ## has an element in scope          ## has an element in scope
7366          my $i;          my $i;
7367          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7368            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7369            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7370              ## 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;  
             }  
7371              $i = $_;              $i = $_;
7372              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7373            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7374                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7375              last INSCOPE;              last INSCOPE;
7376            }            }
7377          } # INSCOPE          } # INSCOPE
7378            
7379          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7380            if (defined $i) {            !!!cp ('t413');
7381              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag',
7382                              text => $token->{tag_name}, token => $token);
7383              ## NOTE: Ignore the token.
7384            } else {
7385              ## Step 1. generate implied end tags
7386              while ({
7387                      ## END_TAG_OPTIONAL_EL
7388                      dd => ($token->{tag_name} ne 'dd'),
7389                      dt => ($token->{tag_name} ne 'dt'),
7390                      li => ($token->{tag_name} ne 'li'),
7391                      p => 1,
7392                      rt => 1,
7393                      rp => 1,
7394                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7395                !!!cp ('t409');
7396                pop @{$self->{open_elements}};
7397              }
7398    
7399              ## Step 2.
7400              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7401                      ne $token->{tag_name}) {
7402                !!!cp ('t412');
7403                !!!parse-error (type => 'not closed',
7404                                text => $self->{open_elements}->[-1]->[0]
7405                                    ->manakai_local_name,
7406                                token => $token);
7407            } else {            } else {
7408              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
7409            }            }
7410          }  
7411                      ## Step 3.
         if (defined $i) {  
7412            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7413          } elsif ($token->{tag_name} eq 'p') {  
7414            ## As if <p>, then reprocess the current token            ## Step 4.
7415            my $el;            $clear_up_to_marker->()
7416            !!!create-element ($el, 'p');                if {
7417            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
7418                  }->{$token->{tag_name}};
7419          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7420          !!!next-token;          !!!next-token;
7421          redo B;          next B;
7422        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7423            undef $self->{form_element};
7424    
7425          ## has an element in scope          ## has an element in scope
7426            my $i;
7427          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7428            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7429            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7430              ## generate implied end tags              !!!cp ('t418');
7431              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;  
             }  
7432              last INSCOPE;              last INSCOPE;
7433            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7434                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7435              last INSCOPE;              last INSCOPE;
7436            }            }
7437          } # INSCOPE          } # INSCOPE
7438            
7439          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7440            pop @{$self->{open_elements}};            !!!cp ('t421');
7441          } else {            !!!parse-error (type => 'unmatched end tag',
7442            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                            text => $token->{tag_name}, token => $token);
7443              ## NOTE: Ignore the token.
7444            } else {
7445              ## Step 1. generate implied end tags
7446              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7447                !!!cp ('t417');
7448                pop @{$self->{open_elements}};
7449              }
7450              
7451              ## Step 2.
7452              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7453                      ne $token->{tag_name}) {
7454                !!!cp ('t417.1');
7455                !!!parse-error (type => 'not closed',
7456                                text => $self->{open_elements}->[-1]->[0]
7457                                    ->manakai_local_name,
7458                                token => $token);
7459              } else {
7460                !!!cp ('t420');
7461              }  
7462              
7463              ## Step 3.
7464              splice @{$self->{open_elements}}, $i;
7465          }          }
7466    
         undef $self->{form_element};  
7467          !!!next-token;          !!!next-token;
7468          redo B;          next B;
7469        } elsif ({        } elsif ({
7470                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7471                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5328  sub _tree_construction_main ($) { Line 7473  sub _tree_construction_main ($) {
7473          my $i;          my $i;
7474          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7475            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7476            if ({            if ($node->[1] & HEADING_EL) {
7477                 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;  
             }  
7478              $i = $_;              $i = $_;
7479              last INSCOPE;              last INSCOPE;
7480            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7481                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7482              last INSCOPE;              last INSCOPE;
7483            }            }
7484          } # INSCOPE          } # INSCOPE
7485            
7486          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7487            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
7488              !!!parse-error (type => 'unmatched end tag',
7489                              text => $token->{tag_name}, token => $token);
7490              ## NOTE: Ignore the token.
7491            } else {
7492              ## Step 1. generate implied end tags
7493              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7494                !!!cp ('t422');
7495                pop @{$self->{open_elements}};
7496              }
7497              
7498              ## Step 2.
7499              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7500                      ne $token->{tag_name}) {
7501                !!!cp ('t425');
7502                !!!parse-error (type => 'unmatched end tag',
7503                                text => $token->{tag_name}, token => $token);
7504              } else {
7505                !!!cp ('t426');
7506              }
7507    
7508              ## Step 3.
7509              splice @{$self->{open_elements}}, $i;
7510          }          }
7511                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7512          !!!next-token;          !!!next-token;
7513          redo B;          next B;
7514          } elsif ($token->{tag_name} eq 'p') {
7515            ## has an element in scope
7516            my $i;
7517            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7518              my $node = $self->{open_elements}->[$_];
7519              if ($node->[1] & P_EL) {
7520                !!!cp ('t410.1');
7521                $i = $_;
7522                last INSCOPE;
7523              } elsif ($node->[1] & SCOPING_EL) {
7524                !!!cp ('t411.1');
7525                last INSCOPE;
7526              }
7527            } # INSCOPE
7528    
7529            if (defined $i) {
7530              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7531                      ne $token->{tag_name}) {
7532                !!!cp ('t412.1');
7533                !!!parse-error (type => 'not closed',
7534                                text => $self->{open_elements}->[-1]->[0]
7535                                    ->manakai_local_name,
7536                                token => $token);
7537              } else {
7538                !!!cp ('t414.1');
7539              }
7540    
7541              splice @{$self->{open_elements}}, $i;
7542            } else {
7543              !!!cp ('t413.1');
7544              !!!parse-error (type => 'unmatched end tag',
7545                              text => $token->{tag_name}, token => $token);
7546    
7547              !!!cp ('t415.1');
7548              ## As if <p>, then reprocess the current token
7549              my $el;
7550              !!!create-element ($el, $HTML_NS, 'p',, $token);
7551              $insert->($el);
7552              ## NOTE: Not inserted into |$self->{open_elements}|.
7553            }
7554    
7555            !!!next-token;
7556            next B;
7557        } elsif ({        } elsif ({
7558                  a => 1,                  a => 1,
7559                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7560                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7561                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7562                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7563          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7564          redo B;          $formatting_end_tag->($token);
7565            next B;
7566        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7567          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7568            !!!parse-error (type => 'unmatched end tag',
7569                            text => 'br', token => $token);
7570    
7571          ## As if <br>          ## As if <br>
7572          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7573                    
7574          my $el;          my $el;
7575          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7576          $insert->($el);          $insert->($el);
7577                    
7578          ## Ignore the token.          ## Ignore the token.
7579          !!!next-token;          !!!next-token;
7580          redo B;          next B;
7581        } elsif ({        } elsif ({
7582                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7583                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5392  sub _tree_construction_main ($) { Line 7590  sub _tree_construction_main ($) {
7590                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7591                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7592                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7593          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7594            !!!parse-error (type => 'unmatched end tag',
7595                            text => $token->{tag_name}, token => $token);
7596          ## Ignore the token          ## Ignore the token
7597          !!!next-token;          !!!next-token;
7598          redo B;          next B;
7599                    
7600          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7601                    
# Line 5406  sub _tree_construction_main ($) { Line 7606  sub _tree_construction_main ($) {
7606    
7607          ## Step 2          ## Step 2
7608          S2: {          S2: {
7609            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7610              ## Step 1              ## Step 1
7611              ## generate implied end tags              ## generate implied end tags
7612              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7613                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7614                   td => 1, th => 1, tr => 1,                ## NOTE: |<ruby><rt></ruby>|.
7615                   tbody => 1, tfoot => 1, thead => 1,                ## ISSUE: <ruby><rt></rt> will also take this code path,
7616                  }->{$self->{open_elements}->[-1]->[1]}) {                ## which seems wrong.
7617                !!!back-token;                pop @{$self->{open_elements}};
7618                $token = {type => END_TAG_TOKEN,                $node_i++;
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7619              }              }
7620                    
7621              ## Step 2              ## Step 2
7622              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7623                        ne $token->{tag_name}) {
7624                  !!!cp ('t431');
7625                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7626                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7627                                  text => $self->{open_elements}->[-1]->[0]
7628                                      ->manakai_local_name,
7629                                  token => $token);
7630                } else {
7631                  !!!cp ('t432');
7632              }              }
7633                            
7634              ## Step 3              ## Step 3
7635              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7636    
7637              !!!next-token;              !!!next-token;
7638              last S2;              last S2;
7639            } else {            } else {
7640              ## Step 3              ## Step 3
7641              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7642                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7643                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7644                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7645                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7646                  !!!parse-error (type => 'unmatched end tag',
7647                                  text => $token->{tag_name}, token => $token);
7648                ## Ignore the token                ## Ignore the token
7649                !!!next-token;                !!!next-token;
7650                last S2;                last S2;
7651              }              }
7652    
7653                !!!cp ('t434');
7654            }            }
7655                        
7656            ## Step 4            ## Step 4
# Line 5451  sub _tree_construction_main ($) { Line 7660  sub _tree_construction_main ($) {
7660            ## Step 5;            ## Step 5;
7661            redo S2;            redo S2;
7662          } # S2          } # S2
7663          redo B;          next B;
7664        }        }
7665      }      }
7666      redo B;      next B;
7667      } continue { # B
7668        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7669          ## NOTE: The code below is executed in cases where it does not have
7670          ## to be, but it it is harmless even in those cases.
7671          ## has an element in scope
7672          INSCOPE: {
7673            for (reverse 0..$#{$self->{open_elements}}) {
7674              my $node = $self->{open_elements}->[$_];
7675              if ($node->[1] & FOREIGN_EL) {
7676                last INSCOPE;
7677              } elsif ($node->[1] & SCOPING_EL) {
7678                last;
7679              }
7680            }
7681            
7682            ## NOTE: No foreign element in scope.
7683            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7684          } # INSCOPE
7685        }
7686    } # B    } # B
7687    
   ## 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  
   
7688    ## Stop parsing # MUST    ## Stop parsing # MUST
7689        
7690    ## TODO: script stuffs    ## TODO: script stuffs
7691  } # _tree_construct_main  } # _tree_construct_main
7692    
7693  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7694    my $class = shift;    my $class = shift;
7695    my $node = shift;    my $node = shift;
7696    my $s = \$_[0];    my $s = \$_[0];
7697    my $onerror = $_[1];    my $onerror = $_[1];
7698      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7699    
7700    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7701    
# Line 5490  sub set_inner_html ($$$) { Line 7714  sub set_inner_html ($$$) {
7714      }      }
7715    
7716      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7717      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7718    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7719      ## TODO: If non-html element      ## TODO: If non-html element
7720    
7721      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7722    
7723    ## TODO: Support for $get_wrapper
7724    
7725      ## Step 1 # MUST      ## Step 1 # MUST
7726      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7727      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5503  sub set_inner_html ($$$) { Line 7729  sub set_inner_html ($$$) {
7729      my $p = $class->new;      my $p = $class->new;
7730      $p->{document} = $doc;      $p->{document} = $doc;
7731    
7732      ## Step 9 # MUST      ## Step 8 # MUST
7733      my $i = 0;      my $i = 0;
7734      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7735      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7736      $p->{set_next_input_character} = sub {      $p->{set_next_char} = sub {
7737        my $self = shift;        my $self = shift;
7738    
7739        pop @{$self->{prev_input_character}};        pop @{$self->{prev_char}};
7740        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        unshift @{$self->{prev_char}}, $self->{next_char};
7741    
7742          $self->{next_char} = -1 and return if $i >= length $$s;
7743          $self->{next_char} = ord substr $$s, $i++, 1;
7744    
7745          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7746          $p->{column}++;
7747    
7748        $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($self->{next_char} == 0x000A) { # LF
7749        $self->{next_input_character} = ord substr $$s, $i++, 1;          $p->{line}++;
7750        $column++;          $p->{column} = 0;
7751            !!!cp ('i1');
7752        if ($self->{next_input_character} == 0x000A) { # LF        } elsif ($self->{next_char} == 0x000D) { # CR
         $line++;  
         $column = 0;  
       } elsif ($self->{next_input_character} == 0x000D) { # CR  
7753          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7754          $self->{next_input_character} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7755          $line++;          $p->{line}++;
7756          $column = 0;          $p->{column} = 0;
7757        } elsif ($self->{next_input_character} > 0x10FFFF) {          !!!cp ('i2');
7758          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        } elsif ($self->{next_char} > 0x10FFFF) {
7759        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7760            !!!cp ('i3');
7761          } elsif ($self->{next_char} == 0x0000) { # NULL
7762            !!!cp ('i4');
7763          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7764          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7765          } elsif ($self->{next_char} <= 0x0008 or
7766                   (0x000E <= $self->{next_char} and
7767                    $self->{next_char} <= 0x001F) or
7768                   (0x007F <= $self->{next_char} and
7769                    $self->{next_char} <= 0x009F) or
7770                   (0xD800 <= $self->{next_char} and
7771                    $self->{next_char} <= 0xDFFF) or
7772                   (0xFDD0 <= $self->{next_char} and
7773                    $self->{next_char} <= 0xFDDF) or
7774                   {
7775                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7776                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7777                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7778                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7779                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7780                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7781                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7782                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7783                    0x10FFFE => 1, 0x10FFFF => 1,
7784                   }->{$self->{next_char}}) {
7785            !!!cp ('i4.1');
7786            if ($self->{next_char} < 0x10000) {
7787              !!!parse-error (type => 'control char',
7788                              text => (sprintf 'U+%04X', $self->{next_char}));
7789            } else {
7790              !!!parse-error (type => 'control char',
7791                              text => (sprintf 'U-%08X', $self->{next_char}));
7792            }
7793        }        }
7794      };      };
7795      $p->{prev_input_character} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7796      $p->{next_input_character} = -1;      $p->{next_char} = -1;
7797        
7798        $p->{read_until} = sub {
7799          ## TODO: ...
7800          return 0;
7801        }; # $p->{read_until};
7802    
7803      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7804        my (%opt) = @_;        my (%opt) = @_;
7805        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7806          my $column = $opt{column};
7807          if (defined $opt{token} and defined $opt{token}->{line}) {
7808            $line = $opt{token}->{line};
7809            $column = $opt{token}->{column};
7810          }
7811          warn "Parse error ($opt{type}) at line $line column $column\n";
7812      };      };
7813      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7814        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7815      };      };
7816            
7817      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 5564  sub set_inner_html ($$$) { Line 7835  sub set_inner_html ($$$) {
7835          unless defined $p->{content_model};          unless defined $p->{content_model};
7836          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7837    
7838      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7839          ## TODO: Foreign element OK?
7840    
7841      ## Step 4      ## Step 3
7842      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7843        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7844    
7845      ## Step 5 # MUST      ## Step 4 # MUST
7846      $doc->append_child ($root);      $doc->append_child ($root);
7847    
7848      ## Step 6 # MUST      ## Step 5 # MUST
7849      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7850    
7851      undef $p->{head_element};      undef $p->{head_element};
7852    
7853      ## Step 7 # MUST      ## Step 6 # MUST
7854      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7855    
7856      ## Step 8 # MUST      ## Step 7 # MUST
7857      my $anode = $node;      my $anode = $node;
7858      AN: while (defined $anode) {      AN: while (defined $anode) {
7859        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7860          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7861          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7862            if ($anode->manakai_local_name eq 'form') {            if ($anode->manakai_local_name eq 'form') {
7863                !!!cp ('i5');
7864              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7865              last AN;              last AN;
7866            }            }
# Line 5596  sub set_inner_html ($$$) { Line 7869  sub set_inner_html ($$$) {
7869        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7870      } # AN      } # AN
7871            
7872      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7873      {      {
7874        my $self = $p;        my $self = $p;
7875        !!!next-token;        !!!next-token;
7876      }      }
7877      $p->_tree_construction_main;      $p->_tree_construction_main;
7878    
7879      ## Step 11 # MUST      ## Step 10 # MUST
7880      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7881      for (@cn) {      for (@cn) {
7882        $node->remove_child ($_);        $node->remove_child ($_);
7883      }      }
7884      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7885    
7886      ## Step 12 # MUST      ## Step 11 # MUST
7887      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7888      for (@cn) {      for (@cn) {
7889        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5620  sub set_inner_html ($$$) { Line 7892  sub set_inner_html ($$$) {
7892      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7893    
7894      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7895    
7896        delete $p->{parse_error}; # delete loop
7897    } else {    } else {
7898      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";
7899    }    }

Legend:
Removed from v.1.72  
changed lines
  Added in v.1.172

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24