/[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.65 by wakaba, Mon Nov 19 12:18:26 2007 UTC revision 1.190 by wakaba, Sun Sep 21 05:08:16 2008 UTC
# Line 3  use strict; Line 3  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4  use Error qw(:try);  use Error qw(:try);
5    
6    ## NOTE: This module don't check all HTML5 parse errors; character
7    ## encoding related parse errors are expected to be handled by relevant
8    ## modules.
9    ## Parse errors for control characters that are not allowed in HTML5
10    ## documents, for surrogate code points, and for noncharacter code
11    ## points, as well as U+FFFD substitions for characters whose code points
12    ## is higher than U+10FFFF may be detected by combining the parser with
13    ## the checker implemented by Whatpm::Charset::UnicodeChecker (for its
14    ## usage example, see |t/HTML-tree.t| in the Whatpm package or the
15    ## WebHACC::Language::HTML module in the WebHACC package).
16    
17  ## ISSUE:  ## ISSUE:
18  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
19  ## doc.write ('');  ## doc.write ('');
20  ## alert (doc.compatMode);  ## alert (doc.compatMode);
21    
22  ## ISSUE: HTML5 revision 967 says that the encoding layer MUST NOT  require IO::Handle;
23  ## strip BOM and the HTML layer MUST ignore it.  Whether we can do it  
24  ## is not yet clear.  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
25  ## "{U+FEFF}..." in UTF-16BE/UTF-16LE is three or four characters?  my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
26  ## "{U+FEFF}..." in GB18030?  my $SVG_NS = q<http://www.w3.org/2000/svg>;
27    my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
28  my $permitted_slash_tag_name = {  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
29    base => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
30    link => 1,  
31    meta => 1,  sub A_EL () { 0b1 }
32    hr => 1,  sub ADDRESS_EL () { 0b10 }
33    br => 1,  sub BODY_EL () { 0b100 }
34    img=> 1,  sub BUTTON_EL () { 0b1000 }
35    embed => 1,  sub CAPTION_EL () { 0b10000 }
36    param => 1,  sub DD_EL () { 0b100000 }
37    area => 1,  sub DIV_EL () { 0b1000000 }
38    col => 1,  sub DT_EL () { 0b10000000 }
39    input => 1,  sub FORM_EL () { 0b100000000 }
40    sub FORMATTING_EL () { 0b1000000000 }
41    sub FRAMESET_EL () { 0b10000000000 }
42    sub HEADING_EL () { 0b100000000000 }
43    sub HTML_EL () { 0b1000000000000 }
44    sub LI_EL () { 0b10000000000000 }
45    sub NOBR_EL () { 0b100000000000000 }
46    sub OPTION_EL () { 0b1000000000000000 }
47    sub OPTGROUP_EL () { 0b10000000000000000 }
48    sub P_EL () { 0b100000000000000000 }
49    sub SELECT_EL () { 0b1000000000000000000 }
50    sub TABLE_EL () { 0b10000000000000000000 }
51    sub TABLE_CELL_EL () { 0b100000000000000000000 }
52    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
53    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
54    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
55    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
56    sub FOREIGN_EL () { 0b10000000000000000000000000 }
57    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
58    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
59    sub RUBY_EL () { 0b10000000000000000000000000000 }
60    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
61    
62    sub TABLE_ROWS_EL () {
63      TABLE_EL |
64      TABLE_ROW_EL |
65      TABLE_ROW_GROUP_EL
66    }
67    
68    ## NOTE: Used in "generate implied end tags" algorithm.
69    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
70    ## is used in "generate implied end tags" implementation (search for the
71    ## function mae).
72    sub END_TAG_OPTIONAL_EL () {
73      DD_EL |
74      DT_EL |
75      LI_EL |
76      P_EL |
77      RUBY_COMPONENT_EL
78    }
79    
80    ## NOTE: Used in </body> and EOF algorithms.
81    sub ALL_END_TAG_OPTIONAL_EL () {
82      DD_EL |
83      DT_EL |
84      LI_EL |
85      P_EL |
86    
87      BODY_EL |
88      HTML_EL |
89      TABLE_CELL_EL |
90      TABLE_ROW_EL |
91      TABLE_ROW_GROUP_EL
92    }
93    
94    sub SCOPING_EL () {
95      BUTTON_EL |
96      CAPTION_EL |
97      HTML_EL |
98      TABLE_EL |
99      TABLE_CELL_EL |
100      MISC_SCOPING_EL
101    }
102    
103    sub TABLE_SCOPING_EL () {
104      HTML_EL |
105      TABLE_EL
106    }
107    
108    sub TABLE_ROWS_SCOPING_EL () {
109      HTML_EL |
110      TABLE_ROW_GROUP_EL
111    }
112    
113    sub TABLE_ROW_SCOPING_EL () {
114      HTML_EL |
115      TABLE_ROW_EL
116    }
117    
118    sub SPECIAL_EL () {
119      ADDRESS_EL |
120      BODY_EL |
121      DIV_EL |
122    
123      DD_EL |
124      DT_EL |
125      LI_EL |
126      P_EL |
127    
128      FORM_EL |
129      FRAMESET_EL |
130      HEADING_EL |
131      OPTION_EL |
132      OPTGROUP_EL |
133      SELECT_EL |
134      TABLE_ROW_EL |
135      TABLE_ROW_GROUP_EL |
136      MISC_SPECIAL_EL
137    }
138    
139    my $el_category = {
140      a => A_EL | FORMATTING_EL,
141      address => ADDRESS_EL,
142      applet => MISC_SCOPING_EL,
143      area => MISC_SPECIAL_EL,
144      b => FORMATTING_EL,
145      base => MISC_SPECIAL_EL,
146      basefont => MISC_SPECIAL_EL,
147      bgsound => MISC_SPECIAL_EL,
148      big => FORMATTING_EL,
149      blockquote => MISC_SPECIAL_EL,
150      body => BODY_EL,
151      br => MISC_SPECIAL_EL,
152      button => BUTTON_EL,
153      caption => CAPTION_EL,
154      center => MISC_SPECIAL_EL,
155      col => MISC_SPECIAL_EL,
156      colgroup => MISC_SPECIAL_EL,
157      dd => DD_EL,
158      dir => MISC_SPECIAL_EL,
159      div => DIV_EL,
160      dl => MISC_SPECIAL_EL,
161      dt => DT_EL,
162      em => FORMATTING_EL,
163      embed => MISC_SPECIAL_EL,
164      fieldset => MISC_SPECIAL_EL,
165      font => FORMATTING_EL,
166      form => FORM_EL,
167      frame => MISC_SPECIAL_EL,
168      frameset => FRAMESET_EL,
169      h1 => HEADING_EL,
170      h2 => HEADING_EL,
171      h3 => HEADING_EL,
172      h4 => HEADING_EL,
173      h5 => HEADING_EL,
174      h6 => HEADING_EL,
175      head => MISC_SPECIAL_EL,
176      hr => MISC_SPECIAL_EL,
177      html => HTML_EL,
178      i => FORMATTING_EL,
179      iframe => MISC_SPECIAL_EL,
180      img => MISC_SPECIAL_EL,
181      input => MISC_SPECIAL_EL,
182      isindex => MISC_SPECIAL_EL,
183      li => LI_EL,
184      link => MISC_SPECIAL_EL,
185      listing => MISC_SPECIAL_EL,
186      marquee => MISC_SCOPING_EL,
187      menu => MISC_SPECIAL_EL,
188      meta => MISC_SPECIAL_EL,
189      nobr => NOBR_EL | FORMATTING_EL,
190      noembed => MISC_SPECIAL_EL,
191      noframes => MISC_SPECIAL_EL,
192      noscript => MISC_SPECIAL_EL,
193      object => MISC_SCOPING_EL,
194      ol => MISC_SPECIAL_EL,
195      optgroup => OPTGROUP_EL,
196      option => OPTION_EL,
197      p => P_EL,
198      param => MISC_SPECIAL_EL,
199      plaintext => MISC_SPECIAL_EL,
200      pre => MISC_SPECIAL_EL,
201      rp => RUBY_COMPONENT_EL,
202      rt => RUBY_COMPONENT_EL,
203      ruby => RUBY_EL,
204      s => FORMATTING_EL,
205      script => MISC_SPECIAL_EL,
206      select => SELECT_EL,
207      small => FORMATTING_EL,
208      spacer => MISC_SPECIAL_EL,
209      strike => FORMATTING_EL,
210      strong => FORMATTING_EL,
211      style => MISC_SPECIAL_EL,
212      table => TABLE_EL,
213      tbody => TABLE_ROW_GROUP_EL,
214      td => TABLE_CELL_EL,
215      textarea => MISC_SPECIAL_EL,
216      tfoot => TABLE_ROW_GROUP_EL,
217      th => TABLE_CELL_EL,
218      thead => TABLE_ROW_GROUP_EL,
219      title => MISC_SPECIAL_EL,
220      tr => TABLE_ROW_EL,
221      tt => FORMATTING_EL,
222      u => FORMATTING_EL,
223      ul => MISC_SPECIAL_EL,
224      wbr => MISC_SPECIAL_EL,
225    };
226    
227    my $el_category_f = {
228      $MML_NS => {
229        'annotation-xml' => MML_AXML_EL,
230        mi => FOREIGN_FLOW_CONTENT_EL,
231        mo => FOREIGN_FLOW_CONTENT_EL,
232        mn => FOREIGN_FLOW_CONTENT_EL,
233        ms => FOREIGN_FLOW_CONTENT_EL,
234        mtext => FOREIGN_FLOW_CONTENT_EL,
235      },
236      $SVG_NS => {
237        foreignObject => FOREIGN_FLOW_CONTENT_EL,
238        desc => FOREIGN_FLOW_CONTENT_EL,
239        title => FOREIGN_FLOW_CONTENT_EL,
240      },
241      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
242    };
243    
244    my $svg_attr_name = {
245      attributename => 'attributeName',
246      attributetype => 'attributeType',
247      basefrequency => 'baseFrequency',
248      baseprofile => 'baseProfile',
249      calcmode => 'calcMode',
250      clippathunits => 'clipPathUnits',
251      contentscripttype => 'contentScriptType',
252      contentstyletype => 'contentStyleType',
253      diffuseconstant => 'diffuseConstant',
254      edgemode => 'edgeMode',
255      externalresourcesrequired => 'externalResourcesRequired',
256      filterres => 'filterRes',
257      filterunits => 'filterUnits',
258      glyphref => 'glyphRef',
259      gradienttransform => 'gradientTransform',
260      gradientunits => 'gradientUnits',
261      kernelmatrix => 'kernelMatrix',
262      kernelunitlength => 'kernelUnitLength',
263      keypoints => 'keyPoints',
264      keysplines => 'keySplines',
265      keytimes => 'keyTimes',
266      lengthadjust => 'lengthAdjust',
267      limitingconeangle => 'limitingConeAngle',
268      markerheight => 'markerHeight',
269      markerunits => 'markerUnits',
270      markerwidth => 'markerWidth',
271      maskcontentunits => 'maskContentUnits',
272      maskunits => 'maskUnits',
273      numoctaves => 'numOctaves',
274      pathlength => 'pathLength',
275      patterncontentunits => 'patternContentUnits',
276      patterntransform => 'patternTransform',
277      patternunits => 'patternUnits',
278      pointsatx => 'pointsAtX',
279      pointsaty => 'pointsAtY',
280      pointsatz => 'pointsAtZ',
281      preservealpha => 'preserveAlpha',
282      preserveaspectratio => 'preserveAspectRatio',
283      primitiveunits => 'primitiveUnits',
284      refx => 'refX',
285      refy => 'refY',
286      repeatcount => 'repeatCount',
287      repeatdur => 'repeatDur',
288      requiredextensions => 'requiredExtensions',
289      requiredfeatures => 'requiredFeatures',
290      specularconstant => 'specularConstant',
291      specularexponent => 'specularExponent',
292      spreadmethod => 'spreadMethod',
293      startoffset => 'startOffset',
294      stddeviation => 'stdDeviation',
295      stitchtiles => 'stitchTiles',
296      surfacescale => 'surfaceScale',
297      systemlanguage => 'systemLanguage',
298      tablevalues => 'tableValues',
299      targetx => 'targetX',
300      targety => 'targetY',
301      textlength => 'textLength',
302      viewbox => 'viewBox',
303      viewtarget => 'viewTarget',
304      xchannelselector => 'xChannelSelector',
305      ychannelselector => 'yChannelSelector',
306      zoomandpan => 'zoomAndPan',
307  };  };
308    
309    my $foreign_attr_xname = {
310      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
311      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
312      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
313      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
314      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
315      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
316      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
317      'xml:base' => [$XML_NS, ['xml', 'base']],
318      'xml:lang' => [$XML_NS, ['xml', 'lang']],
319      'xml:space' => [$XML_NS, ['xml', 'space']],
320      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
321      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
322    };
323    
324    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
325    
326  my $c1_entity_char = {  my $c1_entity_char = {
327    0x80 => 0x20AC,    0x80 => 0x20AC,
328    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 63  my $c1_entity_char = { Line 358  my $c1_entity_char = {
358    0x9F => 0x0178,    0x9F => 0x0178,
359  }; # $c1_entity_char  }; # $c1_entity_char
360    
 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  
   
361  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
362      my $self = shift;
363      my $charset_name = shift;
364      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
365      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
366    } # parse_byte_string
367    
368    sub parse_byte_stream ($$$$;$$) {
369      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
370    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
371    my $charset = shift;    my $charset_name = shift;
372    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;  
   }  
373    
374    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
375      my $self = shift;      my (%opt) = @_;
376      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
377      ## TODO: if $charset is supported    };
378      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
379    
380      ## "Change the encoding" algorithm:    my $get_wrapper = $_[3] || sub ($) {
381        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
382      ## Step 1        };
383      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
384        $charset = 'utf-8';    ## HTML5 encoding sniffing algorithm
385      require Message::Charset::Info;
386      my $charset;
387      my $buffer;
388      my ($char_stream, $e_status);
389    
390      SNIFFING: {
391        ## NOTE: By setting |allow_fallback| option true when the
392        ## |get_decode_handle| method is invoked, we ignore what the HTML5
393        ## spec requires, i.e. unsupported encoding should be ignored.
394          ## TODO: We should not do this unless the parser is invoked
395          ## in the conformance checking mode, in which this behavior
396          ## would be useful.
397    
398        ## Step 1
399        if (defined $charset_name) {
400          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
401              ## TODO: Is this ok?  Transfer protocol's parameter should be
402              ## interpreted in its semantics?
403    
404          ($char_stream, $e_status) = $charset->get_decode_handle
405              ($byte_stream, allow_error_reporting => 1,
406               allow_fallback => 1);
407          if ($char_stream) {
408            $self->{confident} = 1;
409            last SNIFFING;
410          } else {
411            !!!parse-error (type => 'charset:not supported',
412                            layer => 'encode',
413                            line => 1, column => 1,
414                            value => $charset_name,
415                            level => $self->{level}->{uncertain});
416          }
417      }      }
418    
419      ## Step 2      ## Step 2
420      if (defined $self->{input_encoding} and      my $byte_buffer = '';
421          $self->{input_encoding} eq $charset) {      for (1..1024) {
422          my $char = $byte_stream->getc;
423          last unless defined $char;
424          $byte_buffer .= $char;
425        } ## TODO: timeout
426    
427        ## Step 3
428        if ($byte_buffer =~ /^\xFE\xFF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
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;        $self->{confident} = 1;
434        return;        last SNIFFING;
435        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
436          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
437          ($char_stream, $e_status) = $charset->get_decode_handle
438              ($byte_stream, allow_error_reporting => 1,
439               allow_fallback => 1, byte_buffer => \$byte_buffer);
440          $self->{confident} = 1;
441          last SNIFFING;
442        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
443          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
444          ($char_stream, $e_status) = $charset->get_decode_handle
445              ($byte_stream, allow_error_reporting => 1,
446               allow_fallback => 1, byte_buffer => \$byte_buffer);
447          $self->{confident} = 1;
448          last SNIFFING;
449      }      }
450    
451      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
452          ':'.$charset, level => 'w');      ## TODO: <meta charset>
453    
454      ## Step 3      ## Step 5
455      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
456    
457      ## Step 4      ## Step 6
458      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
459        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
460            ($byte_buffer);
461        if (defined $charset_name) {
462          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
463    
464          ## ISSUE: Unsupported encoding is not ignored according to the spec.
465          require Whatpm::Charset::DecodeHandle;
466          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
467              ($byte_stream);
468          ($char_stream, $e_status) = $charset->get_decode_handle
469              ($buffer, allow_error_reporting => 1,
470               allow_fallback => 1, byte_buffer => \$byte_buffer);
471          if ($char_stream) {
472            $buffer->{buffer} = $byte_buffer;
473            !!!parse-error (type => 'sniffing:chardet',
474                            text => $charset_name,
475                            level => $self->{level}->{info},
476                            layer => 'encode',
477                            line => 1, column => 1);
478            $self->{confident} = 0;
479            last SNIFFING;
480          }
481        }
482    
483        ## Step 7: default
484        ## TODO: Make this configurable.
485        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
486            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
487            ## detectable in the step 6.
488        require Whatpm::Charset::DecodeHandle;
489        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
490            ($byte_stream);
491        ($char_stream, $e_status)
492            = $charset->get_decode_handle ($buffer,
493                                           allow_error_reporting => 1,
494                                           allow_fallback => 1,
495                                           byte_buffer => \$byte_buffer);
496        $buffer->{buffer} = $byte_buffer;
497        !!!parse-error (type => 'sniffing:default',
498                        text => 'windows-1252',
499                        level => $self->{level}->{info},
500                        line => 1, column => 1,
501                        layer => 'encode');
502        $self->{confident} = 0;
503      } # SNIFFING
504    
505      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
506        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
507        !!!parse-error (type => 'chardecode:fallback',
508                        #text => $self->{input_encoding},
509                        level => $self->{level}->{uncertain},
510                        line => 1, column => 1,
511                        layer => 'encode');
512      } elsif (not ($e_status &
513                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
514        $self->{input_encoding} = $charset->get_iana_name;
515        !!!parse-error (type => 'chardecode:no error',
516                        text => $self->{input_encoding},
517                        level => $self->{level}->{uncertain},
518                        line => 1, column => 1,
519                        layer => 'encode');
520      } else {
521        $self->{input_encoding} = $charset->get_iana_name;
522      }
523    
524      $self->{change_encoding} = sub {
525        my $self = shift;
526        $charset_name = shift;
527        my $token = shift;
528    
529        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
530        ($char_stream, $e_status) = $charset->get_decode_handle
531            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
532             byte_buffer => \ $buffer->{buffer});
533        
534        if ($char_stream) { # if supported
535          ## "Change the encoding" algorithm:
536    
537          ## Step 1    
538          if ($charset->{category} &
539              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
540            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
541            ($char_stream, $e_status) = $charset->get_decode_handle
542                ($byte_stream,
543                 byte_buffer => \ $buffer->{buffer});
544          }
545          $charset_name = $charset->get_iana_name;
546          
547          ## Step 2
548          if (defined $self->{input_encoding} and
549              $self->{input_encoding} eq $charset_name) {
550            !!!parse-error (type => 'charset label:matching',
551                            text => $charset_name,
552                            level => $self->{level}->{info});
553            $self->{confident} = 1;
554            return;
555          }
556    
557          !!!parse-error (type => 'charset label detected',
558                          text => $self->{input_encoding},
559                          value => $charset_name,
560                          level => $self->{level}->{warn},
561                          token => $token);
562          
563          ## Step 3
564          # if (can) {
565            ## change the encoding on the fly.
566            #$self->{confident} = 1;
567            #return;
568          # }
569          
570          ## Step 4
571          throw Whatpm::HTML::RestartParser ();
572        }
573    }; # $self->{change_encoding}    }; # $self->{change_encoding}
574    
575    my @args = @_; shift @args; # $s    my $char_onerror = sub {
576        my (undef, $type, %opt) = @_;
577        !!!parse-error (layer => 'encode',
578                        line => $self->{line}, column => $self->{column} + 1,
579                        %opt, type => $type);
580        if ($opt{octets}) {
581          ${$opt{octets}} = "\x{FFFD}"; # relacement character
582        }
583      };
584    
585      my $wrapped_char_stream = $get_wrapper->($char_stream);
586      $wrapped_char_stream->onerror ($char_onerror);
587    
588      my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
589    my $return;    my $return;
590    try {    try {
591      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
592    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
593      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
594      $s = \ (Encode::decode ($charset, $$bytes_s));      
595      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
596          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
597          !!!parse-error (type => 'chardecode:fallback',
598                          level => $self->{level}->{uncertain},
599                          #text => $self->{input_encoding},
600                          line => 1, column => 1,
601                          layer => 'encode');
602        } elsif (not ($e_status &
603                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
604          $self->{input_encoding} = $charset->get_iana_name;
605          !!!parse-error (type => 'chardecode:no error',
606                          text => $self->{input_encoding},
607                          level => $self->{level}->{uncertain},
608                          line => 1, column => 1,
609                          layer => 'encode');
610        } else {
611          $self->{input_encoding} = $charset->get_iana_name;
612        }
613      $self->{confident} = 1;      $self->{confident} = 1;
614      $return = $self->parse_char_string ($s, @args);  
615        $wrapped_char_stream = $get_wrapper->($char_stream);
616        $wrapped_char_stream->onerror ($char_onerror);
617    
618        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
619    };    };
620    return $return;    return $return;
621  } # parse_byte_string  } # parse_byte_stream
622    
623  *parse_char_string = \&parse_string;  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
624    ## and the HTML layer MUST ignore it.  However, we does strip BOM in
625    ## the encoding layer and the HTML layer does not ignore any U+FEFF,
626    ## because the core part of our HTML parser expects a string of character,
627    ## not a string of bytes or code units or anything which might contain a BOM.
628    ## Therefore, any parser interface that accepts a string of bytes,
629    ## such as |parse_byte_string| in this module, must ensure that it does
630    ## strip the BOM and never strip any ZWNBSP.
631    
632  sub parse_string ($$$;$) {  sub parse_char_string ($$$;$$) {
633    my $self = ref $_[0] ? shift : shift->new;    #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
634      my $self = shift;
635    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $s = ref $_[0] ? $_[0] : \($_[0]);
636      require Whatpm::Charset::DecodeHandle;
637      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
638      return $self->parse_char_stream ($input, @_[1..$#_]);
639    } # parse_char_string
640    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
641    
642    sub parse_char_stream ($$$;$$) {
643      my $self = ref $_[0] ? shift : shift->new;
644      my $input = $_[0];
645    $self->{document} = $_[1];    $self->{document} = $_[1];
646    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
647    
# Line 168  sub parse_string ($$$;$) { Line 650  sub parse_string ($$$;$) {
650    $self->{confident} = 1 unless exists $self->{confident};    $self->{confident} = 1 unless exists $self->{confident};
651    $self->{document}->input_encoding ($self->{input_encoding})    $self->{document}->input_encoding ($self->{input_encoding})
652        if defined $self->{input_encoding};        if defined $self->{input_encoding};
653    ## TODO: |{input_encoding}| is needless?
654    
655    my $i = 0;    $self->{line_prev} = $self->{line} = 1;
656    my $line = 1;    $self->{column_prev} = -1;
657    my $column = 0;    $self->{column} = 0;
658    $self->{set_next_input_character} = sub {    $self->{set_nc} = sub {
659      my $self = shift;      my $self = shift;
660    
661      pop @{$self->{prev_input_character}};      my $char = '';
662      unshift @{$self->{prev_input_character}}, $self->{next_input_character};      if (defined $self->{next_nc}) {
663          $char = $self->{next_nc};
664          delete $self->{next_nc};
665          $self->{nc} = ord $char;
666        } else {
667          $self->{char_buffer} = '';
668          $self->{char_buffer_pos} = 0;
669    
670          my $count = $input->manakai_read_until
671             ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
672          if ($count) {
673            $self->{line_prev} = $self->{line};
674            $self->{column_prev} = $self->{column};
675            $self->{column}++;
676            $self->{nc}
677                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
678            return;
679          }
680    
681      $self->{next_input_character} = -1 and return if $i >= length $$s;        if ($input->read ($char, 1)) {
682      $self->{next_input_character} = ord substr $$s, $i++, 1;          $self->{nc} = ord $char;
683      $column++;        } else {
684            $self->{nc} = -1;
685            return;
686          }
687        }
688    
689        ($self->{line_prev}, $self->{column_prev})
690            = ($self->{line}, $self->{column});
691        $self->{column}++;
692            
693      if ($self->{next_input_character} == 0x000A) { # LF      if ($self->{nc} == 0x000A) { # LF
694        $line++;        !!!cp ('j1');
695        $column = 0;        $self->{line}++;
696      } elsif ($self->{next_input_character} == 0x000D) { # CR        $self->{column} = 0;
697        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{nc} == 0x000D) { # CR
698        $self->{next_input_character} = 0x000A; # LF # MUST        !!!cp ('j2');
699        $line++;  ## TODO: support for abort/streaming
700        $column = 0;        my $next = '';
701      } elsif ($self->{next_input_character} > 0x10FFFF) {        if ($input->read ($next, 1) and $next ne "\x0A") {
702        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_nc} = $next;
703      } elsif ($self->{next_input_character} == 0x0000) { # NULL        }
704          $self->{nc} = 0x000A; # LF # MUST
705          $self->{line}++;
706          $self->{column} = 0;
707        } elsif ($self->{nc} == 0x0000) { # NULL
708          !!!cp ('j4');
709        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
710        $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
711      }      }
712    };    };
713    $self->{prev_input_character} = [-1, -1, -1];  
714    $self->{next_input_character} = -1;    $self->{read_until} = sub {
715        #my ($scalar, $specials_range, $offset) = @_;
716        return 0 if defined $self->{next_nc};
717    
718        my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
719        my $offset = $_[2] || 0;
720    
721        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
722          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
723          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
724            substr ($_[0], $offset)
725                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
726            my $count = $+[0] - $-[0];
727            if ($count) {
728              $self->{column} += $count;
729              $self->{char_buffer_pos} += $count;
730              $self->{line_prev} = $self->{line};
731              $self->{column_prev} = $self->{column} - 1;
732              $self->{nc} = -1;
733            }
734            return $count;
735          } else {
736            return 0;
737          }
738        } else {
739          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
740          if ($count) {
741            $self->{column} += $count;
742            $self->{line_prev} = $self->{line};
743            $self->{column_prev} = $self->{column} - 1;
744            $self->{nc} = -1;
745          }
746          return $count;
747        }
748      }; # $self->{read_until}
749    
750    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
751      my (%opt) = @_;      my (%opt) = @_;
752      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
753        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
754        warn "Parse error ($opt{type}) at line $line column $column\n";
755    };    };
756    $self->{parse_error} = sub {    $self->{parse_error} = sub {
757      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
758    };    };
759    
760      my $char_onerror = sub {
761        my (undef, $type, %opt) = @_;
762        !!!parse-error (layer => 'encode',
763                        line => $self->{line}, column => $self->{column} + 1,
764                        %opt, type => $type);
765      }; # $char_onerror
766    
767      if ($_[3]) {
768        $input = $_[3]->($input);
769        $input->onerror ($char_onerror);
770      } else {
771        $input->onerror ($char_onerror) unless defined $input->onerror;
772      }
773    
774    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
775    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
776    $self->_construct_tree;    $self->_construct_tree;
777    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
778    
779      delete $self->{parse_error}; # remove loop
780    
781    return $self->{document};    return $self->{document};
782  } # parse_string  } # parse_char_stream
783    
784  sub new ($) {  sub new ($) {
785    my $class = shift;    my $class = shift;
786    my $self = bless {}, $class;    my $self = bless {
787    $self->{set_next_input_character} = sub {      level => {must => 'm',
788      $self->{next_input_character} = -1;                should => 's',
789                  warn => 'w',
790                  info => 'i',
791                  uncertain => 'u'},
792      }, $class;
793      $self->{set_nc} = sub {
794        $self->{nc} = -1;
795    };    };
796    $self->{parse_error} = sub {    $self->{parse_error} = sub {
797      #      #
# Line 247  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 818  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
818  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
819    
820  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
821  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
822  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
823  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
824  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 258  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 829  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
829  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
830  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
831  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
832  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
833  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
834  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
835  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 279  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO Line 850  sub DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUO
850  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE () { 30 }
851  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
852  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
853    sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
854    sub SELF_CLOSING_START_TAG_STATE () { 34 }
855    sub CDATA_SECTION_STATE () { 35 }
856    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
857    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
858    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
859    sub CDATA_RCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
860    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
861    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
862    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
863    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
864    ## NOTE: "Entity data state", "entity in attribute value state", and
865    ## "consume a character reference" algorithm are jointly implemented
866    ## using the following six states:
867    sub ENTITY_STATE () { 44 }
868    sub ENTITY_HASH_STATE () { 45 }
869    sub NCR_NUM_STATE () { 46 }
870    sub HEXREF_X_STATE () { 47 }
871    sub HEXREF_HEX_STATE () { 48 }
872    sub ENTITY_NAME_STATE () { 49 }
873    sub PCDATA_STATE () { 50 } # "data state" in the spec
874    
875  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
876  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 295  sub TABLE_IMS ()      { 0b1000000 } Line 887  sub TABLE_IMS ()      { 0b1000000 }
887  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
888  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
889  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
890    sub SELECT_IMS ()     { 0b10000000000 }
891    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
892        ## NOTE: "in foreign content" insertion mode is special; it is combined
893        ## with the secondary insertion mode.  In this parser, they are stored
894        ## together in the bit-or'ed form.
895    
896    ## NOTE: "initial" and "before html" insertion modes have no constants.
897    
898    ## NOTE: "after after body" insertion mode.
899  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
900    
901    ## NOTE: "after after frameset" insertion mode.
902  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
903    
904  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
905  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
906  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 311  sub IN_TABLE_IM () { TABLE_IMS } Line 914  sub IN_TABLE_IM () { TABLE_IMS }
914  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
915  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
916  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
917  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
918    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
919  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
920    
921  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 319  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 923  sub IN_COLUMN_GROUP_IM () { 0b10 }
923  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
924    my $self = shift;    my $self = shift;
925    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
926      #$self->{s_kwd}; # state keyword - initialized when used
927      #$self->{entity__value}; # initialized when used
928      #$self->{entity__match}; # initialized when used
929    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
930    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{ct}; # current token
931    undef $self->{current_attribute};    undef $self->{ca}; # current attribute
932    undef $self->{last_emitted_start_tag_name};    undef $self->{last_stag_name}; # last emitted start tag name
933    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
934    $self->{char} = [];    delete $self->{self_closing};
935    # $self->{next_input_character}    $self->{char_buffer} = '';
936      $self->{char_buffer_pos} = 0;
937      $self->{nc} = -1; # next input character
938      #$self->{next_nc}
939    !!!next-input-character;    !!!next-input-character;
940    $self->{token} = [];    $self->{token} = [];
941    # $self->{escape}    # $self->{escape}
# Line 336  sub _initialize_tokenizer ($) { Line 946  sub _initialize_tokenizer ($) {
946  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
947  ##   ->{name} (DOCTYPE_TOKEN)  ##   ->{name} (DOCTYPE_TOKEN)
948  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
949  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{pubid} (DOCTYPE_TOKEN)
950  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{sysid} (DOCTYPE_TOKEN)
951  ##   ->{correct} == 1 or 0 (DOCTYPE_TOKEN)  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
952  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
953    ##        ->{name}
954    ##        ->{value}
955    ##        ->{has_reference} == 1 or 0
956  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
957    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
958    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
959    ##     while the token is pushed back to the stack.
960    
961  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
962    
# Line 350  sub _initialize_tokenizer ($) { Line 966  sub _initialize_tokenizer ($) {
966  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
967  ## and removed from the list.  ## and removed from the list.
968    
969  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
970  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
971  ## contains some requirements that are not detected by the  
972  ## parsing algorithm:  my $is_space = {
973  ## - Some requirements on character encoding declarations. ## TODO    0x0009 => 1, # CHARACTER TABULATION (HT)
974  ## - "Elements MUST NOT contain content that their content model disallows."    0x000A => 1, # LINE FEED (LF)
975  ##   ... Some are parse error, some are not (will be reported by c.c.).    #0x000B => 0, # LINE TABULATION (VT)
976  ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO    0x000C => 1, # FORM FEED (FF)
977  ## - Text (in elements, attributes, and comments) SHOULD NOT contain    #0x000D => 1, # CARRIAGE RETURN (CR)
978  ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)    0x0020 => 1, # SPACE (SP)
979    };
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
980    
981  sub _get_next_token ($) {  sub _get_next_token ($) {
982    my $self = shift;    my $self = shift;
983    
984      if ($self->{self_closing}) {
985        !!!parse-error (type => 'nestc', token => $self->{ct});
986        ## NOTE: The |self_closing| flag is only set by start tag token.
987        ## In addition, when a start tag token is emitted, it is always set to
988        ## |ct|.
989        delete $self->{self_closing};
990      }
991    
992    if (@{$self->{token}}) {    if (@{$self->{token}}) {
993        $self->{self_closing} = $self->{token}->[0]->{self_closing};
994      return shift @{$self->{token}};      return shift @{$self->{token}};
995    }    }
996    
997    A: {    A: {
998      if ($self->{state} == DATA_STATE) {      if ($self->{state} == PCDATA_STATE) {
999        if ($self->{next_input_character} == 0x0026) { # &        ## NOTE: Same as |DATA_STATE|, but only for |PCDATA| content model.
1000          if ($self->{content_model} & CM_ENTITY) { # PCDATA | RCDATA  
1001            $self->{state} = ENTITY_DATA_STATE;        if ($self->{nc} == 0x0026) { # &
1002            !!!cp (0.1);
1003            ## NOTE: In the spec, the tokenizer is switched to the
1004            ## "entity data state".  In this implementation, the tokenizer
1005            ## is switched to the |ENTITY_STATE|, which is an implementation
1006            ## of the "consume a character reference" algorithm.
1007            $self->{entity_add} = -1;
1008            $self->{prev_state} = DATA_STATE;
1009            $self->{state} = ENTITY_STATE;
1010            !!!next-input-character;
1011            redo A;
1012          } elsif ($self->{nc} == 0x003C) { # <
1013            !!!cp (0.2);
1014            $self->{state} = TAG_OPEN_STATE;
1015            !!!next-input-character;
1016            redo A;
1017          } elsif ($self->{nc} == -1) {
1018            !!!cp (0.3);
1019            !!!emit ({type => END_OF_FILE_TOKEN,
1020                      line => $self->{line}, column => $self->{column}});
1021            last A; ## TODO: ok?
1022          } else {
1023            !!!cp (0.4);
1024            #
1025          }
1026    
1027          # Anything else
1028          my $token = {type => CHARACTER_TOKEN,
1029                       data => chr $self->{nc},
1030                       line => $self->{line}, column => $self->{column},
1031                      };
1032          $self->{read_until}->($token->{data}, q[<&], length $token->{data});
1033    
1034          ## Stay in the state.
1035          !!!next-input-character;
1036          !!!emit ($token);
1037          redo A;
1038        } elsif ($self->{state} == DATA_STATE) {
1039          $self->{s_kwd} = '' unless defined $self->{s_kwd};
1040          if ($self->{nc} == 0x0026) { # &
1041            $self->{s_kwd} = '';
1042            if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
1043                not $self->{escape}) {
1044              !!!cp (1);
1045              ## NOTE: In the spec, the tokenizer is switched to the
1046              ## "entity data state".  In this implementation, the tokenizer
1047              ## is switched to the |ENTITY_STATE|, which is an implementation
1048              ## of the "consume a character reference" algorithm.
1049              $self->{entity_add} = -1;
1050              $self->{prev_state} = DATA_STATE;
1051              $self->{state} = ENTITY_STATE;
1052            !!!next-input-character;            !!!next-input-character;
1053            redo A;            redo A;
1054          } else {          } else {
1055              !!!cp (2);
1056            #            #
1057          }          }
1058        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
1059          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1060            unless ($self->{escape}) {            $self->{s_kwd} .= '-';
1061              if ($self->{prev_input_character}->[0] == 0x002D and # -            
1062                  $self->{prev_input_character}->[1] == 0x0021 and # !            if ($self->{s_kwd} eq '<!--') {
1063                  $self->{prev_input_character}->[2] == 0x003C) { # <              !!!cp (3);
1064                $self->{escape} = 1;              $self->{escape} = 1; # unless $self->{escape};
1065              }              $self->{s_kwd} = '--';
1066                #
1067              } elsif ($self->{s_kwd} eq '---') {
1068                !!!cp (4);
1069                $self->{s_kwd} = '--';
1070                #
1071              } else {
1072                !!!cp (5);
1073                #
1074            }            }
1075          }          }
1076                    
1077          #          #
1078        } elsif ($self->{next_input_character} == 0x003C) { # <        } elsif ($self->{nc} == 0x0021) { # !
1079            if (length $self->{s_kwd}) {
1080              !!!cp (5.1);
1081              $self->{s_kwd} .= '!';
1082              #
1083            } else {
1084              !!!cp (5.2);
1085              #$self->{s_kwd} = '';
1086              #
1087            }
1088            #
1089          } elsif ($self->{nc} == 0x003C) { # <
1090          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1091              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1092               not $self->{escape})) {               not $self->{escape})) {
1093              !!!cp (6);
1094            $self->{state} = TAG_OPEN_STATE;            $self->{state} = TAG_OPEN_STATE;
1095            !!!next-input-character;            !!!next-input-character;
1096            redo A;            redo A;
1097          } else {          } else {
1098              !!!cp (7);
1099              $self->{s_kwd} = '';
1100            #            #
1101          }          }
1102        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1103          if ($self->{escape} and          if ($self->{escape} and
1104              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1105            if ($self->{prev_input_character}->[0] == 0x002D and # -            if ($self->{s_kwd} eq '--') {
1106                $self->{prev_input_character}->[1] == 0x002D) { # -              !!!cp (8);
1107              delete $self->{escape};              delete $self->{escape};
1108              } else {
1109                !!!cp (9);
1110            }            }
1111            } else {
1112              !!!cp (10);
1113          }          }
1114                    
1115            $self->{s_kwd} = '';
1116          #          #
1117        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1118          !!!emit ({type => END_OF_FILE_TOKEN});          !!!cp (11);
1119            $self->{s_kwd} = '';
1120            !!!emit ({type => END_OF_FILE_TOKEN,
1121                      line => $self->{line}, column => $self->{column}});
1122          last A; ## TODO: ok?          last A; ## TODO: ok?
1123          } else {
1124            !!!cp (12);
1125            $self->{s_kwd} = '';
1126            #
1127        }        }
1128    
1129        # Anything else        # Anything else
1130        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1131                     data => chr $self->{next_input_character}};                     data => chr $self->{nc},
1132        ## Stay in the data state                     line => $self->{line}, column => $self->{column},
1133        !!!next-input-character;                    };
1134          if ($self->{read_until}->($token->{data}, q[-!<>&],
1135        !!!emit ($token);                                  length $token->{data})) {
1136            $self->{s_kwd} = '';
1137        redo A;        }
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
1138    
1139        unless (defined $token) {        ## Stay in the data state.
1140          !!!emit ({type => CHARACTER_TOKEN, data => '&'});        if ($self->{content_model} == PCDATA_CONTENT_MODEL) {
1141            !!!cp (13);
1142            $self->{state} = PCDATA_STATE;
1143        } else {        } else {
1144          !!!emit ($token);          !!!cp (14);
1145            ## Stay in the state.
1146        }        }
1147          !!!next-input-character;
1148          !!!emit ($token);
1149        redo A;        redo A;
1150      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1151        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1152          if ($self->{next_input_character} == 0x002F) { # /          if ($self->{nc} == 0x002F) { # /
1153              !!!cp (15);
1154            !!!next-input-character;            !!!next-input-character;
1155            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1156            redo A;            redo A;
1157            } elsif ($self->{nc} == 0x0021) { # !
1158              !!!cp (15.1);
1159              $self->{s_kwd} = '<' unless $self->{escape};
1160              #
1161          } else {          } else {
1162            ## reconsume            !!!cp (16);
1163            $self->{state} = DATA_STATE;            #
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<'});  
   
           redo A;  
1164          }          }
1165    
1166            ## reconsume
1167            $self->{state} = DATA_STATE;
1168            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1169                      line => $self->{line_prev},
1170                      column => $self->{column_prev},
1171                     });
1172            redo A;
1173        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1174          if ($self->{next_input_character} == 0x0021) { # !          if ($self->{nc} == 0x0021) { # !
1175              !!!cp (17);
1176            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1177            !!!next-input-character;            !!!next-input-character;
1178            redo A;            redo A;
1179          } elsif ($self->{next_input_character} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1180              !!!cp (18);
1181            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1182            !!!next-input-character;            !!!next-input-character;
1183            redo A;            redo A;
1184          } elsif (0x0041 <= $self->{next_input_character} and          } elsif (0x0041 <= $self->{nc} and
1185                   $self->{next_input_character} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1186            $self->{current_token}            !!!cp (19);
1187              $self->{ct}
1188              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1189                 tag_name => chr ($self->{next_input_character} + 0x0020)};                 tag_name => chr ($self->{nc} + 0x0020),
1190                   line => $self->{line_prev},
1191                   column => $self->{column_prev}};
1192            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1193            !!!next-input-character;            !!!next-input-character;
1194            redo A;            redo A;
1195          } elsif (0x0061 <= $self->{next_input_character} and          } elsif (0x0061 <= $self->{nc} and
1196                   $self->{next_input_character} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1197            $self->{current_token} = {type => START_TAG_TOKEN,            !!!cp (20);
1198                              tag_name => chr ($self->{next_input_character})};            $self->{ct} = {type => START_TAG_TOKEN,
1199                                        tag_name => chr ($self->{nc}),
1200                                        line => $self->{line_prev},
1201                                        column => $self->{column_prev}};
1202            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1203            !!!next-input-character;            !!!next-input-character;
1204            redo A;            redo A;
1205          } elsif ($self->{next_input_character} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1206            !!!parse-error (type => 'empty start tag');            !!!cp (21);
1207              !!!parse-error (type => 'empty start tag',
1208                              line => $self->{line_prev},
1209                              column => $self->{column_prev});
1210            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1211            !!!next-input-character;            !!!next-input-character;
1212    
1213            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1214                        line => $self->{line_prev},
1215                        column => $self->{column_prev},
1216                       });
1217    
1218            redo A;            redo A;
1219          } elsif ($self->{next_input_character} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1220            !!!parse-error (type => 'pio');            !!!cp (22);
1221              !!!parse-error (type => 'pio',
1222                              line => $self->{line_prev},
1223                              column => $self->{column_prev});
1224            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1225            ## $self->{next_input_character} is intentionally left as is            $self->{ct} = {type => COMMENT_TOKEN, data => '',
1226                                        line => $self->{line_prev},
1227                                        column => $self->{column_prev},
1228                                       };
1229              ## $self->{nc} is intentionally left as is
1230            redo A;            redo A;
1231          } else {          } else {
1232            !!!parse-error (type => 'bare stago');            !!!cp (23);
1233              !!!parse-error (type => 'bare stago',
1234                              line => $self->{line_prev},
1235                              column => $self->{column_prev});
1236            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1237            ## reconsume            ## reconsume
1238    
1239            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1240                        line => $self->{line_prev},
1241                        column => $self->{column_prev},
1242                       });
1243    
1244            redo A;            redo A;
1245          }          }
# Line 505  sub _get_next_token ($) { Line 1247  sub _get_next_token ($) {
1247          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1248        }        }
1249      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1250        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        ## NOTE: The "close tag open state" in the spec is implemented as
1251          if (defined $self->{last_emitted_start_tag_name}) {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_RCDATA_CLOSE_TAG_STATE|.
           ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>  
           my @next_char;  
           TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
             push @next_char, $self->{next_input_character};  
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_input_character} == $c or $self->{next_input_character} == $C) {  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               $self->{next_input_character} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
1252    
1253                !!!emit ({type => CHARACTER_TOKEN, data => '</'});        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1254            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1255                redo A;          if (defined $self->{last_stag_name}) {
1256              }            $self->{state} = CDATA_RCDATA_CLOSE_TAG_STATE;
1257            }            $self->{s_kwd} = '';
1258            push @next_char, $self->{next_input_character};            ## Reconsume.
1259                    redo A;
           unless ($self->{next_input_character} == 0x0009 or # HT  
                   $self->{next_input_character} == 0x000A or # LF  
                   $self->{next_input_character} == 0x000B or # VT  
                   $self->{next_input_character} == 0x000C or # FF  
                   $self->{next_input_character} == 0x0020 or # SP  
                   $self->{next_input_character} == 0x003E or # >  
                   $self->{next_input_character} == 0x002F or # /  
                   $self->{next_input_character} == -1) {  
             $self->{next_input_character} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
             redo A;  
           } else {  
             $self->{next_input_character} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1260          } else {          } else {
1261            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1262            # next-input-character is already done            ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1263              !!!cp (28);
1264            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1265            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1266              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1267                        line => $l, column => $c,
1268                       });
1269            redo A;            redo A;
1270          }          }
1271        }        }
1272          
1273        if (0x0041 <= $self->{next_input_character} and        if (0x0041 <= $self->{nc} and
1274            $self->{next_input_character} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1275          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (29);
1276                            tag_name => chr ($self->{next_input_character} + 0x0020)};          $self->{ct}
1277                = {type => END_TAG_TOKEN,
1278                   tag_name => chr ($self->{nc} + 0x0020),
1279                   line => $l, column => $c};
1280          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1281          !!!next-input-character;          !!!next-input-character;
1282          redo A;          redo A;
1283        } elsif (0x0061 <= $self->{next_input_character} and        } elsif (0x0061 <= $self->{nc} and
1284                 $self->{next_input_character} <= 0x007A) { # a..z                 $self->{nc} <= 0x007A) { # a..z
1285          $self->{current_token} = {type => END_TAG_TOKEN,          !!!cp (30);
1286                            tag_name => chr ($self->{next_input_character})};          $self->{ct} = {type => END_TAG_TOKEN,
1287                                      tag_name => chr ($self->{nc}),
1288                                      line => $l, column => $c};
1289          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1290          !!!next-input-character;          !!!next-input-character;
1291          redo A;          redo A;
1292        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1293          !!!parse-error (type => 'empty end tag');          !!!cp (31);
1294            !!!parse-error (type => 'empty end tag',
1295                            line => $self->{line_prev}, ## "<" in "</>"
1296                            column => $self->{column_prev} - 1);
1297          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1298          !!!next-input-character;          !!!next-input-character;
1299          redo A;          redo A;
1300        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1301            !!!cp (32);
1302          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1303          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1304          # reconsume          # reconsume
1305    
1306          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1307                      line => $l, column => $c,
1308                     });
1309    
1310          redo A;          redo A;
1311        } else {        } else {
1312            !!!cp (33);
1313          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1314          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1315          ## $self->{next_input_character} is intentionally left as is          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1316          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1317                                      column => $self->{column_prev} - 1,
1318                                     };
1319            ## NOTE: $self->{nc} is intentionally left as is.
1320            ## Although the "anything else" case of the spec not explicitly
1321            ## states that the next input character is to be reconsumed,
1322            ## it will be included to the |data| of the comment token
1323            ## generated from the bogus end tag, as defined in the
1324            ## "bogus comment state" entry.
1325            redo A;
1326          }
1327        } elsif ($self->{state} == CDATA_RCDATA_CLOSE_TAG_STATE) {
1328          my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1329          if (length $ch) {
1330            my $CH = $ch;
1331            $ch =~ tr/a-z/A-Z/;
1332            my $nch = chr $self->{nc};
1333            if ($nch eq $ch or $nch eq $CH) {
1334              !!!cp (24);
1335              ## Stay in the state.
1336              $self->{s_kwd} .= $nch;
1337              !!!next-input-character;
1338              redo A;
1339            } else {
1340              !!!cp (25);
1341              $self->{state} = DATA_STATE;
1342              ## Reconsume.
1343              !!!emit ({type => CHARACTER_TOKEN,
1344                        data => '</' . $self->{s_kwd},
1345                        line => $self->{line_prev},
1346                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1347                       });
1348              redo A;
1349            }
1350          } else { # after "<{tag-name}"
1351            unless ($is_space->{$self->{nc}} or
1352                    {
1353                     0x003E => 1, # >
1354                     0x002F => 1, # /
1355                     -1 => 1, # EOF
1356                    }->{$self->{nc}}) {
1357              !!!cp (26);
1358              ## Reconsume.
1359              $self->{state} = DATA_STATE;
1360              !!!emit ({type => CHARACTER_TOKEN,
1361                        data => '</' . $self->{s_kwd},
1362                        line => $self->{line_prev},
1363                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1364                       });
1365              redo A;
1366            } else {
1367              !!!cp (27);
1368              $self->{ct}
1369                  = {type => END_TAG_TOKEN,
1370                     tag_name => $self->{last_stag_name},
1371                     line => $self->{line_prev},
1372                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1373              $self->{state} = TAG_NAME_STATE;
1374              ## Reconsume.
1375              redo A;
1376            }
1377        }        }
1378      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1379        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1380            $self->{next_input_character} == 0x000A or # LF          !!!cp (34);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
1381          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1382          !!!next-input-character;          !!!next-input-character;
1383          redo A;          redo A;
1384        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1385          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1386            $self->{current_token}->{first_start_tag}            !!!cp (35);
1387                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1388            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1389            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1390            if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1391              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This should never be reached.
1392            }            #  !!! cp (36);
1393              #  !!! parse-error (type => 'end tag attribute');
1394              #} else {
1395                !!!cp (37);
1396              #}
1397          } else {          } else {
1398            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1399          }          }
1400          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1401          !!!next-input-character;          !!!next-input-character;
1402    
1403          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1404    
1405          redo A;          redo A;
1406        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1407                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1408          $self->{current_token}->{tag_name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (38);
1409            $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1410            # start tag or end tag            # start tag or end tag
1411          ## Stay in this state          ## Stay in this state
1412          !!!next-input-character;          !!!next-input-character;
1413          redo A;          redo A;
1414        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1415          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1416          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1417            $self->{current_token}->{first_start_tag}            !!!cp (39);
1418                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1419            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1420            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1421            if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1422              !!!parse-error (type => 'end tag attribute');            #  ## NOTE: This state should never be reached.
1423            }            #  !!! cp (40);
1424              #  !!! parse-error (type => 'end tag attribute');
1425              #} else {
1426                !!!cp (41);
1427              #}
1428          } else {          } else {
1429            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1430          }          }
1431          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1432          # reconsume          # reconsume
1433    
1434          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1435    
1436          redo A;          redo A;
1437        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1438            !!!cp (42);
1439            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1440          !!!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  
1441          redo A;          redo A;
1442        } else {        } else {
1443          $self->{current_token}->{tag_name} .= chr $self->{next_input_character};          !!!cp (44);
1444            $self->{ct}->{tag_name} .= chr $self->{nc};
1445            # start tag or end tag            # start tag or end tag
1446          ## Stay in the state          ## Stay in the state
1447          !!!next-input-character;          !!!next-input-character;
1448          redo A;          redo A;
1449        }        }
1450      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1451        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1452            $self->{next_input_character} == 0x000A or # LF          !!!cp (45);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
1453          ## Stay in the state          ## Stay in the state
1454          !!!next-input-character;          !!!next-input-character;
1455          redo A;          redo A;
1456        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1457          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1458            $self->{current_token}->{first_start_tag}            !!!cp (46);
1459                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1460            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1461            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1462            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1463                !!!cp (47);
1464              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1465              } else {
1466                !!!cp (48);
1467            }            }
1468          } else {          } else {
1469            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1470          }          }
1471          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1472          !!!next-input-character;          !!!next-input-character;
1473    
1474          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1475    
1476          redo A;          redo A;
1477        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1478                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1479          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (49);
1480                                value => ''};          $self->{ca}
1481                = {name => chr ($self->{nc} + 0x0020),
1482                   value => '',
1483                   line => $self->{line}, column => $self->{column}};
1484          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1485          !!!next-input-character;          !!!next-input-character;
1486          redo A;          redo A;
1487        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1488            !!!cp (50);
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');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1491          redo A;          redo A;
1492        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1494          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1495            $self->{current_token}->{first_start_tag}            !!!cp (52);
1496                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1497            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1498            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1499            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1500                !!!cp (53);
1501              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1502              } else {
1503                !!!cp (54);
1504            }            }
1505          } else {          } else {
1506            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1507          }          }
1508          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1509          # reconsume          # reconsume
1510    
1511          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1512    
1513          redo A;          redo A;
1514        } else {        } else {
1515          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ({
1516                                value => ''};               0x0022 => 1, # "
1517                 0x0027 => 1, # '
1518                 0x003D => 1, # =
1519                }->{$self->{nc}}) {
1520              !!!cp (55);
1521              !!!parse-error (type => 'bad attribute name');
1522            } else {
1523              !!!cp (56);
1524            }
1525            $self->{ca}
1526                = {name => chr ($self->{nc}),
1527                   value => '',
1528                   line => $self->{line}, column => $self->{column}};
1529          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1530          !!!next-input-character;          !!!next-input-character;
1531          redo A;          redo A;
1532        }        }
1533      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1534        my $before_leave = sub {        my $before_leave = sub {
1535          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1536              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1537            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!cp (57);
1538            ## Discard $self->{current_attribute} # MUST            !!!parse-error (type => 'duplicate attribute', text => $self->{ca}->{name}, line => $self->{ca}->{line}, column => $self->{ca}->{column});
1539          } else {            ## Discard $self->{ca} # MUST
1540            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}          } else {
1541              = $self->{current_attribute};            !!!cp (58);
1542              $self->{ct}->{attributes}->{$self->{ca}->{name}}
1543                = $self->{ca};
1544          }          }
1545        }; # $before_leave        }; # $before_leave
1546    
1547        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1548            $self->{next_input_character} == 0x000A or # LF          !!!cp (59);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
1549          $before_leave->();          $before_leave->();
1550          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1551          !!!next-input-character;          !!!next-input-character;
1552          redo A;          redo A;
1553        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1554            !!!cp (60);
1555          $before_leave->();          $before_leave->();
1556          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1557          !!!next-input-character;          !!!next-input-character;
1558          redo A;          redo A;
1559        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1560          $before_leave->();          $before_leave->();
1561          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1562            $self->{current_token}->{first_start_tag}            !!!cp (61);
1563                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1564            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1565          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {            !!!cp (62);
1566            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1567            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1568              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1569            }            }
1570          } else {          } else {
1571            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1572          }          }
1573          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1574          !!!next-input-character;          !!!next-input-character;
1575    
1576          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1577    
1578          redo A;          redo A;
1579        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1580                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1581          $self->{current_attribute}->{name} .= chr ($self->{next_input_character} + 0x0020);          !!!cp (63);
1582            $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1583          ## Stay in the state          ## Stay in the state
1584          !!!next-input-character;          !!!next-input-character;
1585          redo A;          redo A;
1586        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1587            !!!cp (64);
1588          $before_leave->();          $before_leave->();
1589            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1590          !!!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  
1591          redo A;          redo A;
1592        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1593          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1594          $before_leave->();          $before_leave->();
1595          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1596            $self->{current_token}->{first_start_tag}            !!!cp (66);
1597                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1598            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1599            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1600            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1601                !!!cp (67);
1602              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1603              } else {
1604                ## NOTE: This state should never be reached.
1605                !!!cp (68);
1606            }            }
1607          } else {          } else {
1608            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1609          }          }
1610          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1611          # reconsume          # reconsume
1612    
1613          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1614    
1615          redo A;          redo A;
1616        } else {        } else {
1617          $self->{current_attribute}->{name} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x0022 or # "
1618                $self->{nc} == 0x0027) { # '
1619              !!!cp (69);
1620              !!!parse-error (type => 'bad attribute name');
1621            } else {
1622              !!!cp (70);
1623            }
1624            $self->{ca}->{name} .= chr ($self->{nc});
1625          ## Stay in the state          ## Stay in the state
1626          !!!next-input-character;          !!!next-input-character;
1627          redo A;          redo A;
1628        }        }
1629      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1630        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1631            $self->{next_input_character} == 0x000A or # LF          !!!cp (71);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
1632          ## Stay in the state          ## Stay in the state
1633          !!!next-input-character;          !!!next-input-character;
1634          redo A;          redo A;
1635        } elsif ($self->{next_input_character} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1636            !!!cp (72);
1637          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1638          !!!next-input-character;          !!!next-input-character;
1639          redo A;          redo A;
1640        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1641          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1642            $self->{current_token}->{first_start_tag}            !!!cp (73);
1643                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1644            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1645            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1646            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1647                !!!cp (74);
1648              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1649              } else {
1650                ## NOTE: This state should never be reached.
1651                !!!cp (75);
1652            }            }
1653          } else {          } else {
1654            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1655          }          }
1656          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1657          !!!next-input-character;          !!!next-input-character;
1658    
1659          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1660    
1661          redo A;          redo A;
1662        } elsif (0x0041 <= $self->{next_input_character} and        } elsif (0x0041 <= $self->{nc} and
1663                 $self->{next_input_character} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1664          $self->{current_attribute} = {name => chr ($self->{next_input_character} + 0x0020),          !!!cp (76);
1665                                value => ''};          $self->{ca}
1666                = {name => chr ($self->{nc} + 0x0020),
1667                   value => '',
1668                   line => $self->{line}, column => $self->{column}};
1669          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1670          !!!next-input-character;          !!!next-input-character;
1671          redo A;          redo A;
1672        } elsif ($self->{next_input_character} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1673            !!!cp (77);
1674            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1675          !!!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  
1676          redo A;          redo A;
1677        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1678          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1679          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1680            $self->{current_token}->{first_start_tag}            !!!cp (79);
1681                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1682            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1683            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1684            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1685                !!!cp (80);
1686              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1687              } else {
1688                ## NOTE: This state should never be reached.
1689                !!!cp (81);
1690            }            }
1691          } else {          } else {
1692            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1693          }          }
1694          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1695          # reconsume          # reconsume
1696    
1697          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1698    
1699          redo A;          redo A;
1700        } else {        } else {
1701          $self->{current_attribute} = {name => chr ($self->{next_input_character}),          if ($self->{nc} == 0x0022 or # "
1702                                value => ''};              $self->{nc} == 0x0027) { # '
1703              !!!cp (78);
1704              !!!parse-error (type => 'bad attribute name');
1705            } else {
1706              !!!cp (82);
1707            }
1708            $self->{ca}
1709                = {name => chr ($self->{nc}),
1710                   value => '',
1711                   line => $self->{line}, column => $self->{column}};
1712          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1713          !!!next-input-character;          !!!next-input-character;
1714          redo A;                  redo A;        
1715        }        }
1716      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1717        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1718            $self->{next_input_character} == 0x000A or # LF          !!!cp (83);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP        
1719          ## Stay in the state          ## Stay in the state
1720          !!!next-input-character;          !!!next-input-character;
1721          redo A;          redo A;
1722        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
1723            !!!cp (84);
1724          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1725          !!!next-input-character;          !!!next-input-character;
1726          redo A;          redo A;
1727        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1728            !!!cp (85);
1729          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1730          ## reconsume          ## reconsume
1731          redo A;          redo A;
1732        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
1733            !!!cp (86);
1734          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1735          !!!next-input-character;          !!!next-input-character;
1736          redo A;          redo A;
1737        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1738          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          !!!parse-error (type => 'empty unquoted attribute value');
1739            $self->{current_token}->{first_start_tag}          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1740                = not defined $self->{last_emitted_start_tag_name};            !!!cp (87);
1741            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1742          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1743            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1744            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1745                !!!cp (88);
1746              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1747              } else {
1748                ## NOTE: This state should never be reached.
1749                !!!cp (89);
1750            }            }
1751          } else {          } else {
1752            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1753          }          }
1754          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1755          !!!next-input-character;          !!!next-input-character;
1756    
1757          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1758    
1759          redo A;          redo A;
1760        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1761          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1762          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1763            $self->{current_token}->{first_start_tag}            !!!cp (90);
1764                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1765            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1766            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1767            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1768                !!!cp (91);
1769              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1770              } else {
1771                ## NOTE: This state should never be reached.
1772                !!!cp (92);
1773            }            }
1774          } else {          } else {
1775            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1776          }          }
1777          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1778          ## reconsume          ## reconsume
1779    
1780          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1781    
1782          redo A;          redo A;
1783        } else {        } else {
1784          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ($self->{nc} == 0x003D) { # =
1785              !!!cp (93);
1786              !!!parse-error (type => 'bad attribute value');
1787            } else {
1788              !!!cp (94);
1789            }
1790            $self->{ca}->{value} .= chr ($self->{nc});
1791          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1792          !!!next-input-character;          !!!next-input-character;
1793          redo A;          redo A;
1794        }        }
1795      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1796        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
1797          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (95);
1798          !!!next-input-character;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1799          redo A;          !!!next-input-character;
1800        } elsif ($self->{next_input_character} == 0x0026) { # &          redo A;
1801          $self->{last_attribute_value_state} = $self->{state};        } elsif ($self->{nc} == 0x0026) { # &
1802          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          !!!cp (96);
1803            ## NOTE: In the spec, the tokenizer is switched to the
1804            ## "entity in attribute value state".  In this implementation, the
1805            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1806            ## implementation of the "consume a character reference" algorithm.
1807            $self->{prev_state} = $self->{state};
1808            $self->{entity_add} = 0x0022; # "
1809            $self->{state} = ENTITY_STATE;
1810          !!!next-input-character;          !!!next-input-character;
1811          redo A;          redo A;
1812        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1813          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1814          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1815            $self->{current_token}->{first_start_tag}            !!!cp (97);
1816                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1817            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1818            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1819            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1820                !!!cp (98);
1821              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1822              } else {
1823                ## NOTE: This state should never be reached.
1824                !!!cp (99);
1825            }            }
1826          } else {          } else {
1827            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1828          }          }
1829          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1830          ## reconsume          ## reconsume
1831    
1832          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1833    
1834          redo A;          redo A;
1835        } else {        } else {
1836          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (100);
1837            $self->{ca}->{value} .= chr ($self->{nc});
1838            $self->{read_until}->($self->{ca}->{value},
1839                                  q["&],
1840                                  length $self->{ca}->{value});
1841    
1842          ## Stay in the state          ## Stay in the state
1843          !!!next-input-character;          !!!next-input-character;
1844          redo A;          redo A;
1845        }        }
1846      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1847        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
1848          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          !!!cp (101);
1849            $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1850            !!!next-input-character;
1851            redo A;
1852          } elsif ($self->{nc} == 0x0026) { # &
1853            !!!cp (102);
1854            ## NOTE: In the spec, the tokenizer is switched to the
1855            ## "entity in attribute value state".  In this implementation, the
1856            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1857            ## implementation of the "consume a character reference" algorithm.
1858            $self->{entity_add} = 0x0027; # '
1859            $self->{prev_state} = $self->{state};
1860            $self->{state} = ENTITY_STATE;
1861          !!!next-input-character;          !!!next-input-character;
1862          redo A;          redo A;
1863        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == -1) {
         $self->{last_attribute_value_state} = $self->{state};  
         $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;  
         !!!next-input-character;  
         redo A;  
       } elsif ($self->{next_input_character} == -1) {  
1864          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1865          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1866            $self->{current_token}->{first_start_tag}            !!!cp (103);
1867                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1868            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1869            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1870            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1871                !!!cp (104);
1872              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1873              } else {
1874                ## NOTE: This state should never be reached.
1875                !!!cp (105);
1876            }            }
1877          } else {          } else {
1878            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1879          }          }
1880          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1881          ## reconsume          ## reconsume
1882    
1883          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1884    
1885          redo A;          redo A;
1886        } else {        } else {
1887          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          !!!cp (106);
1888            $self->{ca}->{value} .= chr ($self->{nc});
1889            $self->{read_until}->($self->{ca}->{value},
1890                                  q['&],
1891                                  length $self->{ca}->{value});
1892    
1893          ## Stay in the state          ## Stay in the state
1894          !!!next-input-character;          !!!next-input-character;
1895          redo A;          redo A;
1896        }        }
1897      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1898        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
1899            $self->{next_input_character} == 0x000A or # LF          !!!cp (107);
           $self->{next_input_character} == 0x000B or # HT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
1900          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1901          !!!next-input-character;          !!!next-input-character;
1902          redo A;          redo A;
1903        } elsif ($self->{next_input_character} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1904          $self->{last_attribute_value_state} = $self->{state};          !!!cp (108);
1905          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## NOTE: In the spec, the tokenizer is switched to the
1906          !!!next-input-character;          ## "entity in attribute value state".  In this implementation, the
1907          redo A;          ## tokenizer is switched to the |ENTITY_STATE|, which is an
1908        } elsif ($self->{next_input_character} == 0x003E) { # >          ## implementation of the "consume a character reference" algorithm.
1909          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          $self->{entity_add} = -1;
1910            $self->{current_token}->{first_start_tag}          $self->{prev_state} = $self->{state};
1911                = not defined $self->{last_emitted_start_tag_name};          $self->{state} = ENTITY_STATE;
1912            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          !!!next-input-character;
1913          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          redo A;
1914          } elsif ($self->{nc} == 0x003E) { # >
1915            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1916              !!!cp (109);
1917              $self->{last_stag_name} = $self->{ct}->{tag_name};
1918            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1919            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1920            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1921                !!!cp (110);
1922              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1923              } else {
1924                ## NOTE: This state should never be reached.
1925                !!!cp (111);
1926            }            }
1927          } else {          } else {
1928            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1929          }          }
1930          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1931          !!!next-input-character;          !!!next-input-character;
1932    
1933          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1934    
1935          redo A;          redo A;
1936        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
1937          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1938          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1939            $self->{current_token}->{first_start_tag}            !!!cp (112);
1940                = not defined $self->{last_emitted_start_tag_name};            $self->{last_stag_name} = $self->{ct}->{tag_name};
1941            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1942            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1943            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1944                !!!cp (113);
1945              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1946              } else {
1947                ## NOTE: This state should never be reached.
1948                !!!cp (114);
1949            }            }
1950          } else {          } else {
1951            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1952          }          }
1953          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1954          ## reconsume          ## reconsume
1955    
1956          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1957    
1958          redo A;          redo A;
1959        } else {        } else {
1960          $self->{current_attribute}->{value} .= chr ($self->{next_input_character});          if ({
1961                 0x0022 => 1, # "
1962                 0x0027 => 1, # '
1963                 0x003D => 1, # =
1964                }->{$self->{nc}}) {
1965              !!!cp (115);
1966              !!!parse-error (type => 'bad attribute value');
1967            } else {
1968              !!!cp (116);
1969            }
1970            $self->{ca}->{value} .= chr ($self->{nc});
1971            $self->{read_until}->($self->{ca}->{value},
1972                                  q["'=& >],
1973                                  length $self->{ca}->{value});
1974    
1975          ## Stay in the state          ## Stay in the state
1976          !!!next-input-character;          !!!next-input-character;
1977          redo A;          redo A;
1978        }        }
1979      } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1980        my $token = $self->_tokenize_attempt_to_consume_an_entity (1);        if ($is_space->{$self->{nc}}) {
1981            !!!cp (118);
1982            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1983            !!!next-input-character;
1984            redo A;
1985          } elsif ($self->{nc} == 0x003E) { # >
1986            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1987              !!!cp (119);
1988              $self->{last_stag_name} = $self->{ct}->{tag_name};
1989            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1990              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1991              if ($self->{ct}->{attributes}) {
1992                !!!cp (120);
1993                !!!parse-error (type => 'end tag attribute');
1994              } else {
1995                ## NOTE: This state should never be reached.
1996                !!!cp (121);
1997              }
1998            } else {
1999              die "$0: $self->{ct}->{type}: Unknown token type";
2000            }
2001            $self->{state} = DATA_STATE;
2002            !!!next-input-character;
2003    
2004            !!!emit ($self->{ct}); # start tag or end tag
2005    
2006        unless (defined $token) {          redo A;
2007          $self->{current_attribute}->{value} .= '&';        } elsif ($self->{nc} == 0x002F) { # /
2008            !!!cp (122);
2009            $self->{state} = SELF_CLOSING_START_TAG_STATE;
2010            !!!next-input-character;
2011            redo A;
2012          } elsif ($self->{nc} == -1) {
2013            !!!parse-error (type => 'unclosed tag');
2014            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2015              !!!cp (122.3);
2016              $self->{last_stag_name} = $self->{ct}->{tag_name};
2017            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2018              if ($self->{ct}->{attributes}) {
2019                !!!cp (122.1);
2020                !!!parse-error (type => 'end tag attribute');
2021              } else {
2022                ## NOTE: This state should never be reached.
2023                !!!cp (122.2);
2024              }
2025            } else {
2026              die "$0: $self->{ct}->{type}: Unknown token type";
2027            }
2028            $self->{state} = DATA_STATE;
2029            ## Reconsume.
2030            !!!emit ($self->{ct}); # start tag or end tag
2031            redo A;
2032        } else {        } else {
2033          $self->{current_attribute}->{value} .= $token->{data};          !!!cp ('124.1');
2034          ## ISSUE: spec says "append the returned character token to the current attribute's value"          !!!parse-error (type => 'no space between attributes');
2035            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2036            ## reconsume
2037            redo A;
2038        }        }
2039        } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
2040          if ($self->{nc} == 0x003E) { # >
2041            if ($self->{ct}->{type} == END_TAG_TOKEN) {
2042              !!!cp ('124.2');
2043              !!!parse-error (type => 'nestc', token => $self->{ct});
2044              ## TODO: Different type than slash in start tag
2045              $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2046              if ($self->{ct}->{attributes}) {
2047                !!!cp ('124.4');
2048                !!!parse-error (type => 'end tag attribute');
2049              } else {
2050                !!!cp ('124.5');
2051              }
2052              ## TODO: Test |<title></title/>|
2053            } else {
2054              !!!cp ('124.3');
2055              $self->{self_closing} = 1;
2056            }
2057    
2058        $self->{state} = $self->{last_attribute_value_state};          $self->{state} = DATA_STATE;
2059        # next-input-character is already done          !!!next-input-character;
2060        redo A;  
2061            !!!emit ($self->{ct}); # start tag or end tag
2062    
2063            redo A;
2064          } elsif ($self->{nc} == -1) {
2065            !!!parse-error (type => 'unclosed tag');
2066            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2067              !!!cp (124.7);
2068              $self->{last_stag_name} = $self->{ct}->{tag_name};
2069            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2070              if ($self->{ct}->{attributes}) {
2071                !!!cp (124.5);
2072                !!!parse-error (type => 'end tag attribute');
2073              } else {
2074                ## NOTE: This state should never be reached.
2075                !!!cp (124.6);
2076              }
2077            } else {
2078              die "$0: $self->{ct}->{type}: Unknown token type";
2079            }
2080            $self->{state} = DATA_STATE;
2081            ## Reconsume.
2082            !!!emit ($self->{ct}); # start tag or end tag
2083            redo A;
2084          } else {
2085            !!!cp ('124.4');
2086            !!!parse-error (type => 'nestc');
2087            ## TODO: This error type is wrong.
2088            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2089            ## Reconsume.
2090            redo A;
2091          }
2092      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2093        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
         
       my $token = {type => COMMENT_TOKEN, data => ''};  
   
       BC: {  
         if ($self->{next_input_character} == 0x003E) { # >  
           $self->{state} = DATA_STATE;  
           !!!next-input-character;  
2094    
2095            !!!emit ($token);        ## NOTE: Unlike spec's "bogus comment state", this implementation
2096          ## consumes characters one-by-one basis.
2097          
2098          if ($self->{nc} == 0x003E) { # >
2099            !!!cp (124);
2100            $self->{state} = DATA_STATE;
2101            !!!next-input-character;
2102    
2103            redo A;          !!!emit ($self->{ct}); # comment
2104          } elsif ($self->{next_input_character} == -1) {          redo A;
2105            $self->{state} = DATA_STATE;        } elsif ($self->{nc} == -1) {
2106            ## reconsume          !!!cp (125);
2107            $self->{state} = DATA_STATE;
2108            ## reconsume
2109    
2110            !!!emit ($token);          !!!emit ($self->{ct}); # comment
2111            redo A;
2112          } else {
2113            !!!cp (126);
2114            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2115            $self->{read_until}->($self->{ct}->{data},
2116                                  q[>],
2117                                  length $self->{ct}->{data});
2118    
2119            redo A;          ## Stay in the state.
2120          } else {          !!!next-input-character;
2121            $token->{data} .= chr ($self->{next_input_character});          redo A;
2122            !!!next-input-character;        }
           redo BC;  
         }  
       } # BC  
2123      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2124        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my @next_char;  
       push @next_char, $self->{next_input_character};  
2125                
2126        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2127            !!!cp (133);
2128            $self->{state} = MD_HYPHEN_STATE;
2129          !!!next-input-character;          !!!next-input-character;
2130          push @next_char, $self->{next_input_character};          redo A;
2131          if ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x0044 or # D
2132            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};                 $self->{nc} == 0x0064) { # d
2133            $self->{state} = COMMENT_START_STATE;          ## ASCII case-insensitive.
2134            !!!next-input-character;          !!!cp (130);
2135            redo A;          $self->{state} = MD_DOCTYPE_STATE;
2136          }          $self->{s_kwd} = chr $self->{nc};
       } elsif ($self->{next_input_character} == 0x0044 or # D  
                $self->{next_input_character} == 0x0064) { # d  
2137          !!!next-input-character;          !!!next-input-character;
2138          push @next_char, $self->{next_input_character};          redo A;
2139          if ($self->{next_input_character} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2140              $self->{next_input_character} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2141            !!!next-input-character;                 $self->{nc} == 0x005B) { # [
2142            push @next_char, $self->{next_input_character};          !!!cp (135.4);                
2143            if ($self->{next_input_character} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2144                $self->{next_input_character} == 0x0063) { # c          $self->{s_kwd} = '[';
2145              !!!next-input-character;          !!!next-input-character;
2146              push @next_char, $self->{next_input_character};          redo A;
2147              if ($self->{next_input_character} == 0x0054 or # T        } else {
2148                  $self->{next_input_character} == 0x0074) { # t          !!!cp (136);
               !!!next-input-character;  
               push @next_char, $self->{next_input_character};  
               if ($self->{next_input_character} == 0x0059 or # Y  
                   $self->{next_input_character} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_input_character};  
                 if ($self->{next_input_character} == 0x0050 or # P  
                     $self->{next_input_character} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_input_character};  
                   if ($self->{next_input_character} == 0x0045 or # E  
                       $self->{next_input_character} == 0x0065) { # e  
                     ## ISSUE: What a stupid code this is!  
                     $self->{state} = DOCTYPE_STATE;  
                     !!!next-input-character;  
                     redo A;  
                   }  
                 }  
               }  
             }  
           }  
         }  
2149        }        }
2150    
2151        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2152        $self->{next_input_character} = shift @next_char;                        line => $self->{line_prev},
2153        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2154          ## Reconsume.
2155        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2156          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2157                                    line => $self->{line_prev},
2158                                    column => $self->{column_prev} - 1,
2159                                   };
2160        redo A;        redo A;
2161              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2162        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2163        ## 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);
2164            $self->{ct} = {type => COMMENT_TOKEN, data => '',
2165                                      line => $self->{line_prev},
2166                                      column => $self->{column_prev} - 2,
2167                                     };
2168            $self->{state} = COMMENT_START_STATE;
2169            !!!next-input-character;
2170            redo A;
2171          } else {
2172            !!!cp (128);
2173            !!!parse-error (type => 'bogus comment',
2174                            line => $self->{line_prev},
2175                            column => $self->{column_prev} - 2);
2176            $self->{state} = BOGUS_COMMENT_STATE;
2177            ## Reconsume.
2178            $self->{ct} = {type => COMMENT_TOKEN,
2179                                      data => '-',
2180                                      line => $self->{line_prev},
2181                                      column => $self->{column_prev} - 2,
2182                                     };
2183            redo A;
2184          }
2185        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2186          ## ASCII case-insensitive.
2187          if ($self->{nc} == [
2188                undef,
2189                0x004F, # O
2190                0x0043, # C
2191                0x0054, # T
2192                0x0059, # Y
2193                0x0050, # P
2194              ]->[length $self->{s_kwd}] or
2195              $self->{nc} == [
2196                undef,
2197                0x006F, # o
2198                0x0063, # c
2199                0x0074, # t
2200                0x0079, # y
2201                0x0070, # p
2202              ]->[length $self->{s_kwd}]) {
2203            !!!cp (131);
2204            ## Stay in the state.
2205            $self->{s_kwd} .= chr $self->{nc};
2206            !!!next-input-character;
2207            redo A;
2208          } elsif ((length $self->{s_kwd}) == 6 and
2209                   ($self->{nc} == 0x0045 or # E
2210                    $self->{nc} == 0x0065)) { # e
2211            !!!cp (129);
2212            $self->{state} = DOCTYPE_STATE;
2213            $self->{ct} = {type => DOCTYPE_TOKEN,
2214                                      quirks => 1,
2215                                      line => $self->{line_prev},
2216                                      column => $self->{column_prev} - 7,
2217                                     };
2218            !!!next-input-character;
2219            redo A;
2220          } else {
2221            !!!cp (132);        
2222            !!!parse-error (type => 'bogus comment',
2223                            line => $self->{line_prev},
2224                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2225            $self->{state} = BOGUS_COMMENT_STATE;
2226            ## Reconsume.
2227            $self->{ct} = {type => COMMENT_TOKEN,
2228                                      data => $self->{s_kwd},
2229                                      line => $self->{line_prev},
2230                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2231                                     };
2232            redo A;
2233          }
2234        } elsif ($self->{state} == MD_CDATA_STATE) {
2235          if ($self->{nc} == {
2236                '[' => 0x0043, # C
2237                '[C' => 0x0044, # D
2238                '[CD' => 0x0041, # A
2239                '[CDA' => 0x0054, # T
2240                '[CDAT' => 0x0041, # A
2241              }->{$self->{s_kwd}}) {
2242            !!!cp (135.1);
2243            ## Stay in the state.
2244            $self->{s_kwd} .= chr $self->{nc};
2245            !!!next-input-character;
2246            redo A;
2247          } elsif ($self->{s_kwd} eq '[CDATA' and
2248                   $self->{nc} == 0x005B) { # [
2249            !!!cp (135.2);
2250            $self->{ct} = {type => CHARACTER_TOKEN,
2251                                      data => '',
2252                                      line => $self->{line_prev},
2253                                      column => $self->{column_prev} - 7};
2254            $self->{state} = CDATA_SECTION_STATE;
2255            !!!next-input-character;
2256            redo A;
2257          } else {
2258            !!!cp (135.3);
2259            !!!parse-error (type => 'bogus comment',
2260                            line => $self->{line_prev},
2261                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2262            $self->{state} = BOGUS_COMMENT_STATE;
2263            ## Reconsume.
2264            $self->{ct} = {type => COMMENT_TOKEN,
2265                                      data => $self->{s_kwd},
2266                                      line => $self->{line_prev},
2267                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2268                                     };
2269            redo A;
2270          }
2271      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2272        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2273            !!!cp (137);
2274          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2275          !!!next-input-character;          !!!next-input-character;
2276          redo A;          redo A;
2277        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2278            !!!cp (138);
2279          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2280          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2281          !!!next-input-character;          !!!next-input-character;
2282    
2283          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2284    
2285          redo A;          redo A;
2286        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2287            !!!cp (139);
2288          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2289          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2290          ## reconsume          ## reconsume
2291    
2292          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2293    
2294          redo A;          redo A;
2295        } else {        } else {
2296          $self->{current_token}->{data} # comment          !!!cp (140);
2297              .= chr ($self->{next_input_character});          $self->{ct}->{data} # comment
2298                .= chr ($self->{nc});
2299          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2300          !!!next-input-character;          !!!next-input-character;
2301          redo A;          redo A;
2302        }        }
2303      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2304        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2305            !!!cp (141);
2306          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2307          !!!next-input-character;          !!!next-input-character;
2308          redo A;          redo A;
2309        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2310            !!!cp (142);
2311          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2312          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2313          !!!next-input-character;          !!!next-input-character;
2314    
2315          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2316    
2317          redo A;          redo A;
2318        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2319            !!!cp (143);
2320          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2321          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2322          ## reconsume          ## reconsume
2323    
2324          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2325    
2326          redo A;          redo A;
2327        } else {        } else {
2328          $self->{current_token}->{data} # comment          !!!cp (144);
2329              .= '-' . chr ($self->{next_input_character});          $self->{ct}->{data} # comment
2330                .= '-' . chr ($self->{nc});
2331          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2332          !!!next-input-character;          !!!next-input-character;
2333          redo A;          redo A;
2334        }        }
2335      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2336        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2337            !!!cp (145);
2338          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2339          !!!next-input-character;          !!!next-input-character;
2340          redo A;          redo A;
2341        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2342            !!!cp (146);
2343          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2344          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2345          ## reconsume          ## reconsume
2346    
2347          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2348    
2349          redo A;          redo A;
2350        } else {        } else {
2351          $self->{current_token}->{data} .= chr ($self->{next_input_character}); # comment          !!!cp (147);
2352            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2353            $self->{read_until}->($self->{ct}->{data},
2354                                  q[-],
2355                                  length $self->{ct}->{data});
2356    
2357          ## Stay in the state          ## Stay in the state
2358          !!!next-input-character;          !!!next-input-character;
2359          redo A;          redo A;
2360        }        }
2361      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2362        if ($self->{next_input_character} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2363            !!!cp (148);
2364          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2365          !!!next-input-character;          !!!next-input-character;
2366          redo A;          redo A;
2367        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2368            !!!cp (149);
2369          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2370          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2371          ## reconsume          ## reconsume
2372    
2373          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2374    
2375          redo A;          redo A;
2376        } else {        } else {
2377          $self->{current_token}->{data} .= '-' . chr ($self->{next_input_character}); # comment          !!!cp (150);
2378            $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2379          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2380          !!!next-input-character;          !!!next-input-character;
2381          redo A;          redo A;
2382        }        }
2383      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2384        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2385            !!!cp (151);
2386          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2387          !!!next-input-character;          !!!next-input-character;
2388    
2389          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2390    
2391          redo A;          redo A;
2392        } elsif ($self->{next_input_character} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2393          !!!parse-error (type => 'dash in comment');          !!!cp (152);
2394          $self->{current_token}->{data} .= '-'; # comment          !!!parse-error (type => 'dash in comment',
2395                            line => $self->{line_prev},
2396                            column => $self->{column_prev});
2397            $self->{ct}->{data} .= '-'; # comment
2398          ## Stay in the state          ## Stay in the state
2399          !!!next-input-character;          !!!next-input-character;
2400          redo A;          redo A;
2401        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2402            !!!cp (153);
2403          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2404          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2405          ## reconsume          ## reconsume
2406    
2407          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2408    
2409          redo A;          redo A;
2410        } else {        } else {
2411          !!!parse-error (type => 'dash in comment');          !!!cp (154);
2412          $self->{current_token}->{data} .= '--' . chr ($self->{next_input_character}); # comment          !!!parse-error (type => 'dash in comment',
2413                            line => $self->{line_prev},
2414                            column => $self->{column_prev});
2415            $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2416          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2417          !!!next-input-character;          !!!next-input-character;
2418          redo A;          redo A;
2419        }        }
2420      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2421        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
2422            $self->{next_input_character} == 0x000A or # LF          !!!cp (155);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
2423          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2424          !!!next-input-character;          !!!next-input-character;
2425          redo A;          redo A;
2426        } else {        } else {
2427            !!!cp (156);
2428          !!!parse-error (type => 'no space before DOCTYPE name');          !!!parse-error (type => 'no space before DOCTYPE name');
2429          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2430          ## reconsume          ## reconsume
2431          redo A;          redo A;
2432        }        }
2433      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2434        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
2435            $self->{next_input_character} == 0x000A or # LF          !!!cp (157);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
2436          ## Stay in the state          ## Stay in the state
2437          !!!next-input-character;          !!!next-input-character;
2438          redo A;          redo A;
2439        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2440            !!!cp (158);
2441          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2442          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2443          !!!next-input-character;          !!!next-input-character;
2444    
2445          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2446    
2447          redo A;          redo A;
2448        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2449            !!!cp (159);
2450          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2451          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2452          ## reconsume          ## reconsume
2453    
2454          !!!emit ({type => DOCTYPE_TOKEN}); # incorrect          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2455    
2456          redo A;          redo A;
2457        } else {        } else {
2458          $self->{current_token}          !!!cp (160);
2459              = {type => DOCTYPE_TOKEN,          $self->{ct}->{name} = chr $self->{nc};
2460                 name => chr ($self->{next_input_character}),          delete $self->{ct}->{quirks};
                correct => 1};  
2461  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2462          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2463          !!!next-input-character;          !!!next-input-character;
# Line 1383  sub _get_next_token ($) { Line 2465  sub _get_next_token ($) {
2465        }        }
2466      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2467  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2468        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
2469            $self->{next_input_character} == 0x000A or # LF          !!!cp (161);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
2470          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2471          !!!next-input-character;          !!!next-input-character;
2472          redo A;          redo A;
2473        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2474            !!!cp (162);
2475          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2476          !!!next-input-character;          !!!next-input-character;
2477    
2478          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2479    
2480          redo A;          redo A;
2481        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2482            !!!cp (163);
2483          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2484          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2485          ## reconsume          ## reconsume
2486    
2487          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2488          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2489    
2490          redo A;          redo A;
2491        } else {        } else {
2492          $self->{current_token}->{name}          !!!cp (164);
2493            .= chr ($self->{next_input_character}); # DOCTYPE          $self->{ct}->{name}
2494              .= chr ($self->{nc}); # DOCTYPE
2495          ## Stay in the state          ## Stay in the state
2496          !!!next-input-character;          !!!next-input-character;
2497          redo A;          redo A;
2498        }        }
2499      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2500        if ($self->{next_input_character} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
2501            $self->{next_input_character} == 0x000A or # LF          !!!cp (165);
           $self->{next_input_character} == 0x000B or # VT  
           $self->{next_input_character} == 0x000C or # FF  
           $self->{next_input_character} == 0x0020) { # SP  
2502          ## Stay in the state          ## Stay in the state
2503          !!!next-input-character;          !!!next-input-character;
2504          redo A;          redo A;
2505        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2506            !!!cp (166);
2507          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2508          !!!next-input-character;          !!!next-input-character;
2509    
2510          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2511    
2512          redo A;          redo A;
2513        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2514            !!!cp (167);
2515          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2516          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2517          ## reconsume          ## reconsume
2518    
2519          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2520          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2521    
2522          redo A;          redo A;
2523        } elsif ($self->{next_input_character} == 0x0050 or # P        } elsif ($self->{nc} == 0x0050 or # P
2524                 $self->{next_input_character} == 0x0070) { # p                 $self->{nc} == 0x0070) { # p
2525            $self->{state} = PUBLIC_STATE;
2526            $self->{s_kwd} = chr $self->{nc};
2527          !!!next-input-character;          !!!next-input-character;
2528          if ($self->{next_input_character} == 0x0055 or # U          redo A;
2529              $self->{next_input_character} == 0x0075) { # u        } elsif ($self->{nc} == 0x0053 or # S
2530            !!!next-input-character;                 $self->{nc} == 0x0073) { # s
2531            if ($self->{next_input_character} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2532                $self->{next_input_character} == 0x0062) { # b          $self->{s_kwd} = chr $self->{nc};
             !!!next-input-character;  
             if ($self->{next_input_character} == 0x004C or # L  
                 $self->{next_input_character} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_input_character} == 0x0049 or # I  
                   $self->{next_input_character} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_input_character} == 0x0043 or # C  
                     $self->{next_input_character} == 0x0063) { # c  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
       } elsif ($self->{next_input_character} == 0x0053 or # S  
                $self->{next_input_character} == 0x0073) { # s  
2533          !!!next-input-character;          !!!next-input-character;
2534          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;  
                 }  
               }  
             }  
           }  
         }  
   
         #  
2535        } else {        } else {
2536            !!!cp (180);
2537            !!!parse-error (type => 'string after DOCTYPE name');
2538            $self->{ct}->{quirks} = 1;
2539    
2540            $self->{state} = BOGUS_DOCTYPE_STATE;
2541          !!!next-input-character;          !!!next-input-character;
2542          #          redo A;
2543        }        }
2544        } elsif ($self->{state} == PUBLIC_STATE) {
2545          ## ASCII case-insensitive
2546          if ($self->{nc} == [
2547                undef,
2548                0x0055, # U
2549                0x0042, # B
2550                0x004C, # L
2551                0x0049, # I
2552              ]->[length $self->{s_kwd}] or
2553              $self->{nc} == [
2554                undef,
2555                0x0075, # u
2556                0x0062, # b
2557                0x006C, # l
2558                0x0069, # i
2559              ]->[length $self->{s_kwd}]) {
2560            !!!cp (175);
2561            ## Stay in the state.
2562            $self->{s_kwd} .= chr $self->{nc};
2563            !!!next-input-character;
2564            redo A;
2565          } elsif ((length $self->{s_kwd}) == 5 and
2566                   ($self->{nc} == 0x0043 or # C
2567                    $self->{nc} == 0x0063)) { # c
2568            !!!cp (168);
2569            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2570            !!!next-input-character;
2571            redo A;
2572          } else {
2573            !!!cp (169);
2574            !!!parse-error (type => 'string after DOCTYPE name',
2575                            line => $self->{line_prev},
2576                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2577            $self->{ct}->{quirks} = 1;
2578    
2579        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2580        $self->{state} = BOGUS_DOCTYPE_STATE;          ## Reconsume.
2581        # next-input-character is already done          redo A;
2582        redo A;        }
2583        } elsif ($self->{state} == SYSTEM_STATE) {
2584          ## ASCII case-insensitive
2585          if ($self->{nc} == [
2586                undef,
2587                0x0059, # Y
2588                0x0053, # S
2589                0x0054, # T
2590                0x0045, # E
2591              ]->[length $self->{s_kwd}] or
2592              $self->{nc} == [
2593                undef,
2594                0x0079, # y
2595                0x0073, # s
2596                0x0074, # t
2597                0x0065, # e
2598              ]->[length $self->{s_kwd}]) {
2599            !!!cp (170);
2600            ## Stay in the state.
2601            $self->{s_kwd} .= chr $self->{nc};
2602            !!!next-input-character;
2603            redo A;
2604          } elsif ((length $self->{s_kwd}) == 5 and
2605                   ($self->{nc} == 0x004D or # M
2606                    $self->{nc} == 0x006D)) { # m
2607            !!!cp (171);
2608            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2609            !!!next-input-character;
2610            redo A;
2611          } else {
2612            !!!cp (172);
2613            !!!parse-error (type => 'string after DOCTYPE name',
2614                            line => $self->{line_prev},
2615                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2616            $self->{ct}->{quirks} = 1;
2617    
2618            $self->{state} = BOGUS_DOCTYPE_STATE;
2619            ## Reconsume.
2620            redo A;
2621          }
2622      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2623        if ({        if ($is_space->{$self->{nc}}) {
2624              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,          !!!cp (181);
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
2625          ## Stay in the state          ## Stay in the state
2626          !!!next-input-character;          !!!next-input-character;
2627          redo A;          redo A;
2628        } elsif ($self->{next_input_character} eq 0x0022) { # "        } elsif ($self->{nc} eq 0x0022) { # "
2629          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          !!!cp (182);
2630            $self->{ct}->{pubid} = ''; # DOCTYPE
2631          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2632          !!!next-input-character;          !!!next-input-character;
2633          redo A;          redo A;
2634        } elsif ($self->{next_input_character} eq 0x0027) { # '        } elsif ($self->{nc} eq 0x0027) { # '
2635          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          !!!cp (183);
2636            $self->{ct}->{pubid} = ''; # DOCTYPE
2637          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2638          !!!next-input-character;          !!!next-input-character;
2639          redo A;          redo A;
2640        } elsif ($self->{next_input_character} eq 0x003E) { # >        } elsif ($self->{nc} eq 0x003E) { # >
2641            !!!cp (184);
2642          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2643    
2644          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2645          !!!next-input-character;          !!!next-input-character;
2646    
2647          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2648          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2649    
2650          redo A;          redo A;
2651        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2652            !!!cp (185);
2653          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2654    
2655          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2656          ## reconsume          ## reconsume
2657    
2658          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2659          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2660    
2661          redo A;          redo A;
2662        } else {        } else {
2663            !!!cp (186);
2664          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2665            $self->{ct}->{quirks} = 1;
2666    
2667          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2668          !!!next-input-character;          !!!next-input-character;
2669          redo A;          redo A;
2670        }        }
2671      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2672        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2673            !!!cp (187);
2674          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2675          !!!next-input-character;          !!!next-input-character;
2676          redo A;          redo A;
2677        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2678            !!!cp (188);
2679            !!!parse-error (type => 'unclosed PUBLIC literal');
2680    
2681            $self->{state} = DATA_STATE;
2682            !!!next-input-character;
2683    
2684            $self->{ct}->{quirks} = 1;
2685            !!!emit ($self->{ct}); # DOCTYPE
2686    
2687            redo A;
2688          } elsif ($self->{nc} == -1) {
2689            !!!cp (189);
2690          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2691    
2692          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2693          ## reconsume          ## reconsume
2694    
2695          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2696          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2697    
2698          redo A;          redo A;
2699        } else {        } else {
2700          $self->{current_token}->{public_identifier} # DOCTYPE          !!!cp (190);
2701              .= chr $self->{next_input_character};          $self->{ct}->{pubid} # DOCTYPE
2702                .= chr $self->{nc};
2703            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2704                                  length $self->{ct}->{pubid});
2705    
2706          ## Stay in the state          ## Stay in the state
2707          !!!next-input-character;          !!!next-input-character;
2708          redo A;          redo A;
2709        }        }
2710      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2711        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2712            !!!cp (191);
2713          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2714          !!!next-input-character;          !!!next-input-character;
2715          redo A;          redo A;
2716        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2717            !!!cp (192);
2718            !!!parse-error (type => 'unclosed PUBLIC literal');
2719    
2720            $self->{state} = DATA_STATE;
2721            !!!next-input-character;
2722    
2723            $self->{ct}->{quirks} = 1;
2724            !!!emit ($self->{ct}); # DOCTYPE
2725    
2726            redo A;
2727          } elsif ($self->{nc} == -1) {
2728            !!!cp (193);
2729          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2730    
2731          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2732          ## reconsume          ## reconsume
2733    
2734          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2735          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2736    
2737          redo A;          redo A;
2738        } else {        } else {
2739          $self->{current_token}->{public_identifier} # DOCTYPE          !!!cp (194);
2740              .= chr $self->{next_input_character};          $self->{ct}->{pubid} # DOCTYPE
2741                .= chr $self->{nc};
2742            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2743                                  length $self->{ct}->{pubid});
2744    
2745          ## Stay in the state          ## Stay in the state
2746          !!!next-input-character;          !!!next-input-character;
2747          redo A;          redo A;
2748        }        }
2749      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2750        if ({        if ($is_space->{$self->{nc}}) {
2751              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,          !!!cp (195);
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
2752          ## Stay in the state          ## Stay in the state
2753          !!!next-input-character;          !!!next-input-character;
2754          redo A;          redo A;
2755        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2756          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (196);
2757            $self->{ct}->{sysid} = ''; # DOCTYPE
2758          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2759          !!!next-input-character;          !!!next-input-character;
2760          redo A;          redo A;
2761        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2762          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (197);
2763            $self->{ct}->{sysid} = ''; # DOCTYPE
2764          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2765          !!!next-input-character;          !!!next-input-character;
2766          redo A;          redo A;
2767        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2768            !!!cp (198);
2769          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2770          !!!next-input-character;          !!!next-input-character;
2771    
2772          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2773    
2774          redo A;          redo A;
2775        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2776            !!!cp (199);
2777          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2778    
2779          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2780          ## reconsume          ## reconsume
2781    
2782          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2783          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2784    
2785          redo A;          redo A;
2786        } else {        } else {
2787            !!!cp (200);
2788          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2789            $self->{ct}->{quirks} = 1;
2790    
2791          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2792          !!!next-input-character;          !!!next-input-character;
2793          redo A;          redo A;
2794        }        }
2795      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2796        if ({        if ($is_space->{$self->{nc}}) {
2797              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,          !!!cp (201);
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
2798          ## Stay in the state          ## Stay in the state
2799          !!!next-input-character;          !!!next-input-character;
2800          redo A;          redo A;
2801        } elsif ($self->{next_input_character} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2802          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (202);
2803            $self->{ct}->{sysid} = ''; # DOCTYPE
2804          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2805          !!!next-input-character;          !!!next-input-character;
2806          redo A;          redo A;
2807        } elsif ($self->{next_input_character} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2808          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          !!!cp (203);
2809            $self->{ct}->{sysid} = ''; # DOCTYPE
2810          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2811          !!!next-input-character;          !!!next-input-character;
2812          redo A;          redo A;
2813        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2814            !!!cp (204);
2815          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2816          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2817          !!!next-input-character;          !!!next-input-character;
2818    
2819          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2820          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2821    
2822          redo A;          redo A;
2823        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2824            !!!cp (205);
2825          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2826    
2827          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2828          ## reconsume          ## reconsume
2829    
2830          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2831          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2832    
2833          redo A;          redo A;
2834        } else {        } else {
2835            !!!cp (206);
2836          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2837            $self->{ct}->{quirks} = 1;
2838    
2839          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2840          !!!next-input-character;          !!!next-input-character;
2841          redo A;          redo A;
2842        }        }
2843      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2844        if ($self->{next_input_character} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2845            !!!cp (207);
2846          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2847          !!!next-input-character;          !!!next-input-character;
2848          redo A;          redo A;
2849        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2850            !!!cp (208);
2851            !!!parse-error (type => 'unclosed SYSTEM literal');
2852    
2853            $self->{state} = DATA_STATE;
2854            !!!next-input-character;
2855    
2856            $self->{ct}->{quirks} = 1;
2857            !!!emit ($self->{ct}); # DOCTYPE
2858    
2859            redo A;
2860          } elsif ($self->{nc} == -1) {
2861            !!!cp (209);
2862          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2863    
2864          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2865          ## reconsume          ## reconsume
2866    
2867          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2868          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2869    
2870          redo A;          redo A;
2871        } else {        } else {
2872          $self->{current_token}->{system_identifier} # DOCTYPE          !!!cp (210);
2873              .= chr $self->{next_input_character};          $self->{ct}->{sysid} # DOCTYPE
2874                .= chr $self->{nc};
2875            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2876                                  length $self->{ct}->{sysid});
2877    
2878          ## Stay in the state          ## Stay in the state
2879          !!!next-input-character;          !!!next-input-character;
2880          redo A;          redo A;
2881        }        }
2882      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2883        if ($self->{next_input_character} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2884            !!!cp (211);
2885          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2886          !!!next-input-character;          !!!next-input-character;
2887          redo A;          redo A;
2888        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == 0x003E) { # >
2889            !!!cp (212);
2890            !!!parse-error (type => 'unclosed SYSTEM literal');
2891    
2892            $self->{state} = DATA_STATE;
2893            !!!next-input-character;
2894    
2895            $self->{ct}->{quirks} = 1;
2896            !!!emit ($self->{ct}); # DOCTYPE
2897    
2898            redo A;
2899          } elsif ($self->{nc} == -1) {
2900            !!!cp (213);
2901          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2902    
2903          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2904          ## reconsume          ## reconsume
2905    
2906          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2907          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2908    
2909          redo A;          redo A;
2910        } else {        } else {
2911          $self->{current_token}->{system_identifier} # DOCTYPE          !!!cp (214);
2912              .= chr $self->{next_input_character};          $self->{ct}->{sysid} # DOCTYPE
2913                .= chr $self->{nc};
2914            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2915                                  length $self->{ct}->{sysid});
2916    
2917          ## Stay in the state          ## Stay in the state
2918          !!!next-input-character;          !!!next-input-character;
2919          redo A;          redo A;
2920        }        }
2921      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2922        if ({        if ($is_space->{$self->{nc}}) {
2923              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,          !!!cp (215);
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_input_character}}) {  
2924          ## Stay in the state          ## Stay in the state
2925          !!!next-input-character;          !!!next-input-character;
2926          redo A;          redo A;
2927        } elsif ($self->{next_input_character} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2928            !!!cp (216);
2929          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2930          !!!next-input-character;          !!!next-input-character;
2931    
2932          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2933    
2934          redo A;          redo A;
2935        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2936            !!!cp (217);
2937          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2938          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2939          ## reconsume          ## reconsume
2940    
2941          delete $self->{current_token}->{correct};          $self->{ct}->{quirks} = 1;
2942          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2943    
2944          redo A;          redo A;
2945        } else {        } else {
2946            !!!cp (218);
2947          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2948            #$self->{ct}->{quirks} = 1;
2949    
2950          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2951          !!!next-input-character;          !!!next-input-character;
2952          redo A;          redo A;
2953        }        }
2954      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2955        if ($self->{next_input_character} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2956            !!!cp (219);
2957          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2958          !!!next-input-character;          !!!next-input-character;
2959    
2960          delete $self->{current_token}->{correct};          !!!emit ($self->{ct}); # DOCTYPE
         !!!emit ($self->{current_token}); # DOCTYPE  
2961    
2962          redo A;          redo A;
2963        } elsif ($self->{next_input_character} == -1) {        } elsif ($self->{nc} == -1) {
2964            !!!cp (220);
2965          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2966          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2967          ## reconsume          ## reconsume
2968    
2969          delete $self->{current_token}->{correct};          !!!emit ($self->{ct}); # DOCTYPE
         !!!emit ($self->{current_token}); # DOCTYPE  
2970    
2971          redo A;          redo A;
2972        } else {        } else {
2973            !!!cp (221);
2974            my $s = '';
2975            $self->{read_until}->($s, q[>], 0);
2976    
2977          ## Stay in the state          ## Stay in the state
2978          !!!next-input-character;          !!!next-input-character;
2979          redo A;          redo A;
2980        }        }
2981      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2982        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2983      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2984    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2985          
2986    die "$0: _get_next_token: unexpected case";        if ($self->{nc} == 0x005D) { # ]
2987  } # _get_next_token          !!!cp (221.1);
2988            $self->{state} = CDATA_SECTION_MSE1_STATE;
2989            !!!next-input-character;
2990            redo A;
2991          } elsif ($self->{nc} == -1) {
2992            $self->{state} = DATA_STATE;
2993            !!!next-input-character;
2994            if (length $self->{ct}->{data}) { # character
2995              !!!cp (221.2);
2996              !!!emit ($self->{ct}); # character
2997            } else {
2998              !!!cp (221.3);
2999              ## No token to emit. $self->{ct} is discarded.
3000            }        
3001            redo A;
3002          } else {
3003            !!!cp (221.4);
3004            $self->{ct}->{data} .= chr $self->{nc};
3005            $self->{read_until}->($self->{ct}->{data},
3006                                  q<]>,
3007                                  length $self->{ct}->{data});
3008    
3009  sub _tokenize_attempt_to_consume_an_entity ($$) {          ## Stay in the state.
3010    my ($self, $in_attr) = @_;          !!!next-input-character;
3011            redo A;
3012          }
3013    
3014    if ({        ## ISSUE: "text tokens" in spec.
3015         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
3016         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{nc} == 0x005D) { # ]
3017        }->{$self->{next_input_character}}) {          !!!cp (221.5);
3018      ## Don't consume          $self->{state} = CDATA_SECTION_MSE2_STATE;
3019      ## No error          !!!next-input-character;
3020      return undef;          redo A;
3021    } elsif ($self->{next_input_character} == 0x0023) { # #        } else {
3022      !!!next-input-character;          !!!cp (221.6);
3023      if ($self->{next_input_character} == 0x0078 or # x          $self->{ct}->{data} .= ']';
3024          $self->{next_input_character} == 0x0058) { # X          $self->{state} = CDATA_SECTION_STATE;
3025        my $code;          ## Reconsume.
3026        X: {          redo A;
3027          my $x_char = $self->{next_input_character};        }
3028          !!!next-input-character;      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3029          if (0x0030 <= $self->{next_input_character} and        if ($self->{nc} == 0x003E) { # >
3030              $self->{next_input_character} <= 0x0039) { # 0..9          $self->{state} = DATA_STATE;
3031            $code ||= 0;          !!!next-input-character;
3032            $code *= 0x10;          if (length $self->{ct}->{data}) { # character
3033            $code += $self->{next_input_character} - 0x0030;            !!!cp (221.7);
3034            redo X;            !!!emit ($self->{ct}); # character
         } elsif (0x0061 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0066) { # a..f  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_input_character} and  
                  $self->{next_input_character} <= 0x0046) { # A..F  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_input_character} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!parse-error (type => 'bare hcro');  
           !!!back-next-input-character ($x_char, $self->{next_input_character});  
           $self->{next_input_character} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_input_character} == 0x003B) { # ;  
           !!!next-input-character;  
3035          } else {          } else {
3036            !!!parse-error (type => 'no refc');            !!!cp (221.8);
3037              ## No token to emit. $self->{ct} is discarded.
3038          }          }
3039            redo A;
3040          } elsif ($self->{nc} == 0x005D) { # ]
3041            !!!cp (221.9); # character
3042            $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3043            ## Stay in the state.
3044            !!!next-input-character;
3045            redo A;
3046          } else {
3047            !!!cp (221.11);
3048            $self->{ct}->{data} .= ']]'; # character
3049            $self->{state} = CDATA_SECTION_STATE;
3050            ## Reconsume.
3051            redo A;
3052          }
3053        } elsif ($self->{state} == ENTITY_STATE) {
3054          if ($is_space->{$self->{nc}} or
3055              {
3056                0x003C => 1, 0x0026 => 1, -1 => 1, # <, &
3057                $self->{entity_add} => 1,
3058              }->{$self->{nc}}) {
3059            !!!cp (1001);
3060            ## Don't consume
3061            ## No error
3062            ## Return nothing.
3063            #
3064          } elsif ($self->{nc} == 0x0023) { # #
3065            !!!cp (999);
3066            $self->{state} = ENTITY_HASH_STATE;
3067            $self->{s_kwd} = '#';
3068            !!!next-input-character;
3069            redo A;
3070          } elsif ((0x0041 <= $self->{nc} and
3071                    $self->{nc} <= 0x005A) or # A..Z
3072                   (0x0061 <= $self->{nc} and
3073                    $self->{nc} <= 0x007A)) { # a..z
3074            !!!cp (998);
3075            require Whatpm::_NamedEntityList;
3076            $self->{state} = ENTITY_NAME_STATE;
3077            $self->{s_kwd} = chr $self->{nc};
3078            $self->{entity__value} = $self->{s_kwd};
3079            $self->{entity__match} = 0;
3080            !!!next-input-character;
3081            redo A;
3082          } else {
3083            !!!cp (1027);
3084            !!!parse-error (type => 'bare ero');
3085            ## Return nothing.
3086            #
3087          }
3088    
3089          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3090            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## reference" algorithm.  In other word, there is an "&" character
3091            $code = 0xFFFD;        ## that does not introduce a character reference, which would be
3092          } elsif ($code > 0x10FFFF) {        ## appended to the parent element or the attribute value in later
3093            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        ## process of the tokenizer.
3094            $code = 0xFFFD;  
3095          } elsif ($code == 0x000D) {        if ($self->{prev_state} == DATA_STATE) {
3096            !!!parse-error (type => 'CR character reference');          !!!cp (997);
3097            $code = 0x000A;          $self->{state} = $self->{prev_state};
3098          } elsif (0x80 <= $code and $code <= 0x9F) {          ## Reconsume.
3099            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3100            $code = $c1_entity_char->{$code};                    line => $self->{line_prev},
3101          }                    column => $self->{column_prev},
3102                     });
3103          return {type => CHARACTER_TOKEN, data => chr $code};          redo A;
3104        } # X        } else {
3105      } elsif (0x0030 <= $self->{next_input_character} and          !!!cp (996);
3106               $self->{next_input_character} <= 0x0039) { # 0..9          $self->{ca}->{value} .= '&';
3107        my $code = $self->{next_input_character} - 0x0030;          $self->{state} = $self->{prev_state};
3108        !!!next-input-character;          ## Reconsume.
3109                  redo A;
3110        while (0x0030 <= $self->{next_input_character} and        }
3111                  $self->{next_input_character} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3112          $code *= 10;        if ($self->{nc} == 0x0078 or # x
3113          $code += $self->{next_input_character} - 0x0030;            $self->{nc} == 0x0058) { # X
3114            !!!cp (995);
3115            $self->{state} = HEXREF_X_STATE;
3116            $self->{s_kwd} .= chr $self->{nc};
3117            !!!next-input-character;
3118            redo A;
3119          } elsif (0x0030 <= $self->{nc} and
3120                   $self->{nc} <= 0x0039) { # 0..9
3121            !!!cp (994);
3122            $self->{state} = NCR_NUM_STATE;
3123            $self->{s_kwd} = $self->{nc} - 0x0030;
3124            !!!next-input-character;
3125            redo A;
3126          } else {
3127            !!!parse-error (type => 'bare nero',
3128                            line => $self->{line_prev},
3129                            column => $self->{column_prev} - 1);
3130    
3131            ## NOTE: According to the spec algorithm, nothing is returned,
3132            ## and then "&#" is appended to the parent element or the attribute
3133            ## value in the later processing.
3134    
3135            if ($self->{prev_state} == DATA_STATE) {
3136              !!!cp (1019);
3137              $self->{state} = $self->{prev_state};
3138              ## Reconsume.
3139              !!!emit ({type => CHARACTER_TOKEN,
3140                        data => '&#',
3141                        line => $self->{line_prev},
3142                        column => $self->{column_prev} - 1,
3143                       });
3144              redo A;
3145            } else {
3146              !!!cp (993);
3147              $self->{ca}->{value} .= '&#';
3148              $self->{state} = $self->{prev_state};
3149              ## Reconsume.
3150              redo A;
3151            }
3152          }
3153        } elsif ($self->{state} == NCR_NUM_STATE) {
3154          if (0x0030 <= $self->{nc} and
3155              $self->{nc} <= 0x0039) { # 0..9
3156            !!!cp (1012);
3157            $self->{s_kwd} *= 10;
3158            $self->{s_kwd} += $self->{nc} - 0x0030;
3159                    
3160            ## Stay in the state.
3161            !!!next-input-character;
3162            redo A;
3163          } elsif ($self->{nc} == 0x003B) { # ;
3164            !!!cp (1013);
3165          !!!next-input-character;          !!!next-input-character;
3166            #
3167          } else {
3168            !!!cp (1014);
3169            !!!parse-error (type => 'no refc');
3170            ## Reconsume.
3171            #
3172          }
3173    
3174          my $code = $self->{s_kwd};
3175          my $l = $self->{line_prev};
3176          my $c = $self->{column_prev};
3177          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3178            !!!cp (1015);
3179            !!!parse-error (type => 'invalid character reference',
3180                            text => (sprintf 'U+%04X', $code),
3181                            line => $l, column => $c);
3182            $code = 0xFFFD;
3183          } elsif ($code > 0x10FFFF) {
3184            !!!cp (1016);
3185            !!!parse-error (type => 'invalid character reference',
3186                            text => (sprintf 'U-%08X', $code),
3187                            line => $l, column => $c);
3188            $code = 0xFFFD;
3189          } elsif ($code == 0x000D) {
3190            !!!cp (1017);
3191            !!!parse-error (type => 'CR character reference',
3192                            line => $l, column => $c);
3193            $code = 0x000A;
3194          } elsif (0x80 <= $code and $code <= 0x9F) {
3195            !!!cp (1018);
3196            !!!parse-error (type => 'C1 character reference',
3197                            text => (sprintf 'U+%04X', $code),
3198                            line => $l, column => $c);
3199            $code = $c1_entity_char->{$code};
3200        }        }
3201    
3202        if ($self->{next_input_character} == 0x003B) { # ;        if ($self->{prev_state} == DATA_STATE) {
3203            !!!cp (992);
3204            $self->{state} = $self->{prev_state};
3205            ## Reconsume.
3206            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3207                      line => $l, column => $c,
3208                     });
3209            redo A;
3210          } else {
3211            !!!cp (991);
3212            $self->{ca}->{value} .= chr $code;
3213            $self->{ca}->{has_reference} = 1;
3214            $self->{state} = $self->{prev_state};
3215            ## Reconsume.
3216            redo A;
3217          }
3218        } elsif ($self->{state} == HEXREF_X_STATE) {
3219          if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3220              (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3221              (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3222            # 0..9, A..F, a..f
3223            !!!cp (990);
3224            $self->{state} = HEXREF_HEX_STATE;
3225            $self->{s_kwd} = 0;
3226            ## Reconsume.
3227            redo A;
3228          } else {
3229            !!!parse-error (type => 'bare hcro',
3230                            line => $self->{line_prev},
3231                            column => $self->{column_prev} - 2);
3232    
3233            ## NOTE: According to the spec algorithm, nothing is returned,
3234            ## and then "&#" followed by "X" or "x" is appended to the parent
3235            ## element or the attribute value in the later processing.
3236    
3237            if ($self->{prev_state} == DATA_STATE) {
3238              !!!cp (1005);
3239              $self->{state} = $self->{prev_state};
3240              ## Reconsume.
3241              !!!emit ({type => CHARACTER_TOKEN,
3242                        data => '&' . $self->{s_kwd},
3243                        line => $self->{line_prev},
3244                        column => $self->{column_prev} - length $self->{s_kwd},
3245                       });
3246              redo A;
3247            } else {
3248              !!!cp (989);
3249              $self->{ca}->{value} .= '&' . $self->{s_kwd};
3250              $self->{state} = $self->{prev_state};
3251              ## Reconsume.
3252              redo A;
3253            }
3254          }
3255        } elsif ($self->{state} == HEXREF_HEX_STATE) {
3256          if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3257            # 0..9
3258            !!!cp (1002);
3259            $self->{s_kwd} *= 0x10;
3260            $self->{s_kwd} += $self->{nc} - 0x0030;
3261            ## Stay in the state.
3262            !!!next-input-character;
3263            redo A;
3264          } elsif (0x0061 <= $self->{nc} and
3265                   $self->{nc} <= 0x0066) { # a..f
3266            !!!cp (1003);
3267            $self->{s_kwd} *= 0x10;
3268            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3269            ## Stay in the state.
3270            !!!next-input-character;
3271            redo A;
3272          } elsif (0x0041 <= $self->{nc} and
3273                   $self->{nc} <= 0x0046) { # A..F
3274            !!!cp (1004);
3275            $self->{s_kwd} *= 0x10;
3276            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3277            ## Stay in the state.
3278          !!!next-input-character;          !!!next-input-character;
3279            redo A;
3280          } elsif ($self->{nc} == 0x003B) { # ;
3281            !!!cp (1006);
3282            !!!next-input-character;
3283            #
3284        } else {        } else {
3285          !!!parse-error (type => 'no refc');          !!!cp (1007);
3286            !!!parse-error (type => 'no refc',
3287                            line => $self->{line},
3288                            column => $self->{column});
3289            ## Reconsume.
3290            #
3291        }        }
3292    
3293          my $code = $self->{s_kwd};
3294          my $l = $self->{line_prev};
3295          my $c = $self->{column_prev};
3296        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3297          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!cp (1008);
3298            !!!parse-error (type => 'invalid character reference',
3299                            text => (sprintf 'U+%04X', $code),
3300                            line => $l, column => $c);
3301          $code = 0xFFFD;          $code = 0xFFFD;
3302        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3303          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!cp (1009);
3304            !!!parse-error (type => 'invalid character reference',
3305                            text => (sprintf 'U-%08X', $code),
3306                            line => $l, column => $c);
3307          $code = 0xFFFD;          $code = 0xFFFD;
3308        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3309          !!!parse-error (type => 'CR character reference');          !!!cp (1010);
3310            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3311          $code = 0x000A;          $code = 0x000A;
3312        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3313          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!cp (1011);
3314            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3315          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3316        }        }
3317          
3318        return {type => CHARACTER_TOKEN, data => chr $code};        if ($self->{prev_state} == DATA_STATE) {
3319      } else {          !!!cp (988);
3320        !!!parse-error (type => 'bare nero');          $self->{state} = $self->{prev_state};
3321        !!!back-next-input-character ($self->{next_input_character});          ## Reconsume.
3322        $self->{next_input_character} = 0x0023; # #          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3323        return undef;                    line => $l, column => $c,
3324      }                   });
3325    } elsif ((0x0041 <= $self->{next_input_character} and          redo A;
3326              $self->{next_input_character} <= 0x005A) or        } else {
3327             (0x0061 <= $self->{next_input_character} and          !!!cp (987);
3328              $self->{next_input_character} <= 0x007A)) {          $self->{ca}->{value} .= chr $code;
3329      my $entity_name = chr $self->{next_input_character};          $self->{ca}->{has_reference} = 1;
3330      !!!next-input-character;          $self->{state} = $self->{prev_state};
3331            ## Reconsume.
3332      my $value = $entity_name;          redo A;
3333      my $match = 0;        }
3334      require Whatpm::_NamedEntityList;      } elsif ($self->{state} == ENTITY_NAME_STATE) {
3335      our $EntityChar;        if (length $self->{s_kwd} < 30 and
3336              ## NOTE: Some number greater than the maximum length of entity name
3337      while (length $entity_name < 10 and            ((0x0041 <= $self->{nc} and # a
3338             ## NOTE: Some number greater than the maximum length of entity name              $self->{nc} <= 0x005A) or # x
3339             ((0x0041 <= $self->{next_input_character} and # a             (0x0061 <= $self->{nc} and # a
3340               $self->{next_input_character} <= 0x005A) or # x              $self->{nc} <= 0x007A) or # z
3341              (0x0061 <= $self->{next_input_character} and # a             (0x0030 <= $self->{nc} and # 0
3342               $self->{next_input_character} <= 0x007A) or # z              $self->{nc} <= 0x0039) or # 9
3343              (0x0030 <= $self->{next_input_character} and # 0             $self->{nc} == 0x003B)) { # ;
3344               $self->{next_input_character} <= 0x0039) or # 9          our $EntityChar;
3345              $self->{next_input_character} == 0x003B)) { # ;          $self->{s_kwd} .= chr $self->{nc};
3346        $entity_name .= chr $self->{next_input_character};          if (defined $EntityChar->{$self->{s_kwd}}) {
3347        if (defined $EntityChar->{$entity_name}) {            if ($self->{nc} == 0x003B) { # ;
3348          if ($self->{next_input_character} == 0x003B) { # ;              !!!cp (1020);
3349            $value = $EntityChar->{$entity_name};              $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3350            $match = 1;              $self->{entity__match} = 1;
3351            !!!next-input-character;              !!!next-input-character;
3352            last;              #
3353              } else {
3354                !!!cp (1021);
3355                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3356                $self->{entity__match} = -1;
3357                ## Stay in the state.
3358                !!!next-input-character;
3359                redo A;
3360              }
3361          } else {          } else {
3362            $value = $EntityChar->{$entity_name};            !!!cp (1022);
3363            $match = -1;            $self->{entity__value} .= chr $self->{nc};
3364              $self->{entity__match} *= 2;
3365              ## Stay in the state.
3366            !!!next-input-character;            !!!next-input-character;
3367              redo A;
3368          }          }
       } else {  
         $value .= chr $self->{next_input_character};  
         $match *= 2;  
         !!!next-input-character;  
3369        }        }
3370      }  
3371              my $data;
3372      if ($match > 0) {        my $has_ref;
3373        return {type => CHARACTER_TOKEN, data => $value};        if ($self->{entity__match} > 0) {
3374      } elsif ($match < 0) {          !!!cp (1023);
3375        !!!parse-error (type => 'no refc');          $data = $self->{entity__value};
3376        if ($in_attr and $match < -1) {          $has_ref = 1;
3377          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          #
3378          } elsif ($self->{entity__match} < 0) {
3379            !!!parse-error (type => 'no refc');
3380            if ($self->{prev_state} != DATA_STATE and # in attribute
3381                $self->{entity__match} < -1) {
3382              !!!cp (1024);
3383              $data = '&' . $self->{s_kwd};
3384              #
3385            } else {
3386              !!!cp (1025);
3387              $data = $self->{entity__value};
3388              $has_ref = 1;
3389              #
3390            }
3391        } else {        } else {
3392          return {type => CHARACTER_TOKEN, data => $value};          !!!cp (1026);
3393            !!!parse-error (type => 'bare ero',
3394                            line => $self->{line_prev},
3395                            column => $self->{column_prev} - length $self->{s_kwd});
3396            $data = '&' . $self->{s_kwd};
3397            #
3398          }
3399      
3400          ## NOTE: In these cases, when a character reference is found,
3401          ## it is consumed and a character token is returned, or, otherwise,
3402          ## nothing is consumed and returned, according to the spec algorithm.
3403          ## In this implementation, anything that has been examined by the
3404          ## tokenizer is appended to the parent element or the attribute value
3405          ## as string, either literal string when no character reference or
3406          ## entity-replaced string otherwise, in this stage, since any characters
3407          ## that would not be consumed are appended in the data state or in an
3408          ## appropriate attribute value state anyway.
3409    
3410          if ($self->{prev_state} == DATA_STATE) {
3411            !!!cp (986);
3412            $self->{state} = $self->{prev_state};
3413            ## Reconsume.
3414            !!!emit ({type => CHARACTER_TOKEN,
3415                      data => $data,
3416                      line => $self->{line_prev},
3417                      column => $self->{column_prev} + 1 - length $self->{s_kwd},
3418                     });
3419            redo A;
3420          } else {
3421            !!!cp (985);
3422            $self->{ca}->{value} .= $data;
3423            $self->{ca}->{has_reference} = 1 if $has_ref;
3424            $self->{state} = $self->{prev_state};
3425            ## Reconsume.
3426            redo A;
3427        }        }
3428      } else {      } else {
3429        !!!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};  
3430      }      }
3431    } else {    } # A  
3432      ## no characters are consumed  
3433      !!!parse-error (type => 'bare ero');    die "$0: _get_next_token: unexpected case";
3434      return undef;  } # _get_next_token
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3435    
3436  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3437    my $self = shift;    my $self = shift;
# Line 1951  sub _initialize_tree_constructor ($) { Line 3440  sub _initialize_tree_constructor ($) {
3440    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3441    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3442    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3443      $self->{document}->set_user_data (manakai_source_line => 1);
3444      $self->{document}->set_user_data (manakai_source_column => 1);
3445  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3446    
3447  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 1977  sub _construct_tree ($) { Line 3468  sub _construct_tree ($) {
3468        
3469    !!!next-token;    !!!next-token;
3470    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3471    undef $self->{form_element};    undef $self->{form_element};
3472    undef $self->{head_element};    undef $self->{head_element};
3473    $self->{open_elements} = [];    $self->{open_elements} = [];
3474    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3475    
3476      ## NOTE: The "initial" insertion mode.
3477    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3478    
3479      ## NOTE: The "before html" insertion mode.
3480    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3481      $self->{insertion_mode} = BEFORE_HEAD_IM;
3482    
3483      ## NOTE: The "before head" insertion mode and so on.
3484    $self->_tree_construction_main;    $self->_tree_construction_main;
3485  } # _construct_tree  } # _construct_tree
3486    
3487  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3488    my $self = shift;    my $self = shift;
3489    
3490      ## NOTE: "initial" insertion mode
3491    
3492    INITIAL: {    INITIAL: {
3493      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3494        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 1997  sub _tree_construction_initial ($) { Line 3496  sub _tree_construction_initial ($) {
3496        ## language.        ## language.
3497        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3498        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3499        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3500        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3501            defined $token->{public_identifier} or            defined $token->{sysid}) {
3502            defined $token->{system_identifier}) {          !!!cp ('t1');
3503          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3504        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3505          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3506          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3507          } elsif (defined $token->{pubid}) {
3508            if ($token->{pubid} eq 'XSLT-compat') {
3509              !!!cp ('t1.2');
3510              !!!parse-error (type => 'XSLT-compat', token => $token,
3511                              level => $self->{level}->{should});
3512            } else {
3513              !!!parse-error (type => 'not HTML5', token => $token);
3514            }
3515          } else {
3516            !!!cp ('t3');
3517            #
3518        }        }
3519                
3520        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3521          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3522        $doctype->public_id ($token->{public_identifier})        ## NOTE: Default value for both |public_id| and |system_id| attributes
3523            if defined $token->{public_identifier};        ## are empty strings, so that we don't set any value in missing cases.
3524        $doctype->system_id ($token->{system_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3525            if defined $token->{system_identifier};        $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
3526        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
3527        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3528        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3529                
3530        if (not $token->{correct} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3531            !!!cp ('t4');
3532          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3533        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
3534          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
3535          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3536          if ({          my $prefix = [
3537            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3538            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3539            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3540            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3541            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3542            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3543            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3544            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3545            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3546            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3547            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3548            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3549            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3550            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3551            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3552            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3553            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3554            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3555            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3556            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3557            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3558            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3559            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3560            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3561            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3562            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3563            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3564            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3565            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3566            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3567            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3568            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3569            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3570            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3571            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3572            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3573            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3574            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3575            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3576            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3577            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3578            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3579            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3580            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3581            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3582            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3583            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3584            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3585            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3586            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3587            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3588            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//W3C//DTD W3 HTML//",
3589            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3590            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3591            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3592            "-//W3C//DTD HTML 3.2//EN" => 1,          ]; # $prefix
3593            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,          my $match;
3594            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,          for (@$prefix) {
3595            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3596            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,              $match = 1;
3597            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,              last;
3598            "-//W3C//DTD W3 HTML//EN" => 1,            }
3599            "-//W3O//DTD W3 HTML 3.0//EN" => 1,          }
3600            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,          if ($match or
3601            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3602            "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3603            "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,              $pubid eq "HTML") {
3604            "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,            !!!cp ('t5');
           "HTML" => 1,  
         }->{$pubid}) {  
3605            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3606          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3607                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3608            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
3609                !!!cp ('t6');
3610              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3611            } else {            } else {
3612                !!!cp ('t7');
3613              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3614            }            }
3615          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3616                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3617              !!!cp ('t8');
3618            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3619            } else {
3620              !!!cp ('t9');
3621          }          }
3622          } else {
3623            !!!cp ('t10');
3624        }        }
3625        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
3626          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
3627          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3628          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") {
3629              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3630              ## marked as quirks.
3631            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3632              !!!cp ('t11');
3633            } else {
3634              !!!cp ('t12');
3635          }          }
3636          } else {
3637            !!!cp ('t13');
3638        }        }
3639                
3640        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3641        !!!next-token;        !!!next-token;
3642        return;        return;
3643      } elsif ({      } elsif ({
# Line 2122  sub _tree_construction_initial ($) { Line 3645  sub _tree_construction_initial ($) {
3645                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3646                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3647               }->{$token->{type}}) {               }->{$token->{type}}) {
3648        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3649          !!!parse-error (type => 'no DOCTYPE', token => $token);
3650        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3651        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3652        ## reprocess        ## reprocess
3653          !!!ack-later;
3654        return;        return;
3655      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3656        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3657          ## Ignore the token          ## Ignore the token
3658    
3659          unless (length $token->{data}) {          unless (length $token->{data}) {
3660            ## Stay in the phase            !!!cp ('t15');
3661              ## Stay in the insertion mode.
3662            !!!next-token;            !!!next-token;
3663            redo INITIAL;            redo INITIAL;
3664            } else {
3665              !!!cp ('t16');
3666          }          }
3667          } else {
3668            !!!cp ('t17');
3669        }        }
3670    
3671        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3672        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3673        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3674        ## reprocess        ## reprocess
3675        return;        return;
3676      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3677          !!!cp ('t18');
3678        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3679        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3680                
3681        ## Stay in the phase.        ## Stay in the insertion mode.
3682        !!!next-token;        !!!next-token;
3683        redo INITIAL;        redo INITIAL;
3684      } else {      } else {
3685        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3686      }      }
3687    } # INITIAL    } # INITIAL
3688    
3689      die "$0: _tree_construction_initial: This should be never reached";
3690  } # _tree_construction_initial  } # _tree_construction_initial
3691    
3692  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3693    my $self = shift;    my $self = shift;
3694    
3695      ## NOTE: "before html" insertion mode.
3696        
3697    B: {    B: {
3698        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3699          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3700            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3701          ## Ignore the token          ## Ignore the token
3702          ## Stay in the phase          ## Stay in the insertion mode.
3703          !!!next-token;          !!!next-token;
3704          redo B;          redo B;
3705        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3706            !!!cp ('t20');
3707          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3708          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3709          ## Stay in the phase          ## Stay in the insertion mode.
3710          !!!next-token;          !!!next-token;
3711          redo B;          redo B;
3712        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3713          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3714            ## Ignore the token.            ## Ignore the token.
3715    
3716            unless (length $token->{data}) {            unless (length $token->{data}) {
3717              ## Stay in the phase              !!!cp ('t21');
3718                ## Stay in the insertion mode.
3719              !!!next-token;              !!!next-token;
3720              redo B;              redo B;
3721              } else {
3722                !!!cp ('t22');
3723            }            }
3724            } else {
3725              !!!cp ('t23');
3726          }          }
3727    
3728          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3729    
3730          #          #
3731        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3732          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3733              $token->{attributes}->{manifest}) { ## ISSUE: Spec spells as "application"            my $root_element;
3734            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3735                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3736            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3737                  [$root_element, $el_category->{html}];
3738    
3739              if ($token->{attributes}->{manifest}) {
3740                !!!cp ('t24');
3741                $self->{application_cache_selection}
3742                    ->($token->{attributes}->{manifest}->{value});
3743                ## ISSUE: Spec is unclear on relative references.
3744                ## According to Hixie (#whatwg 2008-03-19), it should be
3745                ## resolved against the base URI of the document in HTML
3746                ## or xml:base of the element in XHTML.
3747              } else {
3748                !!!cp ('t25');
3749                $self->{application_cache_selection}->(undef);
3750              }
3751    
3752              !!!nack ('t25c');
3753    
3754              !!!next-token;
3755              return; ## Go to the "before head" insertion mode.
3756          } else {          } else {
3757            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3758              #
3759          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3760        } elsif ({        } elsif ({
3761                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3762                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3763                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3764          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3765          #          #
3766        } else {        } else {
3767          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3768        }        }
3769    
3770        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3771        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3772        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3773        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3774        #redo B;  
3775        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3776    
3777        ## NOTE: Reprocess the token.
3778        !!!ack-later;
3779        return; ## Go to the "before head" insertion mode.
3780    
3781        ## ISSUE: There is an issue in the spec
3782    } # B    } # B
3783    
3784      die "$0: _tree_construction_root_element: This should never be reached";
3785  } # _tree_construction_root_element  } # _tree_construction_root_element
3786    
3787  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2231  sub _reset_insertion_mode ($) { Line 3796  sub _reset_insertion_mode ($) {
3796            
3797      ## Step 3      ## Step 3
3798      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"!?  
3799        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3800          $last = 1;          $last = 1;
3801          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3802            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3803                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3804              #          } else {
3805            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3806          }          }
3807        }        }
3808              
3809        ## Step 4..13        ## Step 4..14
3810        my $new_mode = {        my $new_mode;
3811          if ($node->[1] & FOREIGN_EL) {
3812            !!!cp ('t28.1');
3813            ## NOTE: Strictly spaking, the line below only applies to MathML and
3814            ## SVG elements.  Currently the HTML syntax supports only MathML and
3815            ## SVG elements as foreigners.
3816            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3817          } elsif ($node->[1] & TABLE_CELL_EL) {
3818            if ($last) {
3819              !!!cp ('t28.2');
3820              #
3821            } else {
3822              !!!cp ('t28.3');
3823              $new_mode = IN_CELL_IM;
3824            }
3825          } else {
3826            !!!cp ('t28.4');
3827            $new_mode = {
3828                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3829                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3830                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3831                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3832                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3833                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2263  sub _reset_insertion_mode ($) { Line 3838  sub _reset_insertion_mode ($) {
3838                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3839                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3840                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3841                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3842          }
3843        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3844                
3845        ## Step 14        ## Step 15
3846        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3847          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3848              !!!cp ('t29');
3849            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3850          } else {          } else {
3851              ## ISSUE: Can this state be reached?
3852              !!!cp ('t30');
3853            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3854          }          }
3855          return;          return;
3856          } else {
3857            !!!cp ('t31');
3858        }        }
3859                
3860        ## Step 15        ## Step 16
3861        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3862                
3863        ## Step 16        ## Step 17
3864        $i--;        $i--;
3865        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3866                
3867        ## Step 17        ## Step 18
3868        redo S3;        redo S3;
3869      } # S3      } # S3
3870    
3871      die "$0: _reset_insertion_mode: This line should never be reached";
3872  } # _reset_insertion_mode  } # _reset_insertion_mode
3873    
3874  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2307  sub _tree_construction_main ($) { Line 3890  sub _tree_construction_main ($) {
3890      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3891      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3892        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3893            !!!cp ('t32');
3894          return;          return;
3895        }        }
3896      }      }
# Line 2321  sub _tree_construction_main ($) { Line 3905  sub _tree_construction_main ($) {
3905    
3906        ## Step 6        ## Step 6
3907        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3908            !!!cp ('t33_1');
3909          #          #
3910        } else {        } else {
3911          my $in_open_elements;          my $in_open_elements;
3912          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3913            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3914                !!!cp ('t33');
3915              $in_open_elements = 1;              $in_open_elements = 1;
3916              last OE;              last OE;
3917            }            }
3918          }          }
3919          if ($in_open_elements) {          if ($in_open_elements) {
3920              !!!cp ('t34');
3921            #            #
3922          } else {          } else {
3923              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3924              !!!cp ('t35');
3925            redo S4;            redo S4;
3926          }          }
3927        }        }
# Line 2355  sub _tree_construction_main ($) { Line 3944  sub _tree_construction_main ($) {
3944    
3945        ## Step 11        ## Step 11
3946        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3947            !!!cp ('t36');
3948          ## Step 7'          ## Step 7'
3949          $i++;          $i++;
3950          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3951                    
3952          redo S7;          redo S7;
3953        }        }
3954    
3955          !!!cp ('t37');
3956      } # S7      } # S7
3957    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3958    
3959    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3960      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3961        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3962            !!!cp ('t38');
3963          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3964          return;          return;
3965        }        }
3966      }      }
3967    
3968        !!!cp ('t39');
3969    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3970    
3971    my $parse_rcdata = sub ($$) {    my $insert;
3972      my ($content_model_flag, $insert) = @_;  
3973      my $parse_rcdata = sub ($) {
3974        my ($content_model_flag) = @_;
3975    
3976      ## Step 1      ## Step 1
3977      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3978      my $el;      my $el;
3979      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3980    
3981      ## Step 2      ## Step 2
3982      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3983    
3984      ## Step 3      ## Step 3
3985      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2390  sub _tree_construction_main ($) { Line 3987  sub _tree_construction_main ($) {
3987    
3988      ## Step 4      ## Step 4
3989      my $text = '';      my $text = '';
3990        !!!nack ('t40.1');
3991      !!!next-token;      !!!next-token;
3992      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3993          !!!cp ('t40');
3994        $text .= $token->{data};        $text .= $token->{data};
3995        !!!next-token;        !!!next-token;
3996      }      }
3997    
3998      ## Step 5      ## Step 5
3999      if (length $text) {      if (length $text) {
4000          !!!cp ('t41');
4001        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
4002        $el->append_child ($text);        $el->append_child ($text);
4003      }      }
# Line 2406  sub _tree_construction_main ($) { Line 4006  sub _tree_construction_main ($) {
4006      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
4007    
4008      ## Step 7      ## Step 7
4009      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
4010            $token->{tag_name} eq $start_tag_name) {
4011          !!!cp ('t42');
4012        ## 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});  
4013      } else {      } else {
4014        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
4015          if ($content_model_flag == CDATA_CONTENT_MODEL) {
4016            !!!cp ('t43');
4017            !!!parse-error (type => 'in CDATA:#eof', token => $token);
4018          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4019            !!!cp ('t44');
4020            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4021          } else {
4022            die "$0: $content_model_flag in parse_rcdata";
4023          }
4024      }      }
4025      !!!next-token;      !!!next-token;
4026    }; # $parse_rcdata    }; # $parse_rcdata
4027    
4028    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
4029      my $script_el;      my $script_el;
4030      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4031      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4032    
4033      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
4034      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4035            
4036      my $text = '';      my $text = '';
4037        !!!nack ('t45.1');
4038      !!!next-token;      !!!next-token;
4039      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
4040          !!!cp ('t45');
4041        $text .= $token->{data};        $text .= $token->{data};
4042        !!!next-token;        !!!next-token;
4043      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
4044      if (length $text) {      if (length $text) {
4045          !!!cp ('t46');
4046        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
4047      }      }
4048                                
# Line 2441  sub _tree_construction_main ($) { Line 4050  sub _tree_construction_main ($) {
4050    
4051      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
4052          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4053          !!!cp ('t47');
4054        ## Ignore the token        ## Ignore the token
4055      } else {      } else {
4056        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4057          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4058        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4059        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4060      }      }
4061            
4062      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4063          !!!cp ('t49');
4064        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4065      } else {      } else {
4066          !!!cp ('t50');
4067        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4068        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4069    
# Line 2464  sub _tree_construction_main ($) { Line 4077  sub _tree_construction_main ($) {
4077      !!!next-token;      !!!next-token;
4078    }; # $script_start_tag    }; # $script_start_tag
4079    
4080      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4081      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4082      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4083    
4084    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4085      my $tag_name = shift;      my $end_tag_token = shift;
4086        my $tag_name = $end_tag_token->{tag_name};
4087    
4088        ## NOTE: The adoption agency algorithm (AAA).
4089    
4090      FET: {      FET: {
4091        ## Step 1        ## Step 1
4092        my $formatting_element;        my $formatting_element;
4093        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4094        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4095          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4096              !!!cp ('t52');
4097              last AFE;
4098            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4099                         eq $tag_name) {
4100              !!!cp ('t51');
4101            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4102            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4103            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4104          }          }
4105        } # AFE        } # AFE
4106        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4107          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4108            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4109          ## Ignore the token          ## Ignore the token
4110          !!!next-token;          !!!next-token;
4111          return;          return;
# Line 2493  sub _tree_construction_main ($) { Line 4117  sub _tree_construction_main ($) {
4117          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4118          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4119            if ($in_scope) {            if ($in_scope) {
4120                !!!cp ('t54');
4121              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4122              last INSCOPE;              last INSCOPE;
4123            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4124              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4125                !!!parse-error (type => 'unmatched end tag',
4126                                text => $token->{tag_name},
4127                                token => $end_tag_token);
4128              ## Ignore the token              ## Ignore the token
4129              !!!next-token;              !!!next-token;
4130              return;              return;
4131            }            }
4132          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4133                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4134            $in_scope = 0;            $in_scope = 0;
4135          }          }
4136        } # INSCOPE        } # INSCOPE
4137        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4138          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4139            !!!parse-error (type => 'unmatched end tag',
4140                            text => $token->{tag_name},
4141                            token => $end_tag_token);
4142          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4143          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4144          return;          return;
4145        }        }
4146        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4147          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4148            !!!parse-error (type => 'not closed',
4149                            text => $self->{open_elements}->[-1]->[0]
4150                                ->manakai_local_name,
4151                            token => $end_tag_token);
4152        }        }
4153                
4154        ## Step 2        ## Step 2
# Line 2523  sub _tree_construction_main ($) { Line 4156  sub _tree_construction_main ($) {
4156        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4157        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4158          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4159          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4160              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4161              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4162               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4163              !!!cp ('t59');
4164            $furthest_block = $node;            $furthest_block = $node;
4165            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4166          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4167              !!!cp ('t60');
4168            last OE;            last OE;
4169          }          }
4170        } # OE        } # OE
4171                
4172        ## Step 3        ## Step 3
4173        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4174            !!!cp ('t61');
4175          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4176          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4177          !!!next-token;          !!!next-token;
# Line 2548  sub _tree_construction_main ($) { Line 4184  sub _tree_construction_main ($) {
4184        ## Step 5        ## Step 5
4185        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4186        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4187            !!!cp ('t62');
4188          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4189        }        }
4190                
# Line 2570  sub _tree_construction_main ($) { Line 4207  sub _tree_construction_main ($) {
4207          S7S2: {          S7S2: {
4208            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4209              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4210                  !!!cp ('t63');
4211                $node_i_in_active = $_;                $node_i_in_active = $_;
4212                last S7S2;                last S7S2;
4213              }              }
# Line 2583  sub _tree_construction_main ($) { Line 4221  sub _tree_construction_main ($) {
4221                    
4222          ## Step 4          ## Step 4
4223          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4224              !!!cp ('t64');
4225            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4226          }          }
4227                    
4228          ## Step 5          ## Step 5
4229          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4230              !!!cp ('t65');
4231            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4232            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4233            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 2605  sub _tree_construction_main ($) { Line 4245  sub _tree_construction_main ($) {
4245        } # S7          } # S7  
4246                
4247        ## Step 8        ## Step 8
4248        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4249            my $foster_parent_element;
4250            my $next_sibling;
4251            OE: for (reverse 0..$#{$self->{open_elements}}) {
4252              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4253                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4254                                 if (defined $parent and $parent->node_type == 1) {
4255                                   !!!cp ('t65.1');
4256                                   $foster_parent_element = $parent;
4257                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4258                                 } else {
4259                                   !!!cp ('t65.2');
4260                                   $foster_parent_element
4261                                     = $self->{open_elements}->[$_ - 1]->[0];
4262                                 }
4263                                 last OE;
4264                               }
4265                             } # OE
4266                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4267                               unless defined $foster_parent_element;
4268            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4269            $open_tables->[-1]->[1] = 1; # tainted
4270          } else {
4271            !!!cp ('t65.3');
4272            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4273          }
4274                
4275        ## Step 9        ## Step 9
4276        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 2622  sub _tree_construction_main ($) { Line 4287  sub _tree_construction_main ($) {
4287        my $i;        my $i;
4288        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4289          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4290              !!!cp ('t66');
4291            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4292            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4293          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4294              !!!cp ('t67');
4295            $i = $_;            $i = $_;
4296          }          }
4297        } # AFE        } # AFE
# Line 2634  sub _tree_construction_main ($) { Line 4301  sub _tree_construction_main ($) {
4301        undef $i;        undef $i;
4302        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4303          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4304              !!!cp ('t68');
4305            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4306            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4307          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4308              !!!cp ('t69');
4309            $i = $_;            $i = $_;
4310          }          }
4311        } # OE        } # OE
# Line 2647  sub _tree_construction_main ($) { Line 4316  sub _tree_construction_main ($) {
4316      } # FET      } # FET
4317    }; # $formatting_end_tag    }; # $formatting_end_tag
4318    
4319    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4320      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4321    }; # $insert_to_current    }; # $insert_to_current
4322    
4323    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4324                         my $child = shift;      my $child = shift;
4325                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4326                              table => 1, tbody => 1, tfoot => 1,        # MUST
4327                              thead => 1, tr => 1,        my $foster_parent_element;
4328                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4329                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4330                           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') {  
4331                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4332                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4333                                   !!!cp ('t70');
4334                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4335                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4336                               } else {                               } else {
4337                                   !!!cp ('t71');
4338                                 $foster_parent_element                                 $foster_parent_element
4339                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4340                               }                               }
# Line 2677  sub _tree_construction_main ($) { Line 4345  sub _tree_construction_main ($) {
4345                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4346                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4347                             ($child, $next_sibling);                             ($child, $next_sibling);
4348                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4349                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4350                         }        !!!cp ('t72');
4351          $self->{open_elements}->[-1]->[0]->append_child ($child);
4352        }
4353    }; # $insert_to_foster    }; # $insert_to_foster
4354    
4355    my $insert;    B: while (1) {
   
   B: {  
4356      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4357        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
4358          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4359        ## Ignore the token        ## Ignore the token
4360        ## Stay in the phase        ## Stay in the phase
4361        !!!next-token;        !!!next-token;
4362        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;  
4363      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4364               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4365        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4366          ## Turn into the main phase          !!!cp ('t79');
4367          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4368          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4369        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4370          ## Turn into the main phase          !!!cp ('t80');
4371          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4372          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4373          } else {
4374            !!!cp ('t81');
4375        }        }
4376    
4377  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4378  ## 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');  
       }  
4379        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4380        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4381          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4382              !!!cp ('t84');
4383            $top_el->set_attribute_ns            $top_el->set_attribute_ns
4384              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
4385               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4386          }          }
4387        }        }
4388          !!!nack ('t84.1');
4389        !!!next-token;        !!!next-token;
4390        redo B;        next B;
4391      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4392        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4393        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4394            !!!cp ('t85');
4395          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
4396        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4397            !!!cp ('t86');
4398          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
4399        } else {        } else {
4400            !!!cp ('t87');
4401          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4402        }        }
4403        !!!next-token;        !!!next-token;
4404        redo B;        next B;
4405      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4406        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4407          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          !!!cp ('t87.1');
4408            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4409            !!!next-token;
4410            next B;
4411          } elsif ($token->{type} == START_TAG_TOKEN) {
4412            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4413                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4414                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4415                ($token->{tag_name} eq 'svg' and
4416                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4417              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4418              !!!cp ('t87.2');
4419              #
4420            } elsif ({
4421                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4422                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4423                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4424                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4425                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4426                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4427                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4428                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4429                     }->{$token->{tag_name}}) {
4430              !!!cp ('t87.2');
4431              !!!parse-error (type => 'not closed',
4432                              text => $self->{open_elements}->[-1]->[0]
4433                                  ->manakai_local_name,
4434                              token => $token);
4435    
4436              pop @{$self->{open_elements}}
4437                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4438    
4439              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4440              ## Reprocess.
4441              next B;
4442            } else {
4443              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4444              my $tag_name = $token->{tag_name};
4445              if ($nsuri eq $SVG_NS) {
4446                $tag_name = {
4447                   altglyph => 'altGlyph',
4448                   altglyphdef => 'altGlyphDef',
4449                   altglyphitem => 'altGlyphItem',
4450                   animatecolor => 'animateColor',
4451                   animatemotion => 'animateMotion',
4452                   animatetransform => 'animateTransform',
4453                   clippath => 'clipPath',
4454                   feblend => 'feBlend',
4455                   fecolormatrix => 'feColorMatrix',
4456                   fecomponenttransfer => 'feComponentTransfer',
4457                   fecomposite => 'feComposite',
4458                   feconvolvematrix => 'feConvolveMatrix',
4459                   fediffuselighting => 'feDiffuseLighting',
4460                   fedisplacementmap => 'feDisplacementMap',
4461                   fedistantlight => 'feDistantLight',
4462                   feflood => 'feFlood',
4463                   fefunca => 'feFuncA',
4464                   fefuncb => 'feFuncB',
4465                   fefuncg => 'feFuncG',
4466                   fefuncr => 'feFuncR',
4467                   fegaussianblur => 'feGaussianBlur',
4468                   feimage => 'feImage',
4469                   femerge => 'feMerge',
4470                   femergenode => 'feMergeNode',
4471                   femorphology => 'feMorphology',
4472                   feoffset => 'feOffset',
4473                   fepointlight => 'fePointLight',
4474                   fespecularlighting => 'feSpecularLighting',
4475                   fespotlight => 'feSpotLight',
4476                   fetile => 'feTile',
4477                   feturbulence => 'feTurbulence',
4478                   foreignobject => 'foreignObject',
4479                   glyphref => 'glyphRef',
4480                   lineargradient => 'linearGradient',
4481                   radialgradient => 'radialGradient',
4482                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4483                   textpath => 'textPath',  
4484                }->{$tag_name} || $tag_name;
4485              }
4486    
4487              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4488    
4489              ## "adjust foreign attributes" - done in insert-element-f
4490    
4491              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4492    
4493              if ($self->{self_closing}) {
4494                pop @{$self->{open_elements}};
4495                !!!ack ('t87.3');
4496              } else {
4497                !!!cp ('t87.4');
4498              }
4499    
4500              !!!next-token;
4501              next B;
4502            }
4503          } elsif ($token->{type} == END_TAG_TOKEN) {
4504            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4505            !!!cp ('t87.5');
4506            #
4507          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4508            !!!cp ('t87.6');
4509            !!!parse-error (type => 'not closed',
4510                            text => $self->{open_elements}->[-1]->[0]
4511                                ->manakai_local_name,
4512                            token => $token);
4513    
4514            pop @{$self->{open_elements}}
4515                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4516    
4517            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4518            ## Reprocess.
4519            next B;
4520          } else {
4521            die "$0: $token->{type}: Unknown token type";        
4522          }
4523        }
4524    
4525        if ($self->{insertion_mode} & HEAD_IMS) {
4526          if ($token->{type} == CHARACTER_TOKEN) {
4527            if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4528              unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4529                !!!cp ('t88.2');
4530                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4531                #
4532              } else {
4533                !!!cp ('t88.1');
4534                ## Ignore the token.
4535                #
4536              }
4537            unless (length $token->{data}) {            unless (length $token->{data}) {
4538                !!!cp ('t88');
4539              !!!next-token;              !!!next-token;
4540              redo B;              next B;
4541            }            }
4542    ## TODO: set $token->{column} appropriately
4543          }          }
4544    
4545          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4546              !!!cp ('t89');
4547            ## As if <head>            ## As if <head>
4548            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4549            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4550            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4551                  [$self->{head_element}, $el_category->{head}];
4552    
4553            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4554            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4555    
4556            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4557          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4558              !!!cp ('t90');
4559            ## As if </noscript>            ## As if </noscript>
4560            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4561            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4562                        
4563            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4564            ## As if </head>            ## As if </head>
# Line 2788  sub _tree_construction_main ($) { Line 4566  sub _tree_construction_main ($) {
4566    
4567            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4568          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4569              !!!cp ('t91');
4570            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4571    
4572            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4573            } else {
4574              !!!cp ('t92');
4575          }          }
4576    
4577              ## "after head" insertion mode          ## "after head" insertion mode
4578              ## As if <body>          ## As if <body>
4579              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4580              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4581              ## reprocess          ## reprocess
4582              redo B;          next B;
4583            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4584              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4585                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4586                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4587                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4588                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4589                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4590                  !!!next-token;              push @{$self->{open_elements}},
4591                  redo B;                  [$self->{head_element}, $el_category->{head}];
4592                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4593                  #              !!!nack ('t93.1');
4594                } else {              !!!next-token;
4595                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4596                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4597                  !!!next-token;              !!!cp ('t93.2');
4598                  redo B;              !!!parse-error (type => 'after head', text => 'head',
4599                }                              token => $token);
4600              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              ## Ignore the token
4601                ## As if <head>              !!!nack ('t93.3');
4602                !!!create-element ($self->{head_element}, 'head');              !!!next-token;
4603                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              next B;
4604                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
4605                !!!cp ('t95');
4606                !!!parse-error (type => 'in head:head',
4607                                token => $token); # or in head noscript
4608                ## Ignore the token
4609                !!!nack ('t95.1');
4610                !!!next-token;
4611                next B;
4612              }
4613            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4614              !!!cp ('t96');
4615              ## As if <head>
4616              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4617              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4618              push @{$self->{open_elements}},
4619                  [$self->{head_element}, $el_category->{head}];
4620    
4621                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4622                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4623              }          } else {
4624              !!!cp ('t97');
4625            }
4626    
4627              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4628                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4629                    !!!cp ('t98');
4630                  ## As if </noscript>                  ## As if </noscript>
4631                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4632                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4633                                    token => $token);
4634                                
4635                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4636                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4637                  } else {
4638                    !!!cp ('t99');
4639                }                }
4640    
4641                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4642                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4643                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4644                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4645                                    text => $token->{tag_name}, token => $token);
4646                    push @{$self->{open_elements}},
4647                        [$self->{head_element}, $el_category->{head}];
4648                  } else {
4649                    !!!cp ('t101');
4650                }                }
4651                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4652                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4653                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4654                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4655                  !!!nack ('t101.1');
4656                !!!next-token;                !!!next-token;
4657                redo B;                next B;
4658              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4659                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4660                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4661                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4662                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4663                                    text => $token->{tag_name}, token => $token);
4664                    push @{$self->{open_elements}},
4665                        [$self->{head_element}, $el_category->{head}];
4666                  } else {
4667                    !!!cp ('t103');
4668                }                }
4669                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4670                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4671                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4672                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4673                  !!!ack ('t103.1');
4674                !!!next-token;                !!!next-token;
4675                redo B;                next B;
4676              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4677                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4678                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4679                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4680                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4681                                    text => $token->{tag_name}, token => $token);
4682                    push @{$self->{open_elements}},
4683                        [$self->{head_element}, $el_category->{head}];
4684                  } else {
4685                    !!!cp ('t105');
4686                }                }
4687                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4688                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.
4689    
4690                unless ($self->{confident}) {                unless ($self->{confident}) {
4691                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4692                      !!!cp ('t106');
4693                      ## NOTE: Whether the encoding is supported or not is handled
4694                      ## in the {change_encoding} callback.
4695                    $self->{change_encoding}                    $self->{change_encoding}
4696                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4697                             $token);
4698                      
4699                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4700                          ->set_user_data (manakai_has_reference =>
4701                                               $token->{attributes}->{charset}
4702                                                   ->{has_reference});
4703                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4704                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4705                        =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4706                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09\x0A\x0C\x0D\x20]*=
4707                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4708                              ([^"'\x09\x0A\x0C\x0D\x20]
4709                               [^\x09\x0A\x0C\x0D\x20\x3B]*))/x) {
4710                        !!!cp ('t107');
4711                        ## NOTE: Whether the encoding is supported or not is handled
4712                        ## in the {change_encoding} callback.
4713                      $self->{change_encoding}                      $self->{change_encoding}
4714                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4715                               $token);
4716                        $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4717                            ->set_user_data (manakai_has_reference =>
4718                                                 $token->{attributes}->{content}
4719                                                       ->{has_reference});
4720                      } else {
4721                        !!!cp ('t108');
4722                    }                    }
4723                  }                  }
4724                  } else {
4725                    if ($token->{attributes}->{charset}) {
4726                      !!!cp ('t109');
4727                      $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4728                          ->set_user_data (manakai_has_reference =>
4729                                               $token->{attributes}->{charset}
4730                                                   ->{has_reference});
4731                    }
4732                    if ($token->{attributes}->{content}) {
4733                      !!!cp ('t110');
4734                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4735                          ->set_user_data (manakai_has_reference =>
4736                                               $token->{attributes}->{content}
4737                                                   ->{has_reference});
4738                    }
4739                }                }
4740    
4741                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4742                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4743                  !!!ack ('t110.1');
4744                !!!next-token;                !!!next-token;
4745                redo B;                next B;
4746              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4747                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4748                    !!!cp ('t111');
4749                  ## As if </noscript>                  ## As if </noscript>
4750                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4751                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4752                                    token => $token);
4753                                
4754                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4755                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4756                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4757                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4758                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4759                                    text => $token->{tag_name}, token => $token);
4760                    push @{$self->{open_elements}},
4761                        [$self->{head_element}, $el_category->{head}];
4762                  } else {
4763                    !!!cp ('t113');
4764                }                }
4765    
4766                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4767                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4768                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4769                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4770                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4771                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4772                redo B;                next B;
4773              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4774                         $token->{tag_name} eq 'noframes') {
4775                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4776                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4777                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4778                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4779                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4780                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4781                                    text => $token->{tag_name}, token => $token);
4782                    push @{$self->{open_elements}},
4783                        [$self->{head_element}, $el_category->{head}];
4784                  } else {
4785                    !!!cp ('t115');
4786                }                }
4787                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4788                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4789                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4790                redo B;                next B;
4791              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4792                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4793                    !!!cp ('t116');
4794                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4795                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4796                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4797                    !!!nack ('t116.1');
4798                  !!!next-token;                  !!!next-token;
4799                  redo B;                  next B;
4800                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4801                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4802                    !!!parse-error (type => 'in noscript', text => 'noscript',
4803                                    token => $token);
4804                  ## Ignore the token                  ## Ignore the token
4805                    !!!nack ('t117.1');
4806                  !!!next-token;                  !!!next-token;
4807                  redo B;                  next B;
4808                } else {                } else {
4809                    !!!cp ('t118');
4810                  #                  #
4811                }                }
4812              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4813                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4814                    !!!cp ('t119');
4815                  ## As if </noscript>                  ## As if </noscript>
4816                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4817                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4818                                    token => $token);
4819                                
4820                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4821                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4822                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4823                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4824                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4825                                    text => $token->{tag_name}, token => $token);
4826                    push @{$self->{open_elements}},
4827                        [$self->{head_element}, $el_category->{head}];
4828                  } else {
4829                    !!!cp ('t121');
4830                }                }
4831    
4832                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4833                $script_start_tag->($insert_to_current);                $script_start_tag->();
4834                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4835                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4836                redo B;                next B;
4837              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4838                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4839                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4840                    !!!cp ('t122');
4841                  ## As if </noscript>                  ## As if </noscript>
4842                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4843                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4844                                    text => $token->{tag_name}, token => $token);
4845                                    
4846                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4847                  ## As if </head>                  ## As if </head>
# Line 2967  sub _tree_construction_main ($) { Line 4849  sub _tree_construction_main ($) {
4849                                    
4850                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4851                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4852                    !!!cp ('t124');
4853                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4854                                    
4855                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4856                  } else {
4857                    !!!cp ('t125');
4858                }                }
4859    
4860                ## "after head" insertion mode                ## "after head" insertion mode
4861                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4862                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4863                    !!!cp ('t126');
4864                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4865                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4866                    !!!cp ('t127');
4867                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4868                } else {                } else {
4869                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4870                }                }
4871                  !!!nack ('t127.1');
4872                !!!next-token;                !!!next-token;
4873                redo B;                next B;
4874              } else {              } else {
4875                  !!!cp ('t128');
4876                #                #
4877              }              }
4878    
4879              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4880                  !!!cp ('t129');
4881                ## As if </noscript>                ## As if </noscript>
4882                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4883                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4884                                  text => $token->{tag_name}, token => $token);
4885                                
4886                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4887                ## As if </head>                ## As if </head>
# Line 2998  sub _tree_construction_main ($) { Line 4889  sub _tree_construction_main ($) {
4889    
4890                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4891              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4892                  !!!cp ('t130');
4893                ## As if </head>                ## As if </head>
4894                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4895    
4896                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4897                } else {
4898                  !!!cp ('t131');
4899              }              }
4900    
4901              ## "after head" insertion mode              ## "after head" insertion mode
4902              ## As if <body>              ## As if <body>
4903              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4904              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4905              ## reprocess              ## reprocess
4906              redo B;              !!!ack-later;
4907                next B;
4908            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4909              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4910                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4911                    !!!cp ('t132');
4912                  ## As if <head>                  ## As if <head>
4913                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4914                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4915                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4916                        [$self->{head_element}, $el_category->{head}];
4917    
4918                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4919                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4920                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4921                  !!!next-token;                  !!!next-token;
4922                  redo B;                  next B;
4923                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4924                    !!!cp ('t133');
4925                  ## As if </noscript>                  ## As if </noscript>
4926                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4927                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
4928                                    text => 'head', token => $token);
4929                                    
4930                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4931                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4932                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4933                  !!!next-token;                  !!!next-token;
4934                  redo B;                  next B;
4935                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4936                    !!!cp ('t134');
4937                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4938                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4939                  !!!next-token;                  !!!next-token;
4940                  redo B;                  next B;
4941                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4942                    !!!cp ('t134.1');
4943                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4944                                    token => $token);
4945                    ## Ignore the token
4946                    !!!next-token;
4947                    next B;
4948                } else {                } else {
4949                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4950                }                }
4951              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4952                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4953                    !!!cp ('t136');
4954                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4955                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4956                  !!!next-token;                  !!!next-token;
4957                  redo B;                  next B;
4958                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4959                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4960                    !!!cp ('t137');
4961                    !!!parse-error (type => 'unmatched end tag',
4962                                    text => 'noscript', token => $token);
4963                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4964                  !!!next-token;                  !!!next-token;
4965                  redo B;                  next B;
4966                } else {                } else {
4967                    !!!cp ('t138');
4968                  #                  #
4969                }                }
4970              } elsif ({              } elsif ({
4971                        body => 1, html => 1,                        body => 1, html => 1,
4972                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4973                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4974                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4975                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4976                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4977                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag',
4978                                    text => $token->{tag_name}, token => $token);
4979                  $self->{insertion_mode} = IN_HEAD_IM;                  ## Ignore the token
4980                  ## Reprocess in the "in head" insertion mode...                  !!!next-token;
4981                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                  next B;
4982                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4983                    !!!cp ('t140.1');
4984                    !!!parse-error (type => 'unmatched end tag',
4985                                    text => $token->{tag_name}, token => $token);
4986                  ## Ignore the token                  ## Ignore the token
4987                  !!!next-token;                  !!!next-token;
4988                  redo B;                  next B;
4989                  } else {
4990                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4991                }                }
4992                              } elsif ($token->{tag_name} eq 'p') {
4993                #                !!!cp ('t142');
4994              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4995                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4996                       }->{$token->{tag_name}}) {                ## Ignore the token
4997                  !!!next-token;
4998                  next B;
4999                } elsif ($token->{tag_name} eq 'br') {
5000                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5001                  ## As if <head>                  !!!cp ('t142.2');
5002                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
5003                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5004                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
5005                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
5006      
5007                    ## Reprocess in the "after head" insertion mode...
5008                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5009                    !!!cp ('t143.2');
5010                    ## As if </head>
5011                    pop @{$self->{open_elements}};
5012                    $self->{insertion_mode} = AFTER_HEAD_IM;
5013      
5014                    ## Reprocess in the "after head" insertion mode...
5015                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5016                    !!!cp ('t143.3');
5017                    ## ISSUE: Two parse errors for <head><noscript></br>
5018                    !!!parse-error (type => 'unmatched end tag',
5019                                    text => 'br', token => $token);
5020                    ## As if </noscript>
5021                    pop @{$self->{open_elements}};
5022                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
5023    
5024                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
5025                }                  ## As if </head>
5026                    pop @{$self->{open_elements}};
5027                    $self->{insertion_mode} = AFTER_HEAD_IM;
5028    
5029                #                  ## Reprocess in the "after head" insertion mode...
5030              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5031                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
5032                  #                  #
5033                } else {                } else {
5034                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5035                }                }
5036    
5037                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5038                  !!!parse-error (type => 'unmatched end tag',
5039                                  text => 'br', token => $token);
5040                  ## Ignore the token
5041                  !!!next-token;
5042                  next B;
5043                } else {
5044                  !!!cp ('t145');
5045                  !!!parse-error (type => 'unmatched end tag',
5046                                  text => $token->{tag_name}, token => $token);
5047                  ## Ignore the token
5048                  !!!next-token;
5049                  next B;
5050              }              }
5051    
5052              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5053                  !!!cp ('t146');
5054                ## As if </noscript>                ## As if </noscript>
5055                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5056                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
5057                                  text => $token->{tag_name}, token => $token);
5058                                
5059                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5060                ## As if </head>                ## As if </head>
# Line 3110  sub _tree_construction_main ($) { Line 5062  sub _tree_construction_main ($) {
5062    
5063                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
5064              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5065                  !!!cp ('t147');
5066                ## As if </head>                ## As if </head>
5067                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5068    
5069                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
5070              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5071                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
5072                  !!!cp ('t148');
5073                  !!!parse-error (type => 'unmatched end tag',
5074                                  text => $token->{tag_name}, token => $token);
5075                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5076                !!!next-token;                !!!next-token;
5077                redo B;                next B;
5078                } else {
5079                  !!!cp ('t149');
5080              }              }
5081    
5082              ## "after head" insertion mode              ## "after head" insertion mode
5083              ## As if <body>              ## As if <body>
5084              !!!insert-element ('body');              !!!insert-element ('body',, $token);
5085              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
5086              ## reprocess              ## reprocess
5087              redo B;              next B;
5088            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5089              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5090            }            !!!cp ('t149.1');
5091    
5092              ## NOTE: As if <head>
5093              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5094              $self->{open_elements}->[-1]->[0]->append_child
5095                  ($self->{head_element});
5096              #push @{$self->{open_elements}},
5097              #    [$self->{head_element}, $el_category->{head}];
5098              #$self->{insertion_mode} = IN_HEAD_IM;
5099              ## NOTE: Reprocess.
5100    
5101              ## NOTE: As if </head>
5102              #pop @{$self->{open_elements}};
5103              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5104              ## NOTE: Reprocess.
5105              
5106              #
5107            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5108              !!!cp ('t149.2');
5109    
5110              ## NOTE: As if </head>
5111              pop @{$self->{open_elements}};
5112              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5113              ## NOTE: Reprocess.
5114    
5115              #
5116            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5117              !!!cp ('t149.3');
5118    
5119              !!!parse-error (type => 'in noscript:#eof', token => $token);
5120    
5121              ## As if </noscript>
5122              pop @{$self->{open_elements}};
5123              #$self->{insertion_mode} = IN_HEAD_IM;
5124              ## NOTE: Reprocess.
5125    
5126              ## NOTE: As if </head>
5127              pop @{$self->{open_elements}};
5128              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5129              ## NOTE: Reprocess.
5130    
5131              #
5132            } else {
5133              !!!cp ('t149.4');
5134              #
5135            }
5136    
5137            ## NOTE: As if <body>
5138            !!!insert-element ('body',, $token);
5139            $self->{insertion_mode} = IN_BODY_IM;
5140            ## NOTE: Reprocess.
5141            next B;
5142          } else {
5143            die "$0: $token->{type}: Unknown token type";
5144          }
5145    
5146            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5147      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
5148            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5149                !!!cp ('t150');
5150              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
5151              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5152                            
5153              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5154    
5155              !!!next-token;              !!!next-token;
5156              redo B;              next B;
5157            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5158              if ({              if ({
5159                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3148  sub _tree_construction_main ($) { Line 5161  sub _tree_construction_main ($) {
5161                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5162                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5163                  ## have an element in table scope                  ## have an element in table scope
5164                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5165                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5166                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5167                      $tn = $node->[1];                      !!!cp ('t151');
5168                      last INSCOPE;  
5169                    } elsif ({                      ## Close the cell
5170                              table => 1, html => 1,                      !!!back-token; # <x>
5171                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
5172                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
5173                    }                                line => $token->{line},
5174                  } # INSCOPE                                column => $token->{column}};
5175                    unless (defined $tn) {                      next B;
5176                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5177                      ## Ignore the token                      !!!cp ('t152');
5178                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
5179                      redo B;                      last;
5180                    }                    }
5181                                    }
5182                  ## Close the cell  
5183                  !!!back-token; # <?>                  !!!cp ('t153');
5184                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
5185                  redo B;                      text => $token->{tag_name}, token => $token);
5186                    ## Ignore the token
5187                    !!!nack ('t153.1');
5188                    !!!next-token;
5189                    next B;
5190                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5191                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5192                                    token => $token);
5193                                    
5194                  ## As if </caption>                  ## NOTE: As if </caption>.
5195                  ## have a table element in table scope                  ## have a table element in table scope
5196                  my $i;                  my $i;
5197                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5198                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5199                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5200                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5201                      last INSCOPE;                        !!!cp ('t155');
5202                    } elsif ({                        $i = $_;
5203                              table => 1, html => 1,                        last INSCOPE;
5204                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5205                      last INSCOPE;                        !!!cp ('t156');
5206                          last;
5207                        }
5208                    }                    }
5209    
5210                      !!!cp ('t157');
5211                      !!!parse-error (type => 'start tag not allowed',
5212                                      text => $token->{tag_name}, token => $token);
5213                      ## Ignore the token
5214                      !!!nack ('t157.1');
5215                      !!!next-token;
5216                      next B;
5217                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5218                                    
5219                  ## generate implied end tags                  ## generate implied end tags
5220                  if ({                  while ($self->{open_elements}->[-1]->[1]
5221                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5222                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
5223                       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;  
5224                  }                  }
5225    
5226                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5227                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
5228                      !!!parse-error (type => 'not closed',
5229                                      text => $self->{open_elements}->[-1]->[0]
5230                                          ->manakai_local_name,
5231                                      token => $token);
5232                    } else {
5233                      !!!cp ('t160');
5234                  }                  }
5235                                    
5236                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3220  sub _tree_construction_main ($) { Line 5240  sub _tree_construction_main ($) {
5240                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5241                                    
5242                  ## reprocess                  ## reprocess
5243                  redo B;                  !!!ack-later;
5244                    next B;
5245                } else {                } else {
5246                    !!!cp ('t161');
5247                  #                  #
5248                }                }
5249              } else {              } else {
5250                  !!!cp ('t162');
5251                #                #
5252              }              }
5253            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3234  sub _tree_construction_main ($) { Line 5257  sub _tree_construction_main ($) {
5257                  my $i;                  my $i;
5258                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5259                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5260                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5261                        !!!cp ('t163');
5262                      $i = $_;                      $i = $_;
5263                      last INSCOPE;                      last INSCOPE;
5264                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5265                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
5266                      last INSCOPE;                      last INSCOPE;
5267                    }                    }
5268                  } # INSCOPE                  } # INSCOPE
5269                    unless (defined $i) {                    unless (defined $i) {
5270                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
5271                        !!!parse-error (type => 'unmatched end tag',
5272                                        text => $token->{tag_name},
5273                                        token => $token);
5274                      ## Ignore the token                      ## Ignore the token
5275                      !!!next-token;                      !!!next-token;
5276                      redo B;                      next B;
5277                    }                    }
5278                                    
5279                  ## generate implied end tags                  ## generate implied end tags
5280                  if ({                  while ($self->{open_elements}->[-1]->[1]
5281                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5282                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
5283                       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;  
5284                  }                  }
5285                    
5286                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5287                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
5288                      !!!cp ('t167');
5289                      !!!parse-error (type => 'not closed',
5290                                      text => $self->{open_elements}->[-1]->[0]
5291                                          ->manakai_local_name,
5292                                      token => $token);
5293                    } else {
5294                      !!!cp ('t168');
5295                  }                  }
5296                                    
5297                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3275  sub _tree_construction_main ($) { Line 5301  sub _tree_construction_main ($) {
5301                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5302                                    
5303                  !!!next-token;                  !!!next-token;
5304                  redo B;                  next B;
5305                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5306                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
5307                    !!!parse-error (type => 'unmatched end tag',
5308                                    text => $token->{tag_name}, token => $token);
5309                  ## Ignore the token                  ## Ignore the token
5310                  !!!next-token;                  !!!next-token;
5311                  redo B;                  next B;
5312                } else {                } else {
5313                    !!!cp ('t170');
5314                  #                  #
5315                }                }
5316              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
5317                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5318                  ## have a table element in table scope                  ## have a table element in table scope
5319                  my $i;                  my $i;
5320                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5321                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5322                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5323                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5324                      last INSCOPE;                        !!!cp ('t171');
5325                    } elsif ({                        $i = $_;
5326                              table => 1, html => 1,                        last INSCOPE;
5327                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5328                      last INSCOPE;                        !!!cp ('t172');
5329                          last;
5330                        }
5331                    }                    }
5332    
5333                      !!!cp ('t173');
5334                      !!!parse-error (type => 'unmatched end tag',
5335                                      text => $token->{tag_name}, token => $token);
5336                      ## Ignore the token
5337                      !!!next-token;
5338                      next B;
5339                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5340                                    
5341                  ## generate implied end tags                  ## generate implied end tags
5342                  if ({                  while ($self->{open_elements}->[-1]->[1]
5343                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5344                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
5345                       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;  
5346                  }                  }
5347                                    
5348                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5349                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
5350                      !!!parse-error (type => 'not closed',
5351                                      text => $self->{open_elements}->[-1]->[0]
5352                                          ->manakai_local_name,
5353                                      token => $token);
5354                    } else {
5355                      !!!cp ('t176');
5356                  }                  }
5357                                    
5358                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3329  sub _tree_construction_main ($) { Line 5362  sub _tree_construction_main ($) {
5362                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5363                                    
5364                  !!!next-token;                  !!!next-token;
5365                  redo B;                  next B;
5366                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5367                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5368                    !!!parse-error (type => 'unmatched end tag',
5369                                    text => $token->{tag_name}, token => $token);
5370                  ## Ignore the token                  ## Ignore the token
5371                  !!!next-token;                  !!!next-token;
5372                  redo B;                  next B;
5373                } else {                } else {
5374                    !!!cp ('t178');
5375                  #                  #
5376                }                }
5377              } elsif ({              } elsif ({
# Line 3346  sub _tree_construction_main ($) { Line 5382  sub _tree_construction_main ($) {
5382                ## have an element in table scope                ## have an element in table scope
5383                my $i;                my $i;
5384                my $tn;                my $tn;
5385                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5386                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5387                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5388                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5389                    last INSCOPE;                      !!!cp ('t179');
5390                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5391                    $tn = $node->[1];  
5392                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5393                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5394                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5395                            table => 1, html => 1,                                line => $token->{line},
5396                           }->{$node->[1]}) {                                column => $token->{column}};
5397                    last INSCOPE;                      next B;
5398                      } elsif ($node->[1] & TABLE_CELL_EL) {
5399                        !!!cp ('t180');
5400                        $tn = $node->[0]->manakai_local_name;
5401                        ## NOTE: There is exactly one |td| or |th| element
5402                        ## in scope in the stack of open elements by definition.
5403                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5404                        ## ISSUE: Can this be reached?
5405                        !!!cp ('t181');
5406                        last;
5407                      }
5408                  }                  }
5409                } # INSCOPE  
5410                unless (defined $i) {                  !!!cp ('t182');
5411                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5412                        text => $token->{tag_name}, token => $token);
5413                  ## Ignore the token                  ## Ignore the token
5414                  !!!next-token;                  !!!next-token;
5415                  redo B;                  next B;
5416                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5417              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5418                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5419                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5420                                  token => $token);
5421    
5422                ## As if </caption>                ## As if </caption>
5423                ## have a table element in table scope                ## have a table element in table scope
5424                my $i;                my $i;
5425                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5426                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5427                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5428                      !!!cp ('t184');
5429                    $i = $_;                    $i = $_;
5430                    last INSCOPE;                    last INSCOPE;
5431                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5432                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5433                    last INSCOPE;                    last INSCOPE;
5434                  }                  }
5435                } # INSCOPE                } # INSCOPE
5436                unless (defined $i) {                unless (defined $i) {
5437                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5438                    !!!parse-error (type => 'unmatched end tag',
5439                                    text => 'caption', token => $token);
5440                  ## Ignore the token                  ## Ignore the token
5441                  !!!next-token;                  !!!next-token;
5442                  redo B;                  next B;
5443                }                }
5444                                
5445                ## generate implied end tags                ## generate implied end tags
5446                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5447                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5448                     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;  
5449                }                }
5450    
5451                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5452                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5453                    !!!parse-error (type => 'not closed',
5454                                    text => $self->{open_elements}->[-1]->[0]
5455                                        ->manakai_local_name,
5456                                    token => $token);
5457                  } else {
5458                    !!!cp ('t189');
5459                }                }
5460    
5461                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3422  sub _tree_construction_main ($) { Line 5465  sub _tree_construction_main ($) {
5465                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5466    
5467                ## reprocess                ## reprocess
5468                redo B;                next B;
5469              } elsif ({              } elsif ({
5470                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5471                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5472                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5473                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
5474                    !!!parse-error (type => 'unmatched end tag',
5475                                    text => $token->{tag_name}, token => $token);
5476                  ## Ignore the token                  ## Ignore the token
5477                  !!!next-token;                  !!!next-token;
5478                  redo B;                  next B;
5479                } else {                } else {
5480                    !!!cp ('t191');
5481                  #                  #
5482                }                }
5483              } elsif ({              } elsif ({
# Line 3439  sub _tree_construction_main ($) { Line 5485  sub _tree_construction_main ($) {
5485                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5486                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5487                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5488                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5489                  !!!parse-error (type => 'unmatched end tag',
5490                                  text => $token->{tag_name}, token => $token);
5491                ## Ignore the token                ## Ignore the token
5492                !!!next-token;                !!!next-token;
5493                redo B;                next B;
5494              } else {              } else {
5495                  !!!cp ('t193');
5496                #                #
5497              }              }
5498          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5499            for my $entry (@{$self->{open_elements}}) {
5500              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5501                !!!cp ('t75');
5502                !!!parse-error (type => 'in body:#eof', token => $token);
5503                last;
5504              }
5505            }
5506    
5507            ## Stop parsing.
5508            last B;
5509        } else {        } else {
5510          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5511        }        }
# Line 3454  sub _tree_construction_main ($) { Line 5514  sub _tree_construction_main ($) {
5514        #        #
5515      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5516        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5517              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5518                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
5519              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5520                                
5521                unless (length $token->{data}) {            unless (length $token->{data}) {
5522                  !!!next-token;              !!!cp ('t194');
5523                  redo B;              !!!next-token;
5524                }              next B;
5525              }            } else {
5526                !!!cp ('t195');
5527              }
5528            }
5529    
5530              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5531    
5532              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5533              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3471  sub _tree_construction_main ($) { Line 5535  sub _tree_construction_main ($) {
5535              ## result in a new Text node.              ## result in a new Text node.
5536              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5537                            
5538              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]}) {  
5539                # MUST                # MUST
5540                my $foster_parent_element;                my $foster_parent_element;
5541                my $next_sibling;                my $next_sibling;
5542                my $prev_sibling;                my $prev_sibling;
5543                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5544                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5545                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5546                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5547                        !!!cp ('t196');
5548                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5549                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5550                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5551                    } else {                    } else {
5552                        !!!cp ('t197');
5553                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5554                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5555                    }                    }
# Line 3498  sub _tree_construction_main ($) { Line 5561  sub _tree_construction_main ($) {
5561                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5562                if (defined $prev_sibling and                if (defined $prev_sibling and
5563                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5564                    !!!cp ('t198');
5565                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5566                } else {                } else {
5567                    !!!cp ('t199');
5568                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5569                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5570                     $next_sibling);                     $next_sibling);
5571                }                }
5572              } else {            $open_tables->[-1]->[1] = 1; # tainted
5573                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5574              }            !!!cp ('t200');
5575              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5576            }
5577                            
5578              !!!next-token;          !!!next-token;
5579              redo B;          next B;
5580        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5581              if ({          if ({
5582                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5583                   th => 1, td => 1,               th => 1, td => 1,
5584                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5585                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5586                  ## Clear back to table context              ## Clear back to table context
5587                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5588                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5589                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!cp ('t201');
5590                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5591                  }              }
5592                                
5593                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5594                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5595                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5596                }            }
5597              
5598                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5599                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5600                    !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t202');
5601                  }                !!!parse-error (type => 'missing start tag:tr', token => $token);
5602                }
5603                                    
5604                  ## Clear back to table body context              ## Clear back to table body context
5605                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5606                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5607                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5608                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## ISSUE: Can this case be reached?
5609                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5610                  }              }
5611                                    
5612                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5613                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5614                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5615                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5616                      !!!nack ('t204');
5617                    !!!next-token;                    !!!next-token;
5618                    redo B;                    next B;
5619                  } else {                  } else {
5620                    !!!insert-element ('tr');                    !!!cp ('t205');
5621                      !!!insert-element ('tr',, $token);
5622                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5623                  }                  }
5624                  } else {
5625                    !!!cp ('t206');
5626                }                }
5627    
5628                ## Clear back to table row context                ## Clear back to table row context
5629                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5630                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5631                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5632                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5633                }                }
5634                                
5635                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5636                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5637    
5638                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5639                                
5640                  !!!nack ('t207.1');
5641                !!!next-token;                !!!next-token;
5642                redo B;                next B;
5643              } elsif ({              } elsif ({
5644                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5645                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 3578  sub _tree_construction_main ($) { Line 5651  sub _tree_construction_main ($) {
5651                  my $i;                  my $i;
5652                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5653                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5654                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5655                        !!!cp ('t208');
5656                      $i = $_;                      $i = $_;
5657                      last INSCOPE;                      last INSCOPE;
5658                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5659                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5660                      last INSCOPE;                      last INSCOPE;
5661                    }                    }
5662                  } # INSCOPE                  } # INSCOPE
5663                  unless (defined $i) {                  unless (defined $i) {
5664                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5665    ## TODO: This type is wrong.
5666                      !!!parse-error (type => 'unmacthed end tag',
5667                                      text => $token->{tag_name}, token => $token);
5668                    ## Ignore the token                    ## Ignore the token
5669                      !!!nack ('t210.1');
5670                    !!!next-token;                    !!!next-token;
5671                    redo B;                    next B;
5672                  }                  }
5673                                    
5674                  ## Clear back to table row context                  ## Clear back to table row context
5675                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5676                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5677                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5678                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5679                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5680                  }                  }
5681                                    
5682                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5683                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5684                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5685                      !!!cp ('t212');
5686                    ## reprocess                    ## reprocess
5687                    redo B;                    !!!ack-later;
5688                      next B;
5689                  } else {                  } else {
5690                      !!!cp ('t213');
5691                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5692                  }                  }
5693                }                }
# Line 3617  sub _tree_construction_main ($) { Line 5697  sub _tree_construction_main ($) {
5697                  my $i;                  my $i;
5698                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5699                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5700                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5701                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5702                      $i = $_;                      $i = $_;
5703                      last INSCOPE;                      last INSCOPE;
5704                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5705                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5706                      last INSCOPE;                      last INSCOPE;
5707                    }                    }
5708                  } # INSCOPE                  } # INSCOPE
5709                  unless (defined $i) {                  unless (defined $i) {
5710                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5711    ## TODO: This erorr type is wrong.
5712                      !!!parse-error (type => 'unmatched end tag',
5713                                      text => $token->{tag_name}, token => $token);
5714                    ## Ignore the token                    ## Ignore the token
5715                      !!!nack ('t216.1');
5716                    !!!next-token;                    !!!next-token;
5717                    redo B;                    next B;
5718                  }                  }
5719    
5720                  ## Clear back to table body context                  ## Clear back to table body context
5721                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5722                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5723                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5724                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5725                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5726                  }                  }
5727                                    
# Line 3653  sub _tree_construction_main ($) { Line 5735  sub _tree_construction_main ($) {
5735                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5736                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5737                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5738                  } else {
5739                    !!!cp ('t218');
5740                }                }
5741    
5742                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5743                  ## Clear back to table context                  ## Clear back to table context
5744                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5745                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5746                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5747                      ## ISSUE: Can this state be reached?
5748                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5749                  }                  }
5750                                    
5751                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5752                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5753                  ## reprocess                  ## reprocess
5754                  redo B;                  !!!ack-later;
5755                    next B;
5756                } elsif ({                } elsif ({
5757                          caption => 1,                          caption => 1,
5758                          colgroup => 1,                          colgroup => 1,
5759                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5760                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5761                  ## Clear back to table context                  ## Clear back to table context
5762                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5763                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5764                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5765                      ## ISSUE: Can this state be reached?
5766                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5767                  }                  }
5768                                    
5769                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5770                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5771                                    
5772                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5773                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5774                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5775                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 3691  sub _tree_construction_main ($) { Line 5778  sub _tree_construction_main ($) {
5778                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5779                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5780                  !!!next-token;                  !!!next-token;
5781                  redo B;                  !!!nack ('t220.1');
5782                    next B;
5783                } else {                } else {
5784                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5785                }                }
5786              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5787                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5788                                  text => $self->{open_elements}->[-1]->[0]
5789                                      ->manakai_local_name,
5790                                  token => $token);
5791    
5792                ## As if </table>                ## As if </table>
5793                ## have a table element in table scope                ## have a table element in table scope
5794                my $i;                my $i;
5795                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5796                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5797                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5798                      !!!cp ('t221');
5799                    $i = $_;                    $i = $_;
5800                    last INSCOPE;                    last INSCOPE;
5801                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5802                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5803                    last INSCOPE;                    last INSCOPE;
5804                  }                  }
5805                } # INSCOPE                } # INSCOPE
5806                unless (defined $i) {                unless (defined $i) {
5807                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5808    ## TODO: The following is wrong, maybe.
5809                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5810                                    token => $token);
5811                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5812                    !!!nack ('t223.1');
5813                  !!!next-token;                  !!!next-token;
5814                  redo B;                  next B;
5815                }                }
5816                                
5817    ## TODO: Followings are removed from the latest spec.
5818                ## generate implied end tags                ## generate implied end tags
5819                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5820                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5821                     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;  
5822                }                }
5823    
5824                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5825                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5826                    ## NOTE: |<table><tr><table>|
5827                    !!!parse-error (type => 'not closed',
5828                                    text => $self->{open_elements}->[-1]->[0]
5829                                        ->manakai_local_name,
5830                                    token => $token);
5831                  } else {
5832                    !!!cp ('t226');
5833                }                }
5834    
5835                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5836                  pop @{$open_tables};
5837    
5838                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5839    
5840                ## reprocess            ## reprocess
5841                redo B;            !!!ack-later;
5842          } else {            next B;
5843            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5844              if (not $open_tables->[-1]->[1]) { # tainted
5845                !!!cp ('t227.8');
5846                ## NOTE: This is a "as if in head" code clone.
5847                $parse_rcdata->(CDATA_CONTENT_MODEL);
5848                next B;
5849              } else {
5850                !!!cp ('t227.7');
5851                #
5852              }
5853            } elsif ($token->{tag_name} eq 'script') {
5854              if (not $open_tables->[-1]->[1]) { # tainted
5855                !!!cp ('t227.6');
5856                ## NOTE: This is a "as if in head" code clone.
5857                $script_start_tag->();
5858                next B;
5859              } else {
5860                !!!cp ('t227.5');
5861                #
5862              }
5863            } elsif ($token->{tag_name} eq 'input') {
5864              if (not $open_tables->[-1]->[1]) { # tainted
5865                if ($token->{attributes}->{type}) { ## TODO: case
5866                  my $type = lc $token->{attributes}->{type}->{value};
5867                  if ($type eq 'hidden') {
5868                    !!!cp ('t227.3');
5869                    !!!parse-error (type => 'in table',
5870                                    text => $token->{tag_name}, token => $token);
5871    
5872            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5873    
5874                    ## TODO: form element pointer
5875    
5876                    pop @{$self->{open_elements}};
5877    
5878                    !!!next-token;
5879                    !!!ack ('t227.2.1');
5880                    next B;
5881                  } else {
5882                    !!!cp ('t227.2');
5883                    #
5884                  }
5885                } else {
5886                  !!!cp ('t227.1');
5887                  #
5888                }
5889              } else {
5890                !!!cp ('t227.4');
5891                #
5892              }
5893            } else {
5894              !!!cp ('t227');
5895            #            #
5896          }          }
5897    
5898            !!!parse-error (type => 'in table', text => $token->{tag_name},
5899                            token => $token);
5900    
5901            $insert = $insert_to_foster;
5902            #
5903        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5904              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5905                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 3756  sub _tree_construction_main ($) { Line 5907  sub _tree_construction_main ($) {
5907                my $i;                my $i;
5908                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5909                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5910                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5911                      !!!cp ('t228');
5912                    $i = $_;                    $i = $_;
5913                    last INSCOPE;                    last INSCOPE;
5914                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5915                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5916                    last INSCOPE;                    last INSCOPE;
5917                  }                  }
5918                } # INSCOPE                } # INSCOPE
5919                unless (defined $i) {                unless (defined $i) {
5920                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5921                    !!!parse-error (type => 'unmatched end tag',
5922                                    text => $token->{tag_name}, token => $token);
5923                  ## Ignore the token                  ## Ignore the token
5924                    !!!nack ('t230.1');
5925                  !!!next-token;                  !!!next-token;
5926                  redo B;                  next B;
5927                  } else {
5928                    !!!cp ('t232');
5929                }                }
5930    
5931                ## Clear back to table row context                ## Clear back to table row context
5932                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5933                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5934                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5935                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5936                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5937                }                }
5938    
5939                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5940                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5941                !!!next-token;                !!!next-token;
5942                redo B;                !!!nack ('t231.1');
5943                  next B;
5944              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5945                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5946                  ## As if </tr>                  ## As if </tr>
# Line 3791  sub _tree_construction_main ($) { Line 5948  sub _tree_construction_main ($) {
5948                  my $i;                  my $i;
5949                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5950                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5951                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5952                        !!!cp ('t233');
5953                      $i = $_;                      $i = $_;
5954                      last INSCOPE;                      last INSCOPE;
5955                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5956                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5957                      last INSCOPE;                      last INSCOPE;
5958                    }                    }
5959                  } # INSCOPE                  } # INSCOPE
5960                  unless (defined $i) {                  unless (defined $i) {
5961                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5962    ## TODO: The following is wrong.
5963                      !!!parse-error (type => 'unmatched end tag',
5964                                      text => $token->{type}, token => $token);
5965                    ## Ignore the token                    ## Ignore the token
5966                      !!!nack ('t236.1');
5967                    !!!next-token;                    !!!next-token;
5968                    redo B;                    next B;
5969                  }                  }
5970                                    
5971                  ## Clear back to table row context                  ## Clear back to table row context
5972                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5973                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5974                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5975                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5976                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5977                  }                  }
5978                                    
# Line 3825  sub _tree_construction_main ($) { Line 5986  sub _tree_construction_main ($) {
5986                  my $i;                  my $i;
5987                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5988                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5989                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5990                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5991                      $i = $_;                      $i = $_;
5992                      last INSCOPE;                      last INSCOPE;
5993                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5994                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5995                      last INSCOPE;                      last INSCOPE;
5996                    }                    }
5997                  } # INSCOPE                  } # INSCOPE
5998                  unless (defined $i) {                  unless (defined $i) {
5999                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
6000                      !!!parse-error (type => 'unmatched end tag',
6001                                      text => $token->{tag_name}, token => $token);
6002                    ## Ignore the token                    ## Ignore the token
6003                      !!!nack ('t239.1');
6004                    !!!next-token;                    !!!next-token;
6005                    redo B;                    next B;
6006                  }                  }
6007                                    
6008                  ## Clear back to table body context                  ## Clear back to table body context
6009                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6010                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
6011                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6012                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6013                  }                  }
6014                                    
# Line 3863  sub _tree_construction_main ($) { Line 6024  sub _tree_construction_main ($) {
6024                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
6025                }                }
6026    
6027                  ## NOTE: </table> in the "in table" insertion mode.
6028                  ## When you edit the code fragment below, please ensure that
6029                  ## the code for <table> in the "in table" insertion mode
6030                  ## is synced with it.
6031    
6032                ## have a table element in table scope                ## have a table element in table scope
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->[1] & TABLE_EL) {
6037                      !!!cp ('t241');
6038                    $i = $_;                    $i = $_;
6039                    last INSCOPE;                    last INSCOPE;
6040                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6041                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$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 ('t243');
6047                    !!!parse-error (type => 'unmatched end tag',
6048                                    text => $token->{tag_name}, token => $token);
6049                  ## Ignore the token                  ## Ignore the token
6050                    !!!nack ('t243.1');
6051                  !!!next-token;                  !!!next-token;
6052                  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]);  
6053                }                }
6054                                    
6055                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
6056                  pop @{$open_tables};
6057                                
6058                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6059                                
6060                !!!next-token;                !!!next-token;
6061                redo B;                next B;
6062              } elsif ({              } elsif ({
6063                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6064                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 3914  sub _tree_construction_main ($) { Line 6068  sub _tree_construction_main ($) {
6068                  my $i;                  my $i;
6069                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6070                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6071                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6072                        !!!cp ('t247');
6073                      $i = $_;                      $i = $_;
6074                      last INSCOPE;                      last INSCOPE;
6075                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6076                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
6077                      last INSCOPE;                      last INSCOPE;
6078                    }                    }
6079                  } # INSCOPE                  } # INSCOPE
6080                    unless (defined $i) {                    unless (defined $i) {
6081                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
6082                        !!!parse-error (type => 'unmatched end tag',
6083                                        text => $token->{tag_name}, token => $token);
6084                      ## Ignore the token                      ## Ignore the token
6085                        !!!nack ('t249.1');
6086                      !!!next-token;                      !!!next-token;
6087                      redo B;                      next B;
6088                    }                    }
6089                                    
6090                  ## As if </tr>                  ## As if </tr>
# Line 3935  sub _tree_construction_main ($) { Line 6092  sub _tree_construction_main ($) {
6092                  my $i;                  my $i;
6093                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6094                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6095                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6096                        !!!cp ('t250');
6097                      $i = $_;                      $i = $_;
6098                      last INSCOPE;                      last INSCOPE;
6099                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6100                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
6101                      last INSCOPE;                      last INSCOPE;
6102                    }                    }
6103                  } # INSCOPE                  } # INSCOPE
6104                    unless (defined $i) {                    unless (defined $i) {
6105                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
6106                        !!!parse-error (type => 'unmatched end tag',
6107                                        text => 'tr', token => $token);
6108                      ## Ignore the token                      ## Ignore the token
6109                        !!!nack ('t252.1');
6110                      !!!next-token;                      !!!next-token;
6111                      redo B;                      next B;
6112                    }                    }
6113                                    
6114                  ## Clear back to table row context                  ## Clear back to table row context
6115                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6116                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
6117                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
6118                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6119                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6120                  }                  }
6121                                    
# Line 3968  sub _tree_construction_main ($) { Line 6128  sub _tree_construction_main ($) {
6128                my $i;                my $i;
6129                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6130                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6131                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6132                      !!!cp ('t254');
6133                    $i = $_;                    $i = $_;
6134                    last INSCOPE;                    last INSCOPE;
6135                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6136                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6137                    last INSCOPE;                    last INSCOPE;
6138                  }                  }
6139                } # INSCOPE                } # INSCOPE
6140                unless (defined $i) {                unless (defined $i) {
6141                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
6142                    !!!parse-error (type => 'unmatched end tag',
6143                                    text => $token->{tag_name}, token => $token);
6144                  ## Ignore the token                  ## Ignore the token
6145                    !!!nack ('t256.1');
6146                  !!!next-token;                  !!!next-token;
6147                  redo B;                  next B;
6148                }                }
6149    
6150                ## Clear back to table body context                ## Clear back to table body context
6151                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6152                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6153                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6154                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6155                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6156                }                }
6157    
6158                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6159                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6160                  !!!nack ('t257.1');
6161                !!!next-token;                !!!next-token;
6162                redo B;                next B;
6163              } elsif ({              } elsif ({
6164                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6165                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6166                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6167                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6168                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6169                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6170                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6171                !!!next-token;                            text => $token->{tag_name}, token => $token);
6172                redo B;            ## Ignore the token
6173          } else {            !!!nack ('t258.1');
6174            !!!parse-error (type => 'in table:/'.$token->{tag_name});             !!!next-token;
6175              next B;
6176            } else {
6177              !!!cp ('t259');
6178              !!!parse-error (type => 'in table:/',
6179                              text => $token->{tag_name}, token => $token);
6180    
6181            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6182            #            #
6183          }          }
6184          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6185            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6186                    @{$self->{open_elements}} == 1) { # redundant, maybe
6187              !!!parse-error (type => 'in body:#eof', token => $token);
6188              !!!cp ('t259.1');
6189              #
6190            } else {
6191              !!!cp ('t259.2');
6192              #
6193            }
6194    
6195            ## Stop parsing
6196            last B;
6197        } else {        } else {
6198          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6199        }        }
6200      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6201            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6202              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6203                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6204                unless (length $token->{data}) {                unless (length $token->{data}) {
6205                    !!!cp ('t260');
6206                  !!!next-token;                  !!!next-token;
6207                  redo B;                  next B;
6208                }                }
6209              }              }
6210                            
6211                !!!cp ('t261');
6212              #              #
6213            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6214              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6215                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
6216                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6217                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6218                  !!!ack ('t262.1');
6219                !!!next-token;                !!!next-token;
6220                redo B;                next B;
6221              } else {              } else {
6222                  !!!cp ('t263');
6223                #                #
6224              }              }
6225            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6226              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6227                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6228                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
6229                    !!!parse-error (type => 'unmatched end tag',
6230                                    text => 'colgroup', token => $token);
6231                  ## Ignore the token                  ## Ignore the token
6232                  !!!next-token;                  !!!next-token;
6233                  redo B;                  next B;
6234                } else {                } else {
6235                    !!!cp ('t265');
6236                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6237                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6238                  !!!next-token;                  !!!next-token;
6239                  redo B;                              next B;            
6240                }                }
6241              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6242                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
6243                  !!!parse-error (type => 'unmatched end tag',
6244                                  text => 'col', token => $token);
6245                ## Ignore the token                ## Ignore the token
6246                !!!next-token;                !!!next-token;
6247                redo B;                next B;
6248              } else {              } else {
6249                  !!!cp ('t267');
6250                #                #
6251              }              }
6252            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6253              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6254            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6255              !!!cp ('t270.2');
6256              ## Stop parsing.
6257              last B;
6258            } else {
6259              ## NOTE: As if </colgroup>.
6260              !!!cp ('t270.1');
6261              pop @{$self->{open_elements}}; # colgroup
6262              $self->{insertion_mode} = IN_TABLE_IM;
6263              ## Reprocess.
6264              next B;
6265            }
6266          } else {
6267            die "$0: $token->{type}: Unknown token type";
6268          }
6269    
6270            ## As if </colgroup>            ## As if </colgroup>
6271            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6272              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
6273    ## TODO: Wrong error type?
6274                !!!parse-error (type => 'unmatched end tag',
6275                                text => 'colgroup', token => $token);
6276              ## Ignore the token              ## Ignore the token
6277                !!!nack ('t269.1');
6278              !!!next-token;              !!!next-token;
6279              redo B;              next B;
6280            } else {            } else {
6281                !!!cp ('t270');
6282              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6283              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6284                !!!ack-later;
6285              ## reprocess              ## reprocess
6286              redo B;              next B;
6287            }            }
6288      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6289        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6290            !!!cp ('t271');
6291          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6292          !!!next-token;          !!!next-token;
6293          redo B;          next B;
6294        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6295              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6296                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6297                  ## As if </option>              !!!cp ('t272');
6298                  pop @{$self->{open_elements}};              ## As if </option>
6299                }              pop @{$self->{open_elements}};
6300              } else {
6301                !!!cp ('t273');
6302              }
6303    
6304                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6305                !!!next-token;            !!!nack ('t273.1');
6306                redo B;            !!!next-token;
6307              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6308                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6309                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6310                  pop @{$self->{open_elements}};              !!!cp ('t274');
6311                }              ## As if </option>
6312                pop @{$self->{open_elements}};
6313              } else {
6314                !!!cp ('t275');
6315              }
6316    
6317                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6318                  ## As if </optgroup>              !!!cp ('t276');
6319                  pop @{$self->{open_elements}};              ## As if </optgroup>
6320                }              pop @{$self->{open_elements}};
6321              } else {
6322                !!!cp ('t277');
6323              }
6324    
6325                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6326                !!!next-token;            !!!nack ('t277.1');
6327                redo B;            !!!next-token;
6328              } elsif ($token->{tag_name} eq 'select') {            next B;
6329                !!!parse-error (type => 'not closed:select');          } elsif ({
6330                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6331                ## have an element in table scope                   }->{$token->{tag_name}} or
6332                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6333                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6334                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6335                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6336                    $i = $_;                     tr => 1, td => 1, th => 1,
6337                    last INSCOPE;                    }->{$token->{tag_name}})) {
6338                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6339                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6340                           }->{$node->[1]}) {                            token => $token);
6341                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6342                  }            ## as if there were </select> (otherwise).
6343                } # INSCOPE            ## have an element in table scope
6344                unless (defined $i) {            my $i;
6345                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6346                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6347                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6348                  redo B;                !!!cp ('t278');
6349                }                $i = $_;
6350                  last INSCOPE;
6351                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6352                  !!!cp ('t279');
6353                  last INSCOPE;
6354                }
6355              } # INSCOPE
6356              unless (defined $i) {
6357                !!!cp ('t280');
6358                !!!parse-error (type => 'unmatched end tag',
6359                                text => 'select', token => $token);
6360                ## Ignore the token
6361                !!!nack ('t280.1');
6362                !!!next-token;
6363                next B;
6364              }
6365                                
6366                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6367              splice @{$self->{open_elements}}, $i;
6368    
6369                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6370    
6371                !!!next-token;            if ($token->{tag_name} eq 'select') {
6372                redo B;              !!!nack ('t281.2');
6373                !!!next-token;
6374                next B;
6375              } else {
6376                !!!cp ('t281.1');
6377                !!!ack-later;
6378                ## Reprocess the token.
6379                next B;
6380              }
6381          } else {          } else {
6382            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
6383              !!!parse-error (type => 'in select',
6384                              text => $token->{tag_name}, token => $token);
6385            ## Ignore the token            ## Ignore the token
6386              !!!nack ('t282.1');
6387            !!!next-token;            !!!next-token;
6388            redo B;            next B;
6389          }          }
6390        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6391              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6392                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6393                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6394                  ## As if </option>              !!!cp ('t283');
6395                  splice @{$self->{open_elements}}, -2;              ## As if </option>
6396                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
6397                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6398                } else {              !!!cp ('t284');
6399                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
6400                  ## Ignore the token            } else {
6401                }              !!!cp ('t285');
6402                !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6403                redo B;                              text => $token->{tag_name}, token => $token);
6404              } elsif ($token->{tag_name} eq 'option') {              ## Ignore the token
6405                if ($self->{open_elements}->[-1]->[1] eq 'option') {            }
6406                  pop @{$self->{open_elements}};            !!!nack ('t285.1');
6407                } else {            !!!next-token;
6408                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            next B;
6409                  ## Ignore the token          } elsif ($token->{tag_name} eq 'option') {
6410                }            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6411                !!!next-token;              !!!cp ('t286');
6412                redo B;              pop @{$self->{open_elements}};
6413              } elsif ($token->{tag_name} eq 'select') {            } else {
6414                ## have an element in table scope              !!!cp ('t287');
6415                my $i;              !!!parse-error (type => 'unmatched end tag',
6416                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                              text => $token->{tag_name}, token => $token);
6417                  my $node = $self->{open_elements}->[$_];              ## Ignore the token
6418                  if ($node->[1] eq $token->{tag_name}) {            }
6419                    $i = $_;            !!!nack ('t287.1');
6420                    last INSCOPE;            !!!next-token;
6421                  } elsif ({            next B;
6422                            table => 1, html => 1,          } elsif ($token->{tag_name} eq 'select') {
6423                           }->{$node->[1]}) {            ## have an element in table scope
6424                    last INSCOPE;            my $i;
6425                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6426                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6427                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6428                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t288');
6429                  ## Ignore the token                $i = $_;
6430                  !!!next-token;                last INSCOPE;
6431                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6432                }                !!!cp ('t289');
6433                  last INSCOPE;
6434                }
6435              } # INSCOPE
6436              unless (defined $i) {
6437                !!!cp ('t290');
6438                !!!parse-error (type => 'unmatched end tag',
6439                                text => $token->{tag_name}, token => $token);
6440                ## Ignore the token
6441                !!!nack ('t290.1');
6442                !!!next-token;
6443                next B;
6444              }
6445                                
6446                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6447              splice @{$self->{open_elements}}, $i;
6448    
6449                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6450    
6451                !!!next-token;            !!!nack ('t291.1');
6452                redo B;            !!!next-token;
6453              } elsif ({            next B;
6454                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6455                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6456                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6457                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6458                     }->{$token->{tag_name}}) {
6459    ## TODO: The following is wrong?
6460              !!!parse-error (type => 'unmatched end tag',
6461                              text => $token->{tag_name}, token => $token);
6462                                
6463                ## have an element in table scope            ## have an element in table scope
6464                my $i;            my $i;
6465                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6466                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6467                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6468                    $i = $_;                !!!cp ('t292');
6469                    last INSCOPE;                $i = $_;
6470                  } elsif ({                last INSCOPE;
6471                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6472                           }->{$node->[1]}) {                !!!cp ('t293');
6473                    last INSCOPE;                last INSCOPE;
6474                  }              }
6475                } # INSCOPE            } # INSCOPE
6476                unless (defined $i) {            unless (defined $i) {
6477                  ## Ignore the token              !!!cp ('t294');
6478                  !!!next-token;              ## Ignore the token
6479                  redo B;              !!!nack ('t294.1');
6480                }              !!!next-token;
6481                next B;
6482              }
6483                                
6484                ## As if </select>            ## As if </select>
6485                ## have an element in table scope            ## have an element in table scope
6486                undef $i;            undef $i;
6487                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6488                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6489                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6490                    $i = $_;                !!!cp ('t295');
6491                    last INSCOPE;                $i = $_;
6492                  } elsif ({                last INSCOPE;
6493                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6494                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
6495                    last INSCOPE;                !!!cp ('t296');
6496                  }                last INSCOPE;
6497                } # INSCOPE              }
6498                unless (defined $i) {            } # INSCOPE
6499                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
6500                  ## Ignore the </select> token              !!!cp ('t297');
6501                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
6502                  redo B;              !!!parse-error (type => 'unmatched end tag',
6503                }                              text => 'select', token => $token);
6504                ## Ignore the </select> token
6505                !!!nack ('t297.1');
6506                !!!next-token; ## TODO: ok?
6507                next B;
6508              }
6509                                
6510                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
6511              splice @{$self->{open_elements}}, $i;
6512    
6513                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6514    
6515                ## reprocess            !!!ack-later;
6516                redo B;            ## reprocess
6517              next B;
6518          } else {          } else {
6519            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
6520              !!!parse-error (type => 'in select:/',
6521                              text => $token->{tag_name}, token => $token);
6522            ## Ignore the token            ## Ignore the token
6523              !!!nack ('t299.3');
6524            !!!next-token;            !!!next-token;
6525            redo B;            next B;
6526            }
6527          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6528            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6529                    @{$self->{open_elements}} == 1) { # redundant, maybe
6530              !!!cp ('t299.1');
6531              !!!parse-error (type => 'in body:#eof', token => $token);
6532            } else {
6533              !!!cp ('t299.2');
6534          }          }
6535    
6536            ## Stop parsing.
6537            last B;
6538        } else {        } else {
6539          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6540        }        }
6541      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6542        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6543          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6544            my $data = $1;            my $data = $1;
6545            ## As if in body            ## As if in body
6546            $reconstruct_active_formatting_elements->($insert_to_current);            $reconstruct_active_formatting_elements->($insert_to_current);
# Line 4257  sub _tree_construction_main ($) { Line 6548  sub _tree_construction_main ($) {
6548            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6549                        
6550            unless (length $token->{data}) {            unless (length $token->{data}) {
6551                !!!cp ('t300');
6552              !!!next-token;              !!!next-token;
6553              redo B;              next B;
6554            }            }
6555          }          }
6556                    
6557          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6558            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6559              !!!parse-error (type => 'after html:#text', token => $token);
6560            ## Reprocess in the "main" phase, "after body" insertion mode...            #
6561            } else {
6562              !!!cp ('t302');
6563              ## "after body" insertion mode
6564              !!!parse-error (type => 'after body:#text', token => $token);
6565              #
6566          }          }
           
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body:#character');  
6567    
6568          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6569          ## reprocess          ## reprocess
6570          redo B;          next B;
6571        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6572          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6573            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6574                        !!!parse-error (type => 'after html',
6575            ## Reprocess in the "main" phase, "after body" insertion mode...                            text => $token->{tag_name}, token => $token);
6576              #
6577            } else {
6578              !!!cp ('t304');
6579              ## "after body" insertion mode
6580              !!!parse-error (type => 'after body',
6581                              text => $token->{tag_name}, token => $token);
6582              #
6583          }          }
6584    
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body:'.$token->{tag_name});  
   
6585          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6586            !!!ack-later;
6587          ## reprocess          ## reprocess
6588          redo B;          next B;
6589        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6590          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6591            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6592              !!!parse-error (type => 'after html:/',
6593                              text => $token->{tag_name}, token => $token);
6594                        
6595            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6596            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess.
6597              next B;
6598            } else {
6599              !!!cp ('t306');
6600          }          }
6601    
6602          ## "after body" insertion mode          ## "after body" insertion mode
6603          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6604            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6605              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6606                !!!parse-error (type => 'unmatched end tag',
6607                                text => 'html', token => $token);
6608              ## Ignore the token              ## Ignore the token
6609              !!!next-token;              !!!next-token;
6610              redo B;              next B;
6611            } else {            } else {
6612                !!!cp ('t308');
6613              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6614              !!!next-token;              !!!next-token;
6615              redo B;              next B;
6616            }            }
6617          } else {          } else {
6618            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6619              !!!parse-error (type => 'after body:/',
6620                              text => $token->{tag_name}, token => $token);
6621    
6622            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6623            ## reprocess            ## reprocess
6624            redo B;            next B;
6625          }          }
6626          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6627            !!!cp ('t309.2');
6628            ## Stop parsing
6629            last B;
6630        } else {        } else {
6631          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6632        }        }
6633      } elsif ($self->{insertion_mode} & FRAME_IMS) {      } elsif ($self->{insertion_mode} & FRAME_IMS) {
6634        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6635          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6636            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6637                        
6638            unless (length $token->{data}) {            unless (length $token->{data}) {
6639                !!!cp ('t310');
6640              !!!next-token;              !!!next-token;
6641              redo B;              next B;
6642            }            }
6643          }          }
6644                    
6645          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0C\x20]+//) {
6646            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6647              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6648                !!!parse-error (type => 'in frameset:#text', token => $token);
6649            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6650              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6651            } else { # "after html frameset"              !!!parse-error (type => 'after frameset:#text', token => $token);
6652              !!!parse-error (type => 'after html:#character');            } else { # "after after frameset"
6653                !!!cp ('t313');
6654              $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');  
6655            }            }
6656                        
6657            ## Ignore the token.            ## Ignore the token.
6658            if (length $token->{data}) {            if (length $token->{data}) {
6659                !!!cp ('t314');
6660              ## reprocess the rest of characters              ## reprocess the rest of characters
6661            } else {            } else {
6662                !!!cp ('t315');
6663              !!!next-token;              !!!next-token;
6664            }            }
6665            redo B;            next B;
6666          }          }
6667                    
6668          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6669        } 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...  
         }  
   
6670          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6671              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6672            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6673              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6674              !!!nack ('t318.1');
6675            !!!next-token;            !!!next-token;
6676            redo B;            next B;
6677          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6678                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6679            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6680              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6681            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6682              !!!ack ('t319.1');
6683            !!!next-token;            !!!next-token;
6684            redo B;            next B;
6685          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6686            ## NOTE: As if in body.            !!!cp ('t320');
6687            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6688            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6689              next B;
6690    
6691              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6692              ## has no parse error.
6693          } else {          } else {
6694            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6695              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6696            } else {              !!!parse-error (type => 'in frameset',
6697              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6698              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6699                !!!cp ('t322');
6700                !!!parse-error (type => 'after frameset',
6701                                text => $token->{tag_name}, token => $token);
6702              } else { # "after after frameset"
6703                !!!cp ('t322.2');
6704                !!!parse-error (type => 'after after frameset',
6705                                text => $token->{tag_name}, token => $token);
6706            }            }
6707            ## Ignore the token            ## Ignore the token
6708              !!!nack ('t322.1');
6709            !!!next-token;            !!!next-token;
6710            redo B;            next B;
6711          }          }
6712        } 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...  
         }  
   
6713          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6714              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6715            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6716                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6717              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6718                !!!parse-error (type => 'unmatched end tag',
6719                                text => $token->{tag_name}, token => $token);
6720              ## Ignore the token              ## Ignore the token
6721              !!!next-token;              !!!next-token;
6722            } else {            } else {
6723                !!!cp ('t326');
6724              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6725              !!!next-token;              !!!next-token;
6726            }            }
6727    
6728            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6729                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6730                !!!cp ('t327');
6731              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6732              } else {
6733                !!!cp ('t328');
6734            }            }
6735            redo B;            next B;
6736          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6737                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6738              !!!cp ('t329');
6739            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6740            !!!next-token;            !!!next-token;
6741            redo B;            next B;
6742          } else {          } else {
6743            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6744              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6745            } else {              !!!parse-error (type => 'in frameset:/',
6746              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6747              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6748                !!!cp ('t330.1');
6749                !!!parse-error (type => 'after frameset:/',
6750                                text => $token->{tag_name}, token => $token);
6751              } else { # "after after html"
6752                !!!cp ('t331');
6753                !!!parse-error (type => 'after after frameset:/',
6754                                text => $token->{tag_name}, token => $token);
6755            }            }
6756            ## Ignore the token            ## Ignore the token
6757            !!!next-token;            !!!next-token;
6758            redo B;            next B;
6759            }
6760          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6761            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6762                    @{$self->{open_elements}} == 1) { # redundant, maybe
6763              !!!cp ('t331.1');
6764              !!!parse-error (type => 'in body:#eof', token => $token);
6765            } else {
6766              !!!cp ('t331.2');
6767          }          }
6768            
6769            ## Stop parsing
6770            last B;
6771        } else {        } else {
6772          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6773        }        }
# Line 4436  sub _tree_construction_main ($) { Line 6780  sub _tree_construction_main ($) {
6780      ## "in body" insertion mode      ## "in body" insertion mode
6781      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6782        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6783            !!!cp ('t332');
6784          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6785          $script_start_tag->($insert);          $script_start_tag->();
6786          redo B;          next B;
6787        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6788            !!!cp ('t333');
6789          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6790          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6791          redo B;          next B;
6792        } elsif ({        } elsif ({
6793                  base => 1, link => 1,                  base => 1, link => 1,
6794                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6795            !!!cp ('t334');
6796          ## 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
6797          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6798          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6799            !!!ack ('t334.1');
6800          !!!next-token;          !!!next-token;
6801          redo B;          next B;
6802        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6803          ## 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
6804          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6805          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.
6806    
6807          unless ($self->{confident}) {          unless ($self->{confident}) {
6808            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6809                !!!cp ('t335');
6810                ## NOTE: Whether the encoding is supported or not is handled
6811                ## in the {change_encoding} callback.
6812              $self->{change_encoding}              $self->{change_encoding}
6813                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6814                
6815                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6816                    ->set_user_data (manakai_has_reference =>
6817                                         $token->{attributes}->{charset}
6818                                             ->{has_reference});
6819            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6820              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6821                  =~ /\A[^;]*;[\x09-\x0D\x20]*charset[\x09-\x0D\x20]*=                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6822                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09\x0A\x0C\x0D\x20]*=
6823                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6824                        ([^"'\x09\x0A\x0C\x0D\x20][^\x09\x0A\x0C\x0D\x20\x3B]*))
6825                       /x) {
6826                  !!!cp ('t336');
6827                  ## NOTE: Whether the encoding is supported or not is handled
6828                  ## in the {change_encoding} callback.
6829                $self->{change_encoding}                $self->{change_encoding}
6830                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6831                  $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6832                      ->set_user_data (manakai_has_reference =>
6833                                           $token->{attributes}->{content}
6834                                                 ->{has_reference});
6835              }              }
6836            }            }
6837            } else {
6838              if ($token->{attributes}->{charset}) {
6839                !!!cp ('t337');
6840                $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6841                    ->set_user_data (manakai_has_reference =>
6842                                         $token->{attributes}->{charset}
6843                                             ->{has_reference});
6844              }
6845              if ($token->{attributes}->{content}) {
6846                !!!cp ('t338');
6847                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6848                    ->set_user_data (manakai_has_reference =>
6849                                         $token->{attributes}->{content}
6850                                             ->{has_reference});
6851              }
6852          }          }
6853    
6854            !!!ack ('t338.1');
6855          !!!next-token;          !!!next-token;
6856          redo B;          next B;
6857        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6858          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6859          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6860          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6861            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6862        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6863          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6864                                
6865          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6866              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6867              !!!cp ('t342');
6868            ## Ignore the token            ## Ignore the token
6869          } else {          } else {
6870            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6871            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6872              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6873                  !!!cp ('t343');
6874                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6875                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6876                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6877              }              }
6878            }            }
6879          }          }
6880            !!!nack ('t343.1');
6881          !!!next-token;          !!!next-token;
6882          redo B;          next B;
6883        } elsif ({        } elsif ({
6884                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6885                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6886                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6887                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6888                  pre => 1,                  pre => 1, listing => 1,
6889                    form => 1,
6890                    table => 1,
6891                    hr => 1,
6892                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6893            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6894              !!!cp ('t350');
6895              !!!parse-error (type => 'in form:form', token => $token);
6896              ## Ignore the token
6897              !!!nack ('t350.1');
6898              !!!next-token;
6899              next B;
6900            }
6901    
6902          ## has a p element in scope          ## has a p element in scope
6903          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6904            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6905              !!!back-token;              !!!cp ('t344');
6906              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6907              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6908            } elsif ({                        line => $token->{line}, column => $token->{column}};
6909                      table => 1, caption => 1, td => 1, th => 1,              next B;
6910                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6911                     }->{$_->[1]}) {              !!!cp ('t345');
6912              last INSCOPE;              last INSCOPE;
6913            }            }
6914          } # INSCOPE          } # INSCOPE
6915                        
6916          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6917          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6918              !!!nack ('t346.1');
6919            !!!next-token;            !!!next-token;
6920            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6921              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6922              unless (length $token->{data}) {              unless (length $token->{data}) {
6923                  !!!cp ('t346');
6924                !!!next-token;                !!!next-token;
6925                } else {
6926                  !!!cp ('t349');
6927              }              }
6928              } else {
6929                !!!cp ('t348');
6930            }            }
6931          } else {          } elsif ($token->{tag_name} eq 'form') {
6932              !!!cp ('t347.1');
6933              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6934    
6935              !!!nack ('t347.2');
6936            !!!next-token;            !!!next-token;
6937          }          } elsif ($token->{tag_name} eq 'table') {
6938          redo B;            !!!cp ('t382');
6939        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6940          if (defined $self->{form_element}) {            
6941            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6942            ## Ignore the token  
6943              !!!nack ('t382.1');
6944              !!!next-token;
6945            } elsif ($token->{tag_name} eq 'hr') {
6946              !!!cp ('t386');
6947              pop @{$self->{open_elements}};
6948            
6949              !!!nack ('t386.1');
6950            !!!next-token;            !!!next-token;
           redo B;  
6951          } else {          } else {
6952            ## 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];  
6953            !!!next-token;            !!!next-token;
           redo B;  
6954          }          }
6955        } elsif ($token->{tag_name} eq 'li') {          next B;
6956          ## has a p element in scope        } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'li') {  
             if ($i != -1) {  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             last LI;  
           }  
             
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
6957          ## has a p element in scope          ## has a p element in scope
6958          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6959            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6960              !!!back-token;              !!!cp ('t353');
6961              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6962              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6963            } elsif ({                        line => $token->{line}, column => $token->{column}};
6964                      table => 1, caption => 1, td => 1, th => 1,              next B;
6965                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6966                     }->{$_->[1]}) {              !!!cp ('t354');
6967              last INSCOPE;              last INSCOPE;
6968            }            }
6969          } # INSCOPE          } # INSCOPE
# Line 4627  sub _tree_construction_main ($) { Line 6971  sub _tree_construction_main ($) {
6971          ## Step 1          ## Step 1
6972          my $i = -1;          my $i = -1;
6973          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6974            my $li_or_dtdd = {li => {li => 1},
6975                              dt => {dt => 1, dd => 1},
6976                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6977          LI: {          LI: {
6978            ## Step 2            ## Step 2
6979            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6980              if ($i != -1) {              if ($i != -1) {
6981                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6982                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6983                                  text => $self->{open_elements}->[-1]->[0]
6984                                      ->manakai_local_name,
6985                                  token => $token);
6986                } else {
6987                  !!!cp ('t356');
6988              }              }
6989              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6990              last LI;              last LI;
6991              } else {
6992                !!!cp ('t357');
6993            }            }
6994                        
6995            ## Step 3            ## Step 3
6996            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6997                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6998                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6999                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
7000                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
7001                  not ($node->[1] & DIV_EL)) {
7002                !!!cp ('t358');
7003              last LI;              last LI;
7004            }            }
7005                        
7006              !!!cp ('t359');
7007            ## Step 4            ## Step 4
7008            $i--;            $i--;
7009            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
7010            redo LI;            redo LI;
7011          } # LI          } # LI
7012                        
7013          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7014            !!!nack ('t359.1');
7015          !!!next-token;          !!!next-token;
7016          redo B;          next B;
7017        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
7018          ## has a p element in scope          ## has a p element in scope
7019          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
7020            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
7021              !!!back-token;              !!!cp ('t367');
7022              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
7023              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
7024            } elsif ({                        line => $token->{line}, column => $token->{column}};
7025                      table => 1, caption => 1, td => 1, th => 1,              next B;
7026                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
7027                     }->{$_->[1]}) {              !!!cp ('t368');
7028              last INSCOPE;              last INSCOPE;
7029            }            }
7030          } # INSCOPE          } # INSCOPE
7031                        
7032          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7033                        
7034          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7035                        
7036            !!!nack ('t368.1');
7037          !!!next-token;          !!!next-token;
7038          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;  
7039        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
7040          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7041            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
7042            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
7043              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
7044                !!!parse-error (type => 'in a:a', token => $token);
7045                            
7046              !!!back-token;              !!!back-token; # <a>
7047              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
7048              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
7049                $formatting_end_tag->($token);
7050                            
7051              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
7052                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7053                    !!!cp ('t372');
7054                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
7055                  last AFE2;                  last AFE2;
7056                }                }
7057              } # AFE2              } # AFE2
7058              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
7059                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7060                    !!!cp ('t373');
7061                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
7062                  last OE;                  last OE;
7063                }                }
7064              } # OE              } # OE
7065              last AFE;              last AFE;
7066            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
7067                !!!cp ('t374');
7068              last AFE;              last AFE;
7069            }            }
7070          } # AFE          } # AFE
7071                        
7072          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7073    
7074          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7075          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7076    
7077            !!!nack ('t374.1');
7078          !!!next-token;          !!!next-token;
7079          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;  
7080        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
7081          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7082    
7083          ## has a |nobr| element in scope          ## has a |nobr| element in scope
7084          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7085            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7086            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
7087              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
7088              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
7089              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
7090              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7091            } elsif ({                        line => $token->{line}, column => $token->{column}};
7092                      table => 1, caption => 1, td => 1, th => 1,              next B;
7093                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
7094                     }->{$node->[1]}) {              !!!cp ('t377');
7095              last INSCOPE;              last INSCOPE;
7096            }            }
7097          } # INSCOPE          } # INSCOPE
7098                    
7099          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7100          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7101                    
7102            !!!nack ('t377.1');
7103          !!!next-token;          !!!next-token;
7104          redo B;          next B;
7105        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7106          ## has a button element in scope          ## has a button element in scope
7107          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7108            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7109            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7110              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
7111              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
7112              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
7113              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7114            } elsif ({                        line => $token->{line}, column => $token->{column}};
7115                      table => 1, caption => 1, td => 1, th => 1,              next B;
7116                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
7117                     }->{$node->[1]}) {              !!!cp ('t379');
7118              last INSCOPE;              last INSCOPE;
7119            }            }
7120          } # INSCOPE          } # INSCOPE
7121                        
7122          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7123                        
7124          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7125          push @$active_formatting_elements, ['#marker', ''];  
7126            ## TODO: associate with $self->{form_element} if defined
7127    
         !!!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});  
7128          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7129            
7130          !!!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;  
             
7131          !!!next-token;          !!!next-token;
7132          redo B;          next B;
7133        } elsif ({        } elsif ({
7134                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7135                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7136                  image => 1,                  noembed => 1,
7137                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7138                    noscript => 0, ## TODO: 1 if scripting is enabled
7139                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7140          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7141            !!!parse-error (type => 'image');            !!!cp ('t381');
7142            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
7143            } else {
7144              !!!cp ('t399');
7145          }          }
7146            ## NOTE: There is an "as if in body" code clone.
7147          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7148          $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;  
7149        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7150          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7151                    
7152          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7153              !!!cp ('t389');
7154            ## Ignore the token            ## Ignore the token
7155              !!!nack ('t389'); ## NOTE: Not acknowledged.
7156            !!!next-token;            !!!next-token;
7157            redo B;            next B;
7158          } else {          } else {
7159              !!!ack ('t391.1');
7160    
7161            my $at = $token->{attributes};            my $at = $token->{attributes};
7162            my $form_attrs;            my $form_attrs;
7163            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 4915  sub _tree_construction_main ($) { Line 7167  sub _tree_construction_main ($) {
7167            delete $at->{prompt};            delete $at->{prompt};
7168            my @tokens = (            my @tokens = (
7169                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7170                           attributes => $form_attrs},                           attributes => $form_attrs,
7171                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7172                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7173                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7174                            {type => START_TAG_TOKEN, tag_name => 'p',
7175                             line => $token->{line}, column => $token->{column}},
7176                            {type => START_TAG_TOKEN, tag_name => 'label',
7177                             line => $token->{line}, column => $token->{column}},
7178                         );                         );
7179            if ($prompt_attr) {            if ($prompt_attr) {
7180              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
7181                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7182                               #line => $token->{line}, column => $token->{column},
7183                              };
7184            } else {            } else {
7185                !!!cp ('t391');
7186              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7187                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7188                               #line => $token->{line}, column => $token->{column},
7189                              }; # SHOULD
7190              ## TODO: make this configurable              ## TODO: make this configurable
7191            }            }
7192            push @tokens,            push @tokens,
7193                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7194                             line => $token->{line}, column => $token->{column}},
7195                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7196                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7197                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7198                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7199                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7200            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7201                             line => $token->{line}, column => $token->{column}},
7202                            {type => END_TAG_TOKEN, tag_name => 'form',
7203                             line => $token->{line}, column => $token->{column}};
7204            !!!back-token (@tokens);            !!!back-token (@tokens);
7205            redo B;            !!!next-token;
7206              next B;
7207          }          }
7208        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7209          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7210          my $el;          my $el;
7211          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7212                    
7213          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7214          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 4950  sub _tree_construction_main ($) { Line 7217  sub _tree_construction_main ($) {
7217          $insert->($el);          $insert->($el);
7218                    
7219          my $text = '';          my $text = '';
7220            !!!nack ('t392.1');
7221          !!!next-token;          !!!next-token;
7222          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7223            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
7224            unless (length $token->{data}) {            unless (length $token->{data}) {
7225                !!!cp ('t392');
7226              !!!next-token;              !!!next-token;
7227              } else {
7228                !!!cp ('t393');
7229            }            }
7230            } else {
7231              !!!cp ('t394');
7232          }          }
7233          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
7234              !!!cp ('t395');
7235            $text .= $token->{data};            $text .= $token->{data};
7236            !!!next-token;            !!!next-token;
7237          }          }
7238          if (length $text) {          if (length $text) {
7239              !!!cp ('t396');
7240            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
7241          }          }
7242                    
# Line 4969  sub _tree_construction_main ($) { Line 7244  sub _tree_construction_main ($) {
7244                    
7245          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
7246              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
7247              !!!cp ('t397');
7248            ## Ignore the token            ## Ignore the token
7249          } else {          } else {
7250            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
7251              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7252          }          }
7253          !!!next-token;          !!!next-token;
7254            next B;
7255          } elsif ($token->{tag_name} eq 'rt' or
7256                   $token->{tag_name} eq 'rp') {
7257            ## has a |ruby| element in scope
7258            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7259              my $node = $self->{open_elements}->[$_];
7260              if ($node->[1] & RUBY_EL) {
7261                !!!cp ('t398.1');
7262                ## generate implied end tags
7263                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7264                  !!!cp ('t398.2');
7265                  pop @{$self->{open_elements}};
7266                }
7267                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7268                  !!!cp ('t398.3');
7269                  !!!parse-error (type => 'not closed',
7270                                  text => $self->{open_elements}->[-1]->[0]
7271                                      ->manakai_local_name,
7272                                  token => $token);
7273                  pop @{$self->{open_elements}}
7274                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7275                }
7276                last INSCOPE;
7277              } elsif ($node->[1] & SCOPING_EL) {
7278                !!!cp ('t398.4');
7279                last INSCOPE;
7280              }
7281            } # INSCOPE
7282    
7283            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7284    
7285            !!!nack ('t398.5');
7286            !!!next-token;
7287          redo B;          redo B;
7288        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7289                  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') {  
7290          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7291    
7292            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7293    
7294            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7295    
7296            ## "adjust foreign attributes" - done in insert-element-f
7297                    
7298          !!!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);
7299                    
7300          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
7301              pop @{$self->{open_elements}};
7302              !!!ack ('t398.1');
7303            } else {
7304              !!!cp ('t398.2');
7305              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7306              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7307              ## mode, "in body" (not "in foreign content") secondary insertion
7308              ## mode, maybe.
7309            }
7310    
7311          !!!next-token;          !!!next-token;
7312          redo B;          next B;
7313        } elsif ({        } elsif ({
7314                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7315                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
7316                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
7317                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7318                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7319          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
7320            !!!parse-error (type => 'in body',
7321                            text => $token->{tag_name}, token => $token);
7322          ## Ignore the token          ## Ignore the token
7323            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7324          !!!next-token;          !!!next-token;
7325          redo B;          next B;
7326                    
7327          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7328        } else {        } else {
7329            if ($token->{tag_name} eq 'image') {
7330              !!!cp ('t384');
7331              !!!parse-error (type => 'image', token => $token);
7332              $token->{tag_name} = 'img';
7333            } else {
7334              !!!cp ('t385');
7335            }
7336    
7337            ## NOTE: There is an "as if <br>" code clone.
7338          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7339                    
7340          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7341    
7342            if ({
7343                 applet => 1, marquee => 1, object => 1,
7344                }->{$token->{tag_name}}) {
7345              !!!cp ('t380');
7346              push @$active_formatting_elements, ['#marker', ''];
7347              !!!nack ('t380.1');
7348            } elsif ({
7349                      b => 1, big => 1, em => 1, font => 1, i => 1,
7350                      s => 1, small => 1, strile => 1,
7351                      strong => 1, tt => 1, u => 1,
7352                     }->{$token->{tag_name}}) {
7353              !!!cp ('t375');
7354              push @$active_formatting_elements, $self->{open_elements}->[-1];
7355              !!!nack ('t375.1');
7356            } elsif ($token->{tag_name} eq 'input') {
7357              !!!cp ('t388');
7358              ## TODO: associate with $self->{form_element} if defined
7359              pop @{$self->{open_elements}};
7360              !!!ack ('t388.2');
7361            } elsif ({
7362                      area => 1, basefont => 1, bgsound => 1, br => 1,
7363                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7364                      #image => 1,
7365                     }->{$token->{tag_name}}) {
7366              !!!cp ('t388.1');
7367              pop @{$self->{open_elements}};
7368              !!!ack ('t388.3');
7369            } elsif ($token->{tag_name} eq 'select') {
7370              ## TODO: associate with $self->{form_element} if defined
7371            
7372              if ($self->{insertion_mode} & TABLE_IMS or
7373                  $self->{insertion_mode} & BODY_TABLE_IMS or
7374                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7375                !!!cp ('t400.1');
7376                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7377              } else {
7378                !!!cp ('t400.2');
7379                $self->{insertion_mode} = IN_SELECT_IM;
7380              }
7381              !!!nack ('t400.3');
7382            } else {
7383              !!!nack ('t402');
7384            }
7385                    
7386          !!!next-token;          !!!next-token;
7387          redo B;          next B;
7388        }        }
7389      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7390        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7391          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7392              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7393            for (@{$self->{open_elements}}) {          INSCOPE: {
7394              unless ({            for (reverse @{$self->{open_elements}}) {
7395                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7396                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7397                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7398                      }->{$_->[1]}) {                last INSCOPE;
7399                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
7400                  !!!cp ('t405.1');
7401                  last;
7402              }              }
7403            }            }
7404    
7405            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7406            !!!next-token;                            text => $token->{tag_name}, token => $token);
7407            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
7408            !!!next-token;            !!!next-token;
7409            redo B;            next B;
7410            } # INSCOPE
7411    
7412            for (@{$self->{open_elements}}) {
7413              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7414                !!!cp ('t403');
7415                !!!parse-error (type => 'not closed',
7416                                text => $_->[0]->manakai_local_name,
7417                                token => $token);
7418                last;
7419              } else {
7420                !!!cp ('t404');
7421              }
7422          }          }
7423    
7424            $self->{insertion_mode} = AFTER_BODY_IM;
7425            !!!next-token;
7426            next B;
7427        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7428          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
7429            ## up-to-date, though it has same effect as speced.
7430            if (@{$self->{open_elements}} > 1 and
7431                $self->{open_elements}->[1]->[1] & BODY_EL) {
7432            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7433            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7434              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
7435                !!!parse-error (type => 'not closed',
7436                                text => $self->{open_elements}->[1]->[0]
7437                                    ->manakai_local_name,
7438                                token => $token);
7439              } else {
7440                !!!cp ('t407');
7441            }            }
7442            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7443            ## reprocess            ## reprocess
7444            redo B;            next B;
7445          } else {          } else {
7446            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
7447              !!!parse-error (type => 'unmatched end tag',
7448                              text => $token->{tag_name}, token => $token);
7449            ## Ignore the token            ## Ignore the token
7450            !!!next-token;            !!!next-token;
7451            redo B;            next B;
7452          }          }
7453        } elsif ({        } elsif ({
7454                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7455                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7456                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7457                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7458                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7459                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7460          ## has an element in scope          ## has an element in scope
7461          my $i;          my $i;
7462          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7463            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7464            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7465              ## 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;  
             }  
7466              $i = $_;              $i = $_;
7467              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7468            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7469                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7470              last INSCOPE;              last INSCOPE;
7471            }            }
7472          } # INSCOPE          } # INSCOPE
7473            
7474          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7475            if (defined $i) {            !!!cp ('t413');
7476              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag',
7477                              text => $token->{tag_name}, token => $token);
7478              ## NOTE: Ignore the token.
7479            } else {
7480              ## Step 1. generate implied end tags
7481              while ({
7482                      ## END_TAG_OPTIONAL_EL
7483                      dd => ($token->{tag_name} ne 'dd'),
7484                      dt => ($token->{tag_name} ne 'dt'),
7485                      li => ($token->{tag_name} ne 'li'),
7486                      p => 1,
7487                      rt => 1,
7488                      rp => 1,
7489                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7490                !!!cp ('t409');
7491                pop @{$self->{open_elements}};
7492              }
7493    
7494              ## Step 2.
7495              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7496                      ne $token->{tag_name}) {
7497                !!!cp ('t412');
7498                !!!parse-error (type => 'not closed',
7499                                text => $self->{open_elements}->[-1]->[0]
7500                                    ->manakai_local_name,
7501                                token => $token);
7502            } else {            } else {
7503              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
7504            }            }
7505          }  
7506                      ## Step 3.
         if (defined $i) {  
7507            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7508          } elsif ($token->{tag_name} eq 'p') {  
7509            ## As if <p>, then reprocess the current token            ## Step 4.
7510            my $el;            $clear_up_to_marker->()
7511            !!!create-element ($el, 'p');                if {
7512            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
7513                  }->{$token->{tag_name}};
7514          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7515          !!!next-token;          !!!next-token;
7516          redo B;          next B;
7517        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7518            undef $self->{form_element};
7519    
7520          ## has an element in scope          ## has an element in scope
7521            my $i;
7522          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7523            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7524            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7525              ## generate implied end tags              !!!cp ('t418');
7526              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;  
             }  
7527              last INSCOPE;              last INSCOPE;
7528            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7529                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7530              last INSCOPE;              last INSCOPE;
7531            }            }
7532          } # INSCOPE          } # INSCOPE
7533            
7534          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7535            pop @{$self->{open_elements}};            !!!cp ('t421');
7536          } else {            !!!parse-error (type => 'unmatched end tag',
7537            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                            text => $token->{tag_name}, token => $token);
7538              ## NOTE: Ignore the token.
7539            } else {
7540              ## Step 1. generate implied end tags
7541              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7542                !!!cp ('t417');
7543                pop @{$self->{open_elements}};
7544              }
7545              
7546              ## Step 2.
7547              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7548                      ne $token->{tag_name}) {
7549                !!!cp ('t417.1');
7550                !!!parse-error (type => 'not closed',
7551                                text => $self->{open_elements}->[-1]->[0]
7552                                    ->manakai_local_name,
7553                                token => $token);
7554              } else {
7555                !!!cp ('t420');
7556              }  
7557              
7558              ## Step 3.
7559              splice @{$self->{open_elements}}, $i;
7560          }          }
7561    
         undef $self->{form_element};  
7562          !!!next-token;          !!!next-token;
7563          redo B;          next B;
7564        } elsif ({        } elsif ({
7565                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7566                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5150  sub _tree_construction_main ($) { Line 7568  sub _tree_construction_main ($) {
7568          my $i;          my $i;
7569          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7570            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7571            if ({            if ($node->[1] & HEADING_EL) {
7572                 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;  
             }  
7573              $i = $_;              $i = $_;
7574              last INSCOPE;              last INSCOPE;
7575            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7576                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7577              last INSCOPE;              last INSCOPE;
7578            }            }
7579          } # INSCOPE          } # INSCOPE
7580            
7581          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7582            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
7583              !!!parse-error (type => 'unmatched end tag',
7584                              text => $token->{tag_name}, token => $token);
7585              ## NOTE: Ignore the token.
7586            } else {
7587              ## Step 1. generate implied end tags
7588              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7589                !!!cp ('t422');
7590                pop @{$self->{open_elements}};
7591              }
7592              
7593              ## Step 2.
7594              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7595                      ne $token->{tag_name}) {
7596                !!!cp ('t425');
7597                !!!parse-error (type => 'unmatched end tag',
7598                                text => $token->{tag_name}, token => $token);
7599              } else {
7600                !!!cp ('t426');
7601              }
7602    
7603              ## Step 3.
7604              splice @{$self->{open_elements}}, $i;
7605          }          }
7606                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7607          !!!next-token;          !!!next-token;
7608          redo B;          next B;
7609          } elsif ($token->{tag_name} eq 'p') {
7610            ## has an element in scope
7611            my $i;
7612            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7613              my $node = $self->{open_elements}->[$_];
7614              if ($node->[1] & P_EL) {
7615                !!!cp ('t410.1');
7616                $i = $_;
7617                last INSCOPE;
7618              } elsif ($node->[1] & SCOPING_EL) {
7619                !!!cp ('t411.1');
7620                last INSCOPE;
7621              }
7622            } # INSCOPE
7623    
7624            if (defined $i) {
7625              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7626                      ne $token->{tag_name}) {
7627                !!!cp ('t412.1');
7628                !!!parse-error (type => 'not closed',
7629                                text => $self->{open_elements}->[-1]->[0]
7630                                    ->manakai_local_name,
7631                                token => $token);
7632              } else {
7633                !!!cp ('t414.1');
7634              }
7635    
7636              splice @{$self->{open_elements}}, $i;
7637            } else {
7638              !!!cp ('t413.1');
7639              !!!parse-error (type => 'unmatched end tag',
7640                              text => $token->{tag_name}, token => $token);
7641    
7642              !!!cp ('t415.1');
7643              ## As if <p>, then reprocess the current token
7644              my $el;
7645              !!!create-element ($el, $HTML_NS, 'p',, $token);
7646              $insert->($el);
7647              ## NOTE: Not inserted into |$self->{open_elements}|.
7648            }
7649    
7650            !!!next-token;
7651            next B;
7652        } elsif ({        } elsif ({
7653                  a => 1,                  a => 1,
7654                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7655                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7656                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7657                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7658          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7659          redo B;          $formatting_end_tag->($token);
7660            next B;
7661        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7662          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7663            !!!parse-error (type => 'unmatched end tag',
7664                            text => 'br', token => $token);
7665    
7666          ## As if <br>          ## As if <br>
7667          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7668                    
7669          my $el;          my $el;
7670          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7671          $insert->($el);          $insert->($el);
7672                    
7673          ## Ignore the token.          ## Ignore the token.
7674          !!!next-token;          !!!next-token;
7675          redo B;          next B;
7676        } elsif ({        } elsif ({
7677                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7678                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5214  sub _tree_construction_main ($) { Line 7685  sub _tree_construction_main ($) {
7685                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7686                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7687                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7688          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7689            !!!parse-error (type => 'unmatched end tag',
7690                            text => $token->{tag_name}, token => $token);
7691          ## Ignore the token          ## Ignore the token
7692          !!!next-token;          !!!next-token;
7693          redo B;          next B;
7694                    
7695          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7696                    
# Line 5228  sub _tree_construction_main ($) { Line 7701  sub _tree_construction_main ($) {
7701    
7702          ## Step 2          ## Step 2
7703          S2: {          S2: {
7704            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7705              ## Step 1              ## Step 1
7706              ## generate implied end tags              ## generate implied end tags
7707              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7708                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7709                   td => 1, th => 1, tr => 1,                ## NOTE: |<ruby><rt></ruby>|.
7710                   tbody => 1, tfoot => 1, thead => 1,                ## ISSUE: <ruby><rt></rt> will also take this code path,
7711                  }->{$self->{open_elements}->[-1]->[1]}) {                ## which seems wrong.
7712                !!!back-token;                pop @{$self->{open_elements}};
7713                $token = {type => END_TAG_TOKEN,                $node_i++;
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7714              }              }
7715                    
7716              ## Step 2              ## Step 2
7717              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7718                        ne $token->{tag_name}) {
7719                  !!!cp ('t431');
7720                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7721                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7722                                  text => $self->{open_elements}->[-1]->[0]
7723                                      ->manakai_local_name,
7724                                  token => $token);
7725                } else {
7726                  !!!cp ('t432');
7727              }              }
7728                            
7729              ## Step 3              ## Step 3
7730              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7731    
7732              !!!next-token;              !!!next-token;
7733              last S2;              last S2;
7734            } else {            } else {
7735              ## Step 3              ## Step 3
7736              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7737                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7738                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7739                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7740                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7741                  !!!parse-error (type => 'unmatched end tag',
7742                                  text => $token->{tag_name}, token => $token);
7743                ## Ignore the token                ## Ignore the token
7744                !!!next-token;                !!!next-token;
7745                last S2;                last S2;
7746              }              }
7747    
7748                !!!cp ('t434');
7749            }            }
7750                        
7751            ## Step 4            ## Step 4
# Line 5273  sub _tree_construction_main ($) { Line 7755  sub _tree_construction_main ($) {
7755            ## Step 5;            ## Step 5;
7756            redo S2;            redo S2;
7757          } # S2          } # S2
7758          redo B;          next B;
7759        }        }
7760      }      }
7761      redo B;      next B;
7762      } continue { # B
7763        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7764          ## NOTE: The code below is executed in cases where it does not have
7765          ## to be, but it it is harmless even in those cases.
7766          ## has an element in scope
7767          INSCOPE: {
7768            for (reverse 0..$#{$self->{open_elements}}) {
7769              my $node = $self->{open_elements}->[$_];
7770              if ($node->[1] & FOREIGN_EL) {
7771                last INSCOPE;
7772              } elsif ($node->[1] & SCOPING_EL) {
7773                last;
7774              }
7775            }
7776            
7777            ## NOTE: No foreign element in scope.
7778            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7779          } # INSCOPE
7780        }
7781    } # B    } # B
7782    
   ## 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  
   
7783    ## Stop parsing # MUST    ## Stop parsing # MUST
7784        
7785    ## TODO: script stuffs    ## TODO: script stuffs
7786  } # _tree_construct_main  } # _tree_construct_main
7787    
7788  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7789    my $class = shift;    my $class = shift;
7790    my $node = shift;    my $node = shift;
7791    my $s = \$_[0];    #my $s = \$_[0];
7792    my $onerror = $_[1];    my $onerror = $_[1];
7793      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7794    
7795    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7796    
# Line 5312  sub set_inner_html ($$$) { Line 7809  sub set_inner_html ($$$) {
7809      }      }
7810    
7811      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7812      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7813    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7814      ## TODO: If non-html element      ## TODO: If non-html element
7815    
7816      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7817    
7818    ## TODO: Support for $get_wrapper
7819    
7820      ## Step 1 # MUST      ## Step 1 # MUST
7821      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7822      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5325  sub set_inner_html ($$$) { Line 7824  sub set_inner_html ($$$) {
7824      my $p = $class->new;      my $p = $class->new;
7825      $p->{document} = $doc;      $p->{document} = $doc;
7826    
7827      ## Step 9 # MUST      ## Step 8 # MUST
7828      my $i = 0;      my $i = 0;
7829      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7830      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7831      $p->{set_next_input_character} = sub {      require Whatpm::Charset::DecodeHandle;
7832        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7833        $input = $get_wrapper->($input);
7834        $p->{set_nc} = sub {
7835        my $self = shift;        my $self = shift;
7836    
7837        pop @{$self->{prev_input_character}};        my $char = '';
7838        unshift @{$self->{prev_input_character}}, $self->{next_input_character};        if (defined $self->{next_nc}) {
7839            $char = $self->{next_nc};
7840            delete $self->{next_nc};
7841            $self->{nc} = ord $char;
7842          } else {
7843            $self->{char_buffer} = '';
7844            $self->{char_buffer_pos} = 0;
7845            
7846            my $count = $input->manakai_read_until
7847                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7848                 $self->{char_buffer_pos});
7849            if ($count) {
7850              $self->{line_prev} = $self->{line};
7851              $self->{column_prev} = $self->{column};
7852              $self->{column}++;
7853              $self->{nc}
7854                  = ord substr ($self->{char_buffer},
7855                                $self->{char_buffer_pos}++, 1);
7856              return;
7857            }
7858            
7859            if ($input->read ($char, 1)) {
7860              $self->{nc} = ord $char;
7861            } else {
7862              $self->{nc} = -1;
7863              return;
7864            }
7865          }
7866    
7867        $self->{next_input_character} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7868        $self->{next_input_character} = ord substr $$s, $i++, 1;        $p->{column}++;
7869        $column++;  
7870          if ($self->{nc} == 0x000A) { # LF
7871        if ($self->{next_input_character} == 0x000A) { # LF          $p->{line}++;
7872          $line++;          $p->{column} = 0;
7873          $column = 0;          !!!cp ('i1');
7874        } elsif ($self->{next_input_character} == 0x000D) { # CR        } elsif ($self->{nc} == 0x000D) { # CR
7875          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7876          $self->{next_input_character} = 0x000A; # LF # MUST          my $next = '';
7877          $line++;          if ($input->read ($next, 1) and $next ne "\x0A") {
7878          $column = 0;            $self->{next_nc} = $next;
7879        } elsif ($self->{next_input_character} > 0x10FFFF) {          }
7880          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0x000A; # LF # MUST
7881        } elsif ($self->{next_input_character} == 0x0000) { # NULL          $p->{line}++;
7882            $p->{column} = 0;
7883            !!!cp ('i2');
7884          } elsif ($self->{nc} == 0x0000) { # NULL
7885            !!!cp ('i4');
7886          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7887          $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7888        }        }
7889      };      };
7890      $p->{prev_input_character} = [-1, -1, -1];  
7891      $p->{next_input_character} = -1;      $p->{read_until} = sub {
7892              #my ($scalar, $specials_range, $offset) = @_;
7893          return 0 if defined $p->{next_nc};
7894    
7895          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7896          my $offset = $_[2] || 0;
7897          
7898          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7899            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7900            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7901              substr ($_[0], $offset)
7902                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7903              my $count = $+[0] - $-[0];
7904              if ($count) {
7905                $p->{column} += $count;
7906                $p->{char_buffer_pos} += $count;
7907                $p->{line_prev} = $p->{line};
7908                $p->{column_prev} = $p->{column} - 1;
7909                $p->{nc} = -1;
7910              }
7911              return $count;
7912            } else {
7913              return 0;
7914            }
7915          } else {
7916            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7917            if ($count) {
7918              $p->{column} += $count;
7919              $p->{column_prev} += $count;
7920              $p->{nc} = -1;
7921            }
7922            return $count;
7923          }
7924        }; # $p->{read_until}
7925    
7926      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7927        my (%opt) = @_;        my (%opt) = @_;
7928        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7929          my $column = $opt{column};
7930          if (defined $opt{token} and defined $opt{token}->{line}) {
7931            $line = $opt{token}->{line};
7932            $column = $opt{token}->{column};
7933          }
7934          warn "Parse error ($opt{type}) at line $line column $column\n";
7935      };      };
7936      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7937        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7938      };      };
7939            
7940        my $char_onerror = sub {
7941          my (undef, $type, %opt) = @_;
7942          $ponerror->(layer => 'encode',
7943                      line => $p->{line}, column => $p->{column} + 1,
7944                      %opt, type => $type);
7945        }; # $char_onerror
7946        $input->onerror ($char_onerror);
7947    
7948      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7949      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7950    
7951      ## Step 2      ## Step 2
7952      my $node_ln = $node->local_name;      my $node_ln = $node->manakai_local_name;
7953      $p->{content_model} = {      $p->{content_model} = {
7954        title => RCDATA_CONTENT_MODEL,        title => RCDATA_CONTENT_MODEL,
7955        textarea => RCDATA_CONTENT_MODEL,        textarea => RCDATA_CONTENT_MODEL,
# Line 5386  sub set_inner_html ($$$) { Line 7966  sub set_inner_html ($$$) {
7966          unless defined $p->{content_model};          unless defined $p->{content_model};
7967          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7968    
7969      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7970          ## TODO: Foreign element OK?
7971    
7972      ## Step 4      ## Step 3
7973      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7974        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7975    
7976      ## Step 5 # MUST      ## Step 4 # MUST
7977      $doc->append_child ($root);      $doc->append_child ($root);
7978    
7979      ## Step 6 # MUST      ## Step 5 # MUST
7980      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7981    
7982      undef $p->{head_element};      undef $p->{head_element};
7983    
7984      ## Step 7 # MUST      ## Step 6 # MUST
7985      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7986    
7987      ## Step 8 # MUST      ## Step 7 # MUST
7988      my $anode = $node;      my $anode = $node;
7989      AN: while (defined $anode) {      AN: while (defined $anode) {
7990        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7991          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7992          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7993            if ($anode->local_name eq 'form') { ## TODO: case?            if ($anode->manakai_local_name eq 'form') {
7994                !!!cp ('i5');
7995              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7996              last AN;              last AN;
7997            }            }
# Line 5418  sub set_inner_html ($$$) { Line 8000  sub set_inner_html ($$$) {
8000        $anode = $anode->parent_node;        $anode = $anode->parent_node;
8001      } # AN      } # AN
8002            
8003      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
8004      {      {
8005        my $self = $p;        my $self = $p;
8006        !!!next-token;        !!!next-token;
8007      }      }
8008      $p->_tree_construction_main;      $p->_tree_construction_main;
8009    
8010      ## Step 11 # MUST      ## Step 10 # MUST
8011      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
8012      for (@cn) {      for (@cn) {
8013        $node->remove_child ($_);        $node->remove_child ($_);
8014      }      }
8015      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
8016    
8017      ## Step 12 # MUST      ## Step 11 # MUST
8018      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
8019      for (@cn) {      for (@cn) {
8020        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5442  sub set_inner_html ($$$) { Line 8023  sub set_inner_html ($$$) {
8023      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8024    
8025      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8026    
8027        delete $p->{parse_error}; # delete loop
8028    } else {    } else {
8029      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";
8030    }    }

Legend:
Removed from v.1.65  
changed lines
  Added in v.1.190

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24