/[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.78 by wakaba, Mon Mar 3 11:56:18 2008 UTC revision 1.184 by wakaba, Mon Sep 15 09:02:27 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  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
23  ## TODO: 1252 parse error (revision 1264)  
24  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
25    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
26  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
27    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
28    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
29    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
30    hr => 1,  
31    br => 1,  sub A_EL () { 0b1 }
32    img => 1,  sub ADDRESS_EL () { 0b10 }
33    embed => 1,  sub BODY_EL () { 0b100 }
34    param => 1,  sub BUTTON_EL () { 0b1000 }
35    area => 1,  sub CAPTION_EL () { 0b10000 }
36    col => 1,  sub DD_EL () { 0b100000 }
37    input => 1,  sub DIV_EL () { 0b1000000 }
38    sub DT_EL () { 0b10000000 }
39    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 61  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          ## ISSUE: Unsupported encoding is not ignored according to the spec.
405          ($char_stream, $e_status) = $charset->get_decode_handle
406              ($byte_stream, allow_error_reporting => 1,
407               allow_fallback => 1);
408          if ($char_stream) {
409            $self->{confident} = 1;
410            last SNIFFING;
411          } else {
412            ## TODO: unsupported error
413          }
414      }      }
415    
416      ## Step 2      ## Step 2
417      if (defined $self->{input_encoding} and      my $byte_buffer = '';
418          $self->{input_encoding} eq $charset) {      for (1..1024) {
419          my $char = $byte_stream->getc;
420          last unless defined $char;
421          $byte_buffer .= $char;
422        } ## TODO: timeout
423    
424        ## Step 3
425        if ($byte_buffer =~ /^\xFE\xFF/) {
426          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
427          ($char_stream, $e_status) = $charset->get_decode_handle
428              ($byte_stream, allow_error_reporting => 1,
429               allow_fallback => 1, byte_buffer => \$byte_buffer);
430        $self->{confident} = 1;        $self->{confident} = 1;
431        return;        last SNIFFING;
432        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
433          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
434          ($char_stream, $e_status) = $charset->get_decode_handle
435              ($byte_stream, allow_error_reporting => 1,
436               allow_fallback => 1, byte_buffer => \$byte_buffer);
437          $self->{confident} = 1;
438          last SNIFFING;
439        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
440          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
441          ($char_stream, $e_status) = $charset->get_decode_handle
442              ($byte_stream, allow_error_reporting => 1,
443               allow_fallback => 1, byte_buffer => \$byte_buffer);
444          $self->{confident} = 1;
445          last SNIFFING;
446      }      }
447    
448      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
449          ':'.$charset, level => 'w');      ## TODO: <meta charset>
450    
451      ## Step 3      ## Step 5
452      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
453    
454      ## Step 4      ## Step 6
455      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
456        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
457            ($byte_buffer);
458        if (defined $charset_name) {
459          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
460    
461          ## ISSUE: Unsupported encoding is not ignored according to the spec.
462          require Whatpm::Charset::DecodeHandle;
463          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
464              ($byte_stream);
465          ($char_stream, $e_status) = $charset->get_decode_handle
466              ($buffer, allow_error_reporting => 1,
467               allow_fallback => 1, byte_buffer => \$byte_buffer);
468          if ($char_stream) {
469            $buffer->{buffer} = $byte_buffer;
470            !!!parse-error (type => 'sniffing:chardet',
471                            text => $charset_name,
472                            level => $self->{level}->{info},
473                            layer => 'encode',
474                            line => 1, column => 1);
475            $self->{confident} = 0;
476            last SNIFFING;
477          }
478        }
479    
480        ## Step 7: default
481        ## TODO: Make this configurable.
482        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
483            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
484            ## detectable in the step 6.
485        require Whatpm::Charset::DecodeHandle;
486        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
487            ($byte_stream);
488        ($char_stream, $e_status)
489            = $charset->get_decode_handle ($buffer,
490                                           allow_error_reporting => 1,
491                                           allow_fallback => 1,
492                                           byte_buffer => \$byte_buffer);
493        $buffer->{buffer} = $byte_buffer;
494        !!!parse-error (type => 'sniffing:default',
495                        text => 'windows-1252',
496                        level => $self->{level}->{info},
497                        line => 1, column => 1,
498                        layer => 'encode');
499        $self->{confident} = 0;
500      } # SNIFFING
501    
502      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
503        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
504        !!!parse-error (type => 'chardecode:fallback',
505                        #text => $self->{input_encoding},
506                        level => $self->{level}->{uncertain},
507                        line => 1, column => 1,
508                        layer => 'encode');
509      } elsif (not ($e_status &
510                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
511        $self->{input_encoding} = $charset->get_iana_name;
512        !!!parse-error (type => 'chardecode:no error',
513                        text => $self->{input_encoding},
514                        level => $self->{level}->{uncertain},
515                        line => 1, column => 1,
516                        layer => 'encode');
517      } else {
518        $self->{input_encoding} = $charset->get_iana_name;
519      }
520    
521      $self->{change_encoding} = sub {
522        my $self = shift;
523        $charset_name = shift;
524        my $token = shift;
525    
526        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
527        ($char_stream, $e_status) = $charset->get_decode_handle
528            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
529             byte_buffer => \ $buffer->{buffer});
530        
531        if ($char_stream) { # if supported
532          ## "Change the encoding" algorithm:
533    
534          ## Step 1    
535          if ($charset->{category} &
536              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
537            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
538            ($char_stream, $e_status) = $charset->get_decode_handle
539                ($byte_stream,
540                 byte_buffer => \ $buffer->{buffer});
541          }
542          $charset_name = $charset->get_iana_name;
543          
544          ## Step 2
545          if (defined $self->{input_encoding} and
546              $self->{input_encoding} eq $charset_name) {
547            !!!parse-error (type => 'charset label:matching',
548                            text => $charset_name,
549                            level => $self->{level}->{info});
550            $self->{confident} = 1;
551            return;
552          }
553    
554          !!!parse-error (type => 'charset label detected',
555                          text => $self->{input_encoding},
556                          value => $charset_name,
557                          level => $self->{level}->{warn},
558                          token => $token);
559          
560          ## Step 3
561          # if (can) {
562            ## change the encoding on the fly.
563            #$self->{confident} = 1;
564            #return;
565          # }
566          
567          ## Step 4
568          throw Whatpm::HTML::RestartParser ();
569        }
570    }; # $self->{change_encoding}    }; # $self->{change_encoding}
571    
572    my @args = @_; shift @args; # $s    my $char_onerror = sub {
573        my (undef, $type, %opt) = @_;
574        !!!parse-error (layer => 'encode',
575                        line => $self->{line}, column => $self->{column} + 1,
576                        %opt, type => $type);
577        if ($opt{octets}) {
578          ${$opt{octets}} = "\x{FFFD}"; # relacement character
579        }
580      };
581    
582      my $wrapped_char_stream = $get_wrapper->($char_stream);
583      $wrapped_char_stream->onerror ($char_onerror);
584    
585      my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
586    my $return;    my $return;
587    try {    try {
588      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
589    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
590      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
591      $s = \ (Encode::decode ($charset, $$bytes_s));      
592      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
593          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
594          !!!parse-error (type => 'chardecode:fallback',
595                          level => $self->{level}->{uncertain},
596                          #text => $self->{input_encoding},
597                          line => 1, column => 1,
598                          layer => 'encode');
599        } elsif (not ($e_status &
600                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
601          $self->{input_encoding} = $charset->get_iana_name;
602          !!!parse-error (type => 'chardecode:no error',
603                          text => $self->{input_encoding},
604                          level => $self->{level}->{uncertain},
605                          line => 1, column => 1,
606                          layer => 'encode');
607        } else {
608          $self->{input_encoding} = $charset->get_iana_name;
609        }
610      $self->{confident} = 1;      $self->{confident} = 1;
611      $return = $self->parse_char_string ($s, @args);  
612        $wrapped_char_stream = $get_wrapper->($char_stream);
613        $wrapped_char_stream->onerror ($char_onerror);
614    
615        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
616    };    };
617    return $return;    return $return;
618  } # parse_byte_string  } # parse_byte_stream
619    
620  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
621  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 626  sub parse_byte_string ($$$$;$) {
626  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
627  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
628    
629  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
630      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
631      my $self = shift;
632      my $s = ref $_[0] ? $_[0] : \($_[0]);
633      require Whatpm::Charset::DecodeHandle;
634      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
635      return $self->parse_char_stream ($input, @_[1..$#_]);
636    } # parse_char_string
637    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
638    
639  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$$) {
640    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
641    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
642    $self->{document} = $_[1];    $self->{document} = $_[1];
643    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
644    
# Line 175  sub parse_string ($$$;$) { Line 647  sub parse_string ($$$;$) {
647    $self->{confident} = 1 unless exists $self->{confident};    $self->{confident} = 1 unless exists $self->{confident};
648    $self->{document}->input_encoding ($self->{input_encoding})    $self->{document}->input_encoding ($self->{input_encoding})
649        if defined $self->{input_encoding};        if defined $self->{input_encoding};
650    ## TODO: |{input_encoding}| is needless?
651    
652    my $i = 0;    $self->{line_prev} = $self->{line} = 1;
653    my $line = 1;    $self->{column_prev} = -1;
654    my $column = 0;    $self->{column} = 0;
655    $self->{set_next_char} = sub {    $self->{set_nc} = sub {
656      my $self = shift;      my $self = shift;
657    
658      pop @{$self->{prev_char}};      my $char = '';
659      unshift @{$self->{prev_char}}, $self->{next_char};      if (defined $self->{next_nc}) {
660          $char = $self->{next_nc};
661          delete $self->{next_nc};
662          $self->{nc} = ord $char;
663        } else {
664          $self->{char_buffer} = '';
665          $self->{char_buffer_pos} = 0;
666    
667          my $count = $input->manakai_read_until
668             ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
669          if ($count) {
670            $self->{line_prev} = $self->{line};
671            $self->{column_prev} = $self->{column};
672            $self->{column}++;
673            $self->{nc}
674                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
675            return;
676          }
677    
678          if ($input->read ($char, 1)) {
679            $self->{nc} = ord $char;
680          } else {
681            $self->{nc} = -1;
682            return;
683          }
684        }
685    
686      $self->{next_char} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
687      $self->{next_char} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
688      $column++;      $self->{column}++;
689            
690      if ($self->{next_char} == 0x000A) { # LF      if ($self->{nc} == 0x000A) { # LF
691        $line++;        !!!cp ('j1');
692        $column = 0;        $self->{line}++;
693      } elsif ($self->{next_char} == 0x000D) { # CR        $self->{column} = 0;
694        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{nc} == 0x000D) { # CR
695        $self->{next_char} = 0x000A; # LF # MUST        !!!cp ('j2');
696        $line++;  ## TODO: support for abort/streaming
697        $column = 0;        my $next = '';
698      } elsif ($self->{next_char} > 0x10FFFF) {        if ($input->read ($next, 1) and $next ne "\x0A") {
699        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_nc} = $next;
700      } elsif ($self->{next_char} == 0x0000) { # NULL        }
701          $self->{nc} = 0x000A; # LF # MUST
702          $self->{line}++;
703          $self->{column} = 0;
704        } elsif ($self->{nc} == 0x0000) { # NULL
705          !!!cp ('j4');
706        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
707        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
708      }      }
709    };    };
710    $self->{prev_char} = [-1, -1, -1];  
711    $self->{next_char} = -1;    $self->{read_until} = sub {
712        #my ($scalar, $specials_range, $offset) = @_;
713        return 0 if defined $self->{next_nc};
714    
715        my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
716        my $offset = $_[2] || 0;
717    
718        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
719          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
720          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
721            substr ($_[0], $offset)
722                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
723            my $count = $+[0] - $-[0];
724            if ($count) {
725              $self->{column} += $count;
726              $self->{char_buffer_pos} += $count;
727              $self->{line_prev} = $self->{line};
728              $self->{column_prev} = $self->{column} - 1;
729              $self->{nc} = -1;
730            }
731            return $count;
732          } else {
733            return 0;
734          }
735        } else {
736          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
737          if ($count) {
738            $self->{column} += $count;
739            $self->{line_prev} = $self->{line};
740            $self->{column_prev} = $self->{column} - 1;
741            $self->{nc} = -1;
742          }
743          return $count;
744        }
745      }; # $self->{read_until}
746    
747    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
748      my (%opt) = @_;      my (%opt) = @_;
749      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
750        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
751        warn "Parse error ($opt{type}) at line $line column $column\n";
752    };    };
753    $self->{parse_error} = sub {    $self->{parse_error} = sub {
754      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
755    };    };
756    
757      my $char_onerror = sub {
758        my (undef, $type, %opt) = @_;
759        !!!parse-error (layer => 'encode',
760                        line => $self->{line}, column => $self->{column} + 1,
761                        %opt, type => $type);
762      }; # $char_onerror
763    
764      if ($_[3]) {
765        $input = $_[3]->($input);
766        $input->onerror ($char_onerror);
767      } else {
768        $input->onerror ($char_onerror) unless defined $input->onerror;
769      }
770    
771    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
772    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
773    $self->_construct_tree;    $self->_construct_tree;
774    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
775    
776      delete $self->{parse_error}; # remove loop
777    
778    return $self->{document};    return $self->{document};
779  } # parse_string  } # parse_char_stream
780    
781  sub new ($) {  sub new ($) {
782    my $class = shift;    my $class = shift;
783    my $self = bless {}, $class;    my $self = bless {
784    $self->{set_next_char} = sub {      level => {must => 'm',
785      $self->{next_char} = -1;                should => 's',
786                  warn => 'w',
787                  info => 'i',
788                  uncertain => 'u'},
789      }, $class;
790      $self->{set_nc} = sub {
791        $self->{nc} = -1;
792    };    };
793    $self->{parse_error} = sub {    $self->{parse_error} = sub {
794      #      #
# Line 254  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 815  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
815  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
816    
817  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
818  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
819  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
820  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
821  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 265  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 826  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
826  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
827  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
828  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
829  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
830  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
831  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
832  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 848  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
848  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
849  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
850  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
851    sub SELF_CLOSING_START_TAG_STATE () { 34 }
852    sub CDATA_SECTION_STATE () { 35 }
853    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
854    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
855    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
856    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
857    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
858    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
859    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
860    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
861    ## NOTE: "Entity data state", "entity in attribute value state", and
862    ## "consume a character reference" algorithm are jointly implemented
863    ## using the following six states:
864    sub ENTITY_STATE () { 44 }
865    sub ENTITY_HASH_STATE () { 45 }
866    sub NCR_NUM_STATE () { 46 }
867    sub HEXREF_X_STATE () { 47 }
868    sub HEXREF_HEX_STATE () { 48 }
869    sub ENTITY_NAME_STATE () { 49 }
870    
871  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
872  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 883  sub TABLE_IMS ()      { 0b1000000 }
883  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
884  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
885  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
886    sub SELECT_IMS ()     { 0b10000000000 }
887    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
888        ## NOTE: "in foreign content" insertion mode is special; it is combined
889        ## with the secondary insertion mode.  In this parser, they are stored
890        ## together in the bit-or'ed form.
891    
892    ## NOTE: "initial" and "before html" insertion modes have no constants.
893    
894    ## NOTE: "after after body" insertion mode.
895  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
896    
897    ## NOTE: "after after frameset" insertion mode.
898  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
899    
900  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
901  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
902  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 319  sub IN_TABLE_IM () { TABLE_IMS } Line 910  sub IN_TABLE_IM () { TABLE_IMS }
910  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
911  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
912  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
913  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
914    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
915  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
916    
917  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 327  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 919  sub IN_COLUMN_GROUP_IM () { 0b10 }
919  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
920    my $self = shift;    my $self = shift;
921    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
922      #$self->{s_kwd}; # state keyword - initialized when used
923      #$self->{entity__value}; # initialized when used
924      #$self->{entity__match}; # initialized when used
925    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
926    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{ct}; # current token
927    undef $self->{current_attribute};    undef $self->{ca}; # current attribute
928    undef $self->{last_emitted_start_tag_name};    undef $self->{last_stag_name}; # last emitted start tag name
929    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
930    $self->{char} = [];    delete $self->{self_closing};
931    # $self->{next_char}    $self->{char_buffer} = '';
932      $self->{char_buffer_pos} = 0;
933      $self->{nc} = -1; # next input character
934      #$self->{next_nc}
935    !!!next-input-character;    !!!next-input-character;
936    $self->{token} = [];    $self->{token} = [];
937    # $self->{escape}    # $self->{escape}
# Line 344  sub _initialize_tokenizer ($) { Line 942  sub _initialize_tokenizer ($) {
942  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
943  ##   ->{name} (DOCTYPE_TOKEN)  ##   ->{name} (DOCTYPE_TOKEN)
944  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
945  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{pubid} (DOCTYPE_TOKEN)
946  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{sysid} (DOCTYPE_TOKEN)
947  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
948  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
949  ##        ->{name}  ##        ->{name}
950  ##        ->{value}  ##        ->{value}
951  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
952  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
953    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
954    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
955    ##     while the token is pushed back to the stack.
956    
957  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
958    
# Line 361  sub _initialize_tokenizer ($) { Line 962  sub _initialize_tokenizer ($) {
962  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
963  ## and removed from the list.  ## and removed from the list.
964    
965  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
966  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
 ## contains some requirements that are not detected by the  
 ## parsing algorithm:  
 ## - Some requirements on character encoding declarations. ## TODO  
 ## - "Elements MUST NOT contain content that their content model disallows."  
 ##   ... Some are parse error, some are not (will be reported by c.c.).  
 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO  
 ## - Text (in elements, attributes, and comments) SHOULD NOT contain  
 ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)  
   
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
967    
968  sub _get_next_token ($) {  sub _get_next_token ($) {
969    my $self = shift;    my $self = shift;
970    
971      if ($self->{self_closing}) {
972        !!!parse-error (type => 'nestc', token => $self->{ct});
973        ## NOTE: The |self_closing| flag is only set by start tag token.
974        ## In addition, when a start tag token is emitted, it is always set to
975        ## |ct|.
976        delete $self->{self_closing};
977      }
978    
979    if (@{$self->{token}}) {    if (@{$self->{token}}) {
980        $self->{self_closing} = $self->{token}->[0]->{self_closing};
981      return shift @{$self->{token}};      return shift @{$self->{token}};
982    }    }
983    
984    A: {    A: {
985      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
986        if ($self->{next_char} == 0x0026) { # &        if ($self->{nc} == 0x0026) { # &
987            delete $self->{s_kwd};
988          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
989              not $self->{escape}) {              not $self->{escape}) {
990            !!!cp (1);            !!!cp (1);
991            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
992              ## "entity data state".  In this implementation, the tokenizer
993              ## is switched to the |ENTITY_STATE|, which is an implementation
994              ## of the "consume a character reference" algorithm.
995              $self->{entity_add} = -1;
996              $self->{prev_state} = DATA_STATE;
997              $self->{state} = ENTITY_STATE;
998            !!!next-input-character;            !!!next-input-character;
999            redo A;            redo A;
1000          } else {          } else {
1001            !!!cp (2);            !!!cp (2);
1002            #            #
1003          }          }
1004        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
1005          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1006            unless ($self->{escape}) {            if (defined $self->{s_kwd}) {
1007              if ($self->{prev_char}->[0] == 0x002D and # -              !!!cp (2.1);
1008                  $self->{prev_char}->[1] == 0x0021 and # !              $self->{s_kwd} .= '-';
1009                  $self->{prev_char}->[2] == 0x003C) { # <            } else {
1010                !!!cp (3);              !!!cp (2.2);
1011                $self->{escape} = 1;              $self->{s_kwd} = '-';
1012              } else {            }
1013                !!!cp (4);  
1014              }            if ($self->{s_kwd} eq '<!--') {
1015                !!!cp (3);
1016                $self->{escape} = 1; # unless $self->{escape};
1017                $self->{s_kwd} = '--';
1018                #
1019              } elsif ($self->{s_kwd} eq '---') {
1020                !!!cp (4);
1021                $self->{s_kwd} = '--';
1022                #
1023            } else {            } else {
1024              !!!cp (5);              !!!cp (5);
1025                #
1026            }            }
1027          }          }
1028                    
1029          #          #
1030        } elsif ($self->{next_char} == 0x003C) { # <        } elsif ($self->{nc} == 0x0021) { # !
1031            if (defined $self->{s_kwd}) {
1032              !!!cp (5.1);
1033              $self->{s_kwd} .= '!';
1034              #
1035            } else {
1036              !!!cp (5.2);
1037              #
1038            }
1039            #
1040          } elsif ($self->{nc} == 0x003C) { # <
1041            delete $self->{s_kwd};
1042          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1043              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1044               not $self->{escape})) {               not $self->{escape})) {
# Line 424  sub _get_next_token ($) { Line 1050  sub _get_next_token ($) {
1050            !!!cp (7);            !!!cp (7);
1051            #            #
1052          }          }
1053        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1054          if ($self->{escape} and          if ($self->{escape} and
1055              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1056            if ($self->{prev_char}->[0] == 0x002D and # -            if (defined $self->{s_kwd} and $self->{s_kwd} eq '--') {
               $self->{prev_char}->[1] == 0x002D) { # -  
1057              !!!cp (8);              !!!cp (8);
1058              delete $self->{escape};              delete $self->{escape};
1059            } else {            } else {
# Line 438  sub _get_next_token ($) { Line 1063  sub _get_next_token ($) {
1063            !!!cp (10);            !!!cp (10);
1064          }          }
1065                    
1066            delete $self->{s_kwd};
1067          #          #
1068        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1069          !!!cp (11);          !!!cp (11);
1070          !!!emit ({type => END_OF_FILE_TOKEN});          delete $self->{s_kwd};
1071            !!!emit ({type => END_OF_FILE_TOKEN,
1072                      line => $self->{line}, column => $self->{column}});
1073          last A; ## TODO: ok?          last A; ## TODO: ok?
1074        } else {        } else {
1075          !!!cp (12);          !!!cp (12);
1076            delete $self->{s_kwd};
1077            #
1078        }        }
1079    
1080        # Anything else        # Anything else
1081        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1082                     data => chr $self->{next_char}};                     data => chr $self->{nc},
1083                       line => $self->{line}, column => $self->{column},
1084                      };
1085          if ($self->{read_until}->($token->{data}, q[-!<>&],
1086                                    length $token->{data})) {
1087            delete $self->{s_kwd};
1088          }
1089    
1090        ## Stay in the data state        ## Stay in the data state
1091        !!!next-input-character;        !!!next-input-character;
   
1092        !!!emit ($token);        !!!emit ($token);
   
       redo A;  
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!cp (13);  
         !!!emit ({type => CHARACTER_TOKEN, data => '&'});  
       } else {  
         !!!cp (14);  
         !!!emit ($token);  
       }  
   
1093        redo A;        redo A;
1094      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1095        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1096          if ($self->{next_char} == 0x002F) { # /          if ($self->{nc} == 0x002F) { # /
1097            !!!cp (15);            !!!cp (15);
1098            !!!next-input-character;            !!!next-input-character;
1099            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1100            redo A;            redo A;
1101            } elsif ($self->{nc} == 0x0021) { # !
1102              !!!cp (15.1);
1103              $self->{s_kwd} = '<' unless $self->{escape};
1104              #
1105          } else {          } else {
1106            !!!cp (16);            !!!cp (16);
1107            ## reconsume            #
           $self->{state} = DATA_STATE;  
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<'});  
   
           redo A;  
1108          }          }
1109    
1110            ## reconsume
1111            $self->{state} = DATA_STATE;
1112            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1113                      line => $self->{line_prev},
1114                      column => $self->{column_prev},
1115                     });
1116            redo A;
1117        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1118          if ($self->{next_char} == 0x0021) { # !          if ($self->{nc} == 0x0021) { # !
1119            !!!cp (17);            !!!cp (17);
1120            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1121            !!!next-input-character;            !!!next-input-character;
1122            redo A;            redo A;
1123          } elsif ($self->{next_char} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1124            !!!cp (18);            !!!cp (18);
1125            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1126            !!!next-input-character;            !!!next-input-character;
1127            redo A;            redo A;
1128          } elsif (0x0041 <= $self->{next_char} and          } elsif (0x0041 <= $self->{nc} and
1129                   $self->{next_char} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1130            !!!cp (19);            !!!cp (19);
1131            $self->{current_token}            $self->{ct}
1132              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1133                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{nc} + 0x0020),
1134                   line => $self->{line_prev},
1135                   column => $self->{column_prev}};
1136            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1137            !!!next-input-character;            !!!next-input-character;
1138            redo A;            redo A;
1139          } elsif (0x0061 <= $self->{next_char} and          } elsif (0x0061 <= $self->{nc} and
1140                   $self->{next_char} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1141            !!!cp (20);            !!!cp (20);
1142            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{ct} = {type => START_TAG_TOKEN,
1143                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{nc}),
1144                                        line => $self->{line_prev},
1145                                        column => $self->{column_prev}};
1146            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1147            !!!next-input-character;            !!!next-input-character;
1148            redo A;            redo A;
1149          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1150            !!!cp (21);            !!!cp (21);
1151            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1152                              line => $self->{line_prev},
1153                              column => $self->{column_prev});
1154            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1155            !!!next-input-character;            !!!next-input-character;
1156    
1157            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1158                        line => $self->{line_prev},
1159                        column => $self->{column_prev},
1160                       });
1161    
1162            redo A;            redo A;
1163          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1164            !!!cp (22);            !!!cp (22);
1165            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1166                              line => $self->{line_prev},
1167                              column => $self->{column_prev});
1168            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1169            ## $self->{next_char} is intentionally left as is            $self->{ct} = {type => COMMENT_TOKEN, data => '',
1170                                        line => $self->{line_prev},
1171                                        column => $self->{column_prev},
1172                                       };
1173              ## $self->{nc} is intentionally left as is
1174            redo A;            redo A;
1175          } else {          } else {
1176            !!!cp (23);            !!!cp (23);
1177            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1178                              line => $self->{line_prev},
1179                              column => $self->{column_prev});
1180            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1181            ## reconsume            ## reconsume
1182    
1183            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1184                        line => $self->{line_prev},
1185                        column => $self->{column_prev},
1186                       });
1187    
1188            redo A;            redo A;
1189          }          }
# Line 545  sub _get_next_token ($) { Line 1191  sub _get_next_token ($) {
1191          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1192        }        }
1193      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1194        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        ## NOTE: The "close tag open state" in the spec is implemented as
1195          if (defined $self->{last_emitted_start_tag_name}) {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_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_char};  
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_char} == $c or $self->{next_char} == $C) {  
               !!!cp (24);  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               !!!cp (25);  
               $self->{next_char} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
1196    
1197                !!!emit ({type => CHARACTER_TOKEN, data => '</'});        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1198            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1199                redo A;          if (defined $self->{last_stag_name}) {
1200              }            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1201            }            $self->{s_kwd} = '';
1202            push @next_char, $self->{next_char};            ## Reconsume.
1203                    redo A;
           unless ($self->{next_char} == 0x0009 or # HT  
                   $self->{next_char} == 0x000A or # LF  
                   $self->{next_char} == 0x000B or # VT  
                   $self->{next_char} == 0x000C or # FF  
                   $self->{next_char} == 0x0020 or # SP  
                   $self->{next_char} == 0x003E or # >  
                   $self->{next_char} == 0x002F or # /  
                   $self->{next_char} == -1) {  
             !!!cp (26);  
             $self->{next_char} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
             redo A;  
           } else {  
             !!!cp (27);  
             $self->{next_char} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1204          } else {          } else {
1205            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1206              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1207            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1208            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1209            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1210              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1211                        line => $l, column => $c,
1212                       });
1213            redo A;            redo A;
1214          }          }
1215        }        }
1216          
1217        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{nc} and
1218            $self->{next_char} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1219          !!!cp (29);          !!!cp (29);
1220          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{ct}
1221                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1222                   tag_name => chr ($self->{nc} + 0x0020),
1223                   line => $l, column => $c};
1224          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1225          !!!next-input-character;          !!!next-input-character;
1226          redo A;          redo A;
1227        } elsif (0x0061 <= $self->{next_char} and        } elsif (0x0061 <= $self->{nc} and
1228                 $self->{next_char} <= 0x007A) { # a..z                 $self->{nc} <= 0x007A) { # a..z
1229          !!!cp (30);          !!!cp (30);
1230          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{ct} = {type => END_TAG_TOKEN,
1231                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{nc}),
1232                                      line => $l, column => $c};
1233          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1234          !!!next-input-character;          !!!next-input-character;
1235          redo A;          redo A;
1236        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1237          !!!cp (31);          !!!cp (31);
1238          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1239                            line => $self->{line_prev}, ## "<" in "</>"
1240                            column => $self->{column_prev} - 1);
1241          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1242          !!!next-input-character;          !!!next-input-character;
1243          redo A;          redo A;
1244        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1245          !!!cp (32);          !!!cp (32);
1246          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1247          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1248          # reconsume          # reconsume
1249    
1250          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1251                      line => $l, column => $c,
1252                     });
1253    
1254          redo A;          redo A;
1255        } else {        } else {
1256          !!!cp (33);          !!!cp (33);
1257          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1258          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1259          ## $self->{next_char} is intentionally left as is          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1260          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1261                                      column => $self->{column_prev} - 1,
1262                                     };
1263            ## NOTE: $self->{nc} is intentionally left as is.
1264            ## Although the "anything else" case of the spec not explicitly
1265            ## states that the next input character is to be reconsumed,
1266            ## it will be included to the |data| of the comment token
1267            ## generated from the bogus end tag, as defined in the
1268            ## "bogus comment state" entry.
1269            redo A;
1270          }
1271        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1272          my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1273          if (length $ch) {
1274            my $CH = $ch;
1275            $ch =~ tr/a-z/A-Z/;
1276            my $nch = chr $self->{nc};
1277            if ($nch eq $ch or $nch eq $CH) {
1278              !!!cp (24);
1279              ## Stay in the state.
1280              $self->{s_kwd} .= $nch;
1281              !!!next-input-character;
1282              redo A;
1283            } else {
1284              !!!cp (25);
1285              $self->{state} = DATA_STATE;
1286              ## Reconsume.
1287              !!!emit ({type => CHARACTER_TOKEN,
1288                        data => '</' . $self->{s_kwd},
1289                        line => $self->{line_prev},
1290                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1291                       });
1292              redo A;
1293            }
1294          } else { # after "<{tag-name}"
1295            unless ({
1296                     0x0009 => 1, # HT
1297                     0x000A => 1, # LF
1298                     0x000B => 1, # VT
1299                     0x000C => 1, # FF
1300                     0x0020 => 1, # SP
1301                     0x003E => 1, # >
1302                     0x002F => 1, # /
1303                     -1 => 1, # EOF
1304                    }->{$self->{nc}}) {
1305              !!!cp (26);
1306              ## Reconsume.
1307              $self->{state} = DATA_STATE;
1308              !!!emit ({type => CHARACTER_TOKEN,
1309                        data => '</' . $self->{s_kwd},
1310                        line => $self->{line_prev},
1311                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1312                       });
1313              redo A;
1314            } else {
1315              !!!cp (27);
1316              $self->{ct}
1317                  = {type => END_TAG_TOKEN,
1318                     tag_name => $self->{last_stag_name},
1319                     line => $self->{line_prev},
1320                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1321              $self->{state} = TAG_NAME_STATE;
1322              ## Reconsume.
1323              redo A;
1324            }
1325        }        }
1326      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1327        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1328            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1329            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1330            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1331            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1332          !!!cp (34);          !!!cp (34);
1333          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1334          !!!next-input-character;          !!!next-input-character;
1335          redo A;          redo A;
1336        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1337          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1338            !!!cp (35);            !!!cp (35);
1339            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1340                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1341            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1342            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1343            #  ## NOTE: This should never be reached.            #  ## NOTE: This should never be reached.
1344            #  !!! cp (36);            #  !!! cp (36);
1345            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 664  sub _get_next_token ($) { Line 1347  sub _get_next_token ($) {
1347              !!!cp (37);              !!!cp (37);
1348            #}            #}
1349          } else {          } else {
1350            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1351          }          }
1352          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1353          !!!next-input-character;          !!!next-input-character;
1354    
1355          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1356    
1357          redo A;          redo A;
1358        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1359                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1360          !!!cp (38);          !!!cp (38);
1361          $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);          $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1362            # start tag or end tag            # start tag or end tag
1363          ## Stay in this state          ## Stay in this state
1364          !!!next-input-character;          !!!next-input-character;
1365          redo A;          redo A;
1366        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1367          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1368          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1369            !!!cp (39);            !!!cp (39);
1370            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1371                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1372            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1373            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1374            #  ## NOTE: This state should never be reached.            #  ## NOTE: This state should never be reached.
1375            #  !!! cp (40);            #  !!! cp (40);
1376            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 697  sub _get_next_token ($) { Line 1378  sub _get_next_token ($) {
1378              !!!cp (41);              !!!cp (41);
1379            #}            #}
1380          } else {          } else {
1381            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1382          }          }
1383          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1384          # reconsume          # reconsume
1385    
1386          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1387    
1388          redo A;          redo A;
1389        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1390            !!!cp (42);
1391            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1392          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (42);  
           #  
         } else {  
           !!!cp (43);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1393          redo A;          redo A;
1394        } else {        } else {
1395          !!!cp (44);          !!!cp (44);
1396          $self->{current_token}->{tag_name} .= chr $self->{next_char};          $self->{ct}->{tag_name} .= chr $self->{nc};
1397            # start tag or end tag            # start tag or end tag
1398          ## Stay in the state          ## Stay in the state
1399          !!!next-input-character;          !!!next-input-character;
1400          redo A;          redo A;
1401        }        }
1402      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1403        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1404            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1405            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1406            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1407            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1408          !!!cp (45);          !!!cp (45);
1409          ## Stay in the state          ## Stay in the state
1410          !!!next-input-character;          !!!next-input-character;
1411          redo A;          redo A;
1412        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1413          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1414            !!!cp (46);            !!!cp (46);
1415            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1416                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1417            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1418            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1419              !!!cp (47);              !!!cp (47);
1420              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1421            } else {            } else {
1422              !!!cp (48);              !!!cp (48);
1423            }            }
1424          } else {          } else {
1425            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1426          }          }
1427          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1428          !!!next-input-character;          !!!next-input-character;
1429    
1430          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1431    
1432          redo A;          redo A;
1433        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1434                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1435          !!!cp (49);          !!!cp (49);
1436          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{ca}
1437                                value => ''};              = {name => chr ($self->{nc} + 0x0020),
1438                   value => '',
1439                   line => $self->{line}, column => $self->{column}};
1440          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1441          !!!next-input-character;          !!!next-input-character;
1442          redo A;          redo A;
1443        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1444            !!!cp (50);
1445            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1446          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (50);  
           #  
         } else {  
           !!!cp (51);  
           !!!parse-error (type => 'nestc');  
         }  
         ## Stay in the state  
         # next-input-character is already done  
1447          redo A;          redo A;
1448        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1449          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1450          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1451            !!!cp (52);            !!!cp (52);
1452            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1453                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1454            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1455            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1456              !!!cp (53);              !!!cp (53);
1457              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1458            } else {            } else {
1459              !!!cp (54);              !!!cp (54);
1460            }            }
1461          } else {          } else {
1462            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1463          }          }
1464          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1465          # reconsume          # reconsume
1466    
1467          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1468    
1469          redo A;          redo A;
1470        } else {        } else {
# Line 813  sub _get_next_token ($) { Line 1472  sub _get_next_token ($) {
1472               0x0022 => 1, # "               0x0022 => 1, # "
1473               0x0027 => 1, # '               0x0027 => 1, # '
1474               0x003D => 1, # =               0x003D => 1, # =
1475              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1476            !!!cp (55);            !!!cp (55);
1477            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1478          } else {          } else {
1479            !!!cp (56);            !!!cp (56);
1480          }          }
1481          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{ca}
1482                                value => ''};              = {name => chr ($self->{nc}),
1483                   value => '',
1484                   line => $self->{line}, column => $self->{column}};
1485          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1486          !!!next-input-character;          !!!next-input-character;
1487          redo A;          redo A;
1488        }        }
1489      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1490        my $before_leave = sub {        my $before_leave = sub {
1491          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1492              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1493            !!!cp (57);            !!!cp (57);
1494            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute', text => $self->{ca}->{name}, line => $self->{ca}->{line}, column => $self->{ca}->{column});
1495            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{ca} # MUST
1496          } else {          } else {
1497            !!!cp (58);            !!!cp (58);
1498            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{ct}->{attributes}->{$self->{ca}->{name}}
1499              = $self->{current_attribute};              = $self->{ca};
1500          }          }
1501        }; # $before_leave        }; # $before_leave
1502    
1503        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1504            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1505            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1506            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1507            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1508          !!!cp (59);          !!!cp (59);
1509          $before_leave->();          $before_leave->();
1510          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1511          !!!next-input-character;          !!!next-input-character;
1512          redo A;          redo A;
1513        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1514          !!!cp (60);          !!!cp (60);
1515          $before_leave->();          $before_leave->();
1516          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1517          !!!next-input-character;          !!!next-input-character;
1518          redo A;          redo A;
1519        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1520          $before_leave->();          $before_leave->();
1521          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1522            !!!cp (61);            !!!cp (61);
1523            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1524                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1525            !!!cp (62);            !!!cp (62);
1526            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1527            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1528              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1529            }            }
1530          } else {          } else {
1531            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1532          }          }
1533          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1534          !!!next-input-character;          !!!next-input-character;
1535    
1536          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1537    
1538          redo A;          redo A;
1539        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1540                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1541          !!!cp (63);          !!!cp (63);
1542          $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);          $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1543          ## Stay in the state          ## Stay in the state
1544          !!!next-input-character;          !!!next-input-character;
1545          redo A;          redo A;
1546        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1547            !!!cp (64);
1548          $before_leave->();          $before_leave->();
1549            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1550          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (64);  
           #  
         } else {  
           !!!cp (65);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1551          redo A;          redo A;
1552        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1553          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1554          $before_leave->();          $before_leave->();
1555          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1556            !!!cp (66);            !!!cp (66);
1557            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1558                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1559            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1560            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1561              !!!cp (67);              !!!cp (67);
1562              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1563            } else {            } else {
# Line 918  sub _get_next_token ($) { Line 1565  sub _get_next_token ($) {
1565              !!!cp (68);              !!!cp (68);
1566            }            }
1567          } else {          } else {
1568            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1569          }          }
1570          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1571          # reconsume          # reconsume
1572    
1573          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1574    
1575          redo A;          redo A;
1576        } else {        } else {
1577          if ($self->{next_char} == 0x0022 or # "          if ($self->{nc} == 0x0022 or # "
1578              $self->{next_char} == 0x0027) { # '              $self->{nc} == 0x0027) { # '
1579            !!!cp (69);            !!!cp (69);
1580            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1581          } else {          } else {
1582            !!!cp (70);            !!!cp (70);
1583          }          }
1584          $self->{current_attribute}->{name} .= chr ($self->{next_char});          $self->{ca}->{name} .= chr ($self->{nc});
1585          ## Stay in the state          ## Stay in the state
1586          !!!next-input-character;          !!!next-input-character;
1587          redo A;          redo A;
1588        }        }
1589      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1590        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1591            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1592            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1593            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1594            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1595          !!!cp (71);          !!!cp (71);
1596          ## Stay in the state          ## Stay in the state
1597          !!!next-input-character;          !!!next-input-character;
1598          redo A;          redo A;
1599        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1600          !!!cp (72);          !!!cp (72);
1601          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1602          !!!next-input-character;          !!!next-input-character;
1603          redo A;          redo A;
1604        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1605          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1606            !!!cp (73);            !!!cp (73);
1607            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1608                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1609            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1610            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1611              !!!cp (74);              !!!cp (74);
1612              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1613            } else {            } else {
# Line 970  sub _get_next_token ($) { Line 1615  sub _get_next_token ($) {
1615              !!!cp (75);              !!!cp (75);
1616            }            }
1617          } else {          } else {
1618            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1619          }          }
1620          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1621          !!!next-input-character;          !!!next-input-character;
1622    
1623          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1624    
1625          redo A;          redo A;
1626        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1627                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1628          !!!cp (76);          !!!cp (76);
1629          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{ca}
1630                                value => ''};              = {name => chr ($self->{nc} + 0x0020),
1631                   value => '',
1632                   line => $self->{line}, column => $self->{column}};
1633          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1634          !!!next-input-character;          !!!next-input-character;
1635          redo A;          redo A;
1636        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1637            !!!cp (77);
1638            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1639          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (77);  
           #  
         } else {  
           !!!cp (78);  
           !!!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  
1640          redo A;          redo A;
1641        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1642          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1643          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1644            !!!cp (79);            !!!cp (79);
1645            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1646                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1647            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1648            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1649              !!!cp (80);              !!!cp (80);
1650              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1651            } else {            } else {
# Line 1019  sub _get_next_token ($) { Line 1653  sub _get_next_token ($) {
1653              !!!cp (81);              !!!cp (81);
1654            }            }
1655          } else {          } else {
1656            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1657          }          }
1658          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1659          # reconsume          # reconsume
1660    
1661          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1662    
1663          redo A;          redo A;
1664        } else {        } else {
1665          !!!cp (82);          if ($self->{nc} == 0x0022 or # "
1666          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{nc} == 0x0027) { # '
1667                                value => ''};            !!!cp (78);
1668              !!!parse-error (type => 'bad attribute name');
1669            } else {
1670              !!!cp (82);
1671            }
1672            $self->{ca}
1673                = {name => chr ($self->{nc}),
1674                   value => '',
1675                   line => $self->{line}, column => $self->{column}};
1676          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1677          !!!next-input-character;          !!!next-input-character;
1678          redo A;                  redo A;        
1679        }        }
1680      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1681        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1682            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1683            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1684            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1685            $self->{next_char} == 0x0020) { # SP                  $self->{nc} == 0x0020) { # SP      
1686          !!!cp (83);          !!!cp (83);
1687          ## Stay in the state          ## Stay in the state
1688          !!!next-input-character;          !!!next-input-character;
1689          redo A;          redo A;
1690        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
1691          !!!cp (84);          !!!cp (84);
1692          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1693          !!!next-input-character;          !!!next-input-character;
1694          redo A;          redo A;
1695        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1696          !!!cp (85);          !!!cp (85);
1697          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1698          ## reconsume          ## reconsume
1699          redo A;          redo A;
1700        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
1701          !!!cp (86);          !!!cp (86);
1702          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1703          !!!next-input-character;          !!!next-input-character;
1704          redo A;          redo A;
1705        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1706          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          !!!parse-error (type => 'empty unquoted attribute value');
1707            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1708            !!!cp (87);            !!!cp (87);
1709            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1710                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1711            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1712            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1713              !!!cp (88);              !!!cp (88);
1714              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1715            } else {            } else {
# Line 1076  sub _get_next_token ($) { Line 1717  sub _get_next_token ($) {
1717              !!!cp (89);              !!!cp (89);
1718            }            }
1719          } else {          } else {
1720            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1721          }          }
1722          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1723          !!!next-input-character;          !!!next-input-character;
1724    
1725          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1726    
1727          redo A;          redo A;
1728        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1729          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1730          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1731            !!!cp (90);            !!!cp (90);
1732            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1733                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1734            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1735            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1736              !!!cp (91);              !!!cp (91);
1737              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1738            } else {            } else {
# Line 1101  sub _get_next_token ($) { Line 1740  sub _get_next_token ($) {
1740              !!!cp (92);              !!!cp (92);
1741            }            }
1742          } else {          } else {
1743            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1744          }          }
1745          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1746          ## reconsume          ## reconsume
1747    
1748          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1749    
1750          redo A;          redo A;
1751        } else {        } else {
1752          if ($self->{next_char} == 0x003D) { # =          if ($self->{nc} == 0x003D) { # =
1753            !!!cp (93);            !!!cp (93);
1754            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1755          } else {          } else {
1756            !!!cp (94);            !!!cp (94);
1757          }          }
1758          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1759          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1760          !!!next-input-character;          !!!next-input-character;
1761          redo A;          redo A;
1762        }        }
1763      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1764        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
1765          !!!cp (95);          !!!cp (95);
1766          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1767          !!!next-input-character;          !!!next-input-character;
1768          redo A;          redo A;
1769        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1770          !!!cp (96);          !!!cp (96);
1771          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1772          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1773            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1774            ## implementation of the "consume a character reference" algorithm.
1775            $self->{prev_state} = $self->{state};
1776            $self->{entity_add} = 0x0022; # "
1777            $self->{state} = ENTITY_STATE;
1778          !!!next-input-character;          !!!next-input-character;
1779          redo A;          redo A;
1780        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1781          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1782          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1783            !!!cp (97);            !!!cp (97);
1784            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1785                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1786            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1787            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1788              !!!cp (98);              !!!cp (98);
1789              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1790            } else {            } else {
# Line 1150  sub _get_next_token ($) { Line 1792  sub _get_next_token ($) {
1792              !!!cp (99);              !!!cp (99);
1793            }            }
1794          } else {          } else {
1795            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1796          }          }
1797          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1798          ## reconsume          ## reconsume
1799    
1800          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1801    
1802          redo A;          redo A;
1803        } else {        } else {
1804          !!!cp (100);          !!!cp (100);
1805          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1806            $self->{read_until}->($self->{ca}->{value},
1807                                  q["&],
1808                                  length $self->{ca}->{value});
1809    
1810          ## Stay in the state          ## Stay in the state
1811          !!!next-input-character;          !!!next-input-character;
1812          redo A;          redo A;
1813        }        }
1814      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1815        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
1816          !!!cp (101);          !!!cp (101);
1817          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1818          !!!next-input-character;          !!!next-input-character;
1819          redo A;          redo A;
1820        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1821          !!!cp (102);          !!!cp (102);
1822          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1823          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1824            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1825            ## implementation of the "consume a character reference" algorithm.
1826            $self->{entity_add} = 0x0027; # '
1827            $self->{prev_state} = $self->{state};
1828            $self->{state} = ENTITY_STATE;
1829          !!!next-input-character;          !!!next-input-character;
1830          redo A;          redo A;
1831        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1832          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1833          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1834            !!!cp (103);            !!!cp (103);
1835            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1836                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1837            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1838            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1839              !!!cp (104);              !!!cp (104);
1840              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1841            } else {            } else {
# Line 1194  sub _get_next_token ($) { Line 1843  sub _get_next_token ($) {
1843              !!!cp (105);              !!!cp (105);
1844            }            }
1845          } else {          } else {
1846            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1847          }          }
1848          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1849          ## reconsume          ## reconsume
1850    
1851          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1852    
1853          redo A;          redo A;
1854        } else {        } else {
1855          !!!cp (106);          !!!cp (106);
1856          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1857            $self->{read_until}->($self->{ca}->{value},
1858                                  q['&],
1859                                  length $self->{ca}->{value});
1860    
1861          ## Stay in the state          ## Stay in the state
1862          !!!next-input-character;          !!!next-input-character;
1863          redo A;          redo A;
1864        }        }
1865      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1866        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1867            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1868            $self->{next_char} == 0x000B or # HT            $self->{nc} == 0x000B or # HT
1869            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1870            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1871          !!!cp (107);          !!!cp (107);
1872          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1873          !!!next-input-character;          !!!next-input-character;
1874          redo A;          redo A;
1875        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1876          !!!cp (108);          !!!cp (108);
1877          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1878          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1879            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1880            ## implementation of the "consume a character reference" algorithm.
1881            $self->{entity_add} = -1;
1882            $self->{prev_state} = $self->{state};
1883            $self->{state} = ENTITY_STATE;
1884          !!!next-input-character;          !!!next-input-character;
1885          redo A;          redo A;
1886        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1887          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1888            !!!cp (109);            !!!cp (109);
1889            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1890                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1891            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1892            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1893              !!!cp (110);              !!!cp (110);
1894              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1895            } else {            } else {
# Line 1241  sub _get_next_token ($) { Line 1897  sub _get_next_token ($) {
1897              !!!cp (111);              !!!cp (111);
1898            }            }
1899          } else {          } else {
1900            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1901          }          }
1902          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1903          !!!next-input-character;          !!!next-input-character;
1904    
1905          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1906    
1907          redo A;          redo A;
1908        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1909          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1910          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1911            !!!cp (112);            !!!cp (112);
1912            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1913                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1914            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1915            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1916              !!!cp (113);              !!!cp (113);
1917              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1918            } else {            } else {
# Line 1266  sub _get_next_token ($) { Line 1920  sub _get_next_token ($) {
1920              !!!cp (114);              !!!cp (114);
1921            }            }
1922          } else {          } else {
1923            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1924          }          }
1925          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1926          ## reconsume          ## reconsume
1927    
1928          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1929    
1930          redo A;          redo A;
1931        } else {        } else {
# Line 1279  sub _get_next_token ($) { Line 1933  sub _get_next_token ($) {
1933               0x0022 => 1, # "               0x0022 => 1, # "
1934               0x0027 => 1, # '               0x0027 => 1, # '
1935               0x003D => 1, # =               0x003D => 1, # =
1936              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1937            !!!cp (115);            !!!cp (115);
1938            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1939          } else {          } else {
1940            !!!cp (116);            !!!cp (116);
1941          }          }
1942          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1943            $self->{read_until}->($self->{ca}->{value},
1944                                  q["'=& >],
1945                                  length $self->{ca}->{value});
1946    
1947          ## Stay in the state          ## Stay in the state
1948          !!!next-input-character;          !!!next-input-character;
1949          redo A;          redo A;
1950        }        }
     } elsif ($self->{state} == ENTITY_IN_ATTRIBUTE_VALUE_STATE) {  
       my $token = $self->_tokenize_attempt_to_consume_an_entity  
           (1,  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ? 0x0022 : # "  
            $self->{last_attribute_value_state}  
              == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ? 0x0027 : # '  
            -1);  
   
       unless (defined $token) {  
         !!!cp (117);  
         $self->{current_attribute}->{value} .= '&';  
       } else {  
         !!!cp (118);  
         $self->{current_attribute}->{value} .= $token->{data};  
         $self->{current_attribute}->{has_reference} = $token->{has_reference};  
         ## ISSUE: spec says "append the returned character token to the current attribute's value"  
       }  
   
       $self->{state} = $self->{last_attribute_value_state};  
       # next-input-character is already done  
       redo A;  
1951      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1952        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1953            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1954            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1955            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1956            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1957          !!!cp (118);          !!!cp (118);
1958          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1959          !!!next-input-character;          !!!next-input-character;
1960          redo A;          redo A;
1961        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1962          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1963            !!!cp (119);            !!!cp (119);
1964            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1965                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1966            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1967            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1968              !!!cp (120);              !!!cp (120);
1969              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1970            } else {            } else {
# Line 1338  sub _get_next_token ($) { Line 1972  sub _get_next_token ($) {
1972              !!!cp (121);              !!!cp (121);
1973            }            }
1974          } else {          } else {
1975            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1976          }          }
1977          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1978          !!!next-input-character;          !!!next-input-character;
1979    
1980          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1981    
1982          redo A;          redo A;
1983        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1984            !!!cp (122);
1985            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1986          !!!next-input-character;          !!!next-input-character;
1987          if ($self->{next_char} == 0x003E and # >          redo A;
1988              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{nc} == -1) {
1989              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1990            # permitted slash          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1991            !!!cp (122);            !!!cp (122.3);
1992            #            $self->{last_stag_name} = $self->{ct}->{tag_name};
1993            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1994              if ($self->{ct}->{attributes}) {
1995                !!!cp (122.1);
1996                !!!parse-error (type => 'end tag attribute');
1997              } else {
1998                ## NOTE: This state should never be reached.
1999                !!!cp (122.2);
2000              }
2001          } else {          } else {
2002            !!!cp (123);            die "$0: $self->{ct}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
2003          }          }
2004          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
2005          # next-input-character is already done          ## Reconsume.
2006            !!!emit ($self->{ct}); # start tag or end tag
2007          redo A;          redo A;
2008        } else {        } else {
2009          !!!cp (124);          !!!cp ('124.1');
2010          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
2011          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2012          ## reconsume          ## reconsume
2013          redo A;          redo A;
2014        }        }
2015      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
2016        ## (only happen if PCDATA state)        if ($self->{nc} == 0x003E) { # >
2017                  if ($self->{ct}->{type} == END_TAG_TOKEN) {
2018        my $token = {type => COMMENT_TOKEN, data => ''};            !!!cp ('124.2');
2019              !!!parse-error (type => 'nestc', token => $self->{ct});
2020        BC: {            ## TODO: Different type than slash in start tag
2021          if ($self->{next_char} == 0x003E) { # >            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2022            !!!cp (124);            if ($self->{ct}->{attributes}) {
2023            $self->{state} = DATA_STATE;              !!!cp ('124.4');
2024            !!!next-input-character;              !!!parse-error (type => 'end tag attribute');
2025              } else {
2026            !!!emit ($token);              !!!cp ('124.5');
2027              }
2028              ## TODO: Test |<title></title/>|
2029            } else {
2030              !!!cp ('124.3');
2031              $self->{self_closing} = 1;
2032            }
2033    
2034            redo A;          $self->{state} = DATA_STATE;
2035          } elsif ($self->{next_char} == -1) {          !!!next-input-character;
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
2036    
2037            !!!emit ($token);          !!!emit ($self->{ct}); # start tag or end tag
2038    
2039            redo A;          redo A;
2040          } elsif ($self->{nc} == -1) {
2041            !!!parse-error (type => 'unclosed tag');
2042            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2043              !!!cp (124.7);
2044              $self->{last_stag_name} = $self->{ct}->{tag_name};
2045            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2046              if ($self->{ct}->{attributes}) {
2047                !!!cp (124.5);
2048                !!!parse-error (type => 'end tag attribute');
2049              } else {
2050                ## NOTE: This state should never be reached.
2051                !!!cp (124.6);
2052              }
2053          } else {          } else {
2054            !!!cp (126);            die "$0: $self->{ct}->{type}: Unknown token type";
           $token->{data} .= chr ($self->{next_char});  
           !!!next-input-character;  
           redo BC;  
2055          }          }
2056        } # BC          $self->{state} = DATA_STATE;
2057            ## Reconsume.
2058        die "$0: _get_next_token: unexpected case [BC]";          !!!emit ($self->{ct}); # start tag or end tag
2059      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {          redo A;
2060          } else {
2061            !!!cp ('124.4');
2062            !!!parse-error (type => 'nestc');
2063            ## TODO: This error type is wrong.
2064            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2065            ## Reconsume.
2066            redo A;
2067          }
2068        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2069        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2070    
2071        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2072        push @next_char, $self->{next_char};        ## consumes characters one-by-one basis.
2073                
2074        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x003E) { # >
2075            !!!cp (124);
2076            $self->{state} = DATA_STATE;
2077          !!!next-input-character;          !!!next-input-character;
2078          push @next_char, $self->{next_char};  
2079          if ($self->{next_char} == 0x002D) { # -          !!!emit ($self->{ct}); # comment
2080            !!!cp (127);          redo A;
2081            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};        } elsif ($self->{nc} == -1) {
2082            $self->{state} = COMMENT_START_STATE;          !!!cp (125);
2083            !!!next-input-character;          $self->{state} = DATA_STATE;
2084            redo A;          ## reconsume
2085          } else {  
2086            !!!cp (128);          !!!emit ($self->{ct}); # comment
2087          }          redo A;
2088        } elsif ($self->{next_char} == 0x0044 or # D        } else {
2089                 $self->{next_char} == 0x0064) { # d          !!!cp (126);
2090            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2091            $self->{read_until}->($self->{ct}->{data},
2092                                  q[>],
2093                                  length $self->{ct}->{data});
2094    
2095            ## Stay in the state.
2096          !!!next-input-character;          !!!next-input-character;
2097          push @next_char, $self->{next_char};          redo A;
2098          if ($self->{next_char} == 0x004F or # O        }
2099              $self->{next_char} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2100            !!!next-input-character;        ## (only happen if PCDATA state)
2101            push @next_char, $self->{next_char};        
2102            if ($self->{next_char} == 0x0043 or # C        if ($self->{nc} == 0x002D) { # -
2103                $self->{next_char} == 0x0063) { # c          !!!cp (133);
2104              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2105              push @next_char, $self->{next_char};          !!!next-input-character;
2106              if ($self->{next_char} == 0x0054 or # T          redo A;
2107                  $self->{next_char} == 0x0074) { # t        } elsif ($self->{nc} == 0x0044 or # D
2108                !!!next-input-character;                 $self->{nc} == 0x0064) { # d
2109                push @next_char, $self->{next_char};          ## ASCII case-insensitive.
2110                if ($self->{next_char} == 0x0059 or # Y          !!!cp (130);
2111                    $self->{next_char} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2112                  !!!next-input-character;          $self->{s_kwd} = chr $self->{nc};
2113                  push @next_char, $self->{next_char};          !!!next-input-character;
2114                  if ($self->{next_char} == 0x0050 or # P          redo A;
2115                      $self->{next_char} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2116                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2117                    push @next_char, $self->{next_char};                 $self->{nc} == 0x005B) { # [
2118                    if ($self->{next_char} == 0x0045 or # E          !!!cp (135.4);                
2119                        $self->{next_char} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2120                      !!!cp (129);          $self->{s_kwd} = '[';
2121                      ## TODO: What a stupid code this is!          !!!next-input-character;
2122                      $self->{state} = DOCTYPE_STATE;          redo A;
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (130);  
                   }  
                 } else {  
                   !!!cp (131);  
                 }  
               } else {  
                 !!!cp (132);  
               }  
             } else {  
               !!!cp (133);  
             }  
           } else {  
             !!!cp (134);  
           }  
         } else {  
           !!!cp (135);  
         }  
2123        } else {        } else {
2124          !!!cp (136);          !!!cp (136);
2125        }        }
2126    
2127        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2128        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2129        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2130          ## Reconsume.
2131        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2132          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2133                                    line => $self->{line_prev},
2134                                    column => $self->{column_prev} - 1,
2135                                   };
2136        redo A;        redo A;
2137              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2138        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2139        ## 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);
2140            $self->{ct} = {type => COMMENT_TOKEN, data => '',
2141                                      line => $self->{line_prev},
2142                                      column => $self->{column_prev} - 2,
2143                                     };
2144            $self->{state} = COMMENT_START_STATE;
2145            !!!next-input-character;
2146            redo A;
2147          } else {
2148            !!!cp (128);
2149            !!!parse-error (type => 'bogus comment',
2150                            line => $self->{line_prev},
2151                            column => $self->{column_prev} - 2);
2152            $self->{state} = BOGUS_COMMENT_STATE;
2153            ## Reconsume.
2154            $self->{ct} = {type => COMMENT_TOKEN,
2155                                      data => '-',
2156                                      line => $self->{line_prev},
2157                                      column => $self->{column_prev} - 2,
2158                                     };
2159            redo A;
2160          }
2161        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2162          ## ASCII case-insensitive.
2163          if ($self->{nc} == [
2164                undef,
2165                0x004F, # O
2166                0x0043, # C
2167                0x0054, # T
2168                0x0059, # Y
2169                0x0050, # P
2170              ]->[length $self->{s_kwd}] or
2171              $self->{nc} == [
2172                undef,
2173                0x006F, # o
2174                0x0063, # c
2175                0x0074, # t
2176                0x0079, # y
2177                0x0070, # p
2178              ]->[length $self->{s_kwd}]) {
2179            !!!cp (131);
2180            ## Stay in the state.
2181            $self->{s_kwd} .= chr $self->{nc};
2182            !!!next-input-character;
2183            redo A;
2184          } elsif ((length $self->{s_kwd}) == 6 and
2185                   ($self->{nc} == 0x0045 or # E
2186                    $self->{nc} == 0x0065)) { # e
2187            !!!cp (129);
2188            $self->{state} = DOCTYPE_STATE;
2189            $self->{ct} = {type => DOCTYPE_TOKEN,
2190                                      quirks => 1,
2191                                      line => $self->{line_prev},
2192                                      column => $self->{column_prev} - 7,
2193                                     };
2194            !!!next-input-character;
2195            redo A;
2196          } else {
2197            !!!cp (132);        
2198            !!!parse-error (type => 'bogus comment',
2199                            line => $self->{line_prev},
2200                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2201            $self->{state} = BOGUS_COMMENT_STATE;
2202            ## Reconsume.
2203            $self->{ct} = {type => COMMENT_TOKEN,
2204                                      data => $self->{s_kwd},
2205                                      line => $self->{line_prev},
2206                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2207                                     };
2208            redo A;
2209          }
2210        } elsif ($self->{state} == MD_CDATA_STATE) {
2211          if ($self->{nc} == {
2212                '[' => 0x0043, # C
2213                '[C' => 0x0044, # D
2214                '[CD' => 0x0041, # A
2215                '[CDA' => 0x0054, # T
2216                '[CDAT' => 0x0041, # A
2217              }->{$self->{s_kwd}}) {
2218            !!!cp (135.1);
2219            ## Stay in the state.
2220            $self->{s_kwd} .= chr $self->{nc};
2221            !!!next-input-character;
2222            redo A;
2223          } elsif ($self->{s_kwd} eq '[CDATA' and
2224                   $self->{nc} == 0x005B) { # [
2225            !!!cp (135.2);
2226            $self->{ct} = {type => CHARACTER_TOKEN,
2227                                      data => '',
2228                                      line => $self->{line_prev},
2229                                      column => $self->{column_prev} - 7};
2230            $self->{state} = CDATA_SECTION_STATE;
2231            !!!next-input-character;
2232            redo A;
2233          } else {
2234            !!!cp (135.3);
2235            !!!parse-error (type => 'bogus comment',
2236                            line => $self->{line_prev},
2237                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2238            $self->{state} = BOGUS_COMMENT_STATE;
2239            ## Reconsume.
2240            $self->{ct} = {type => COMMENT_TOKEN,
2241                                      data => $self->{s_kwd},
2242                                      line => $self->{line_prev},
2243                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2244                                     };
2245            redo A;
2246          }
2247      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2248        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2249          !!!cp (137);          !!!cp (137);
2250          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2251          !!!next-input-character;          !!!next-input-character;
2252          redo A;          redo A;
2253        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2254          !!!cp (138);          !!!cp (138);
2255          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2256          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2257          !!!next-input-character;          !!!next-input-character;
2258    
2259          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2260    
2261          redo A;          redo A;
2262        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2263          !!!cp (139);          !!!cp (139);
2264          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2265          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2266          ## reconsume          ## reconsume
2267    
2268          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2269    
2270          redo A;          redo A;
2271        } else {        } else {
2272          !!!cp (140);          !!!cp (140);
2273          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2274              .= chr ($self->{next_char});              .= chr ($self->{nc});
2275          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2276          !!!next-input-character;          !!!next-input-character;
2277          redo A;          redo A;
2278        }        }
2279      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2280        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2281          !!!cp (141);          !!!cp (141);
2282          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2283          !!!next-input-character;          !!!next-input-character;
2284          redo A;          redo A;
2285        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2286          !!!cp (142);          !!!cp (142);
2287          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2288          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2289          !!!next-input-character;          !!!next-input-character;
2290    
2291          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2292    
2293          redo A;          redo A;
2294        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2295          !!!cp (143);          !!!cp (143);
2296          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2297          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2298          ## reconsume          ## reconsume
2299    
2300          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2301    
2302          redo A;          redo A;
2303        } else {        } else {
2304          !!!cp (144);          !!!cp (144);
2305          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2306              .= '-' . chr ($self->{next_char});              .= '-' . chr ($self->{nc});
2307          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2308          !!!next-input-character;          !!!next-input-character;
2309          redo A;          redo A;
2310        }        }
2311      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2312        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2313          !!!cp (145);          !!!cp (145);
2314          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2315          !!!next-input-character;          !!!next-input-character;
2316          redo A;          redo A;
2317        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2318          !!!cp (146);          !!!cp (146);
2319          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2320          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2321          ## reconsume          ## reconsume
2322    
2323          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2324    
2325          redo A;          redo A;
2326        } else {        } else {
2327          !!!cp (147);          !!!cp (147);
2328          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{ct}->{data} .= chr ($self->{nc}); # comment
2329            $self->{read_until}->($self->{ct}->{data},
2330                                  q[-],
2331                                  length $self->{ct}->{data});
2332    
2333          ## Stay in the state          ## Stay in the state
2334          !!!next-input-character;          !!!next-input-character;
2335          redo A;          redo A;
2336        }        }
2337      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2338        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2339          !!!cp (148);          !!!cp (148);
2340          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2341          !!!next-input-character;          !!!next-input-character;
2342          redo A;          redo A;
2343        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2344          !!!cp (149);          !!!cp (149);
2345          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2346          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2347          ## reconsume          ## reconsume
2348    
2349          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2350    
2351          redo A;          redo A;
2352        } else {        } else {
2353          !!!cp (150);          !!!cp (150);
2354          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment          $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2355          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2356          !!!next-input-character;          !!!next-input-character;
2357          redo A;          redo A;
2358        }        }
2359      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2360        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2361          !!!cp (151);          !!!cp (151);
2362          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2363          !!!next-input-character;          !!!next-input-character;
2364    
2365          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2366    
2367          redo A;          redo A;
2368        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2369          !!!cp (152);          !!!cp (152);
2370          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2371          $self->{current_token}->{data} .= '-'; # comment                          line => $self->{line_prev},
2372                            column => $self->{column_prev});
2373            $self->{ct}->{data} .= '-'; # comment
2374          ## Stay in the state          ## Stay in the state
2375          !!!next-input-character;          !!!next-input-character;
2376          redo A;          redo A;
2377        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2378          !!!cp (153);          !!!cp (153);
2379          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2380          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2381          ## reconsume          ## reconsume
2382    
2383          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2384    
2385          redo A;          redo A;
2386        } else {        } else {
2387          !!!cp (154);          !!!cp (154);
2388          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2389          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment                          line => $self->{line_prev},
2390                            column => $self->{column_prev});
2391            $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2392          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2393          !!!next-input-character;          !!!next-input-character;
2394          redo A;          redo A;
2395        }        }
2396      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2397        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2398            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2399            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2400            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2401            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2402          !!!cp (155);          !!!cp (155);
2403          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2404          !!!next-input-character;          !!!next-input-character;
# Line 1637  sub _get_next_token ($) { Line 2411  sub _get_next_token ($) {
2411          redo A;          redo A;
2412        }        }
2413      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2414        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2415            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2416            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2417            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2418            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2419          !!!cp (157);          !!!cp (157);
2420          ## Stay in the state          ## Stay in the state
2421          !!!next-input-character;          !!!next-input-character;
2422          redo A;          redo A;
2423        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2424          !!!cp (158);          !!!cp (158);
2425          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2426          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2427          !!!next-input-character;          !!!next-input-character;
2428    
2429          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2430    
2431          redo A;          redo A;
2432        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2433          !!!cp (159);          !!!cp (159);
2434          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2435          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2436          ## reconsume          ## reconsume
2437    
2438          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2439    
2440          redo A;          redo A;
2441        } else {        } else {
2442          !!!cp (160);          !!!cp (160);
2443          $self->{current_token}          $self->{ct}->{name} = chr $self->{nc};
2444              = {type => DOCTYPE_TOKEN,          delete $self->{ct}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2445  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2446          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2447          !!!next-input-character;          !!!next-input-character;
# Line 1678  sub _get_next_token ($) { Line 2449  sub _get_next_token ($) {
2449        }        }
2450      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2451  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2452        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2453            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2454            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2455            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2456            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2457          !!!cp (161);          !!!cp (161);
2458          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2459          !!!next-input-character;          !!!next-input-character;
2460          redo A;          redo A;
2461        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2462          !!!cp (162);          !!!cp (162);
2463          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2464          !!!next-input-character;          !!!next-input-character;
2465    
2466          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2467    
2468          redo A;          redo A;
2469        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2470          !!!cp (163);          !!!cp (163);
2471          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2472          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2473          ## reconsume          ## reconsume
2474    
2475          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2476          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2477    
2478          redo A;          redo A;
2479        } else {        } else {
2480          !!!cp (164);          !!!cp (164);
2481          $self->{current_token}->{name}          $self->{ct}->{name}
2482            .= chr ($self->{next_char}); # DOCTYPE            .= chr ($self->{nc}); # DOCTYPE
2483          ## Stay in the state          ## Stay in the state
2484          !!!next-input-character;          !!!next-input-character;
2485          redo A;          redo A;
2486        }        }
2487      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2488        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2489            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2490            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2491            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2492            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2493          !!!cp (165);          !!!cp (165);
2494          ## Stay in the state          ## Stay in the state
2495          !!!next-input-character;          !!!next-input-character;
2496          redo A;          redo A;
2497        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2498          !!!cp (166);          !!!cp (166);
2499          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2500          !!!next-input-character;          !!!next-input-character;
2501    
2502          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2503    
2504          redo A;          redo A;
2505        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2506          !!!cp (167);          !!!cp (167);
2507          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2508          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2509          ## reconsume          ## reconsume
2510    
2511          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2512          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2513    
2514          redo A;          redo A;
2515        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{nc} == 0x0050 or # P
2516                 $self->{next_char} == 0x0070) { # p                 $self->{nc} == 0x0070) { # p
2517            $self->{state} = PUBLIC_STATE;
2518            $self->{s_kwd} = chr $self->{nc};
2519          !!!next-input-character;          !!!next-input-character;
2520          if ($self->{next_char} == 0x0055 or # U          redo A;
2521              $self->{next_char} == 0x0075) { # u        } elsif ($self->{nc} == 0x0053 or # S
2522            !!!next-input-character;                 $self->{nc} == 0x0073) { # s
2523            if ($self->{next_char} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2524                $self->{next_char} == 0x0062) { # b          $self->{s_kwd} = chr $self->{nc};
             !!!next-input-character;  
             if ($self->{next_char} == 0x004C or # L  
                 $self->{next_char} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0049 or # I  
                   $self->{next_char} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x0043 or # C  
                     $self->{next_char} == 0x0063) { # c  
                   !!!cp (168);  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (169);  
                 }  
               } else {  
                 !!!cp (170);  
               }  
             } else {  
               !!!cp (171);  
             }  
           } else {  
             !!!cp (172);  
           }  
         } else {  
           !!!cp (173);  
         }  
   
         #  
       } elsif ($self->{next_char} == 0x0053 or # S  
                $self->{next_char} == 0x0073) { # s  
2525          !!!next-input-character;          !!!next-input-character;
2526          if ($self->{next_char} == 0x0059 or # Y          redo A;
             $self->{next_char} == 0x0079) { # y  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0053 or # S  
               $self->{next_char} == 0x0073) { # s  
             !!!next-input-character;  
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0045 or # E  
                   $self->{next_char} == 0x0065) { # e  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x004D or # M  
                     $self->{next_char} == 0x006D) { # m  
                   !!!cp (174);  
                   $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (175);  
                 }  
               } else {  
                 !!!cp (176);  
               }  
             } else {  
               !!!cp (177);  
             }  
           } else {  
             !!!cp (178);  
           }  
         } else {  
           !!!cp (179);  
         }  
   
         #  
2527        } else {        } else {
2528          !!!cp (180);          !!!cp (180);
2529            !!!parse-error (type => 'string after DOCTYPE name');
2530            $self->{ct}->{quirks} = 1;
2531    
2532            $self->{state} = BOGUS_DOCTYPE_STATE;
2533          !!!next-input-character;          !!!next-input-character;
2534          #          redo A;
2535        }        }
2536        } elsif ($self->{state} == PUBLIC_STATE) {
2537          ## ASCII case-insensitive
2538          if ($self->{nc} == [
2539                undef,
2540                0x0055, # U
2541                0x0042, # B
2542                0x004C, # L
2543                0x0049, # I
2544              ]->[length $self->{s_kwd}] or
2545              $self->{nc} == [
2546                undef,
2547                0x0075, # u
2548                0x0062, # b
2549                0x006C, # l
2550                0x0069, # i
2551              ]->[length $self->{s_kwd}]) {
2552            !!!cp (175);
2553            ## Stay in the state.
2554            $self->{s_kwd} .= chr $self->{nc};
2555            !!!next-input-character;
2556            redo A;
2557          } elsif ((length $self->{s_kwd}) == 5 and
2558                   ($self->{nc} == 0x0043 or # C
2559                    $self->{nc} == 0x0063)) { # c
2560            !!!cp (168);
2561            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2562            !!!next-input-character;
2563            redo A;
2564          } else {
2565            !!!cp (169);
2566            !!!parse-error (type => 'string after DOCTYPE name',
2567                            line => $self->{line_prev},
2568                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2569            $self->{ct}->{quirks} = 1;
2570    
2571        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2572        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2573            redo A;
2574          }
2575        } elsif ($self->{state} == SYSTEM_STATE) {
2576          ## ASCII case-insensitive
2577          if ($self->{nc} == [
2578                undef,
2579                0x0059, # Y
2580                0x0053, # S
2581                0x0054, # T
2582                0x0045, # E
2583              ]->[length $self->{s_kwd}] or
2584              $self->{nc} == [
2585                undef,
2586                0x0079, # y
2587                0x0073, # s
2588                0x0074, # t
2589                0x0065, # e
2590              ]->[length $self->{s_kwd}]) {
2591            !!!cp (170);
2592            ## Stay in the state.
2593            $self->{s_kwd} .= chr $self->{nc};
2594            !!!next-input-character;
2595            redo A;
2596          } elsif ((length $self->{s_kwd}) == 5 and
2597                   ($self->{nc} == 0x004D or # M
2598                    $self->{nc} == 0x006D)) { # m
2599            !!!cp (171);
2600            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2601            !!!next-input-character;
2602            redo A;
2603          } else {
2604            !!!cp (172);
2605            !!!parse-error (type => 'string after DOCTYPE name',
2606                            line => $self->{line_prev},
2607                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2608            $self->{ct}->{quirks} = 1;
2609    
2610        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2611        # next-input-character is already done          ## Reconsume.
2612        redo A;          redo A;
2613          }
2614      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2615        if ({        if ({
2616              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2617              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2618            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2619          !!!cp (181);          !!!cp (181);
2620          ## Stay in the state          ## Stay in the state
2621          !!!next-input-character;          !!!next-input-character;
2622          redo A;          redo A;
2623        } elsif ($self->{next_char} eq 0x0022) { # "        } elsif ($self->{nc} eq 0x0022) { # "
2624          !!!cp (182);          !!!cp (182);
2625          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2626          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2627          !!!next-input-character;          !!!next-input-character;
2628          redo A;          redo A;
2629        } elsif ($self->{next_char} eq 0x0027) { # '        } elsif ($self->{nc} eq 0x0027) { # '
2630          !!!cp (183);          !!!cp (183);
2631          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2632          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2633          !!!next-input-character;          !!!next-input-character;
2634          redo A;          redo A;
2635        } elsif ($self->{next_char} eq 0x003E) { # >        } elsif ($self->{nc} eq 0x003E) { # >
2636          !!!cp (184);          !!!cp (184);
2637          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2638    
2639          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2640          !!!next-input-character;          !!!next-input-character;
2641    
2642          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2643          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2644    
2645          redo A;          redo A;
2646        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2647          !!!cp (185);          !!!cp (185);
2648          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2649    
2650          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2651          ## reconsume          ## reconsume
2652    
2653          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2654          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2655    
2656          redo A;          redo A;
2657        } else {        } else {
2658          !!!cp (186);          !!!cp (186);
2659          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2660          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2661    
2662          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2663          !!!next-input-character;          !!!next-input-character;
2664          redo A;          redo A;
2665        }        }
2666      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2667        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2668          !!!cp (187);          !!!cp (187);
2669          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2670          !!!next-input-character;          !!!next-input-character;
2671          redo A;          redo A;
2672        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2673          !!!cp (188);          !!!cp (188);
2674          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2675    
2676          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2677          !!!next-input-character;          !!!next-input-character;
2678    
2679          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2680          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2681    
2682          redo A;          redo A;
2683        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2684          !!!cp (189);          !!!cp (189);
2685          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2686    
2687          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2688          ## reconsume          ## reconsume
2689    
2690          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2691          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2692    
2693          redo A;          redo A;
2694        } else {        } else {
2695          !!!cp (190);          !!!cp (190);
2696          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2697              .= chr $self->{next_char};              .= chr $self->{nc};
2698            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2699                                  length $self->{ct}->{pubid});
2700    
2701          ## Stay in the state          ## Stay in the state
2702          !!!next-input-character;          !!!next-input-character;
2703          redo A;          redo A;
2704        }        }
2705      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2706        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2707          !!!cp (191);          !!!cp (191);
2708          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2709          !!!next-input-character;          !!!next-input-character;
2710          redo A;          redo A;
2711        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2712          !!!cp (192);          !!!cp (192);
2713          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2714    
2715          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2716          !!!next-input-character;          !!!next-input-character;
2717    
2718          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2719          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2720    
2721          redo A;          redo A;
2722        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2723          !!!cp (193);          !!!cp (193);
2724          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2725    
2726          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2727          ## reconsume          ## reconsume
2728    
2729          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2730          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2731    
2732          redo A;          redo A;
2733        } else {        } else {
2734          !!!cp (194);          !!!cp (194);
2735          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2736              .= chr $self->{next_char};              .= chr $self->{nc};
2737            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2738                                  length $self->{ct}->{pubid});
2739    
2740          ## Stay in the state          ## Stay in the state
2741          !!!next-input-character;          !!!next-input-character;
2742          redo A;          redo A;
# Line 1957  sub _get_next_token ($) { Line 2745  sub _get_next_token ($) {
2745        if ({        if ({
2746              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2747              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2748            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2749          !!!cp (195);          !!!cp (195);
2750          ## Stay in the state          ## Stay in the state
2751          !!!next-input-character;          !!!next-input-character;
2752          redo A;          redo A;
2753        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2754          !!!cp (196);          !!!cp (196);
2755          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2756          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2757          !!!next-input-character;          !!!next-input-character;
2758          redo A;          redo A;
2759        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2760          !!!cp (197);          !!!cp (197);
2761          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2762          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2763          !!!next-input-character;          !!!next-input-character;
2764          redo A;          redo A;
2765        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2766          !!!cp (198);          !!!cp (198);
2767          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2768          !!!next-input-character;          !!!next-input-character;
2769    
2770          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2771    
2772          redo A;          redo A;
2773        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2774          !!!cp (199);          !!!cp (199);
2775          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2776    
2777          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2778          ## reconsume          ## reconsume
2779    
2780          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2781          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2782    
2783          redo A;          redo A;
2784        } else {        } else {
2785          !!!cp (200);          !!!cp (200);
2786          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2787          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2788    
2789          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2790          !!!next-input-character;          !!!next-input-character;
# Line 2006  sub _get_next_token ($) { Line 2794  sub _get_next_token ($) {
2794        if ({        if ({
2795              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2796              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2797            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2798          !!!cp (201);          !!!cp (201);
2799          ## Stay in the state          ## Stay in the state
2800          !!!next-input-character;          !!!next-input-character;
2801          redo A;          redo A;
2802        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2803          !!!cp (202);          !!!cp (202);
2804          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2805          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2806          !!!next-input-character;          !!!next-input-character;
2807          redo A;          redo A;
2808        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2809          !!!cp (203);          !!!cp (203);
2810          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2811          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2812          !!!next-input-character;          !!!next-input-character;
2813          redo A;          redo A;
2814        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2815          !!!cp (204);          !!!cp (204);
2816          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2817          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2818          !!!next-input-character;          !!!next-input-character;
2819    
2820          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2821          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2822    
2823          redo A;          redo A;
2824        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2825          !!!cp (205);          !!!cp (205);
2826          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2827    
2828          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2829          ## reconsume          ## reconsume
2830    
2831          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2832          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2833    
2834          redo A;          redo A;
2835        } else {        } else {
2836          !!!cp (206);          !!!cp (206);
2837          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2838          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2839    
2840          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2841          !!!next-input-character;          !!!next-input-character;
2842          redo A;          redo A;
2843        }        }
2844      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2845        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2846          !!!cp (207);          !!!cp (207);
2847          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2848          !!!next-input-character;          !!!next-input-character;
2849          redo A;          redo A;
2850        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2851          !!!cp (208);          !!!cp (208);
2852          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2853    
2854          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2855          !!!next-input-character;          !!!next-input-character;
2856    
2857          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2858          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2859    
2860          redo A;          redo A;
2861        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2862          !!!cp (209);          !!!cp (209);
2863          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2864    
2865          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2866          ## reconsume          ## reconsume
2867    
2868          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2869          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2870    
2871          redo A;          redo A;
2872        } else {        } else {
2873          !!!cp (210);          !!!cp (210);
2874          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2875              .= chr $self->{next_char};              .= chr $self->{nc};
2876            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2877                                  length $self->{ct}->{sysid});
2878    
2879          ## Stay in the state          ## Stay in the state
2880          !!!next-input-character;          !!!next-input-character;
2881          redo A;          redo A;
2882        }        }
2883      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2884        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2885          !!!cp (211);          !!!cp (211);
2886          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2887          !!!next-input-character;          !!!next-input-character;
2888          redo A;          redo A;
2889        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2890          !!!cp (212);          !!!cp (212);
2891          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2892    
2893          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2894          !!!next-input-character;          !!!next-input-character;
2895    
2896          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2897          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2898    
2899          redo A;          redo A;
2900        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2901          !!!cp (213);          !!!cp (213);
2902          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2903    
2904          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2905          ## reconsume          ## reconsume
2906    
2907          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2908          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2909    
2910          redo A;          redo A;
2911        } else {        } else {
2912          !!!cp (214);          !!!cp (214);
2913          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2914              .= chr $self->{next_char};              .= chr $self->{nc};
2915            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2916                                  length $self->{ct}->{sysid});
2917    
2918          ## Stay in the state          ## Stay in the state
2919          !!!next-input-character;          !!!next-input-character;
2920          redo A;          redo A;
# Line 2129  sub _get_next_token ($) { Line 2923  sub _get_next_token ($) {
2923        if ({        if ({
2924              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2925              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2926            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2927          !!!cp (215);          !!!cp (215);
2928          ## Stay in the state          ## Stay in the state
2929          !!!next-input-character;          !!!next-input-character;
2930          redo A;          redo A;
2931        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2932          !!!cp (216);          !!!cp (216);
2933          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2934          !!!next-input-character;          !!!next-input-character;
2935    
2936          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2937    
2938          redo A;          redo A;
2939        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2940          !!!cp (217);          !!!cp (217);
2941          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2942          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2943          ## reconsume          ## reconsume
2944    
2945          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2946          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2947    
2948          redo A;          redo A;
2949        } else {        } else {
2950          !!!cp (218);          !!!cp (218);
2951          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2952          #$self->{current_token}->{quirks} = 1;          #$self->{ct}->{quirks} = 1;
2953    
2954          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2955          !!!next-input-character;          !!!next-input-character;
2956          redo A;          redo A;
2957        }        }
2958      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2959        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2960          !!!cp (219);          !!!cp (219);
2961          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2962          !!!next-input-character;          !!!next-input-character;
2963    
2964          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2965    
2966          redo A;          redo A;
2967        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2968          !!!cp (220);          !!!cp (220);
2969          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2970          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2971          ## reconsume          ## reconsume
2972    
2973          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2974    
2975          redo A;          redo A;
2976        } else {        } else {
2977          !!!cp (221);          !!!cp (221);
2978            my $s = '';
2979            $self->{read_until}->($s, q[>], 0);
2980    
2981          ## Stay in the state          ## Stay in the state
2982          !!!next-input-character;          !!!next-input-character;
2983          redo A;          redo A;
2984        }        }
2985      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2986        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2987      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2988    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2989          
2990    die "$0: _get_next_token: unexpected case";        if ($self->{nc} == 0x005D) { # ]
2991  } # _get_next_token          !!!cp (221.1);
2992            $self->{state} = CDATA_SECTION_MSE1_STATE;
2993            !!!next-input-character;
2994            redo A;
2995          } elsif ($self->{nc} == -1) {
2996            $self->{state} = DATA_STATE;
2997            !!!next-input-character;
2998            if (length $self->{ct}->{data}) { # character
2999              !!!cp (221.2);
3000              !!!emit ($self->{ct}); # character
3001            } else {
3002              !!!cp (221.3);
3003              ## No token to emit. $self->{ct} is discarded.
3004            }        
3005            redo A;
3006          } else {
3007            !!!cp (221.4);
3008            $self->{ct}->{data} .= chr $self->{nc};
3009            $self->{read_until}->($self->{ct}->{data},
3010                                  q<]>,
3011                                  length $self->{ct}->{data});
3012    
3013  sub _tokenize_attempt_to_consume_an_entity ($$$) {          ## Stay in the state.
3014    my ($self, $in_attr, $additional) = @_;          !!!next-input-character;
3015            redo A;
3016          }
3017    
3018    if ({        ## ISSUE: "text tokens" in spec.
3019         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
3020         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{nc} == 0x005D) { # ]
3021         $additional => 1,          !!!cp (221.5);
3022        }->{$self->{next_char}}) {          $self->{state} = CDATA_SECTION_MSE2_STATE;
3023      !!!cp (1001);          !!!next-input-character;
3024      ## Don't consume          redo A;
3025      ## No error        } else {
3026      return undef;          !!!cp (221.6);
3027    } elsif ($self->{next_char} == 0x0023) { # #          $self->{ct}->{data} .= ']';
3028      !!!next-input-character;          $self->{state} = CDATA_SECTION_STATE;
3029      if ($self->{next_char} == 0x0078 or # x          ## Reconsume.
3030          $self->{next_char} == 0x0058) { # X          redo A;
3031        my $code;        }
3032        X: {      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3033          my $x_char = $self->{next_char};        if ($self->{nc} == 0x003E) { # >
3034          !!!next-input-character;          $self->{state} = DATA_STATE;
3035          if (0x0030 <= $self->{next_char} and          !!!next-input-character;
3036              $self->{next_char} <= 0x0039) { # 0..9          if (length $self->{ct}->{data}) { # character
3037            !!!cp (1002);            !!!cp (221.7);
3038            $code ||= 0;            !!!emit ($self->{ct}); # character
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0030;  
           redo X;  
         } elsif (0x0061 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0066) { # a..f  
           !!!cp (1003);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0046) { # A..F  
           !!!cp (1004);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!cp (1005);  
           !!!parse-error (type => 'bare hcro');  
           !!!back-next-input-character ($x_char, $self->{next_char});  
           $self->{next_char} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_char} == 0x003B) { # ;  
           !!!cp (1006);  
           !!!next-input-character;  
3039          } else {          } else {
3040            !!!cp (1007);            !!!cp (221.8);
3041            !!!parse-error (type => 'no refc');            ## No token to emit. $self->{ct} is discarded.
3042          }          }
3043            redo A;
3044          } elsif ($self->{nc} == 0x005D) { # ]
3045            !!!cp (221.9); # character
3046            $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3047            ## Stay in the state.
3048            !!!next-input-character;
3049            redo A;
3050          } else {
3051            !!!cp (221.11);
3052            $self->{ct}->{data} .= ']]'; # character
3053            $self->{state} = CDATA_SECTION_STATE;
3054            ## Reconsume.
3055            redo A;
3056          }
3057        } elsif ($self->{state} == ENTITY_STATE) {
3058          if ({
3059            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3060            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3061            $self->{entity_add} => 1,
3062          }->{$self->{nc}}) {
3063            !!!cp (1001);
3064            ## Don't consume
3065            ## No error
3066            ## Return nothing.
3067            #
3068          } elsif ($self->{nc} == 0x0023) { # #
3069            !!!cp (999);
3070            $self->{state} = ENTITY_HASH_STATE;
3071            $self->{s_kwd} = '#';
3072            !!!next-input-character;
3073            redo A;
3074          } elsif ((0x0041 <= $self->{nc} and
3075                    $self->{nc} <= 0x005A) or # A..Z
3076                   (0x0061 <= $self->{nc} and
3077                    $self->{nc} <= 0x007A)) { # a..z
3078            !!!cp (998);
3079            require Whatpm::_NamedEntityList;
3080            $self->{state} = ENTITY_NAME_STATE;
3081            $self->{s_kwd} = chr $self->{nc};
3082            $self->{entity__value} = $self->{s_kwd};
3083            $self->{entity__match} = 0;
3084            !!!next-input-character;
3085            redo A;
3086          } else {
3087            !!!cp (1027);
3088            !!!parse-error (type => 'bare ero');
3089            ## Return nothing.
3090            #
3091          }
3092    
3093          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3094            !!!cp (1008);        ## reference" algorithm.  In other word, there is an "&" character
3095            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## that does not introduce a character reference, which would be
3096            $code = 0xFFFD;        ## appended to the parent element or the attribute value in later
3097          } elsif ($code > 0x10FFFF) {        ## process of the tokenizer.
3098            !!!cp (1009);  
3099            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        if ($self->{prev_state} == DATA_STATE) {
3100            $code = 0xFFFD;          !!!cp (997);
3101          } elsif ($code == 0x000D) {          $self->{state} = $self->{prev_state};
3102            !!!cp (1010);          ## Reconsume.
3103            !!!parse-error (type => 'CR character reference');          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3104            $code = 0x000A;                    line => $self->{line_prev},
3105          } elsif (0x80 <= $code and $code <= 0x9F) {                    column => $self->{column_prev},
3106            !!!cp (1011);                   });
3107            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          redo A;
3108            $code = $c1_entity_char->{$code};        } else {
3109          }          !!!cp (996);
3110            $self->{ca}->{value} .= '&';
3111          return {type => CHARACTER_TOKEN, data => chr $code,          $self->{state} = $self->{prev_state};
3112                  has_reference => 1};          ## Reconsume.
3113        } # X          redo A;
3114      } elsif (0x0030 <= $self->{next_char} and        }
3115               $self->{next_char} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3116        my $code = $self->{next_char} - 0x0030;        if ($self->{nc} == 0x0078 or # x
3117        !!!next-input-character;            $self->{nc} == 0x0058) { # X
3118                  !!!cp (995);
3119        while (0x0030 <= $self->{next_char} and          $self->{state} = HEXREF_X_STATE;
3120                  $self->{next_char} <= 0x0039) { # 0..9          $self->{s_kwd} .= chr $self->{nc};
3121            !!!next-input-character;
3122            redo A;
3123          } elsif (0x0030 <= $self->{nc} and
3124                   $self->{nc} <= 0x0039) { # 0..9
3125            !!!cp (994);
3126            $self->{state} = NCR_NUM_STATE;
3127            $self->{s_kwd} = $self->{nc} - 0x0030;
3128            !!!next-input-character;
3129            redo A;
3130          } else {
3131            !!!parse-error (type => 'bare nero',
3132                            line => $self->{line_prev},
3133                            column => $self->{column_prev} - 1);
3134    
3135            ## NOTE: According to the spec algorithm, nothing is returned,
3136            ## and then "&#" is appended to the parent element or the attribute
3137            ## value in the later processing.
3138    
3139            if ($self->{prev_state} == DATA_STATE) {
3140              !!!cp (1019);
3141              $self->{state} = $self->{prev_state};
3142              ## Reconsume.
3143              !!!emit ({type => CHARACTER_TOKEN,
3144                        data => '&#',
3145                        line => $self->{line_prev},
3146                        column => $self->{column_prev} - 1,
3147                       });
3148              redo A;
3149            } else {
3150              !!!cp (993);
3151              $self->{ca}->{value} .= '&#';
3152              $self->{state} = $self->{prev_state};
3153              ## Reconsume.
3154              redo A;
3155            }
3156          }
3157        } elsif ($self->{state} == NCR_NUM_STATE) {
3158          if (0x0030 <= $self->{nc} and
3159              $self->{nc} <= 0x0039) { # 0..9
3160          !!!cp (1012);          !!!cp (1012);
3161          $code *= 10;          $self->{s_kwd} *= 10;
3162          $code += $self->{next_char} - 0x0030;          $self->{s_kwd} += $self->{nc} - 0x0030;
3163                    
3164            ## Stay in the state.
3165          !!!next-input-character;          !!!next-input-character;
3166        }          redo A;
3167          } elsif ($self->{nc} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3168          !!!cp (1013);          !!!cp (1013);
3169          !!!next-input-character;          !!!next-input-character;
3170            #
3171        } else {        } else {
3172          !!!cp (1014);          !!!cp (1014);
3173          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3174            ## Reconsume.
3175            #
3176        }        }
3177    
3178          my $code = $self->{s_kwd};
3179          my $l = $self->{line_prev};
3180          my $c = $self->{column_prev};
3181        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3182          !!!cp (1015);          !!!cp (1015);
3183          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
3184                            text => (sprintf 'U+%04X', $code),
3185                            line => $l, column => $c);
3186          $code = 0xFFFD;          $code = 0xFFFD;
3187        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3188          !!!cp (1016);          !!!cp (1016);
3189          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
3190                            text => (sprintf 'U-%08X', $code),
3191                            line => $l, column => $c);
3192          $code = 0xFFFD;          $code = 0xFFFD;
3193        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3194          !!!cp (1017);          !!!cp (1017);
3195          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
3196                            line => $l, column => $c);
3197          $code = 0x000A;          $code = 0x000A;
3198        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3199          !!!cp (1018);          !!!cp (1018);
3200          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3201                            text => (sprintf 'U+%04X', $code),
3202                            line => $l, column => $c);
3203          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3204        }        }
3205          
3206        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        if ($self->{prev_state} == DATA_STATE) {
3207      } else {          !!!cp (992);
3208        !!!cp (1019);          $self->{state} = $self->{prev_state};
3209        !!!parse-error (type => 'bare nero');          ## Reconsume.
3210        !!!back-next-input-character ($self->{next_char});          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3211        $self->{next_char} = 0x0023; # #                    line => $l, column => $c,
3212        return undef;                   });
3213      }          redo A;
3214    } elsif ((0x0041 <= $self->{next_char} and        } else {
3215              $self->{next_char} <= 0x005A) or          !!!cp (991);
3216             (0x0061 <= $self->{next_char} and          $self->{ca}->{value} .= chr $code;
3217              $self->{next_char} <= 0x007A)) {          $self->{ca}->{has_reference} = 1;
3218      my $entity_name = chr $self->{next_char};          $self->{state} = $self->{prev_state};
3219      !!!next-input-character;          ## Reconsume.
3220            redo A;
3221      my $value = $entity_name;        }
3222      my $match = 0;      } elsif ($self->{state} == HEXREF_X_STATE) {
3223      require Whatpm::_NamedEntityList;        if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3224      our $EntityChar;            (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3225              (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3226      while (length $entity_name < 10 and          # 0..9, A..F, a..f
3227             ## NOTE: Some number greater than the maximum length of entity name          !!!cp (990);
3228             ((0x0041 <= $self->{next_char} and # a          $self->{state} = HEXREF_HEX_STATE;
3229               $self->{next_char} <= 0x005A) or # x          $self->{s_kwd} = 0;
3230              (0x0061 <= $self->{next_char} and # a          ## Reconsume.
3231               $self->{next_char} <= 0x007A) or # z          redo A;
3232              (0x0030 <= $self->{next_char} and # 0        } else {
3233               $self->{next_char} <= 0x0039) or # 9          !!!parse-error (type => 'bare hcro',
3234              $self->{next_char} == 0x003B)) { # ;                          line => $self->{line_prev},
3235        $entity_name .= chr $self->{next_char};                          column => $self->{column_prev} - 2);
3236        if (defined $EntityChar->{$entity_name}) {  
3237          if ($self->{next_char} == 0x003B) { # ;          ## NOTE: According to the spec algorithm, nothing is returned,
3238            !!!cp (1020);          ## and then "&#" followed by "X" or "x" is appended to the parent
3239            $value = $EntityChar->{$entity_name};          ## element or the attribute value in the later processing.
3240            $match = 1;  
3241            !!!next-input-character;          if ($self->{prev_state} == DATA_STATE) {
3242            last;            !!!cp (1005);
3243              $self->{state} = $self->{prev_state};
3244              ## Reconsume.
3245              !!!emit ({type => CHARACTER_TOKEN,
3246                        data => '&' . $self->{s_kwd},
3247                        line => $self->{line_prev},
3248                        column => $self->{column_prev} - length $self->{s_kwd},
3249                       });
3250              redo A;
3251          } else {          } else {
3252            !!!cp (1021);            !!!cp (989);
3253            $value = $EntityChar->{$entity_name};            $self->{ca}->{value} .= '&' . $self->{s_kwd};
3254            $match = -1;            $self->{state} = $self->{prev_state};
3255            !!!next-input-character;            ## Reconsume.
3256              redo A;
3257          }          }
3258        } else {        }
3259          !!!cp (1022);      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3260          $value .= chr $self->{next_char};        if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3261          $match *= 2;          # 0..9
3262            !!!cp (1002);
3263            $self->{s_kwd} *= 0x10;
3264            $self->{s_kwd} += $self->{nc} - 0x0030;
3265            ## Stay in the state.
3266            !!!next-input-character;
3267            redo A;
3268          } elsif (0x0061 <= $self->{nc} and
3269                   $self->{nc} <= 0x0066) { # a..f
3270            !!!cp (1003);
3271            $self->{s_kwd} *= 0x10;
3272            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3273            ## Stay in the state.
3274            !!!next-input-character;
3275            redo A;
3276          } elsif (0x0041 <= $self->{nc} and
3277                   $self->{nc} <= 0x0046) { # A..F
3278            !!!cp (1004);
3279            $self->{s_kwd} *= 0x10;
3280            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3281            ## Stay in the state.
3282            !!!next-input-character;
3283            redo A;
3284          } elsif ($self->{nc} == 0x003B) { # ;
3285            !!!cp (1006);
3286          !!!next-input-character;          !!!next-input-character;
3287            #
3288          } else {
3289            !!!cp (1007);
3290            !!!parse-error (type => 'no refc',
3291                            line => $self->{line},
3292                            column => $self->{column});
3293            ## Reconsume.
3294            #
3295        }        }
3296      }  
3297              my $code = $self->{s_kwd};
3298      if ($match > 0) {        my $l = $self->{line_prev};
3299        !!!cp (1023);        my $c = $self->{column_prev};
3300        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3301      } elsif ($match < 0) {          !!!cp (1008);
3302        !!!parse-error (type => 'no refc');          !!!parse-error (type => 'invalid character reference',
3303        if ($in_attr and $match < -1) {                          text => (sprintf 'U+%04X', $code),
3304          !!!cp (1024);                          line => $l, column => $c);
3305          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          $code = 0xFFFD;
3306          } elsif ($code > 0x10FFFF) {
3307            !!!cp (1009);
3308            !!!parse-error (type => 'invalid character reference',
3309                            text => (sprintf 'U-%08X', $code),
3310                            line => $l, column => $c);
3311            $code = 0xFFFD;
3312          } elsif ($code == 0x000D) {
3313            !!!cp (1010);
3314            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3315            $code = 0x000A;
3316          } elsif (0x80 <= $code and $code <= 0x9F) {
3317            !!!cp (1011);
3318            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3319            $code = $c1_entity_char->{$code};
3320          }
3321    
3322          if ($self->{prev_state} == DATA_STATE) {
3323            !!!cp (988);
3324            $self->{state} = $self->{prev_state};
3325            ## Reconsume.
3326            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3327                      line => $l, column => $c,
3328                     });
3329            redo A;
3330          } else {
3331            !!!cp (987);
3332            $self->{ca}->{value} .= chr $code;
3333            $self->{ca}->{has_reference} = 1;
3334            $self->{state} = $self->{prev_state};
3335            ## Reconsume.
3336            redo A;
3337          }
3338        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3339          if (length $self->{s_kwd} < 30 and
3340              ## NOTE: Some number greater than the maximum length of entity name
3341              ((0x0041 <= $self->{nc} and # a
3342                $self->{nc} <= 0x005A) or # x
3343               (0x0061 <= $self->{nc} and # a
3344                $self->{nc} <= 0x007A) or # z
3345               (0x0030 <= $self->{nc} and # 0
3346                $self->{nc} <= 0x0039) or # 9
3347               $self->{nc} == 0x003B)) { # ;
3348            our $EntityChar;
3349            $self->{s_kwd} .= chr $self->{nc};
3350            if (defined $EntityChar->{$self->{s_kwd}}) {
3351              if ($self->{nc} == 0x003B) { # ;
3352                !!!cp (1020);
3353                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3354                $self->{entity__match} = 1;
3355                !!!next-input-character;
3356                #
3357              } else {
3358                !!!cp (1021);
3359                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3360                $self->{entity__match} = -1;
3361                ## Stay in the state.
3362                !!!next-input-character;
3363                redo A;
3364              }
3365            } else {
3366              !!!cp (1022);
3367              $self->{entity__value} .= chr $self->{nc};
3368              $self->{entity__match} *= 2;
3369              ## Stay in the state.
3370              !!!next-input-character;
3371              redo A;
3372            }
3373          }
3374    
3375          my $data;
3376          my $has_ref;
3377          if ($self->{entity__match} > 0) {
3378            !!!cp (1023);
3379            $data = $self->{entity__value};
3380            $has_ref = 1;
3381            #
3382          } elsif ($self->{entity__match} < 0) {
3383            !!!parse-error (type => 'no refc');
3384            if ($self->{prev_state} != DATA_STATE and # in attribute
3385                $self->{entity__match} < -1) {
3386              !!!cp (1024);
3387              $data = '&' . $self->{s_kwd};
3388              #
3389            } else {
3390              !!!cp (1025);
3391              $data = $self->{entity__value};
3392              $has_ref = 1;
3393              #
3394            }
3395        } else {        } else {
3396          !!!cp (1025);          !!!cp (1026);
3397          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          !!!parse-error (type => 'bare ero',
3398                            line => $self->{line_prev},
3399                            column => $self->{column_prev} - length $self->{s_kwd});
3400            $data = '&' . $self->{s_kwd};
3401            #
3402          }
3403      
3404          ## NOTE: In these cases, when a character reference is found,
3405          ## it is consumed and a character token is returned, or, otherwise,
3406          ## nothing is consumed and returned, according to the spec algorithm.
3407          ## In this implementation, anything that has been examined by the
3408          ## tokenizer is appended to the parent element or the attribute value
3409          ## as string, either literal string when no character reference or
3410          ## entity-replaced string otherwise, in this stage, since any characters
3411          ## that would not be consumed are appended in the data state or in an
3412          ## appropriate attribute value state anyway.
3413    
3414          if ($self->{prev_state} == DATA_STATE) {
3415            !!!cp (986);
3416            $self->{state} = $self->{prev_state};
3417            ## Reconsume.
3418            !!!emit ({type => CHARACTER_TOKEN,
3419                      data => $data,
3420                      line => $self->{line_prev},
3421                      column => $self->{column_prev} + 1 - length $self->{s_kwd},
3422                     });
3423            redo A;
3424          } else {
3425            !!!cp (985);
3426            $self->{ca}->{value} .= $data;
3427            $self->{ca}->{has_reference} = 1 if $has_ref;
3428            $self->{state} = $self->{prev_state};
3429            ## Reconsume.
3430            redo A;
3431        }        }
3432      } else {      } else {
3433        !!!cp (1026);        die "$0: $self->{state}: Unknown state";
       !!!parse-error (type => 'bare ero');  
       ## NOTE: "No characters are consumed" in the spec.  
       return {type => CHARACTER_TOKEN, data => '&'.$value};  
3434      }      }
3435    } else {    } # A  
3436      !!!cp (1027);  
3437      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3438      !!!parse-error (type => 'bare ero');  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3439    
3440  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3441    my $self = shift;    my $self = shift;
# Line 2394  sub _initialize_tree_constructor ($) { Line 3444  sub _initialize_tree_constructor ($) {
3444    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3445    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3446    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3447      $self->{document}->set_user_data (manakai_source_line => 1);
3448      $self->{document}->set_user_data (manakai_source_column => 1);
3449  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3450    
3451  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2420  sub _construct_tree ($) { Line 3472  sub _construct_tree ($) {
3472        
3473    !!!next-token;    !!!next-token;
3474    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3475    undef $self->{form_element};    undef $self->{form_element};
3476    undef $self->{head_element};    undef $self->{head_element};
3477    $self->{open_elements} = [];    $self->{open_elements} = [];
3478    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3479    
3480      ## NOTE: The "initial" insertion mode.
3481    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3482    
3483      ## NOTE: The "before html" insertion mode.
3484    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3485      $self->{insertion_mode} = BEFORE_HEAD_IM;
3486    
3487      ## NOTE: The "before head" insertion mode and so on.
3488    $self->_tree_construction_main;    $self->_tree_construction_main;
3489  } # _construct_tree  } # _construct_tree
3490    
3491  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3492    my $self = shift;    my $self = shift;
3493    
3494      ## NOTE: "initial" insertion mode
3495    
3496    INITIAL: {    INITIAL: {
3497      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3498        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2440  sub _tree_construction_initial ($) { Line 3500  sub _tree_construction_initial ($) {
3500        ## language.        ## language.
3501        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3502        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3503        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3504        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3505            defined $token->{public_identifier} or            defined $token->{sysid}) {
3506            defined $token->{system_identifier}) {          !!!cp ('t1');
3507          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3508        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3509          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!cp ('t2');
3510          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3511          } elsif (defined $token->{pubid}) {
3512            if ($token->{pubid} eq 'XSLT-compat') {
3513              !!!cp ('t1.2');
3514              !!!parse-error (type => 'XSLT-compat', token => $token,
3515                              level => $self->{level}->{should});
3516            } else {
3517              !!!parse-error (type => 'not HTML5', token => $token);
3518            }
3519          } else {
3520            !!!cp ('t3');
3521            #
3522        }        }
3523                
3524        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3525          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3526        $doctype->public_id ($token->{public_identifier})        ## NOTE: Default value for both |public_id| and |system_id| attributes
3527            if defined $token->{public_identifier};        ## are empty strings, so that we don't set any value in missing cases.
3528        $doctype->system_id ($token->{system_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3529            if defined $token->{system_identifier};        $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
3530        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
3531        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3532        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
3533                
3534        if ($token->{quirks} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3535            !!!cp ('t4');
3536          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3537        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
3538          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
3539          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3540          if ({          my $prefix = [
3541            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3542            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3543            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3544            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3545            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3546            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3547            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3548            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3549            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3550            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3551            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3552            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3553            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3554            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3555            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3556            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3557            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3558            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3559            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3560            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3561            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3562            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3563            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3564            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3565            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3566            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3567            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3568            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3569            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3570            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3571            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3572            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3573            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3574            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3575            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3576            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3577            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3578            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3579            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3580            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3581            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3582            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3583            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3584            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3585            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3586            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3587            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3588            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3589            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3590            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3591            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3592            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3593            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3594            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3595            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3596            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3597            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3598            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3599            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3600            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3601            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3602            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3603            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3604            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3605            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3606            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3607            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,              $pubid eq "HTML") {
3608            "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,            !!!cp ('t5');
           "-//WEBTECHS//DTD MOZILLA HTML 2.0//EN" => 1,  
           "-//WEBTECHS//DTD MOZILLA HTML//EN" => 1,  
           "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" => 1,  
           "HTML" => 1,  
         }->{$pubid}) {  
3609            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3610          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3611                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3612            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
3613                !!!cp ('t6');
3614              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3615            } else {            } else {
3616                !!!cp ('t7');
3617              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3618            }            }
3619          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 Frameset//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3620                   $pubid eq "-//W3C//DTD XHTML 1.0 Transitional//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3621              !!!cp ('t8');
3622            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3623            } else {
3624              !!!cp ('t9');
3625          }          }
3626          } else {
3627            !!!cp ('t10');
3628        }        }
3629        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
3630          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
3631          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3632          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") {
3633              ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3634              ## marked as quirks.
3635            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3636              !!!cp ('t11');
3637            } else {
3638              !!!cp ('t12');
3639          }          }
3640          } else {
3641            !!!cp ('t13');
3642        }        }
3643                
3644        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3645        !!!next-token;        !!!next-token;
3646        return;        return;
3647      } elsif ({      } elsif ({
# Line 2568  sub _tree_construction_initial ($) { Line 3649  sub _tree_construction_initial ($) {
3649                END_TAG_TOKEN, 1,                END_TAG_TOKEN, 1,
3650                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3651               }->{$token->{type}}) {               }->{$token->{type}}) {
3652        !!!parse-error (type => 'no DOCTYPE');        !!!cp ('t14');
3653          !!!parse-error (type => 'no DOCTYPE', token => $token);
3654        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3655        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3656        ## reprocess        ## reprocess
3657          !!!ack-later;
3658        return;        return;
3659      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3660        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
3661          ## Ignore the token          ## Ignore the token
3662    
3663          unless (length $token->{data}) {          unless (length $token->{data}) {
3664            ## Stay in the phase            !!!cp ('t15');
3665              ## Stay in the insertion mode.
3666            !!!next-token;            !!!next-token;
3667            redo INITIAL;            redo INITIAL;
3668            } else {
3669              !!!cp ('t16');
3670          }          }
3671          } else {
3672            !!!cp ('t17');
3673        }        }
3674    
3675        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3676        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3677        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3678        ## reprocess        ## reprocess
3679        return;        return;
3680      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
3681          !!!cp ('t18');
3682        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3683        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3684                
3685        ## Stay in the phase.        ## Stay in the insertion mode.
3686        !!!next-token;        !!!next-token;
3687        redo INITIAL;        redo INITIAL;
3688      } else {      } else {
3689        die "$0: $token->{type}: Unknown token type";        die "$0: $token->{type}: Unknown token type";
3690      }      }
3691    } # INITIAL    } # INITIAL
3692    
3693      die "$0: _tree_construction_initial: This should be never reached";
3694  } # _tree_construction_initial  } # _tree_construction_initial
3695    
3696  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3697    my $self = shift;    my $self = shift;
3698    
3699      ## NOTE: "before html" insertion mode.
3700        
3701    B: {    B: {
3702        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3703          !!!parse-error (type => 'in html:#DOCTYPE');          !!!cp ('t19');
3704            !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3705          ## Ignore the token          ## Ignore the token
3706          ## Stay in the phase          ## Stay in the insertion mode.
3707          !!!next-token;          !!!next-token;
3708          redo B;          redo B;
3709        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3710            !!!cp ('t20');
3711          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3712          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3713          ## Stay in the phase          ## Stay in the insertion mode.
3714          !!!next-token;          !!!next-token;
3715          redo B;          redo B;
3716        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2623  sub _tree_construction_root_element ($) Line 3718  sub _tree_construction_root_element ($)
3718            ## Ignore the token.            ## Ignore the token.
3719    
3720            unless (length $token->{data}) {            unless (length $token->{data}) {
3721              ## Stay in the phase              !!!cp ('t21');
3722                ## Stay in the insertion mode.
3723              !!!next-token;              !!!next-token;
3724              redo B;              redo B;
3725              } else {
3726                !!!cp ('t22');
3727            }            }
3728            } else {
3729              !!!cp ('t23');
3730          }          }
3731    
3732          $self->{application_cache_selection}->(undef);          $self->{application_cache_selection}->(undef);
3733    
3734          #          #
3735        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3736          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3737              $token->{attributes}->{manifest}) {            my $root_element;
3738            $self->{application_cache_selection}            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3739                 ->($token->{attributes}->{manifest}->{value});            $self->{document}->append_child ($root_element);
3740            ## ISSUE: No relative reference resolution?            push @{$self->{open_elements}},
3741                  [$root_element, $el_category->{html}];
3742    
3743              if ($token->{attributes}->{manifest}) {
3744                !!!cp ('t24');
3745                $self->{application_cache_selection}
3746                    ->($token->{attributes}->{manifest}->{value});
3747                ## ISSUE: Spec is unclear on relative references.
3748                ## According to Hixie (#whatwg 2008-03-19), it should be
3749                ## resolved against the base URI of the document in HTML
3750                ## or xml:base of the element in XHTML.
3751              } else {
3752                !!!cp ('t25');
3753                $self->{application_cache_selection}->(undef);
3754              }
3755    
3756              !!!nack ('t25c');
3757    
3758              !!!next-token;
3759              return; ## Go to the "before head" insertion mode.
3760          } else {          } else {
3761            $self->{application_cache_selection}->(undef);            !!!cp ('t25.1');
3762              #
3763          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3764        } elsif ({        } elsif ({
3765                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3766                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3767                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3768          $self->{application_cache_selection}->(undef);          !!!cp ('t26');
   
         ## ISSUE: There is an issue in the spec  
3769          #          #
3770        } else {        } else {
3771          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3772        }        }
3773    
3774        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3775        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3776        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3777        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3778        #redo B;  
3779        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3780    
3781        ## NOTE: Reprocess the token.
3782        !!!ack-later;
3783        return; ## Go to the "before head" insertion mode.
3784    
3785        ## ISSUE: There is an issue in the spec
3786    } # B    } # B
3787    
3788      die "$0: _tree_construction_root_element: This should never be reached";
3789  } # _tree_construction_root_element  } # _tree_construction_root_element
3790    
3791  sub _reset_insertion_mode ($) {  sub _reset_insertion_mode ($) {
# Line 2677  sub _reset_insertion_mode ($) { Line 3800  sub _reset_insertion_mode ($) {
3800            
3801      ## Step 3      ## Step 3
3802      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"!?  
3803        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3804          $last = 1;          $last = 1;
3805          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3806            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3807                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3808              #          } else {
3809            } else {            die "_reset_insertion_mode: t27";
             $node = $self->{inner_html_node};  
           }  
3810          }          }
3811        }        }
3812              
3813        ## Step 4..13        ## Step 4..14
3814        my $new_mode = {        my $new_mode;
3815          if ($node->[1] & FOREIGN_EL) {
3816            !!!cp ('t28.1');
3817            ## NOTE: Strictly spaking, the line below only applies to MathML and
3818            ## SVG elements.  Currently the HTML syntax supports only MathML and
3819            ## SVG elements as foreigners.
3820            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3821          } elsif ($node->[1] & TABLE_CELL_EL) {
3822            if ($last) {
3823              !!!cp ('t28.2');
3824              #
3825            } else {
3826              !!!cp ('t28.3');
3827              $new_mode = IN_CELL_IM;
3828            }
3829          } else {
3830            !!!cp ('t28.4');
3831            $new_mode = {
3832                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3833                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3834                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3835                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3836                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3837                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2709  sub _reset_insertion_mode ($) { Line 3842  sub _reset_insertion_mode ($) {
3842                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3843                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3844                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3845                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3846          }
3847        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3848                
3849        ## Step 14        ## Step 15
3850        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3851          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3852              !!!cp ('t29');
3853            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
3854          } else {          } else {
3855              ## ISSUE: Can this state be reached?
3856              !!!cp ('t30');
3857            $self->{insertion_mode} = AFTER_HEAD_IM;            $self->{insertion_mode} = AFTER_HEAD_IM;
3858          }          }
3859          return;          return;
3860          } else {
3861            !!!cp ('t31');
3862        }        }
3863                
3864        ## Step 15        ## Step 16
3865        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3866                
3867        ## Step 16        ## Step 17
3868        $i--;        $i--;
3869        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3870                
3871        ## Step 17        ## Step 18
3872        redo S3;        redo S3;
3873      } # S3      } # S3
3874    
3875      die "$0: _reset_insertion_mode: This line should never be reached";
3876  } # _reset_insertion_mode  } # _reset_insertion_mode
3877    
3878  sub _tree_construction_main ($) {  sub _tree_construction_main ($) {
# Line 2753  sub _tree_construction_main ($) { Line 3894  sub _tree_construction_main ($) {
3894      return if $entry->[0] eq '#marker';      return if $entry->[0] eq '#marker';
3895      for (@{$self->{open_elements}}) {      for (@{$self->{open_elements}}) {
3896        if ($entry->[0] eq $_->[0]) {        if ($entry->[0] eq $_->[0]) {
3897            !!!cp ('t32');
3898          return;          return;
3899        }        }
3900      }      }
# Line 2767  sub _tree_construction_main ($) { Line 3909  sub _tree_construction_main ($) {
3909    
3910        ## Step 6        ## Step 6
3911        if ($entry->[0] eq '#marker') {        if ($entry->[0] eq '#marker') {
3912            !!!cp ('t33_1');
3913          #          #
3914        } else {        } else {
3915          my $in_open_elements;          my $in_open_elements;
3916          OE: for (@{$self->{open_elements}}) {          OE: for (@{$self->{open_elements}}) {
3917            if ($entry->[0] eq $_->[0]) {            if ($entry->[0] eq $_->[0]) {
3918                !!!cp ('t33');
3919              $in_open_elements = 1;              $in_open_elements = 1;
3920              last OE;              last OE;
3921            }            }
3922          }          }
3923          if ($in_open_elements) {          if ($in_open_elements) {
3924              !!!cp ('t34');
3925            #            #
3926          } else {          } else {
3927              ## NOTE: <!DOCTYPE HTML><p><b><i><u></p> <p>X
3928              !!!cp ('t35');
3929            redo S4;            redo S4;
3930          }          }
3931        }        }
# Line 2801  sub _tree_construction_main ($) { Line 3948  sub _tree_construction_main ($) {
3948    
3949        ## Step 11        ## Step 11
3950        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {        unless ($clone->[0] eq $active_formatting_elements->[-1]->[0]) {
3951            !!!cp ('t36');
3952          ## Step 7'          ## Step 7'
3953          $i++;          $i++;
3954          $entry = $active_formatting_elements->[$i];          $entry = $active_formatting_elements->[$i];
3955                    
3956          redo S7;          redo S7;
3957        }        }
3958    
3959          !!!cp ('t37');
3960      } # S7      } # S7
3961    }; # $reconstruct_active_formatting_elements    }; # $reconstruct_active_formatting_elements
3962    
3963    my $clear_up_to_marker = sub {    my $clear_up_to_marker = sub {
3964      for (reverse 0..$#$active_formatting_elements) {      for (reverse 0..$#$active_formatting_elements) {
3965        if ($active_formatting_elements->[$_]->[0] eq '#marker') {        if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3966            !!!cp ('t38');
3967          splice @$active_formatting_elements, $_;          splice @$active_formatting_elements, $_;
3968          return;          return;
3969        }        }
3970      }      }
3971    
3972        !!!cp ('t39');
3973    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3974    
3975    my $parse_rcdata = sub ($$) {    my $insert;
3976      my ($content_model_flag, $insert) = @_;  
3977      my $parse_rcdata = sub ($) {
3978        my ($content_model_flag) = @_;
3979    
3980      ## Step 1      ## Step 1
3981      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3982      my $el;      my $el;
3983      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3984    
3985      ## Step 2      ## Step 2
3986      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3987    
3988      ## Step 3      ## Step 3
3989      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2836  sub _tree_construction_main ($) { Line 3991  sub _tree_construction_main ($) {
3991    
3992      ## Step 4      ## Step 4
3993      my $text = '';      my $text = '';
3994        !!!nack ('t40.1');
3995      !!!next-token;      !!!next-token;
3996      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3997          !!!cp ('t40');
3998        $text .= $token->{data};        $text .= $token->{data};
3999        !!!next-token;        !!!next-token;
4000      }      }
4001    
4002      ## Step 5      ## Step 5
4003      if (length $text) {      if (length $text) {
4004          !!!cp ('t41');
4005        my $text = $self->{document}->create_text_node ($text);        my $text = $self->{document}->create_text_node ($text);
4006        $el->append_child ($text);        $el->append_child ($text);
4007      }      }
# Line 2852  sub _tree_construction_main ($) { Line 4010  sub _tree_construction_main ($) {
4010      $self->{content_model} = PCDATA_CONTENT_MODEL;      $self->{content_model} = PCDATA_CONTENT_MODEL;
4011    
4012      ## Step 7      ## Step 7
4013      if ($token->{type} == END_TAG_TOKEN and $token->{tag_name} eq $start_tag_name) {      if ($token->{type} == END_TAG_TOKEN and
4014            $token->{tag_name} eq $start_tag_name) {
4015          !!!cp ('t42');
4016        ## 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});  
4017      } else {      } else {
4018        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
4019          if ($content_model_flag == CDATA_CONTENT_MODEL) {
4020            !!!cp ('t43');
4021            !!!parse-error (type => 'in CDATA:#eof', token => $token);
4022          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4023            !!!cp ('t44');
4024            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4025          } else {
4026            die "$0: $content_model_flag in parse_rcdata";
4027          }
4028      }      }
4029      !!!next-token;      !!!next-token;
4030    }; # $parse_rcdata    }; # $parse_rcdata
4031    
4032    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
4033      my $script_el;      my $script_el;
4034      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4035      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4036    
4037      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
4038      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4039            
4040      my $text = '';      my $text = '';
4041        !!!nack ('t45.1');
4042      !!!next-token;      !!!next-token;
4043      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
4044          !!!cp ('t45');
4045        $text .= $token->{data};        $text .= $token->{data};
4046        !!!next-token;        !!!next-token;
4047      } # stop if non-character token or tokenizer stops tokenising      } # stop if non-character token or tokenizer stops tokenising
4048      if (length $text) {      if (length $text) {
4049          !!!cp ('t46');
4050        $script_el->manakai_append_text ($text);        $script_el->manakai_append_text ($text);
4051      }      }
4052                                
# Line 2887  sub _tree_construction_main ($) { Line 4054  sub _tree_construction_main ($) {
4054    
4055      if ($token->{type} == END_TAG_TOKEN and      if ($token->{type} == END_TAG_TOKEN and
4056          $token->{tag_name} eq 'script') {          $token->{tag_name} eq 'script') {
4057          !!!cp ('t47');
4058        ## Ignore the token        ## Ignore the token
4059      } else {      } else {
4060        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!cp ('t48');
4061          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4062        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4063        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4064      }      }
4065            
4066      if (defined $self->{inner_html_node}) {      if (defined $self->{inner_html_node}) {
4067          !!!cp ('t49');
4068        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4069      } else {      } else {
4070          !!!cp ('t50');
4071        ## TODO: $old_insertion_point = current insertion point        ## TODO: $old_insertion_point = current insertion point
4072        ## TODO: insertion point = just before the next input character        ## TODO: insertion point = just before the next input character
4073    
# Line 2910  sub _tree_construction_main ($) { Line 4081  sub _tree_construction_main ($) {
4081      !!!next-token;      !!!next-token;
4082    }; # $script_start_tag    }; # $script_start_tag
4083    
4084      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4085      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4086      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4087    
4088    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4089      my $tag_name = shift;      my $end_tag_token = shift;
4090        my $tag_name = $end_tag_token->{tag_name};
4091    
4092        ## NOTE: The adoption agency algorithm (AAA).
4093    
4094      FET: {      FET: {
4095        ## Step 1        ## Step 1
4096        my $formatting_element;        my $formatting_element;
4097        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4098        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4099          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4100              !!!cp ('t52');
4101              last AFE;
4102            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4103                         eq $tag_name) {
4104              !!!cp ('t51');
4105            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4106            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4107            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           last AFE;  
4108          }          }
4109        } # AFE        } # AFE
4110        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4111          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!cp ('t53');
4112            !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4113          ## Ignore the token          ## Ignore the token
4114          !!!next-token;          !!!next-token;
4115          return;          return;
# Line 2939  sub _tree_construction_main ($) { Line 4121  sub _tree_construction_main ($) {
4121          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4122          if ($node->[0] eq $formatting_element->[0]) {          if ($node->[0] eq $formatting_element->[0]) {
4123            if ($in_scope) {            if ($in_scope) {
4124                !!!cp ('t54');
4125              $formatting_element_i_in_open = $_;              $formatting_element_i_in_open = $_;
4126              last INSCOPE;              last INSCOPE;
4127            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4128              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t55');
4129                !!!parse-error (type => 'unmatched end tag',
4130                                text => $token->{tag_name},
4131                                token => $end_tag_token);
4132              ## Ignore the token              ## Ignore the token
4133              !!!next-token;              !!!next-token;
4134              return;              return;
4135            }            }
4136          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
4137                    table => 1, caption => 1, td => 1, th => 1,            !!!cp ('t56');
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4138            $in_scope = 0;            $in_scope = 0;
4139          }          }
4140        } # INSCOPE        } # INSCOPE
4141        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4142          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t57');
4143            !!!parse-error (type => 'unmatched end tag',
4144                            text => $token->{tag_name},
4145                            token => $end_tag_token);
4146          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4147          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4148          return;          return;
4149        }        }
4150        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4151          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!cp ('t58');
4152            !!!parse-error (type => 'not closed',
4153                            text => $self->{open_elements}->[-1]->[0]
4154                                ->manakai_local_name,
4155                            token => $end_tag_token);
4156        }        }
4157                
4158        ## Step 2        ## Step 2
# Line 2969  sub _tree_construction_main ($) { Line 4160  sub _tree_construction_main ($) {
4160        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4161        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4162          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4163          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4164              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4165              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4166               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4167              !!!cp ('t59');
4168            $furthest_block = $node;            $furthest_block = $node;
4169            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
4170          } elsif ($node->[0] eq $formatting_element->[0]) {          } elsif ($node->[0] eq $formatting_element->[0]) {
4171              !!!cp ('t60');
4172            last OE;            last OE;
4173          }          }
4174        } # OE        } # OE
4175                
4176        ## Step 3        ## Step 3
4177        unless (defined $furthest_block) { # MUST        unless (defined $furthest_block) { # MUST
4178            !!!cp ('t61');
4179          splice @{$self->{open_elements}}, $formatting_element_i_in_open;          splice @{$self->{open_elements}}, $formatting_element_i_in_open;
4180          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;          splice @$active_formatting_elements, $formatting_element_i_in_active, 1;
4181          !!!next-token;          !!!next-token;
# Line 2994  sub _tree_construction_main ($) { Line 4188  sub _tree_construction_main ($) {
4188        ## Step 5        ## Step 5
4189        my $furthest_block_parent = $furthest_block->[0]->parent_node;        my $furthest_block_parent = $furthest_block->[0]->parent_node;
4190        if (defined $furthest_block_parent) {        if (defined $furthest_block_parent) {
4191            !!!cp ('t62');
4192          $furthest_block_parent->remove_child ($furthest_block->[0]);          $furthest_block_parent->remove_child ($furthest_block->[0]);
4193        }        }
4194                
# Line 3016  sub _tree_construction_main ($) { Line 4211  sub _tree_construction_main ($) {
4211          S7S2: {          S7S2: {
4212            for (reverse 0..$#$active_formatting_elements) {            for (reverse 0..$#$active_formatting_elements) {
4213              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {              if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
4214                  !!!cp ('t63');
4215                $node_i_in_active = $_;                $node_i_in_active = $_;
4216                last S7S2;                last S7S2;
4217              }              }
# Line 3029  sub _tree_construction_main ($) { Line 4225  sub _tree_construction_main ($) {
4225                    
4226          ## Step 4          ## Step 4
4227          if ($last_node->[0] eq $furthest_block->[0]) {          if ($last_node->[0] eq $furthest_block->[0]) {
4228              !!!cp ('t64');
4229            $bookmark_prev_el = $node->[0];            $bookmark_prev_el = $node->[0];
4230          }          }
4231                    
4232          ## Step 5          ## Step 5
4233          if ($node->[0]->has_child_nodes ()) {          if ($node->[0]->has_child_nodes ()) {
4234              !!!cp ('t65');
4235            my $clone = [$node->[0]->clone_node (0), $node->[1]];            my $clone = [$node->[0]->clone_node (0), $node->[1]];
4236            $active_formatting_elements->[$node_i_in_active] = $clone;            $active_formatting_elements->[$node_i_in_active] = $clone;
4237            $self->{open_elements}->[$node_i_in_open] = $clone;            $self->{open_elements}->[$node_i_in_open] = $clone;
# Line 3051  sub _tree_construction_main ($) { Line 4249  sub _tree_construction_main ($) {
4249        } # S7          } # S7  
4250                
4251        ## Step 8        ## Step 8
4252        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4253            my $foster_parent_element;
4254            my $next_sibling;
4255            OE: for (reverse 0..$#{$self->{open_elements}}) {
4256              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4257                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4258                                 if (defined $parent and $parent->node_type == 1) {
4259                                   !!!cp ('t65.1');
4260                                   $foster_parent_element = $parent;
4261                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4262                                 } else {
4263                                   !!!cp ('t65.2');
4264                                   $foster_parent_element
4265                                     = $self->{open_elements}->[$_ - 1]->[0];
4266                                 }
4267                                 last OE;
4268                               }
4269                             } # OE
4270                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4271                               unless defined $foster_parent_element;
4272            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4273            $open_tables->[-1]->[1] = 1; # tainted
4274          } else {
4275            !!!cp ('t65.3');
4276            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4277          }
4278                
4279        ## Step 9        ## Step 9
4280        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3068  sub _tree_construction_main ($) { Line 4291  sub _tree_construction_main ($) {
4291        my $i;        my $i;
4292        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4293          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {          if ($active_formatting_elements->[$_]->[0] eq $formatting_element->[0]) {
4294              !!!cp ('t66');
4295            splice @$active_formatting_elements, $_, 1;            splice @$active_formatting_elements, $_, 1;
4296            $i-- and last AFE if defined $i;            $i-- and last AFE if defined $i;
4297          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {          } elsif ($active_formatting_elements->[$_]->[0] eq $bookmark_prev_el) {
4298              !!!cp ('t67');
4299            $i = $_;            $i = $_;
4300          }          }
4301        } # AFE        } # AFE
# Line 3080  sub _tree_construction_main ($) { Line 4305  sub _tree_construction_main ($) {
4305        undef $i;        undef $i;
4306        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4307          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {          if ($self->{open_elements}->[$_]->[0] eq $formatting_element->[0]) {
4308              !!!cp ('t68');
4309            splice @{$self->{open_elements}}, $_, 1;            splice @{$self->{open_elements}}, $_, 1;
4310            $i-- and last OE if defined $i;            $i-- and last OE if defined $i;
4311          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {          } elsif ($self->{open_elements}->[$_]->[0] eq $furthest_block->[0]) {
4312              !!!cp ('t69');
4313            $i = $_;            $i = $_;
4314          }          }
4315        } # OE        } # OE
# Line 3093  sub _tree_construction_main ($) { Line 4320  sub _tree_construction_main ($) {
4320      } # FET      } # FET
4321    }; # $formatting_end_tag    }; # $formatting_end_tag
4322    
4323    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4324      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4325    }; # $insert_to_current    }; # $insert_to_current
4326    
4327    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4328                         my $child = shift;      my $child = shift;
4329                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4330                              table => 1, tbody => 1, tfoot => 1,        # MUST
4331                              thead => 1, tr => 1,        my $foster_parent_element;
4332                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4333                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4334                           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') {  
4335                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4336                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4337                                   !!!cp ('t70');
4338                                 $foster_parent_element = $parent;                                 $foster_parent_element = $parent;
4339                                 $next_sibling = $self->{open_elements}->[$_]->[0];                                 $next_sibling = $self->{open_elements}->[$_]->[0];
4340                               } else {                               } else {
4341                                   !!!cp ('t71');
4342                                 $foster_parent_element                                 $foster_parent_element
4343                                   = $self->{open_elements}->[$_ - 1]->[0];                                   = $self->{open_elements}->[$_ - 1]->[0];
4344                               }                               }
# Line 3123  sub _tree_construction_main ($) { Line 4349  sub _tree_construction_main ($) {
4349                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4350                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4351                             ($child, $next_sibling);                             ($child, $next_sibling);
4352                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4353                           $self->{open_elements}->[-1]->[0]->append_child ($child);      } else {
4354                         }        !!!cp ('t72');
4355          $self->{open_elements}->[-1]->[0]->append_child ($child);
4356        }
4357    }; # $insert_to_foster    }; # $insert_to_foster
4358    
4359    my $insert;    B: while (1) {
   
   B: {  
4360      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4361        !!!parse-error (type => 'DOCTYPE in the middle');        !!!cp ('t73');
4362          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4363        ## Ignore the token        ## Ignore the token
4364        ## Stay in the phase        ## Stay in the phase
4365        !!!next-token;        !!!next-token;
4366        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;  
4367      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4368               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4369        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4370          ## Turn into the main phase          !!!cp ('t79');
4371          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4372          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4373        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4374          ## Turn into the main phase          !!!cp ('t80');
4375          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4376          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4377          } else {
4378            !!!cp ('t81');
4379        }        }
4380    
4381  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4382  ## 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');  
       }  
4383        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4384        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4385          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
4386              !!!cp ('t84');
4387            $top_el->set_attribute_ns            $top_el->set_attribute_ns
4388              (undef, [undef, $attr_name],              (undef, [undef, $attr_name],
4389               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4390          }          }
4391        }        }
4392          !!!nack ('t84.1');
4393        !!!next-token;        !!!next-token;
4394        redo B;        next B;
4395      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4396        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4397        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
4398            !!!cp ('t85');
4399          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
4400        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {        } elsif ($self->{insertion_mode} == AFTER_BODY_IM) {
4401            !!!cp ('t86');
4402          $self->{open_elements}->[0]->[0]->append_child ($comment);          $self->{open_elements}->[0]->[0]->append_child ($comment);
4403        } else {        } else {
4404            !!!cp ('t87');
4405          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4406        }        }
4407        !!!next-token;        !!!next-token;
4408        redo B;        next B;
4409      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4410          if ($token->{type} == CHARACTER_TOKEN) {
4411            !!!cp ('t87.1');
4412            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4413            !!!next-token;
4414            next B;
4415          } elsif ($token->{type} == START_TAG_TOKEN) {
4416            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4417                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4418                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4419                ($token->{tag_name} eq 'svg' and
4420                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4421              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4422              !!!cp ('t87.2');
4423              #
4424            } elsif ({
4425                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4426                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4427                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4428                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4429                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4430                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4431                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4432                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4433                     }->{$token->{tag_name}}) {
4434              !!!cp ('t87.2');
4435              !!!parse-error (type => 'not closed',
4436                              text => $self->{open_elements}->[-1]->[0]
4437                                  ->manakai_local_name,
4438                              token => $token);
4439    
4440              pop @{$self->{open_elements}}
4441                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4442    
4443              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4444              ## Reprocess.
4445              next B;
4446            } else {
4447              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4448              my $tag_name = $token->{tag_name};
4449              if ($nsuri eq $SVG_NS) {
4450                $tag_name = {
4451                   altglyph => 'altGlyph',
4452                   altglyphdef => 'altGlyphDef',
4453                   altglyphitem => 'altGlyphItem',
4454                   animatecolor => 'animateColor',
4455                   animatemotion => 'animateMotion',
4456                   animatetransform => 'animateTransform',
4457                   clippath => 'clipPath',
4458                   feblend => 'feBlend',
4459                   fecolormatrix => 'feColorMatrix',
4460                   fecomponenttransfer => 'feComponentTransfer',
4461                   fecomposite => 'feComposite',
4462                   feconvolvematrix => 'feConvolveMatrix',
4463                   fediffuselighting => 'feDiffuseLighting',
4464                   fedisplacementmap => 'feDisplacementMap',
4465                   fedistantlight => 'feDistantLight',
4466                   feflood => 'feFlood',
4467                   fefunca => 'feFuncA',
4468                   fefuncb => 'feFuncB',
4469                   fefuncg => 'feFuncG',
4470                   fefuncr => 'feFuncR',
4471                   fegaussianblur => 'feGaussianBlur',
4472                   feimage => 'feImage',
4473                   femerge => 'feMerge',
4474                   femergenode => 'feMergeNode',
4475                   femorphology => 'feMorphology',
4476                   feoffset => 'feOffset',
4477                   fepointlight => 'fePointLight',
4478                   fespecularlighting => 'feSpecularLighting',
4479                   fespotlight => 'feSpotLight',
4480                   fetile => 'feTile',
4481                   feturbulence => 'feTurbulence',
4482                   foreignobject => 'foreignObject',
4483                   glyphref => 'glyphRef',
4484                   lineargradient => 'linearGradient',
4485                   radialgradient => 'radialGradient',
4486                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4487                   textpath => 'textPath',  
4488                }->{$tag_name} || $tag_name;
4489              }
4490    
4491              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4492    
4493              ## "adjust foreign attributes" - done in insert-element-f
4494    
4495              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4496    
4497              if ($self->{self_closing}) {
4498                pop @{$self->{open_elements}};
4499                !!!ack ('t87.3');
4500              } else {
4501                !!!cp ('t87.4');
4502              }
4503    
4504              !!!next-token;
4505              next B;
4506            }
4507          } elsif ($token->{type} == END_TAG_TOKEN) {
4508            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4509            !!!cp ('t87.5');
4510            #
4511          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4512            !!!cp ('t87.6');
4513            !!!parse-error (type => 'not closed',
4514                            text => $self->{open_elements}->[-1]->[0]
4515                                ->manakai_local_name,
4516                            token => $token);
4517    
4518            pop @{$self->{open_elements}}
4519                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4520    
4521            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4522            ## Reprocess.
4523            next B;
4524          } else {
4525            die "$0: $token->{type}: Unknown token type";        
4526          }
4527        }
4528    
4529        if ($self->{insertion_mode} & HEAD_IMS) {
4530        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4531          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4532            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4533                !!!cp ('t88.2');
4534                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4535                #
4536              } else {
4537                !!!cp ('t88.1');
4538                ## Ignore the token.
4539                #
4540              }
4541            unless (length $token->{data}) {            unless (length $token->{data}) {
4542                !!!cp ('t88');
4543              !!!next-token;              !!!next-token;
4544              redo B;              next B;
4545            }            }
4546    ## TODO: set $token->{column} appropriately
4547          }          }
4548    
4549          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4550              !!!cp ('t89');
4551            ## As if <head>            ## As if <head>
4552            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4553            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4554            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4555                  [$self->{head_element}, $el_category->{head}];
4556    
4557            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4558            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4559    
4560            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4561          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4562              !!!cp ('t90');
4563            ## As if </noscript>            ## As if </noscript>
4564            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4565            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4566                        
4567            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4568            ## As if </head>            ## As if </head>
# Line 3234  sub _tree_construction_main ($) { Line 4570  sub _tree_construction_main ($) {
4570    
4571            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4572          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4573              !!!cp ('t91');
4574            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4575    
4576            ## Reprocess in the "after head" insertion mode...            ## Reprocess in the "after head" insertion mode...
4577            } else {
4578              !!!cp ('t92');
4579          }          }
4580    
4581              ## "after head" insertion mode          ## "after head" insertion mode
4582              ## As if <body>          ## As if <body>
4583              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4584              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4585              ## reprocess          ## reprocess
4586              redo B;          next B;
4587            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4588              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4589                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4590                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!cp ('t93');
4591                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4592                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];              $self->{open_elements}->[-1]->[0]->append_child
4593                  $self->{insertion_mode} = IN_HEAD_IM;                  ($self->{head_element});
4594                  !!!next-token;              push @{$self->{open_elements}},
4595                  redo B;                  [$self->{head_element}, $el_category->{head}];
4596                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              $self->{insertion_mode} = IN_HEAD_IM;
4597                  #              !!!nack ('t93.1');
4598                } else {              !!!next-token;
4599                  !!!parse-error (type => 'in head:head'); # or in head noscript              next B;
4600                  ## Ignore the token            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4601                  !!!next-token;              !!!cp ('t93.2');
4602                  redo B;              !!!parse-error (type => 'after head', text => 'head',
4603                }                              token => $token);
4604              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              ## Ignore the token
4605                ## As if <head>              !!!nack ('t93.3');
4606                !!!create-element ($self->{head_element}, 'head');              !!!next-token;
4607                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              next B;
4608                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            } else {
4609                !!!cp ('t95');
4610                !!!parse-error (type => 'in head:head',
4611                                token => $token); # or in head noscript
4612                ## Ignore the token
4613                !!!nack ('t95.1');
4614                !!!next-token;
4615                next B;
4616              }
4617            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4618              !!!cp ('t96');
4619              ## As if <head>
4620              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4621              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4622              push @{$self->{open_elements}},
4623                  [$self->{head_element}, $el_category->{head}];
4624    
4625                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4626                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4627              }          } else {
4628              !!!cp ('t97');
4629            }
4630    
4631              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4632                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4633                    !!!cp ('t98');
4634                  ## As if </noscript>                  ## As if </noscript>
4635                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4636                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4637                                    token => $token);
4638                                
4639                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4640                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4641                  } else {
4642                    !!!cp ('t99');
4643                }                }
4644    
4645                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4646                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4647                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t100');
4648                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4649                                    text => $token->{tag_name}, token => $token);
4650                    push @{$self->{open_elements}},
4651                        [$self->{head_element}, $el_category->{head}];
4652                  } else {
4653                    !!!cp ('t101');
4654                }                }
4655                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4656                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4657                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4658                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4659                  !!!nack ('t101.1');
4660                !!!next-token;                !!!next-token;
4661                redo B;                next B;
4662              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4663                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4664                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4665                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t102');
4666                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4667                                    text => $token->{tag_name}, token => $token);
4668                    push @{$self->{open_elements}},
4669                        [$self->{head_element}, $el_category->{head}];
4670                  } else {
4671                    !!!cp ('t103');
4672                }                }
4673                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4674                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4675                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4676                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4677                  !!!ack ('t103.1');
4678                !!!next-token;                !!!next-token;
4679                redo B;                next B;
4680              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4681                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4682                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4683                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t104');
4684                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4685                                    text => $token->{tag_name}, token => $token);
4686                    push @{$self->{open_elements}},
4687                        [$self->{head_element}, $el_category->{head}];
4688                  } else {
4689                    !!!cp ('t105');
4690                }                }
4691                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4692                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4693    
4694                unless ($self->{confident}) {                unless ($self->{confident}) {
4695                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4696                      !!!cp ('t106');
4697                      ## NOTE: Whether the encoding is supported or not is handled
4698                      ## in the {change_encoding} callback.
4699                    $self->{change_encoding}                    $self->{change_encoding}
4700                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4701                             $token);
4702                                        
4703                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4704                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4705                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4706                                                 ->{has_reference});                                                 ->{has_reference});
4707                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4708                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4709                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4710                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4711                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4712                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4713                        !!!cp ('t107');
4714                        ## NOTE: Whether the encoding is supported or not is handled
4715                        ## in the {change_encoding} callback.
4716                      $self->{change_encoding}                      $self->{change_encoding}
4717                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4718                               $token);
4719                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4720                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4721                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
4722                                                     ->{has_reference});                                                     ->{has_reference});
4723                      } else {
4724                        !!!cp ('t108');
4725                    }                    }
4726                  }                  }
4727                } else {                } else {
4728                  if ($token->{attributes}->{charset}) {                  if ($token->{attributes}->{charset}) {
4729                      !!!cp ('t109');
4730                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4731                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4732                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4733                                                 ->{has_reference});                                                 ->{has_reference});
4734                  }                  }
4735                  if ($token->{attributes}->{content}) {                  if ($token->{attributes}->{content}) {
4736                      !!!cp ('t110');
4737                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')                    $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4738                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4739                                             $token->{attributes}->{content}                                             $token->{attributes}->{content}
# Line 3353  sub _tree_construction_main ($) { Line 4741  sub _tree_construction_main ($) {
4741                  }                  }
4742                }                }
4743    
4744                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4745                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4746                  !!!ack ('t110.1');
4747                !!!next-token;                !!!next-token;
4748                redo B;                next B;
4749              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4750                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4751                    !!!cp ('t111');
4752                  ## As if </noscript>                  ## As if </noscript>
4753                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4754                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4755                                    token => $token);
4756                                
4757                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4758                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4759                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4760                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t112');
4761                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4762                                    text => $token->{tag_name}, token => $token);
4763                    push @{$self->{open_elements}},
4764                        [$self->{head_element}, $el_category->{head}];
4765                  } else {
4766                    !!!cp ('t113');
4767                }                }
4768    
4769                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4770                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4771                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4772                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4773                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4774                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4775                redo B;                next B;
4776              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4777                         $token->{tag_name} eq 'noframes') {
4778                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4779                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4780                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4781                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4782                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t114');
4783                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4784                                    text => $token->{tag_name}, token => $token);
4785                    push @{$self->{open_elements}},
4786                        [$self->{head_element}, $el_category->{head}];
4787                  } else {
4788                    !!!cp ('t115');
4789                }                }
4790                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4791                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4792                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4793                redo B;                next B;
4794              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4795                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4796                    !!!cp ('t116');
4797                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4798                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4799                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4800                    !!!nack ('t116.1');
4801                  !!!next-token;                  !!!next-token;
4802                  redo B;                  next B;
4803                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4804                  !!!parse-error (type => 'in noscript:noscript');                  !!!cp ('t117');
4805                    !!!parse-error (type => 'in noscript', text => 'noscript',
4806                                    token => $token);
4807                  ## Ignore the token                  ## Ignore the token
4808                    !!!nack ('t117.1');
4809                  !!!next-token;                  !!!next-token;
4810                  redo B;                  next B;
4811                } else {                } else {
4812                    !!!cp ('t118');
4813                  #                  #
4814                }                }
4815              } elsif ($token->{tag_name} eq 'script') {              } elsif ($token->{tag_name} eq 'script') {
4816                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4817                    !!!cp ('t119');
4818                  ## As if </noscript>                  ## As if </noscript>
4819                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4820                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4821                                    token => $token);
4822                                
4823                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4824                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4825                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4826                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!cp ('t120');
4827                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'after head',
4828                                    text => $token->{tag_name}, token => $token);
4829                    push @{$self->{open_elements}},
4830                        [$self->{head_element}, $el_category->{head}];
4831                  } else {
4832                    !!!cp ('t121');
4833                }                }
4834    
4835                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4836                $script_start_tag->($insert_to_current);                $script_start_tag->();
4837                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4838                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4839                redo B;                next B;
4840              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4841                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4842                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4843                    !!!cp ('t122');
4844                  ## As if </noscript>                  ## As if </noscript>
4845                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4846                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4847                                    text => $token->{tag_name}, token => $token);
4848                                    
4849                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4850                  ## As if </head>                  ## As if </head>
# Line 3436  sub _tree_construction_main ($) { Line 4852  sub _tree_construction_main ($) {
4852                                    
4853                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4854                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4855                    !!!cp ('t124');
4856                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4857                                    
4858                  ## Reprocess in the "after head" insertion mode...                  ## Reprocess in the "after head" insertion mode...
4859                  } else {
4860                    !!!cp ('t125');
4861                }                }
4862    
4863                ## "after head" insertion mode                ## "after head" insertion mode
4864                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4865                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4866                    !!!cp ('t126');
4867                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
4868                } elsif ($token->{tag_name} eq 'frameset') {                } elsif ($token->{tag_name} eq 'frameset') {
4869                    !!!cp ('t127');
4870                  $self->{insertion_mode} = IN_FRAMESET_IM;                  $self->{insertion_mode} = IN_FRAMESET_IM;
4871                } else {                } else {
4872                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4873                }                }
4874                  !!!nack ('t127.1');
4875                !!!next-token;                !!!next-token;
4876                redo B;                next B;
4877              } else {              } else {
4878                  !!!cp ('t128');
4879                #                #
4880              }              }
4881    
4882              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4883                  !!!cp ('t129');
4884                ## As if </noscript>                ## As if </noscript>
4885                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4886                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4887                                  text => $token->{tag_name}, token => $token);
4888                                
4889                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4890                ## As if </head>                ## As if </head>
# Line 3467  sub _tree_construction_main ($) { Line 4892  sub _tree_construction_main ($) {
4892    
4893                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4894              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4895                  !!!cp ('t130');
4896                ## As if </head>                ## As if </head>
4897                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4898    
4899                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4900                } else {
4901                  !!!cp ('t131');
4902              }              }
4903    
4904              ## "after head" insertion mode              ## "after head" insertion mode
4905              ## As if <body>              ## As if <body>
4906              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4907              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4908              ## reprocess              ## reprocess
4909              redo B;              !!!ack-later;
4910                next B;
4911            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4912              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4913                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4914                    !!!cp ('t132');
4915                  ## As if <head>                  ## As if <head>
4916                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4917                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4918                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4919                        [$self->{head_element}, $el_category->{head}];
4920    
4921                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4922                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4923                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4924                  !!!next-token;                  !!!next-token;
4925                  redo B;                  next B;
4926                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4927                    !!!cp ('t133');
4928                  ## As if </noscript>                  ## As if </noscript>
4929                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4930                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
4931                                    text => 'head', token => $token);
4932                                    
4933                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4934                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4935                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4936                  !!!next-token;                  !!!next-token;
4937                  redo B;                  next B;
4938                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4939                    !!!cp ('t134');
4940                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4941                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4942                  !!!next-token;                  !!!next-token;
4943                  redo B;                  next B;
4944                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4945                    !!!cp ('t134.1');
4946                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4947                                    token => $token);
4948                    ## Ignore the token
4949                    !!!next-token;
4950                    next B;
4951                } else {                } else {
4952                  #                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4953                }                }
4954              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4955                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4956                    !!!cp ('t136');
4957                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4958                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4959                  !!!next-token;                  !!!next-token;
4960                  redo B;                  next B;
4961                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4962                  !!!parse-error (type => 'unmatched end tag:noscript');                         $self->{insertion_mode} == AFTER_HEAD_IM) {
4963                    !!!cp ('t137');
4964                    !!!parse-error (type => 'unmatched end tag',
4965                                    text => 'noscript', token => $token);
4966                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4967                  !!!next-token;                  !!!next-token;
4968                  redo B;                  next B;
4969                } else {                } else {
4970                    !!!cp ('t138');
4971                  #                  #
4972                }                }
4973              } elsif ({              } elsif ({
4974                        body => 1, html => 1,                        body => 1, html => 1,
4975                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4976                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4977                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_IM or
4978                  !!!create-element ($self->{head_element}, 'head');                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4979                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  !!!cp ('t140');
4980                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  !!!parse-error (type => 'unmatched end tag',
4981                                    text => $token->{tag_name}, token => $token);
4982                  $self->{insertion_mode} = IN_HEAD_IM;                  ## Ignore the token
4983                  ## Reprocess in the "in head" insertion mode...                  !!!next-token;
4984                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                  next B;
4985                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4986                    !!!cp ('t140.1');
4987                    !!!parse-error (type => 'unmatched end tag',
4988                                    text => $token->{tag_name}, token => $token);
4989                  ## Ignore the token                  ## Ignore the token
4990                  !!!next-token;                  !!!next-token;
4991                  redo B;                  next B;
4992                  } else {
4993                    die "$0: $self->{insertion_mode}: Unknown insertion mode";
4994                }                }
4995                              } elsif ($token->{tag_name} eq 'p') {
4996                #                !!!cp ('t142');
4997              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4998                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4999                       }->{$token->{tag_name}}) {                ## Ignore the token
5000                  !!!next-token;
5001                  next B;
5002                } elsif ($token->{tag_name} eq 'br') {
5003                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5004                  ## As if <head>                  !!!cp ('t142.2');
5005                  !!!create-element ($self->{head_element}, 'head');                  ## (before head) as if <head>, (in head) as if </head>
5006                    !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5007                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
5008                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
5009      
5010                    ## Reprocess in the "after head" insertion mode...
5011                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5012                    !!!cp ('t143.2');
5013                    ## As if </head>
5014                    pop @{$self->{open_elements}};
5015                    $self->{insertion_mode} = AFTER_HEAD_IM;
5016      
5017                    ## Reprocess in the "after head" insertion mode...
5018                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5019                    !!!cp ('t143.3');
5020                    ## ISSUE: Two parse errors for <head><noscript></br>
5021                    !!!parse-error (type => 'unmatched end tag',
5022                                    text => 'br', token => $token);
5023                    ## As if </noscript>
5024                    pop @{$self->{open_elements}};
5025                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
5026    
5027                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
5028                }                  ## As if </head>
5029                    pop @{$self->{open_elements}};
5030                    $self->{insertion_mode} = AFTER_HEAD_IM;
5031    
5032                #                  ## Reprocess in the "after head" insertion mode...
5033              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5034                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
5035                  #                  #
5036                } else {                } else {
5037                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5038                }                }
5039    
5040                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5041                  !!!parse-error (type => 'unmatched end tag',
5042                                  text => 'br', token => $token);
5043                  ## Ignore the token
5044                  !!!next-token;
5045                  next B;
5046                } else {
5047                  !!!cp ('t145');
5048                  !!!parse-error (type => 'unmatched end tag',
5049                                  text => $token->{tag_name}, token => $token);
5050                  ## Ignore the token
5051                  !!!next-token;
5052                  next B;
5053              }              }
5054    
5055              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5056                  !!!cp ('t146');
5057                ## As if </noscript>                ## As if </noscript>
5058                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5059                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
5060                                  text => $token->{tag_name}, token => $token);
5061                                
5062                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5063                ## As if </head>                ## As if </head>
# Line 3579  sub _tree_construction_main ($) { Line 5065  sub _tree_construction_main ($) {
5065    
5066                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
5067              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {              } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5068                  !!!cp ('t147');
5069                ## As if </head>                ## As if </head>
5070                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5071    
5072                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
5073              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5074                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## ISSUE: This case cannot be reached?
5075                  !!!cp ('t148');
5076                  !!!parse-error (type => 'unmatched end tag',
5077                                  text => $token->{tag_name}, token => $token);
5078                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5079                !!!next-token;                !!!next-token;
5080                redo B;                next B;
5081                } else {
5082                  !!!cp ('t149');
5083              }              }
5084    
5085              ## "after head" insertion mode              ## "after head" insertion mode
5086              ## As if <body>              ## As if <body>
5087              !!!insert-element ('body');              !!!insert-element ('body',, $token);
5088              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
5089              ## reprocess              ## reprocess
5090              redo B;              next B;
5091            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5092              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5093            }            !!!cp ('t149.1');
5094    
5095              ## NOTE: As if <head>
5096              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5097              $self->{open_elements}->[-1]->[0]->append_child
5098                  ($self->{head_element});
5099              #push @{$self->{open_elements}},
5100              #    [$self->{head_element}, $el_category->{head}];
5101              #$self->{insertion_mode} = IN_HEAD_IM;
5102              ## NOTE: Reprocess.
5103    
5104              ## NOTE: As if </head>
5105              #pop @{$self->{open_elements}};
5106              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5107              ## NOTE: Reprocess.
5108              
5109              #
5110            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5111              !!!cp ('t149.2');
5112    
5113              ## NOTE: As if </head>
5114              pop @{$self->{open_elements}};
5115              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5116              ## NOTE: Reprocess.
5117    
5118              #
5119            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5120              !!!cp ('t149.3');
5121    
5122              !!!parse-error (type => 'in noscript:#eof', token => $token);
5123    
5124              ## As if </noscript>
5125              pop @{$self->{open_elements}};
5126              #$self->{insertion_mode} = IN_HEAD_IM;
5127              ## NOTE: Reprocess.
5128    
5129              ## NOTE: As if </head>
5130              pop @{$self->{open_elements}};
5131              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5132              ## NOTE: Reprocess.
5133    
5134              #
5135            } else {
5136              !!!cp ('t149.4');
5137              #
5138            }
5139    
5140            ## NOTE: As if <body>
5141            !!!insert-element ('body',, $token);
5142            $self->{insertion_mode} = IN_BODY_IM;
5143            ## NOTE: Reprocess.
5144            next B;
5145          } else {
5146            die "$0: $token->{type}: Unknown token type";
5147          }
5148    
5149            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5150      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
5151            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
5152                !!!cp ('t150');
5153              ## NOTE: There is a code clone of "character in body".              ## NOTE: There is a code clone of "character in body".
5154              $reconstruct_active_formatting_elements->($insert_to_current);              $reconstruct_active_formatting_elements->($insert_to_current);
5155                            
5156              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5157    
5158              !!!next-token;              !!!next-token;
5159              redo B;              next B;
5160            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5161              if ({              if ({
5162                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3617  sub _tree_construction_main ($) { Line 5164  sub _tree_construction_main ($) {
5164                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5165                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5166                  ## have an element in table scope                  ## have an element in table scope
5167                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5168                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5169                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5170                      $tn = $node->[1];                      !!!cp ('t151');
5171                      last INSCOPE;  
5172                    } elsif ({                      ## Close the cell
5173                              table => 1, html => 1,                      !!!back-token; # <x>
5174                             }->{$node->[1]}) {                      $token = {type => END_TAG_TOKEN,
5175                      last INSCOPE;                                tag_name => $node->[0]->manakai_local_name,
5176                    }                                line => $token->{line},
5177                  } # INSCOPE                                column => $token->{column}};
5178                    unless (defined $tn) {                      next B;
5179                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5180                      ## Ignore the token                      !!!cp ('t152');
5181                      !!!next-token;                      ## ISSUE: This case can never be reached, maybe.
5182                      redo B;                      last;
5183                    }                    }
5184                                    }
5185                  ## Close the cell  
5186                  !!!back-token; # <?>                  !!!cp ('t153');
5187                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                  !!!parse-error (type => 'start tag not allowed',
5188                  redo B;                      text => $token->{tag_name}, token => $token);
5189                    ## Ignore the token
5190                    !!!nack ('t153.1');
5191                    !!!next-token;
5192                    next B;
5193                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5194                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5195                                    token => $token);
5196                                    
5197                  ## As if </caption>                  ## NOTE: As if </caption>.
5198                  ## have a table element in table scope                  ## have a table element in table scope
5199                  my $i;                  my $i;
5200                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5201                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5202                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5203                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5204                      last INSCOPE;                        !!!cp ('t155');
5205                    } elsif ({                        $i = $_;
5206                              table => 1, html => 1,                        last INSCOPE;
5207                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5208                      last INSCOPE;                        !!!cp ('t156');
5209                          last;
5210                        }
5211                    }                    }
5212    
5213                      !!!cp ('t157');
5214                      !!!parse-error (type => 'start tag not allowed',
5215                                      text => $token->{tag_name}, token => $token);
5216                      ## Ignore the token
5217                      !!!nack ('t157.1');
5218                      !!!next-token;
5219                      next B;
5220                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5221                                    
5222                  ## generate implied end tags                  ## generate implied end tags
5223                  if ({                  while ($self->{open_elements}->[-1]->[1]
5224                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5225                       td => 1, th => 1, tr => 1,                    !!!cp ('t158');
5226                       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;  
5227                  }                  }
5228    
5229                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5230                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t159');
5231                      !!!parse-error (type => 'not closed',
5232                                      text => $self->{open_elements}->[-1]->[0]
5233                                          ->manakai_local_name,
5234                                      token => $token);
5235                    } else {
5236                      !!!cp ('t160');
5237                  }                  }
5238                                    
5239                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3689  sub _tree_construction_main ($) { Line 5243  sub _tree_construction_main ($) {
5243                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5244                                    
5245                  ## reprocess                  ## reprocess
5246                  redo B;                  !!!ack-later;
5247                    next B;
5248                } else {                } else {
5249                    !!!cp ('t161');
5250                  #                  #
5251                }                }
5252              } else {              } else {
5253                  !!!cp ('t162');
5254                #                #
5255              }              }
5256            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
# Line 3703  sub _tree_construction_main ($) { Line 5260  sub _tree_construction_main ($) {
5260                  my $i;                  my $i;
5261                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5262                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5263                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5264                        !!!cp ('t163');
5265                      $i = $_;                      $i = $_;
5266                      last INSCOPE;                      last INSCOPE;
5267                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5268                              table => 1, html => 1,                      !!!cp ('t164');
                            }->{$node->[1]}) {  
5269                      last INSCOPE;                      last INSCOPE;
5270                    }                    }
5271                  } # INSCOPE                  } # INSCOPE
5272                    unless (defined $i) {                    unless (defined $i) {
5273                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t165');
5274                        !!!parse-error (type => 'unmatched end tag',
5275                                        text => $token->{tag_name},
5276                                        token => $token);
5277                      ## Ignore the token                      ## Ignore the token
5278                      !!!next-token;                      !!!next-token;
5279                      redo B;                      next B;
5280                    }                    }
5281                                    
5282                  ## generate implied end tags                  ## generate implied end tags
5283                  if ({                  while ($self->{open_elements}->[-1]->[1]
5284                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5285                       td => ($token->{tag_name} eq 'th'),                    !!!cp ('t166');
5286                       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;  
5287                  }                  }
5288                    
5289                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5290                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                          ne $token->{tag_name}) {
5291                      !!!cp ('t167');
5292                      !!!parse-error (type => 'not closed',
5293                                      text => $self->{open_elements}->[-1]->[0]
5294                                          ->manakai_local_name,
5295                                      token => $token);
5296                    } else {
5297                      !!!cp ('t168');
5298                  }                  }
5299                                    
5300                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3744  sub _tree_construction_main ($) { Line 5304  sub _tree_construction_main ($) {
5304                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5305                                    
5306                  !!!next-token;                  !!!next-token;
5307                  redo B;                  next B;
5308                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5309                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t169');
5310                    !!!parse-error (type => 'unmatched end tag',
5311                                    text => $token->{tag_name}, token => $token);
5312                  ## Ignore the token                  ## Ignore the token
5313                  !!!next-token;                  !!!next-token;
5314                  redo B;                  next B;
5315                } else {                } else {
5316                    !!!cp ('t170');
5317                  #                  #
5318                }                }
5319              } elsif ($token->{tag_name} eq 'caption') {              } elsif ($token->{tag_name} eq 'caption') {
5320                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5321                  ## have a table element in table scope                  ## have a table element in table scope
5322                  my $i;                  my $i;
5323                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5324                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5325                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5326                      $i = $_;                      if ($node->[1] & CAPTION_EL) {
5327                      last INSCOPE;                        !!!cp ('t171');
5328                    } elsif ({                        $i = $_;
5329                              table => 1, html => 1,                        last INSCOPE;
5330                             }->{$node->[1]}) {                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5331                      last INSCOPE;                        !!!cp ('t172');
5332                          last;
5333                        }
5334                    }                    }
5335    
5336                      !!!cp ('t173');
5337                      !!!parse-error (type => 'unmatched end tag',
5338                                      text => $token->{tag_name}, token => $token);
5339                      ## Ignore the token
5340                      !!!next-token;
5341                      next B;
5342                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5343                                    
5344                  ## generate implied end tags                  ## generate implied end tags
5345                  if ({                  while ($self->{open_elements}->[-1]->[1]
5346                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
5347                       td => 1, th => 1, tr => 1,                    !!!cp ('t174');
5348                       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;  
5349                  }                  }
5350                                    
5351                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5352                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t175');
5353                      !!!parse-error (type => 'not closed',
5354                                      text => $self->{open_elements}->[-1]->[0]
5355                                          ->manakai_local_name,
5356                                      token => $token);
5357                    } else {
5358                      !!!cp ('t176');
5359                  }                  }
5360                                    
5361                  splice @{$self->{open_elements}}, $i;                  splice @{$self->{open_elements}}, $i;
# Line 3798  sub _tree_construction_main ($) { Line 5365  sub _tree_construction_main ($) {
5365                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5366                                    
5367                  !!!next-token;                  !!!next-token;
5368                  redo B;                  next B;
5369                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5370                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t177');
5371                    !!!parse-error (type => 'unmatched end tag',
5372                                    text => $token->{tag_name}, token => $token);
5373                  ## Ignore the token                  ## Ignore the token
5374                  !!!next-token;                  !!!next-token;
5375                  redo B;                  next B;
5376                } else {                } else {
5377                    !!!cp ('t178');
5378                  #                  #
5379                }                }
5380              } elsif ({              } elsif ({
# Line 3815  sub _tree_construction_main ($) { Line 5385  sub _tree_construction_main ($) {
5385                ## have an element in table scope                ## have an element in table scope
5386                my $i;                my $i;
5387                my $tn;                my $tn;
5388                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5389                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5390                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5391                    $i = $_;                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5392                    last INSCOPE;                      !!!cp ('t179');
5393                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {                      $i = $_;
5394                    $tn = $node->[1];  
5395                    ## NOTE: There is exactly one |td| or |th| element                      ## Close the cell
5396                    ## in scope in the stack of open elements by definition.                      !!!back-token; # </x>
5397                  } elsif ({                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5398                            table => 1, html => 1,                                line => $token->{line},
5399                           }->{$node->[1]}) {                                column => $token->{column}};
5400                    last INSCOPE;                      next B;
5401                      } elsif ($node->[1] & TABLE_CELL_EL) {
5402                        !!!cp ('t180');
5403                        $tn = $node->[0]->manakai_local_name;
5404                        ## NOTE: There is exactly one |td| or |th| element
5405                        ## in scope in the stack of open elements by definition.
5406                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5407                        ## ISSUE: Can this be reached?
5408                        !!!cp ('t181');
5409                        last;
5410                      }
5411                  }                  }
5412                } # INSCOPE  
5413                unless (defined $i) {                  !!!cp ('t182');
5414                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5415                        text => $token->{tag_name}, token => $token);
5416                  ## Ignore the token                  ## Ignore the token
5417                  !!!next-token;                  !!!next-token;
5418                  redo B;                  next B;
5419                }                } # INSCOPE
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5420              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5421                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5422                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5423                                  token => $token);
5424    
5425                ## As if </caption>                ## As if </caption>
5426                ## have a table element in table scope                ## have a table element in table scope
5427                my $i;                my $i;
5428                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5429                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5430                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5431                      !!!cp ('t184');
5432                    $i = $_;                    $i = $_;
5433                    last INSCOPE;                    last INSCOPE;
5434                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5435                            table => 1, html => 1,                    !!!cp ('t185');
                          }->{$node->[1]}) {  
5436                    last INSCOPE;                    last INSCOPE;
5437                  }                  }
5438                } # INSCOPE                } # INSCOPE
5439                unless (defined $i) {                unless (defined $i) {
5440                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!cp ('t186');
5441                    !!!parse-error (type => 'unmatched end tag',
5442                                    text => 'caption', token => $token);
5443                  ## Ignore the token                  ## Ignore the token
5444                  !!!next-token;                  !!!next-token;
5445                  redo B;                  next B;
5446                }                }
5447                                
5448                ## generate implied end tags                ## generate implied end tags
5449                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5450                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t187');
5451                     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;  
5452                }                }
5453    
5454                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5455                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t188');
5456                    !!!parse-error (type => 'not closed',
5457                                    text => $self->{open_elements}->[-1]->[0]
5458                                        ->manakai_local_name,
5459                                    token => $token);
5460                  } else {
5461                    !!!cp ('t189');
5462                }                }
5463    
5464                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 3891  sub _tree_construction_main ($) { Line 5468  sub _tree_construction_main ($) {
5468                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5469    
5470                ## reprocess                ## reprocess
5471                redo B;                next B;
5472              } elsif ({              } elsif ({
5473                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5474                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5475                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5476                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t190');
5477                    !!!parse-error (type => 'unmatched end tag',
5478                                    text => $token->{tag_name}, token => $token);
5479                  ## Ignore the token                  ## Ignore the token
5480                  !!!next-token;                  !!!next-token;
5481                  redo B;                  next B;
5482                } else {                } else {
5483                    !!!cp ('t191');
5484                  #                  #
5485                }                }
5486              } elsif ({              } elsif ({
# Line 3908  sub _tree_construction_main ($) { Line 5488  sub _tree_construction_main ($) {
5488                        thead => 1, tr => 1,                        thead => 1, tr => 1,
5489                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5490                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5491                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t192');
5492                  !!!parse-error (type => 'unmatched end tag',
5493                                  text => $token->{tag_name}, token => $token);
5494                ## Ignore the token                ## Ignore the token
5495                !!!next-token;                !!!next-token;
5496                redo B;                next B;
5497              } else {              } else {
5498                  !!!cp ('t193');
5499                #                #
5500              }              }
5501          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5502            for my $entry (@{$self->{open_elements}}) {
5503              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5504                !!!cp ('t75');
5505                !!!parse-error (type => 'in body:#eof', token => $token);
5506                last;
5507              }
5508            }
5509    
5510            ## Stop parsing.
5511            last B;
5512        } else {        } else {
5513          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5514        }        }
# Line 3923  sub _tree_construction_main ($) { Line 5517  sub _tree_construction_main ($) {
5517        #        #
5518      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5519        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5520              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5521                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5522              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5523                                
5524                unless (length $token->{data}) {            unless (length $token->{data}) {
5525                  !!!next-token;              !!!cp ('t194');
5526                  redo B;              !!!next-token;
5527                }              next B;
5528              }            } else {
5529                !!!cp ('t195');
5530              }
5531            }
5532    
5533              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5534    
5535              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5536              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 3940  sub _tree_construction_main ($) { Line 5538  sub _tree_construction_main ($) {
5538              ## result in a new Text node.              ## result in a new Text node.
5539              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5540                            
5541              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]}) {  
5542                # MUST                # MUST
5543                my $foster_parent_element;                my $foster_parent_element;
5544                my $next_sibling;                my $next_sibling;
5545                my $prev_sibling;                my $prev_sibling;
5546                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5547                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5548                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5549                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5550                        !!!cp ('t196');
5551                      $foster_parent_element = $parent;                      $foster_parent_element = $parent;
5552                      $next_sibling = $self->{open_elements}->[$_]->[0];                      $next_sibling = $self->{open_elements}->[$_]->[0];
5553                      $prev_sibling = $next_sibling->previous_sibling;                      $prev_sibling = $next_sibling->previous_sibling;
5554                    } else {                    } else {
5555                        !!!cp ('t197');
5556                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];                      $foster_parent_element = $self->{open_elements}->[$_ - 1]->[0];
5557                      $prev_sibling = $foster_parent_element->last_child;                      $prev_sibling = $foster_parent_element->last_child;
5558                    }                    }
# Line 3967  sub _tree_construction_main ($) { Line 5564  sub _tree_construction_main ($) {
5564                  unless defined $foster_parent_element;                  unless defined $foster_parent_element;
5565                if (defined $prev_sibling and                if (defined $prev_sibling and
5566                    $prev_sibling->node_type == 3) {                    $prev_sibling->node_type == 3) {
5567                    !!!cp ('t198');
5568                  $prev_sibling->manakai_append_text ($token->{data});                  $prev_sibling->manakai_append_text ($token->{data});
5569                } else {                } else {
5570                    !!!cp ('t199');
5571                  $foster_parent_element->insert_before                  $foster_parent_element->insert_before
5572                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5573                     $next_sibling);                     $next_sibling);
5574                }                }
5575              } else {            $open_tables->[-1]->[1] = 1; # tainted
5576                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          } else {
5577              }            !!!cp ('t200');
5578              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5579            }
5580                            
5581              !!!next-token;          !!!next-token;
5582              redo B;          next B;
5583        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5584              if ({          if ({
5585                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5586                   th => 1, td => 1,               th => 1, td => 1,
5587                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5588                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5589                  ## Clear back to table context              ## Clear back to table context
5590                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5591                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5592                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!cp ('t201');
5593                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5594                  }              }
5595                                
5596                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5597                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5598                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5599                }            }
5600              
5601                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5602                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5603                    !!!parse-error (type => 'missing start tag:tr');                !!!cp ('t202');
5604                  }                !!!parse-error (type => 'missing start tag:tr', token => $token);
5605                }
5606                                    
5607                  ## Clear back to table body context              ## Clear back to table body context
5608                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5609                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5610                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5611                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                ## ISSUE: Can this case be reached?
5612                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5613                  }              }
5614                                    
5615                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5616                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5617                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!cp ('t204');
5618                      !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5619                      !!!nack ('t204');
5620                    !!!next-token;                    !!!next-token;
5621                    redo B;                    next B;
5622                  } else {                  } else {
5623                    !!!insert-element ('tr');                    !!!cp ('t205');
5624                      !!!insert-element ('tr',, $token);
5625                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5626                  }                  }
5627                  } else {
5628                    !!!cp ('t206');
5629                }                }
5630    
5631                ## Clear back to table row context                ## Clear back to table row context
5632                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5633                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5634                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5635                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5636                }                }
5637                                
5638                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5639                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5640    
5641                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5642                                
5643                  !!!nack ('t207.1');
5644                !!!next-token;                !!!next-token;
5645                redo B;                next B;
5646              } elsif ({              } elsif ({
5647                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5648                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4047  sub _tree_construction_main ($) { Line 5654  sub _tree_construction_main ($) {
5654                  my $i;                  my $i;
5655                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5656                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5657                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5658                        !!!cp ('t208');
5659                      $i = $_;                      $i = $_;
5660                      last INSCOPE;                      last INSCOPE;
5661                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5662                              table => 1, html => 1,                      !!!cp ('t209');
                            }->{$node->[1]}) {  
5663                      last INSCOPE;                      last INSCOPE;
5664                    }                    }
5665                  } # INSCOPE                  } # INSCOPE
5666                  unless (defined $i) {                  unless (defined $i) {
5667                    !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!cp ('t210');
5668    ## TODO: This type is wrong.
5669                      !!!parse-error (type => 'unmacthed end tag',
5670                                      text => $token->{tag_name}, token => $token);
5671                    ## Ignore the token                    ## Ignore the token
5672                      !!!nack ('t210.1');
5673                    !!!next-token;                    !!!next-token;
5674                    redo B;                    next B;
5675                  }                  }
5676                                    
5677                  ## Clear back to table row context                  ## Clear back to table row context
5678                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5679                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5680                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t211');
5681                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5682                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5683                  }                  }
5684                                    
5685                  pop @{$self->{open_elements}}; # tr                  pop @{$self->{open_elements}}; # tr
5686                  $self->{insertion_mode} = IN_TABLE_BODY_IM;                  $self->{insertion_mode} = IN_TABLE_BODY_IM;
5687                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5688                      !!!cp ('t212');
5689                    ## reprocess                    ## reprocess
5690                    redo B;                    !!!ack-later;
5691                      next B;
5692                  } else {                  } else {
5693                      !!!cp ('t213');
5694                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
5695                  }                  }
5696                }                }
# Line 4086  sub _tree_construction_main ($) { Line 5700  sub _tree_construction_main ($) {
5700                  my $i;                  my $i;
5701                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5702                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5703                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5704                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t214');
                       }->{$node->[1]}) {  
5705                      $i = $_;                      $i = $_;
5706                      last INSCOPE;                      last INSCOPE;
5707                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5708                              table => 1, html => 1,                      !!!cp ('t215');
                            }->{$node->[1]}) {  
5709                      last INSCOPE;                      last INSCOPE;
5710                    }                    }
5711                  } # INSCOPE                  } # INSCOPE
5712                  unless (defined $i) {                  unless (defined $i) {
5713                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t216');
5714    ## TODO: This erorr type is wrong.
5715                      !!!parse-error (type => 'unmatched end tag',
5716                                      text => $token->{tag_name}, token => $token);
5717                    ## Ignore the token                    ## Ignore the token
5718                      !!!nack ('t216.1');
5719                    !!!next-token;                    !!!next-token;
5720                    redo B;                    next B;
5721                  }                  }
5722    
5723                  ## Clear back to table body context                  ## Clear back to table body context
5724                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5725                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
5726                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t217');
5727                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5728                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5729                  }                  }
5730                                    
# Line 4122  sub _tree_construction_main ($) { Line 5738  sub _tree_construction_main ($) {
5738                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5739                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5740                  ## reprocess in "in table" insertion mode...                  ## reprocess in "in table" insertion mode...
5741                  } else {
5742                    !!!cp ('t218');
5743                }                }
5744    
5745                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5746                  ## Clear back to table context                  ## Clear back to table context
5747                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5748                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5749                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t219');
5750                      ## ISSUE: Can this state be reached?
5751                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5752                  }                  }
5753                                    
5754                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5755                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5756                  ## reprocess                  ## reprocess
5757                  redo B;                  !!!ack-later;
5758                    next B;
5759                } elsif ({                } elsif ({
5760                          caption => 1,                          caption => 1,
5761                          colgroup => 1,                          colgroup => 1,
5762                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5763                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5764                  ## Clear back to table context                  ## Clear back to table context
5765                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5766                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5767                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!cp ('t220');
5768                      ## ISSUE: Can this state be reached?
5769                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5770                  }                  }
5771                                    
5772                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5773                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5774                                    
5775                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5776                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5777                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5778                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4160  sub _tree_construction_main ($) { Line 5781  sub _tree_construction_main ($) {
5781                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5782                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5783                  !!!next-token;                  !!!next-token;
5784                  redo B;                  !!!nack ('t220.1');
5785                    next B;
5786                } else {                } else {
5787                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5788                }                }
5789              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5790                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5791                                  text => $self->{open_elements}->[-1]->[0]
5792                                      ->manakai_local_name,
5793                                  token => $token);
5794    
5795                ## As if </table>                ## As if </table>
5796                ## have a table element in table scope                ## have a table element in table scope
5797                my $i;                my $i;
5798                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5799                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5800                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5801                      !!!cp ('t221');
5802                    $i = $_;                    $i = $_;
5803                    last INSCOPE;                    last INSCOPE;
5804                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5805                            table => 1, html => 1,                    !!!cp ('t222');
                          }->{$node->[1]}) {  
5806                    last INSCOPE;                    last INSCOPE;
5807                  }                  }
5808                } # INSCOPE                } # INSCOPE
5809                unless (defined $i) {                unless (defined $i) {
5810                  !!!parse-error (type => 'unmatched end tag:table');                  !!!cp ('t223');
5811    ## TODO: The following is wrong, maybe.
5812                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5813                                    token => $token);
5814                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5815                    !!!nack ('t223.1');
5816                  !!!next-token;                  !!!next-token;
5817                  redo B;                  next B;
5818                }                }
5819                                
5820    ## TODO: Followings are removed from the latest spec.
5821                ## generate implied end tags                ## generate implied end tags
5822                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
5823                     dd => 1, dt => 1, li => 1, p => 1,                  !!!cp ('t224');
5824                     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;  
5825                }                }
5826    
5827                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5828                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!cp ('t225');
5829                    ## NOTE: |<table><tr><table>|
5830                    !!!parse-error (type => 'not closed',
5831                                    text => $self->{open_elements}->[-1]->[0]
5832                                        ->manakai_local_name,
5833                                    token => $token);
5834                  } else {
5835                    !!!cp ('t226');
5836                }                }
5837    
5838                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5839                  pop @{$open_tables};
5840    
5841                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5842    
5843                ## reprocess            ## reprocess
5844                redo B;            !!!ack-later;
5845          } else {            next B;
5846            !!!parse-error (type => 'in table:'.$token->{tag_name});          } elsif ($token->{tag_name} eq 'style') {
5847              if (not $open_tables->[-1]->[1]) { # tainted
5848                !!!cp ('t227.8');
5849                ## NOTE: This is a "as if in head" code clone.
5850                $parse_rcdata->(CDATA_CONTENT_MODEL);
5851                next B;
5852              } else {
5853                !!!cp ('t227.7');
5854                #
5855              }
5856            } elsif ($token->{tag_name} eq 'script') {
5857              if (not $open_tables->[-1]->[1]) { # tainted
5858                !!!cp ('t227.6');
5859                ## NOTE: This is a "as if in head" code clone.
5860                $script_start_tag->();
5861                next B;
5862              } else {
5863                !!!cp ('t227.5');
5864                #
5865              }
5866            } elsif ($token->{tag_name} eq 'input') {
5867              if (not $open_tables->[-1]->[1]) { # tainted
5868                if ($token->{attributes}->{type}) { ## TODO: case
5869                  my $type = lc $token->{attributes}->{type}->{value};
5870                  if ($type eq 'hidden') {
5871                    !!!cp ('t227.3');
5872                    !!!parse-error (type => 'in table',
5873                                    text => $token->{tag_name}, token => $token);
5874    
5875            $insert = $insert_to_foster;                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5876    
5877                    ## TODO: form element pointer
5878    
5879                    pop @{$self->{open_elements}};
5880    
5881                    !!!next-token;
5882                    !!!ack ('t227.2.1');
5883                    next B;
5884                  } else {
5885                    !!!cp ('t227.2');
5886                    #
5887                  }
5888                } else {
5889                  !!!cp ('t227.1');
5890                  #
5891                }
5892              } else {
5893                !!!cp ('t227.4');
5894                #
5895              }
5896            } else {
5897              !!!cp ('t227');
5898            #            #
5899          }          }
5900    
5901            !!!parse-error (type => 'in table', text => $token->{tag_name},
5902                            token => $token);
5903    
5904            $insert = $insert_to_foster;
5905            #
5906        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5907              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5908                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4225  sub _tree_construction_main ($) { Line 5910  sub _tree_construction_main ($) {
5910                my $i;                my $i;
5911                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5912                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5913                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5914                      !!!cp ('t228');
5915                    $i = $_;                    $i = $_;
5916                    last INSCOPE;                    last INSCOPE;
5917                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
5918                            table => 1, html => 1,                    !!!cp ('t229');
                          }->{$node->[1]}) {  
5919                    last INSCOPE;                    last INSCOPE;
5920                  }                  }
5921                } # INSCOPE                } # INSCOPE
5922                unless (defined $i) {                unless (defined $i) {
5923                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t230');
5924                    !!!parse-error (type => 'unmatched end tag',
5925                                    text => $token->{tag_name}, token => $token);
5926                  ## Ignore the token                  ## Ignore the token
5927                    !!!nack ('t230.1');
5928                  !!!next-token;                  !!!next-token;
5929                  redo B;                  next B;
5930                  } else {
5931                    !!!cp ('t232');
5932                }                }
5933    
5934                ## Clear back to table row context                ## Clear back to table row context
5935                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5936                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
5937                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t231');
5938                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5939                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5940                }                }
5941    
5942                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5943                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5944                !!!next-token;                !!!next-token;
5945                redo B;                !!!nack ('t231.1');
5946                  next B;
5947              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5948                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5949                  ## As if </tr>                  ## As if </tr>
# Line 4260  sub _tree_construction_main ($) { Line 5951  sub _tree_construction_main ($) {
5951                  my $i;                  my $i;
5952                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5953                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5954                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5955                        !!!cp ('t233');
5956                      $i = $_;                      $i = $_;
5957                      last INSCOPE;                      last INSCOPE;
5958                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5959                              table => 1, html => 1,                      !!!cp ('t234');
                            }->{$node->[1]}) {  
5960                      last INSCOPE;                      last INSCOPE;
5961                    }                    }
5962                  } # INSCOPE                  } # INSCOPE
5963                  unless (defined $i) {                  unless (defined $i) {
5964                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!cp ('t235');
5965    ## TODO: The following is wrong.
5966                      !!!parse-error (type => 'unmatched end tag',
5967                                      text => $token->{type}, token => $token);
5968                    ## Ignore the token                    ## Ignore the token
5969                      !!!nack ('t236.1');
5970                    !!!next-token;                    !!!next-token;
5971                    redo B;                    next B;
5972                  }                  }
5973                                    
5974                  ## Clear back to table row context                  ## Clear back to table row context
5975                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5976                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
5977                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t236');
5978                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5979                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5980                  }                  }
5981                                    
# Line 4294  sub _tree_construction_main ($) { Line 5989  sub _tree_construction_main ($) {
5989                  my $i;                  my $i;
5990                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5991                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5992                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
5993                         tbody => 1, thead => 1, tfoot => 1,                      !!!cp ('t237');
                       }->{$node->[1]}) {  
5994                      $i = $_;                      $i = $_;
5995                      last INSCOPE;                      last INSCOPE;
5996                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
5997                              table => 1, html => 1,                      !!!cp ('t238');
                            }->{$node->[1]}) {  
5998                      last INSCOPE;                      last INSCOPE;
5999                    }                    }
6000                  } # INSCOPE                  } # INSCOPE
6001                  unless (defined $i) {                  unless (defined $i) {
6002                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!cp ('t239');
6003                      !!!parse-error (type => 'unmatched end tag',
6004                                      text => $token->{tag_name}, token => $token);
6005                    ## Ignore the token                    ## Ignore the token
6006                      !!!nack ('t239.1');
6007                    !!!next-token;                    !!!next-token;
6008                    redo B;                    next B;
6009                  }                  }
6010                                    
6011                  ## Clear back to table body context                  ## Clear back to table body context
6012                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6013                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
6014                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6015                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6016                  }                  }
6017                                    
# Line 4332  sub _tree_construction_main ($) { Line 6027  sub _tree_construction_main ($) {
6027                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
6028                }                }
6029    
6030                  ## NOTE: </table> in the "in table" insertion mode.
6031                  ## When you edit the code fragment below, please ensure that
6032                  ## the code for <table> in the "in table" insertion mode
6033                  ## is synced with it.
6034    
6035                ## have a table element in table scope                ## have a table element in table scope
6036                my $i;                my $i;
6037                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6038                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6039                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
6040                      !!!cp ('t241');
6041                    $i = $_;                    $i = $_;
6042                    last INSCOPE;                    last INSCOPE;
6043                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6044                            table => 1, html => 1,                    !!!cp ('t242');
                          }->{$node->[1]}) {  
6045                    last INSCOPE;                    last INSCOPE;
6046                  }                  }
6047                } # INSCOPE                } # INSCOPE
6048                unless (defined $i) {                unless (defined $i) {
6049                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t243');
6050                    !!!parse-error (type => 'unmatched end tag',
6051                                    text => $token->{tag_name}, token => $token);
6052                  ## Ignore the token                  ## Ignore the token
6053                    !!!nack ('t243.1');
6054                  !!!next-token;                  !!!next-token;
6055                  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]);  
6056                }                }
6057                                    
6058                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
6059                  pop @{$open_tables};
6060                                
6061                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6062                                
6063                !!!next-token;                !!!next-token;
6064                redo B;                next B;
6065              } elsif ({              } elsif ({
6066                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6067                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4383  sub _tree_construction_main ($) { Line 6071  sub _tree_construction_main ($) {
6071                  my $i;                  my $i;
6072                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6073                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6074                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6075                        !!!cp ('t247');
6076                      $i = $_;                      $i = $_;
6077                      last INSCOPE;                      last INSCOPE;
6078                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6079                              table => 1, html => 1,                      !!!cp ('t248');
                            }->{$node->[1]}) {  
6080                      last INSCOPE;                      last INSCOPE;
6081                    }                    }
6082                  } # INSCOPE                  } # INSCOPE
6083                    unless (defined $i) {                    unless (defined $i) {
6084                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!cp ('t249');
6085                        !!!parse-error (type => 'unmatched end tag',
6086                                        text => $token->{tag_name}, token => $token);
6087                      ## Ignore the token                      ## Ignore the token
6088                        !!!nack ('t249.1');
6089                      !!!next-token;                      !!!next-token;
6090                      redo B;                      next B;
6091                    }                    }
6092                                    
6093                  ## As if </tr>                  ## As if </tr>
# Line 4404  sub _tree_construction_main ($) { Line 6095  sub _tree_construction_main ($) {
6095                  my $i;                  my $i;
6096                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6097                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6098                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6099                        !!!cp ('t250');
6100                      $i = $_;                      $i = $_;
6101                      last INSCOPE;                      last INSCOPE;
6102                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
6103                              table => 1, html => 1,                      !!!cp ('t251');
                            }->{$node->[1]}) {  
6104                      last INSCOPE;                      last INSCOPE;
6105                    }                    }
6106                  } # INSCOPE                  } # INSCOPE
6107                    unless (defined $i) {                    unless (defined $i) {
6108                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!cp ('t252');
6109                        !!!parse-error (type => 'unmatched end tag',
6110                                        text => 'tr', token => $token);
6111                      ## Ignore the token                      ## Ignore the token
6112                        !!!nack ('t252.1');
6113                      !!!next-token;                      !!!next-token;
6114                      redo B;                      next B;
6115                    }                    }
6116                                    
6117                  ## Clear back to table row context                  ## Clear back to table row context
6118                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6119                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
6120                  }->{$self->{open_elements}->[-1]->[1]}) {                    !!!cp ('t253');
6121                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6122                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6123                  }                  }
6124                                    
# Line 4437  sub _tree_construction_main ($) { Line 6131  sub _tree_construction_main ($) {
6131                my $i;                my $i;
6132                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6133                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6134                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6135                      !!!cp ('t254');
6136                    $i = $_;                    $i = $_;
6137                    last INSCOPE;                    last INSCOPE;
6138                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
6139                            table => 1, html => 1,                    !!!cp ('t255');
                          }->{$node->[1]}) {  
6140                    last INSCOPE;                    last INSCOPE;
6141                  }                  }
6142                } # INSCOPE                } # INSCOPE
6143                unless (defined $i) {                unless (defined $i) {
6144                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!cp ('t256');
6145                    !!!parse-error (type => 'unmatched end tag',
6146                                    text => $token->{tag_name}, token => $token);
6147                  ## Ignore the token                  ## Ignore the token
6148                    !!!nack ('t256.1');
6149                  !!!next-token;                  !!!next-token;
6150                  redo B;                  next B;
6151                }                }
6152    
6153                ## Clear back to table body context                ## Clear back to table body context
6154                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6155                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
6156                }->{$self->{open_elements}->[-1]->[1]}) {                  !!!cp ('t257');
6157                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6158                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6159                }                }
6160    
6161                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6162                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6163                  !!!nack ('t257.1');
6164                !!!next-token;                !!!next-token;
6165                redo B;                next B;
6166              } elsif ({              } elsif ({
6167                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6168                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6169                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6170                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6171                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6172                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t258');
6173                ## Ignore the token            !!!parse-error (type => 'unmatched end tag',
6174                !!!next-token;                            text => $token->{tag_name}, token => $token);
6175                redo B;            ## Ignore the token
6176          } else {            !!!nack ('t258.1');
6177            !!!parse-error (type => 'in table:/'.$token->{tag_name});             !!!next-token;
6178              next B;
6179            } else {
6180              !!!cp ('t259');
6181              !!!parse-error (type => 'in table:/',
6182                              text => $token->{tag_name}, token => $token);
6183    
6184            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6185            #            #
6186          }          }
6187          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6188            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6189                    @{$self->{open_elements}} == 1) { # redundant, maybe
6190              !!!parse-error (type => 'in body:#eof', token => $token);
6191              !!!cp ('t259.1');
6192              #
6193            } else {
6194              !!!cp ('t259.2');
6195              #
6196            }
6197    
6198            ## Stop parsing
6199            last B;
6200        } else {        } else {
6201          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6202        }        }
# Line 4489  sub _tree_construction_main ($) { Line 6205  sub _tree_construction_main ($) {
6205              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
6206                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6207                unless (length $token->{data}) {                unless (length $token->{data}) {
6208                    !!!cp ('t260');
6209                  !!!next-token;                  !!!next-token;
6210                  redo B;                  next B;
6211                }                }
6212              }              }
6213                            
6214                !!!cp ('t261');
6215              #              #
6216            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6217              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6218                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!cp ('t262');
6219                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6220                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6221                  !!!ack ('t262.1');
6222                !!!next-token;                !!!next-token;
6223                redo B;                next B;
6224              } else {              } else {
6225                  !!!cp ('t263');
6226                #                #
6227              }              }
6228            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6229              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6230                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6231                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!cp ('t264');
6232                    !!!parse-error (type => 'unmatched end tag',
6233                                    text => 'colgroup', token => $token);
6234                  ## Ignore the token                  ## Ignore the token
6235                  !!!next-token;                  !!!next-token;
6236                  redo B;                  next B;
6237                } else {                } else {
6238                    !!!cp ('t265');
6239                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6240                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6241                  !!!next-token;                  !!!next-token;
6242                  redo B;                              next B;            
6243                }                }
6244              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6245                !!!parse-error (type => 'unmatched end tag:col');                !!!cp ('t266');
6246                  !!!parse-error (type => 'unmatched end tag',
6247                                  text => 'col', token => $token);
6248                ## Ignore the token                ## Ignore the token
6249                !!!next-token;                !!!next-token;
6250                redo B;                next B;
6251              } else {              } else {
6252                  !!!cp ('t267');
6253                #                #
6254              }              }
6255            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6256              #          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6257            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6258              !!!cp ('t270.2');
6259              ## Stop parsing.
6260              last B;
6261            } else {
6262              ## NOTE: As if </colgroup>.
6263              !!!cp ('t270.1');
6264              pop @{$self->{open_elements}}; # colgroup
6265              $self->{insertion_mode} = IN_TABLE_IM;
6266              ## Reprocess.
6267              next B;
6268            }
6269          } else {
6270            die "$0: $token->{type}: Unknown token type";
6271          }
6272    
6273            ## As if </colgroup>            ## As if </colgroup>
6274            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6275              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!cp ('t269');
6276    ## TODO: Wrong error type?
6277                !!!parse-error (type => 'unmatched end tag',
6278                                text => 'colgroup', token => $token);
6279              ## Ignore the token              ## Ignore the token
6280                !!!nack ('t269.1');
6281              !!!next-token;              !!!next-token;
6282              redo B;              next B;
6283            } else {            } else {
6284                !!!cp ('t270');
6285              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6286              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6287                !!!ack-later;
6288              ## reprocess              ## reprocess
6289              redo B;              next B;
6290            }            }
6291      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6292        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6293            !!!cp ('t271');
6294          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6295          !!!next-token;          !!!next-token;
6296          redo B;          next B;
6297        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6298              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6299                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6300                  ## As if </option>              !!!cp ('t272');
6301                  pop @{$self->{open_elements}};              ## As if </option>
6302                }              pop @{$self->{open_elements}};
6303              } else {
6304                !!!cp ('t273');
6305              }
6306    
6307                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6308                !!!next-token;            !!!nack ('t273.1');
6309                redo B;            !!!next-token;
6310              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6311                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6312                  ## As if </option>            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6313                  pop @{$self->{open_elements}};              !!!cp ('t274');
6314                }              ## As if </option>
6315                pop @{$self->{open_elements}};
6316              } else {
6317                !!!cp ('t275');
6318              }
6319    
6320                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6321                  ## As if </optgroup>              !!!cp ('t276');
6322                  pop @{$self->{open_elements}};              ## As if </optgroup>
6323                }              pop @{$self->{open_elements}};
6324              } else {
6325                !!!cp ('t277');
6326              }
6327    
6328                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6329                !!!next-token;            !!!nack ('t277.1');
6330                redo B;            !!!next-token;
6331              } elsif ($token->{tag_name} eq 'select') {            next B;
6332                !!!parse-error (type => 'not closed:select');          } elsif ({
6333                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6334                ## have an element in table scope                   }->{$token->{tag_name}} or
6335                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6336                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6337                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6338                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6339                    $i = $_;                     tr => 1, td => 1, th => 1,
6340                    last INSCOPE;                    }->{$token->{tag_name}})) {
6341                  } elsif ({            ## TODO: The type below is not good - <select> is replaced by </select>
6342                            table => 1, html => 1,            !!!parse-error (type => 'not closed', text => 'select',
6343                           }->{$node->[1]}) {                            token => $token);
6344                    last INSCOPE;            ## NOTE: As if the token were </select> (<select> case) or
6345                  }            ## as if there were </select> (otherwise).
6346                } # INSCOPE            ## have an element in table scope
6347                unless (defined $i) {            my $i;
6348                  !!!parse-error (type => 'unmatched end tag:select');            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6349                  ## Ignore the token              my $node = $self->{open_elements}->[$_];
6350                  !!!next-token;              if ($node->[1] & SELECT_EL) {
6351                  redo B;                !!!cp ('t278');
6352                }                $i = $_;
6353                  last INSCOPE;
6354                } elsif ($node->[1] & TABLE_SCOPING_EL) {
6355                  !!!cp ('t279');
6356                  last INSCOPE;
6357                }
6358              } # INSCOPE
6359              unless (defined $i) {
6360                !!!cp ('t280');
6361                !!!parse-error (type => 'unmatched end tag',
6362                                text => 'select', token => $token);
6363                ## Ignore the token
6364                !!!nack ('t280.1');
6365                !!!next-token;
6366                next B;
6367              }
6368                                
6369                splice @{$self->{open_elements}}, $i;            !!!cp ('t281');
6370              splice @{$self->{open_elements}}, $i;
6371    
6372                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6373    
6374                !!!next-token;            if ($token->{tag_name} eq 'select') {
6375                redo B;              !!!nack ('t281.2');
6376                !!!next-token;
6377                next B;
6378              } else {
6379                !!!cp ('t281.1');
6380                !!!ack-later;
6381                ## Reprocess the token.
6382                next B;
6383              }
6384          } else {          } else {
6385            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!cp ('t282');
6386              !!!parse-error (type => 'in select',
6387                              text => $token->{tag_name}, token => $token);
6388            ## Ignore the token            ## Ignore the token
6389              !!!nack ('t282.1');
6390            !!!next-token;            !!!next-token;
6391            redo B;            next B;
6392          }          }
6393        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6394              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6395                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6396                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6397                  ## As if </option>              !!!cp ('t283');
6398                  splice @{$self->{open_elements}}, -2;              ## As if </option>
6399                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {              splice @{$self->{open_elements}}, -2;
6400                  pop @{$self->{open_elements}};            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6401                } else {              !!!cp ('t284');
6402                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              pop @{$self->{open_elements}};
6403                  ## Ignore the token            } else {
6404                }              !!!cp ('t285');
6405                !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6406                redo B;                              text => $token->{tag_name}, token => $token);
6407              } elsif ($token->{tag_name} eq 'option') {              ## Ignore the token
6408                if ($self->{open_elements}->[-1]->[1] eq 'option') {            }
6409                  pop @{$self->{open_elements}};            !!!nack ('t285.1');
6410                } else {            !!!next-token;
6411                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            next B;
6412                  ## Ignore the token          } elsif ($token->{tag_name} eq 'option') {
6413                }            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6414                !!!next-token;              !!!cp ('t286');
6415                redo B;              pop @{$self->{open_elements}};
6416              } elsif ($token->{tag_name} eq 'select') {            } else {
6417                ## have an element in table scope              !!!cp ('t287');
6418                my $i;              !!!parse-error (type => 'unmatched end tag',
6419                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                              text => $token->{tag_name}, token => $token);
6420                  my $node = $self->{open_elements}->[$_];              ## Ignore the token
6421                  if ($node->[1] eq $token->{tag_name}) {            }
6422                    $i = $_;            !!!nack ('t287.1');
6423                    last INSCOPE;            !!!next-token;
6424                  } elsif ({            next B;
6425                            table => 1, html => 1,          } elsif ($token->{tag_name} eq 'select') {
6426                           }->{$node->[1]}) {            ## have an element in table scope
6427                    last INSCOPE;            my $i;
6428                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6429                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6430                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6431                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t288');
6432                  ## Ignore the token                $i = $_;
6433                  !!!next-token;                last INSCOPE;
6434                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6435                }                !!!cp ('t289');
6436                  last INSCOPE;
6437                }
6438              } # INSCOPE
6439              unless (defined $i) {
6440                !!!cp ('t290');
6441                !!!parse-error (type => 'unmatched end tag',
6442                                text => $token->{tag_name}, token => $token);
6443                ## Ignore the token
6444                !!!nack ('t290.1');
6445                !!!next-token;
6446                next B;
6447              }
6448                                
6449                splice @{$self->{open_elements}}, $i;            !!!cp ('t291');
6450              splice @{$self->{open_elements}}, $i;
6451    
6452                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6453    
6454                !!!next-token;            !!!nack ('t291.1');
6455                redo B;            !!!next-token;
6456              } elsif ({            next B;
6457                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6458                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6459                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6460                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6461                     }->{$token->{tag_name}}) {
6462    ## TODO: The following is wrong?
6463              !!!parse-error (type => 'unmatched end tag',
6464                              text => $token->{tag_name}, token => $token);
6465                                
6466                ## have an element in table scope            ## have an element in table scope
6467                my $i;            my $i;
6468                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6469                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6470                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6471                    $i = $_;                !!!cp ('t292');
6472                    last INSCOPE;                $i = $_;
6473                  } elsif ({                last INSCOPE;
6474                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6475                           }->{$node->[1]}) {                !!!cp ('t293');
6476                    last INSCOPE;                last INSCOPE;
6477                  }              }
6478                } # INSCOPE            } # INSCOPE
6479                unless (defined $i) {            unless (defined $i) {
6480                  ## Ignore the token              !!!cp ('t294');
6481                  !!!next-token;              ## Ignore the token
6482                  redo B;              !!!nack ('t294.1');
6483                }              !!!next-token;
6484                next B;
6485              }
6486                                
6487                ## As if </select>            ## As if </select>
6488                ## have an element in table scope            ## have an element in table scope
6489                undef $i;            undef $i;
6490                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6491                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6492                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6493                    $i = $_;                !!!cp ('t295');
6494                    last INSCOPE;                $i = $_;
6495                  } elsif ({                last INSCOPE;
6496                            table => 1, html => 1,              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6497                           }->{$node->[1]}) {  ## ISSUE: Can this state be reached?
6498                    last INSCOPE;                !!!cp ('t296');
6499                  }                last INSCOPE;
6500                } # INSCOPE              }
6501                unless (defined $i) {            } # INSCOPE
6502                  !!!parse-error (type => 'unmatched end tag:select');            unless (defined $i) {
6503                  ## Ignore the </select> token              !!!cp ('t297');
6504                  !!!next-token; ## TODO: ok?  ## TODO: The following error type is correct?
6505                  redo B;              !!!parse-error (type => 'unmatched end tag',
6506                }                              text => 'select', token => $token);
6507                ## Ignore the </select> token
6508                !!!nack ('t297.1');
6509                !!!next-token; ## TODO: ok?
6510                next B;
6511              }
6512                                
6513                splice @{$self->{open_elements}}, $i;            !!!cp ('t298');
6514              splice @{$self->{open_elements}}, $i;
6515    
6516                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6517    
6518                ## reprocess            !!!ack-later;
6519                redo B;            ## reprocess
6520              next B;
6521          } else {          } else {
6522            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!cp ('t299');
6523              !!!parse-error (type => 'in select:/',
6524                              text => $token->{tag_name}, token => $token);
6525            ## Ignore the token            ## Ignore the token
6526              !!!nack ('t299.3');
6527            !!!next-token;            !!!next-token;
6528            redo B;            next B;
6529            }
6530          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6531            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6532                    @{$self->{open_elements}} == 1) { # redundant, maybe
6533              !!!cp ('t299.1');
6534              !!!parse-error (type => 'in body:#eof', token => $token);
6535            } else {
6536              !!!cp ('t299.2');
6537          }          }
6538    
6539            ## Stop parsing.
6540            last B;
6541        } else {        } else {
6542          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6543        }        }
# Line 4726  sub _tree_construction_main ($) { Line 6551  sub _tree_construction_main ($) {
6551            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6552                        
6553            unless (length $token->{data}) {            unless (length $token->{data}) {
6554                !!!cp ('t300');
6555              !!!next-token;              !!!next-token;
6556              redo B;              next B;
6557            }            }
6558          }          }
6559                    
6560          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6561            !!!parse-error (type => 'after html:#character');            !!!cp ('t301');
6562              !!!parse-error (type => 'after html:#text', token => $token);
6563    
6564            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6565            } else {
6566              !!!cp ('t302');
6567          }          }
6568                    
6569          ## "after body" insertion mode          ## "after body" insertion mode
6570          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6571    
6572          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6573          ## reprocess          ## reprocess
6574          redo B;          next B;
6575        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6576          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6577            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!cp ('t303');
6578              !!!parse-error (type => 'after html',
6579                              text => $token->{tag_name}, token => $token);
6580                        
6581            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6582            } else {
6583              !!!cp ('t304');
6584          }          }
6585    
6586          ## "after body" insertion mode          ## "after body" insertion mode
6587          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6588                            text => $token->{tag_name}, token => $token);
6589    
6590          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6591            !!!ack-later;
6592          ## reprocess          ## reprocess
6593          redo B;          next B;
6594        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6595          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6596            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!cp ('t305');
6597              !!!parse-error (type => 'after html:/',
6598                              text => $token->{tag_name}, token => $token);
6599                        
6600            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6601            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6602            } else {
6603              !!!cp ('t306');
6604          }          }
6605    
6606          ## "after body" insertion mode          ## "after body" insertion mode
6607          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6608            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6609              !!!parse-error (type => 'unmatched end tag:html');              !!!cp ('t307');
6610                !!!parse-error (type => 'unmatched end tag',
6611                                text => 'html', token => $token);
6612              ## Ignore the token              ## Ignore the token
6613              !!!next-token;              !!!next-token;
6614              redo B;              next B;
6615            } else {            } else {
6616                !!!cp ('t308');
6617              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6618              !!!next-token;              !!!next-token;
6619              redo B;              next B;
6620            }            }
6621          } else {          } else {
6622            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!cp ('t309');
6623              !!!parse-error (type => 'after body:/',
6624                              text => $token->{tag_name}, token => $token);
6625    
6626            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6627            ## reprocess            ## reprocess
6628            redo B;            next B;
6629          }          }
6630          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6631            !!!cp ('t309.2');
6632            ## Stop parsing
6633            last B;
6634        } else {        } else {
6635          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6636        }        }
# Line 4792  sub _tree_construction_main ($) { Line 6640  sub _tree_construction_main ($) {
6640            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6641                        
6642            unless (length $token->{data}) {            unless (length $token->{data}) {
6643                !!!cp ('t310');
6644              !!!next-token;              !!!next-token;
6645              redo B;              next B;
6646            }            }
6647          }          }
6648                    
6649          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6650            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6651              !!!parse-error (type => 'in frameset:#character');              !!!cp ('t311');
6652                !!!parse-error (type => 'in frameset:#text', token => $token);
6653            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6654              !!!parse-error (type => 'after frameset:#character');              !!!cp ('t312');
6655            } else { # "after html frameset"              !!!parse-error (type => 'after frameset:#text', token => $token);
6656              !!!parse-error (type => 'after html:#character');            } else { # "after after frameset"
6657                !!!cp ('t313');
6658              $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');  
6659            }            }
6660                        
6661            ## Ignore the token.            ## Ignore the token.
6662            if (length $token->{data}) {            if (length $token->{data}) {
6663                !!!cp ('t314');
6664              ## reprocess the rest of characters              ## reprocess the rest of characters
6665            } else {            } else {
6666                !!!cp ('t315');
6667              !!!next-token;              !!!next-token;
6668            }            }
6669            redo B;            next B;
6670          }          }
6671                    
6672          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6673        } 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...  
         }  
   
6674          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6675              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6676            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t318');
6677              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6678              !!!nack ('t318.1');
6679            !!!next-token;            !!!next-token;
6680            redo B;            next B;
6681          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6682                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6683            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!cp ('t319');
6684              !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6685            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6686              !!!ack ('t319.1');
6687            !!!next-token;            !!!next-token;
6688            redo B;            next B;
6689          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6690            ## NOTE: As if in body.            !!!cp ('t320');
6691            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            ## NOTE: As if in head.
6692            redo B;            $parse_rcdata->(CDATA_CONTENT_MODEL);
6693              next B;
6694    
6695              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6696              ## has no parse error.
6697          } else {          } else {
6698            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6699              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!cp ('t321');
6700            } else {              !!!parse-error (type => 'in frameset',
6701              !!!parse-error (type => 'after frameset:'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6702              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6703                !!!cp ('t322');
6704                !!!parse-error (type => 'after frameset',
6705                                text => $token->{tag_name}, token => $token);
6706              } else { # "after after frameset"
6707                !!!cp ('t322.2');
6708                !!!parse-error (type => 'after after frameset',
6709                                text => $token->{tag_name}, token => $token);
6710            }            }
6711            ## Ignore the token            ## Ignore the token
6712              !!!nack ('t322.1');
6713            !!!next-token;            !!!next-token;
6714            redo B;            next B;
6715          }          }
6716        } 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...  
         }  
   
6717          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6718              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6719            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6720                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6721              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t325');
6722                !!!parse-error (type => 'unmatched end tag',
6723                                text => $token->{tag_name}, token => $token);
6724              ## Ignore the token              ## Ignore the token
6725              !!!next-token;              !!!next-token;
6726            } else {            } else {
6727                !!!cp ('t326');
6728              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6729              !!!next-token;              !!!next-token;
6730            }            }
6731    
6732            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6733                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6734                !!!cp ('t327');
6735              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6736              } else {
6737                !!!cp ('t328');
6738            }            }
6739            redo B;            next B;
6740          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6741                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6742              !!!cp ('t329');
6743            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6744            !!!next-token;            !!!next-token;
6745            redo B;            next B;
6746          } else {          } else {
6747            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6748              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!cp ('t330');
6749            } else {              !!!parse-error (type => 'in frameset:/',
6750              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});                              text => $token->{tag_name}, token => $token);
6751              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6752                !!!cp ('t330.1');
6753                !!!parse-error (type => 'after frameset:/',
6754                                text => $token->{tag_name}, token => $token);
6755              } else { # "after after html"
6756                !!!cp ('t331');
6757                !!!parse-error (type => 'after after frameset:/',
6758                                text => $token->{tag_name}, token => $token);
6759            }            }
6760            ## Ignore the token            ## Ignore the token
6761            !!!next-token;            !!!next-token;
6762            redo B;            next B;
6763          }          }
6764          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6765            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6766                    @{$self->{open_elements}} == 1) { # redundant, maybe
6767              !!!cp ('t331.1');
6768              !!!parse-error (type => 'in body:#eof', token => $token);
6769            } else {
6770              !!!cp ('t331.2');
6771            }
6772            
6773            ## Stop parsing
6774            last B;
6775        } else {        } else {
6776          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6777        }        }
# Line 4905  sub _tree_construction_main ($) { Line 6784  sub _tree_construction_main ($) {
6784      ## "in body" insertion mode      ## "in body" insertion mode
6785      if ($token->{type} == START_TAG_TOKEN) {      if ($token->{type} == START_TAG_TOKEN) {
6786        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6787            !!!cp ('t332');
6788          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6789          $script_start_tag->($insert);          $script_start_tag->();
6790          redo B;          next B;
6791        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6792            !!!cp ('t333');
6793          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6794          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6795          redo B;          next B;
6796        } elsif ({        } elsif ({
6797                  base => 1, link => 1,                  base => 1, link => 1,
6798                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6799            !!!cp ('t334');
6800          ## 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
6801          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6802          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6803            !!!ack ('t334.1');
6804          !!!next-token;          !!!next-token;
6805          redo B;          next B;
6806        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6807          ## 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
6808          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6809          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          my $meta_el = pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6810    
6811          unless ($self->{confident}) {          unless ($self->{confident}) {
6812            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6813                !!!cp ('t335');
6814                ## NOTE: Whether the encoding is supported or not is handled
6815                ## in the {change_encoding} callback.
6816              $self->{change_encoding}              $self->{change_encoding}
6817                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6818                            
6819              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6820                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6821                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6822                                           ->{has_reference});                                           ->{has_reference});
6823            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6824              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6825                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6826                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6827                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6828                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6829                  !!!cp ('t336');
6830                  ## NOTE: Whether the encoding is supported or not is handled
6831                  ## in the {change_encoding} callback.
6832                $self->{change_encoding}                $self->{change_encoding}
6833                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6834                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6835                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6836                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 4951  sub _tree_construction_main ($) { Line 6839  sub _tree_construction_main ($) {
6839            }            }
6840          } else {          } else {
6841            if ($token->{attributes}->{charset}) {            if ($token->{attributes}->{charset}) {
6842                !!!cp ('t337');
6843              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6844                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6845                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6846                                           ->{has_reference});                                           ->{has_reference});
6847            }            }
6848            if ($token->{attributes}->{content}) {            if ($token->{attributes}->{content}) {
6849                !!!cp ('t338');
6850              $meta_el->[0]->get_attribute_node_ns (undef, 'content')              $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6851                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6852                                       $token->{attributes}->{content}                                       $token->{attributes}->{content}
# Line 4964  sub _tree_construction_main ($) { Line 6854  sub _tree_construction_main ($) {
6854            }            }
6855          }          }
6856    
6857            !!!ack ('t338.1');
6858          !!!next-token;          !!!next-token;
6859          redo B;          next B;
6860        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6861          !!!parse-error (type => 'in body:title');          !!!cp ('t341');
6862          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6863          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6864            if (defined $self->{head_element}) {          next B;
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6865        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6866          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6867                                
6868          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6869              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6870              !!!cp ('t342');
6871            ## Ignore the token            ## Ignore the token
6872          } else {          } else {
6873            my $body_el = $self->{open_elements}->[1]->[0];            my $body_el = $self->{open_elements}->[1]->[0];
6874            for my $attr_name (keys %{$token->{attributes}}) {            for my $attr_name (keys %{$token->{attributes}}) {
6875              unless ($body_el->has_attribute_ns (undef, $attr_name)) {              unless ($body_el->has_attribute_ns (undef, $attr_name)) {
6876                  !!!cp ('t343');
6877                $body_el->set_attribute_ns                $body_el->set_attribute_ns
6878                  (undef, [undef, $attr_name],                  (undef, [undef, $attr_name],
6879                   $token->{attributes}->{$attr_name}->{value});                   $token->{attributes}->{$attr_name}->{value});
6880              }              }
6881            }            }
6882          }          }
6883            !!!nack ('t343.1');
6884          !!!next-token;          !!!next-token;
6885          redo B;          next B;
6886        } elsif ({        } elsif ({
6887                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6888                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6889                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6890                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6891                  pre => 1,                  pre => 1, listing => 1,
6892                    form => 1,
6893                    table => 1,
6894                    hr => 1,
6895                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6896            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6897              !!!cp ('t350');
6898              !!!parse-error (type => 'in form:form', token => $token);
6899              ## Ignore the token
6900              !!!nack ('t350.1');
6901              !!!next-token;
6902              next B;
6903            }
6904    
6905          ## has a p element in scope          ## has a p element in scope
6906          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6907            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6908              !!!back-token;              !!!cp ('t344');
6909              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <form>
6910              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6911            } elsif ({                        line => $token->{line}, column => $token->{column}};
6912                      table => 1, caption => 1, td => 1, th => 1,              next B;
6913                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6914                     }->{$_->[1]}) {              !!!cp ('t345');
6915              last INSCOPE;              last INSCOPE;
6916            }            }
6917          } # INSCOPE          } # INSCOPE
6918                        
6919          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6920          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6921              !!!nack ('t346.1');
6922            !!!next-token;            !!!next-token;
6923            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6924              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
6925              unless (length $token->{data}) {              unless (length $token->{data}) {
6926                  !!!cp ('t346');
6927                !!!next-token;                !!!next-token;
6928                } else {
6929                  !!!cp ('t349');
6930              }              }
6931              } else {
6932                !!!cp ('t348');
6933            }            }
6934          } else {          } elsif ($token->{tag_name} eq 'form') {
6935              !!!cp ('t347.1');
6936              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6937    
6938              !!!nack ('t347.2');
6939            !!!next-token;            !!!next-token;
6940          }          } elsif ($token->{tag_name} eq 'table') {
6941          redo B;            !!!cp ('t382');
6942        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6943          if (defined $self->{form_element}) {            
6944            !!!parse-error (type => 'in form:form');            $self->{insertion_mode} = IN_TABLE_IM;
6945            ## Ignore the token  
6946              !!!nack ('t382.1');
6947              !!!next-token;
6948            } elsif ($token->{tag_name} eq 'hr') {
6949              !!!cp ('t386');
6950              pop @{$self->{open_elements}};
6951            
6952              !!!nack ('t386.1');
6953            !!!next-token;            !!!next-token;
           redo B;  
6954          } else {          } else {
6955            ## 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];  
6956            !!!next-token;            !!!next-token;
           redo B;  
6957          }          }
6958        } elsif ($token->{tag_name} eq 'li') {          next B;
6959          ## 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') {  
6960          ## has a p element in scope          ## has a p element in scope
6961          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6962            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6963              !!!back-token;              !!!cp ('t353');
6964              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <x>
6965              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6966            } elsif ({                        line => $token->{line}, column => $token->{column}};
6967                      table => 1, caption => 1, td => 1, th => 1,              next B;
6968                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
6969                     }->{$_->[1]}) {              !!!cp ('t354');
6970              last INSCOPE;              last INSCOPE;
6971            }            }
6972          } # INSCOPE          } # INSCOPE
# Line 5119  sub _tree_construction_main ($) { Line 6974  sub _tree_construction_main ($) {
6974          ## Step 1          ## Step 1
6975          my $i = -1;          my $i = -1;
6976          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6977            my $li_or_dtdd = {li => {li => 1},
6978                              dt => {dt => 1, dd => 1},
6979                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6980          LI: {          LI: {
6981            ## Step 2            ## Step 2
6982            if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6983              if ($i != -1) {              if ($i != -1) {
6984                !!!parse-error (type => 'end tag missing:'.                !!!cp ('t355');
6985                                $self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
6986                                  text => $self->{open_elements}->[-1]->[0]
6987                                      ->manakai_local_name,
6988                                  token => $token);
6989                } else {
6990                  !!!cp ('t356');
6991              }              }
6992              splice @{$self->{open_elements}}, $i;              splice @{$self->{open_elements}}, $i;
6993              last LI;              last LI;
6994              } else {
6995                !!!cp ('t357');
6996            }            }
6997                        
6998            ## Step 3            ## Step 3
6999            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
7000                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
7001                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
7002                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
7003                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
7004                  not ($node->[1] & DIV_EL)) {
7005                !!!cp ('t358');
7006              last LI;              last LI;
7007            }            }
7008                        
7009              !!!cp ('t359');
7010            ## Step 4            ## Step 4
7011            $i--;            $i--;
7012            $node = $self->{open_elements}->[$i];            $node = $self->{open_elements}->[$i];
7013            redo LI;            redo LI;
7014          } # LI          } # LI
7015                        
7016          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7017            !!!nack ('t359.1');
7018          !!!next-token;          !!!next-token;
7019          redo B;          next B;
7020        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
7021          ## has a p element in scope          ## has a p element in scope
7022          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
7023            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
7024              !!!back-token;              !!!cp ('t367');
7025              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              !!!back-token; # <plaintext>
7026              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'p',
7027            } elsif ({                        line => $token->{line}, column => $token->{column}};
7028                      table => 1, caption => 1, td => 1, th => 1,              next B;
7029                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($_->[1] & SCOPING_EL) {
7030                     }->{$_->[1]}) {              !!!cp ('t368');
7031              last INSCOPE;              last INSCOPE;
7032            }            }
7033          } # INSCOPE          } # INSCOPE
7034                        
7035          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7036                        
7037          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7038                        
7039            !!!nack ('t368.1');
7040          !!!next-token;          !!!next-token;
7041          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;  
7042        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
7043          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7044            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
7045            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
7046              !!!parse-error (type => 'in a:a');              !!!cp ('t371');
7047                !!!parse-error (type => 'in a:a', token => $token);
7048                            
7049              !!!back-token;              !!!back-token; # <a>
7050              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
7051              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
7052                $formatting_end_tag->($token);
7053                            
7054              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
7055                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
7056                    !!!cp ('t372');
7057                  splice @$active_formatting_elements, $_, 1;                  splice @$active_formatting_elements, $_, 1;
7058                  last AFE2;                  last AFE2;
7059                }                }
7060              } # AFE2              } # AFE2
7061              OE: for (reverse 0..$#{$self->{open_elements}}) {              OE: for (reverse 0..$#{$self->{open_elements}}) {
7062                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {                if ($self->{open_elements}->[$_]->[0] eq $node->[0]) {
7063                    !!!cp ('t373');
7064                  splice @{$self->{open_elements}}, $_, 1;                  splice @{$self->{open_elements}}, $_, 1;
7065                  last OE;                  last OE;
7066                }                }
7067              } # OE              } # OE
7068              last AFE;              last AFE;
7069            } elsif ($node->[0] eq '#marker') {            } elsif ($node->[0] eq '#marker') {
7070                !!!cp ('t374');
7071              last AFE;              last AFE;
7072            }            }
7073          } # AFE          } # AFE
7074                        
7075          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7076    
7077          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7078          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7079    
7080            !!!nack ('t374.1');
7081          !!!next-token;          !!!next-token;
7082          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;  
7083        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
7084          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7085    
7086          ## has a |nobr| element in scope          ## has a |nobr| element in scope
7087          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7088            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7089            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
7090              !!!parse-error (type => 'in nobr:nobr');              !!!cp ('t376');
7091              !!!back-token;              !!!parse-error (type => 'in nobr:nobr', token => $token);
7092              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              !!!back-token; # <nobr>
7093              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7094            } elsif ({                        line => $token->{line}, column => $token->{column}};
7095                      table => 1, caption => 1, td => 1, th => 1,              next B;
7096                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
7097                     }->{$node->[1]}) {              !!!cp ('t377');
7098              last INSCOPE;              last INSCOPE;
7099            }            }
7100          } # INSCOPE          } # INSCOPE
7101                    
7102          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7103          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7104                    
7105            !!!nack ('t377.1');
7106          !!!next-token;          !!!next-token;
7107          redo B;          next B;
7108        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7109          ## has a button element in scope          ## has a button element in scope
7110          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7111            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7112            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7113              !!!parse-error (type => 'in button:button');              !!!cp ('t378');
7114              !!!back-token;              !!!parse-error (type => 'in button:button', token => $token);
7115              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              !!!back-token; # <button>
7116              redo B;              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7117            } elsif ({                        line => $token->{line}, column => $token->{column}};
7118                      table => 1, caption => 1, td => 1, th => 1,              next B;
7119                      button => 1, marquee => 1, object => 1, html => 1,            } elsif ($node->[1] & SCOPING_EL) {
7120                     }->{$node->[1]}) {              !!!cp ('t379');
7121              last INSCOPE;              last INSCOPE;
7122            }            }
7123          } # INSCOPE          } # INSCOPE
7124                        
7125          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7126                        
7127          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7128          push @$active_formatting_elements, ['#marker', ''];  
7129            ## TODO: associate with $self->{form_element} if defined
7130    
         !!!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});  
7131          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7132            
7133          !!!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;  
             
7134          !!!next-token;          !!!next-token;
7135          redo B;          next B;
7136        } elsif ({        } elsif ({
7137                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7138                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7139                  image => 1,                  noembed => 1,
7140                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7141                    noscript => 0, ## TODO: 1 if scripting is enabled
7142                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7143          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7144            !!!parse-error (type => 'image');            !!!cp ('t381');
7145            $token->{tag_name} = 'img';            $reconstruct_active_formatting_elements->($insert_to_current);
7146            } else {
7147              !!!cp ('t399');
7148          }          }
7149            ## NOTE: There is an "as if in body" code clone.
7150          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7151          $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;  
7152        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7153          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7154                    
7155          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7156              !!!cp ('t389');
7157            ## Ignore the token            ## Ignore the token
7158              !!!nack ('t389'); ## NOTE: Not acknowledged.
7159            !!!next-token;            !!!next-token;
7160            redo B;            next B;
7161          } else {          } else {
7162              !!!ack ('t391.1');
7163    
7164            my $at = $token->{attributes};            my $at = $token->{attributes};
7165            my $form_attrs;            my $form_attrs;
7166            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5407  sub _tree_construction_main ($) { Line 7170  sub _tree_construction_main ($) {
7170            delete $at->{prompt};            delete $at->{prompt};
7171            my @tokens = (            my @tokens = (
7172                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7173                           attributes => $form_attrs},                           attributes => $form_attrs,
7174                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7175                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7176                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7177                            {type => START_TAG_TOKEN, tag_name => 'p',
7178                             line => $token->{line}, column => $token->{column}},
7179                            {type => START_TAG_TOKEN, tag_name => 'label',
7180                             line => $token->{line}, column => $token->{column}},
7181                         );                         );
7182            if ($prompt_attr) {            if ($prompt_attr) {
7183              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              !!!cp ('t390');
7184                push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7185                               #line => $token->{line}, column => $token->{column},
7186                              };
7187            } else {            } else {
7188                !!!cp ('t391');
7189              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7190                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7191                               #line => $token->{line}, column => $token->{column},
7192                              }; # SHOULD
7193              ## TODO: make this configurable              ## TODO: make this configurable
7194            }            }
7195            push @tokens,            push @tokens,
7196                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7197                             line => $token->{line}, column => $token->{column}},
7198                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7199                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7200                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7201                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7202                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7203            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7204                             line => $token->{line}, column => $token->{column}},
7205                            {type => END_TAG_TOKEN, tag_name => 'form',
7206                             line => $token->{line}, column => $token->{column}};
7207            !!!back-token (@tokens);            !!!back-token (@tokens);
7208            redo B;            !!!next-token;
7209              next B;
7210          }          }
7211        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7212          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7213          my $el;          my $el;
7214          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7215                    
7216          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7217          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5442  sub _tree_construction_main ($) { Line 7220  sub _tree_construction_main ($) {
7220          $insert->($el);          $insert->($el);
7221                    
7222          my $text = '';          my $text = '';
7223            !!!nack ('t392.1');
7224          !!!next-token;          !!!next-token;
7225          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7226            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
7227            unless (length $token->{data}) {            unless (length $token->{data}) {
7228                !!!cp ('t392');
7229              !!!next-token;              !!!next-token;
7230              } else {
7231                !!!cp ('t393');
7232            }            }
7233            } else {
7234              !!!cp ('t394');
7235          }          }
7236          while ($token->{type} == CHARACTER_TOKEN) {          while ($token->{type} == CHARACTER_TOKEN) {
7237              !!!cp ('t395');
7238            $text .= $token->{data};            $text .= $token->{data};
7239            !!!next-token;            !!!next-token;
7240          }          }
7241          if (length $text) {          if (length $text) {
7242              !!!cp ('t396');
7243            $el->manakai_append_text ($text);            $el->manakai_append_text ($text);
7244          }          }
7245                    
# Line 5461  sub _tree_construction_main ($) { Line 7247  sub _tree_construction_main ($) {
7247                    
7248          if ($token->{type} == END_TAG_TOKEN and          if ($token->{type} == END_TAG_TOKEN and
7249              $token->{tag_name} eq $tag_name) {              $token->{tag_name} eq $tag_name) {
7250              !!!cp ('t397');
7251            ## Ignore the token            ## Ignore the token
7252          } else {          } else {
7253            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!cp ('t398');
7254              !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7255          }          }
7256          !!!next-token;          !!!next-token;
7257            next B;
7258          } elsif ($token->{tag_name} eq 'rt' or
7259                   $token->{tag_name} eq 'rp') {
7260            ## has a |ruby| element in scope
7261            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7262              my $node = $self->{open_elements}->[$_];
7263              if ($node->[1] & RUBY_EL) {
7264                !!!cp ('t398.1');
7265                ## generate implied end tags
7266                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7267                  !!!cp ('t398.2');
7268                  pop @{$self->{open_elements}};
7269                }
7270                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7271                  !!!cp ('t398.3');
7272                  !!!parse-error (type => 'not closed',
7273                                  text => $self->{open_elements}->[-1]->[0]
7274                                      ->manakai_local_name,
7275                                  token => $token);
7276                  pop @{$self->{open_elements}}
7277                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7278                }
7279                last INSCOPE;
7280              } elsif ($node->[1] & SCOPING_EL) {
7281                !!!cp ('t398.4');
7282                last INSCOPE;
7283              }
7284            } # INSCOPE
7285    
7286            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7287    
7288            !!!nack ('t398.5');
7289            !!!next-token;
7290          redo B;          redo B;
7291        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7292                  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') {  
7293          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7294    
7295            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7296    
7297            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7298    
7299            ## "adjust foreign attributes" - done in insert-element-f
7300                    
7301          !!!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);
7302                    
7303          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
7304              pop @{$self->{open_elements}};
7305              !!!ack ('t398.1');
7306            } else {
7307              !!!cp ('t398.2');
7308              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7309              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7310              ## mode, "in body" (not "in foreign content") secondary insertion
7311              ## mode, maybe.
7312            }
7313    
7314          !!!next-token;          !!!next-token;
7315          redo B;          next B;
7316        } elsif ({        } elsif ({
7317                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7318                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
7319                  tbody => 1, td => 1, tfoot => 1, th => 1,                  tbody => 1, td => 1, tfoot => 1, th => 1,
7320                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7321                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7322          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!cp ('t401');
7323            !!!parse-error (type => 'in body',
7324                            text => $token->{tag_name}, token => $token);
7325          ## Ignore the token          ## Ignore the token
7326            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7327          !!!next-token;          !!!next-token;
7328          redo B;          next B;
7329                    
7330          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7331        } else {        } else {
7332            if ($token->{tag_name} eq 'image') {
7333              !!!cp ('t384');
7334              !!!parse-error (type => 'image', token => $token);
7335              $token->{tag_name} = 'img';
7336            } else {
7337              !!!cp ('t385');
7338            }
7339    
7340            ## NOTE: There is an "as if <br>" code clone.
7341          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7342                    
7343          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7344    
7345            if ({
7346                 applet => 1, marquee => 1, object => 1,
7347                }->{$token->{tag_name}}) {
7348              !!!cp ('t380');
7349              push @$active_formatting_elements, ['#marker', ''];
7350              !!!nack ('t380.1');
7351            } elsif ({
7352                      b => 1, big => 1, em => 1, font => 1, i => 1,
7353                      s => 1, small => 1, strile => 1,
7354                      strong => 1, tt => 1, u => 1,
7355                     }->{$token->{tag_name}}) {
7356              !!!cp ('t375');
7357              push @$active_formatting_elements, $self->{open_elements}->[-1];
7358              !!!nack ('t375.1');
7359            } elsif ($token->{tag_name} eq 'input') {
7360              !!!cp ('t388');
7361              ## TODO: associate with $self->{form_element} if defined
7362              pop @{$self->{open_elements}};
7363              !!!ack ('t388.2');
7364            } elsif ({
7365                      area => 1, basefont => 1, bgsound => 1, br => 1,
7366                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7367                      #image => 1,
7368                     }->{$token->{tag_name}}) {
7369              !!!cp ('t388.1');
7370              pop @{$self->{open_elements}};
7371              !!!ack ('t388.3');
7372            } elsif ($token->{tag_name} eq 'select') {
7373              ## TODO: associate with $self->{form_element} if defined
7374            
7375              if ($self->{insertion_mode} & TABLE_IMS or
7376                  $self->{insertion_mode} & BODY_TABLE_IMS or
7377                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7378                !!!cp ('t400.1');
7379                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7380              } else {
7381                !!!cp ('t400.2');
7382                $self->{insertion_mode} = IN_SELECT_IM;
7383              }
7384              !!!nack ('t400.3');
7385            } else {
7386              !!!nack ('t402');
7387            }
7388                    
7389          !!!next-token;          !!!next-token;
7390          redo B;          next B;
7391        }        }
7392      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7393        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7394          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7395              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7396            for (@{$self->{open_elements}}) {          INSCOPE: {
7397              unless ({            for (reverse @{$self->{open_elements}}) {
7398                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7399                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7400                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7401                      }->{$_->[1]}) {                last INSCOPE;
7402                !!!parse-error (type => 'not closed:'.$_->[1]);              } elsif ($_->[1] & SCOPING_EL) {
7403                  !!!cp ('t405.1');
7404                  last;
7405              }              }
7406            }            }
7407    
7408            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7409            !!!next-token;                            text => $token->{tag_name}, token => $token);
7410            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
7411            !!!next-token;            !!!next-token;
7412            redo B;            next B;
7413            } # INSCOPE
7414    
7415            for (@{$self->{open_elements}}) {
7416              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7417                !!!cp ('t403');
7418                !!!parse-error (type => 'not closed',
7419                                text => $_->[0]->manakai_local_name,
7420                                token => $token);
7421                last;
7422              } else {
7423                !!!cp ('t404');
7424              }
7425          }          }
7426    
7427            $self->{insertion_mode} = AFTER_BODY_IM;
7428            !!!next-token;
7429            next B;
7430        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7431          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
7432            ## up-to-date, though it has same effect as speced.
7433            if (@{$self->{open_elements}} > 1 and
7434                $self->{open_elements}->[1]->[1] & BODY_EL) {
7435            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7436            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7437              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!cp ('t406');
7438                !!!parse-error (type => 'not closed',
7439                                text => $self->{open_elements}->[1]->[0]
7440                                    ->manakai_local_name,
7441                                token => $token);
7442              } else {
7443                !!!cp ('t407');
7444            }            }
7445            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7446            ## reprocess            ## reprocess
7447            redo B;            next B;
7448          } else {          } else {
7449            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t408');
7450              !!!parse-error (type => 'unmatched end tag',
7451                              text => $token->{tag_name}, token => $token);
7452            ## Ignore the token            ## Ignore the token
7453            !!!next-token;            !!!next-token;
7454            redo B;            next B;
7455          }          }
7456        } elsif ({        } elsif ({
7457                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7458                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7459                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7460                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7461                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7462                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7463          ## has an element in scope          ## has an element in scope
7464          my $i;          my $i;
7465          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7466            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7467            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7468              ## 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;  
             }  
7469              $i = $_;              $i = $_;
7470              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7471            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7472                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t411');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7473              last INSCOPE;              last INSCOPE;
7474            }            }
7475          } # INSCOPE          } # INSCOPE
7476            
7477          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7478            if (defined $i) {            !!!cp ('t413');
7479              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);            !!!parse-error (type => 'unmatched end tag',
7480                              text => $token->{tag_name}, token => $token);
7481              ## NOTE: Ignore the token.
7482            } else {
7483              ## Step 1. generate implied end tags
7484              while ({
7485                      ## END_TAG_OPTIONAL_EL
7486                      dd => ($token->{tag_name} ne 'dd'),
7487                      dt => ($token->{tag_name} ne 'dt'),
7488                      li => ($token->{tag_name} ne 'li'),
7489                      p => 1,
7490                      rt => 1,
7491                      rp => 1,
7492                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7493                !!!cp ('t409');
7494                pop @{$self->{open_elements}};
7495              }
7496    
7497              ## Step 2.
7498              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7499                      ne $token->{tag_name}) {
7500                !!!cp ('t412');
7501                !!!parse-error (type => 'not closed',
7502                                text => $self->{open_elements}->[-1]->[0]
7503                                    ->manakai_local_name,
7504                                token => $token);
7505            } else {            } else {
7506              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!cp ('t414');
7507            }            }
7508          }  
7509                      ## Step 3.
         if (defined $i) {  
7510            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7511          } elsif ($token->{tag_name} eq 'p') {  
7512            ## As if <p>, then reprocess the current token            ## Step 4.
7513            my $el;            $clear_up_to_marker->()
7514            !!!create-element ($el, 'p');                if {
7515            $insert->($el);                  applet => 1, button => 1, marquee => 1, object => 1,
7516                  }->{$token->{tag_name}};
7517          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7518          !!!next-token;          !!!next-token;
7519          redo B;          next B;
7520        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7521            undef $self->{form_element};
7522    
7523          ## has an element in scope          ## has an element in scope
7524            my $i;
7525          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7526            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7527            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7528              ## generate implied end tags              !!!cp ('t418');
7529              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;  
             }  
7530              last INSCOPE;              last INSCOPE;
7531            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7532                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t419');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7533              last INSCOPE;              last INSCOPE;
7534            }            }
7535          } # INSCOPE          } # INSCOPE
7536            
7537          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7538            pop @{$self->{open_elements}};            !!!cp ('t421');
7539          } else {            !!!parse-error (type => 'unmatched end tag',
7540            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                            text => $token->{tag_name}, token => $token);
7541              ## NOTE: Ignore the token.
7542            } else {
7543              ## Step 1. generate implied end tags
7544              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7545                !!!cp ('t417');
7546                pop @{$self->{open_elements}};
7547              }
7548              
7549              ## Step 2.
7550              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7551                      ne $token->{tag_name}) {
7552                !!!cp ('t417.1');
7553                !!!parse-error (type => 'not closed',
7554                                text => $self->{open_elements}->[-1]->[0]
7555                                    ->manakai_local_name,
7556                                token => $token);
7557              } else {
7558                !!!cp ('t420');
7559              }  
7560              
7561              ## Step 3.
7562              splice @{$self->{open_elements}}, $i;
7563          }          }
7564    
         undef $self->{form_element};  
7565          !!!next-token;          !!!next-token;
7566          redo B;          next B;
7567        } elsif ({        } elsif ({
7568                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7569                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 5642  sub _tree_construction_main ($) { Line 7571  sub _tree_construction_main ($) {
7571          my $i;          my $i;
7572          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7573            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7574            if ({            if ($node->[1] & HEADING_EL) {
7575                 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;  
             }  
7576              $i = $_;              $i = $_;
7577              last INSCOPE;              last INSCOPE;
7578            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
7579                      table => 1, caption => 1, td => 1, th => 1,              !!!cp ('t424');
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7580              last INSCOPE;              last INSCOPE;
7581            }            }
7582          } # INSCOPE          } # INSCOPE
7583            
7584          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7585            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!cp ('t425.1');
7586              !!!parse-error (type => 'unmatched end tag',
7587                              text => $token->{tag_name}, token => $token);
7588              ## NOTE: Ignore the token.
7589            } else {
7590              ## Step 1. generate implied end tags
7591              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7592                !!!cp ('t422');
7593                pop @{$self->{open_elements}};
7594              }
7595              
7596              ## Step 2.
7597              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7598                      ne $token->{tag_name}) {
7599                !!!cp ('t425');
7600                !!!parse-error (type => 'unmatched end tag',
7601                                text => $token->{tag_name}, token => $token);
7602              } else {
7603                !!!cp ('t426');
7604              }
7605    
7606              ## Step 3.
7607              splice @{$self->{open_elements}}, $i;
7608          }          }
7609                    
         splice @{$self->{open_elements}}, $i if defined $i;  
7610          !!!next-token;          !!!next-token;
7611          redo B;          next B;
7612          } elsif ($token->{tag_name} eq 'p') {
7613            ## has an element in scope
7614            my $i;
7615            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7616              my $node = $self->{open_elements}->[$_];
7617              if ($node->[1] & P_EL) {
7618                !!!cp ('t410.1');
7619                $i = $_;
7620                last INSCOPE;
7621              } elsif ($node->[1] & SCOPING_EL) {
7622                !!!cp ('t411.1');
7623                last INSCOPE;
7624              }
7625            } # INSCOPE
7626    
7627            if (defined $i) {
7628              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7629                      ne $token->{tag_name}) {
7630                !!!cp ('t412.1');
7631                !!!parse-error (type => 'not closed',
7632                                text => $self->{open_elements}->[-1]->[0]
7633                                    ->manakai_local_name,
7634                                token => $token);
7635              } else {
7636                !!!cp ('t414.1');
7637              }
7638    
7639              splice @{$self->{open_elements}}, $i;
7640            } else {
7641              !!!cp ('t413.1');
7642              !!!parse-error (type => 'unmatched end tag',
7643                              text => $token->{tag_name}, token => $token);
7644    
7645              !!!cp ('t415.1');
7646              ## As if <p>, then reprocess the current token
7647              my $el;
7648              !!!create-element ($el, $HTML_NS, 'p',, $token);
7649              $insert->($el);
7650              ## NOTE: Not inserted into |$self->{open_elements}|.
7651            }
7652    
7653            !!!next-token;
7654            next B;
7655        } elsif ({        } elsif ({
7656                  a => 1,                  a => 1,
7657                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
7658                  nobr => 1, s => 1, small => 1, strile => 1,                  nobr => 1, s => 1, small => 1, strile => 1,
7659                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7660                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7661          $formatting_end_tag->($token->{tag_name});          !!!cp ('t427');
7662          redo B;          $formatting_end_tag->($token);
7663            next B;
7664        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7665          !!!parse-error (type => 'unmatched end tag:br');          !!!cp ('t428');
7666            !!!parse-error (type => 'unmatched end tag',
7667                            text => 'br', token => $token);
7668    
7669          ## As if <br>          ## As if <br>
7670          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7671                    
7672          my $el;          my $el;
7673          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7674          $insert->($el);          $insert->($el);
7675                    
7676          ## Ignore the token.          ## Ignore the token.
7677          !!!next-token;          !!!next-token;
7678          redo B;          next B;
7679        } elsif ({        } elsif ({
7680                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7681                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5706  sub _tree_construction_main ($) { Line 7688  sub _tree_construction_main ($) {
7688                  table => 1, textarea => 1, wbr => 1,                  table => 1, textarea => 1, wbr => 1,
7689                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7690                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7691          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!cp ('t429');
7692            !!!parse-error (type => 'unmatched end tag',
7693                            text => $token->{tag_name}, token => $token);
7694          ## Ignore the token          ## Ignore the token
7695          !!!next-token;          !!!next-token;
7696          redo B;          next B;
7697                    
7698          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7699                    
# Line 5720  sub _tree_construction_main ($) { Line 7704  sub _tree_construction_main ($) {
7704    
7705          ## Step 2          ## Step 2
7706          S2: {          S2: {
7707            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7708              ## Step 1              ## Step 1
7709              ## generate implied end tags              ## generate implied end tags
7710              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7711                   dd => 1, dt => 1, li => 1, p => 1,                !!!cp ('t430');
7712                   td => 1, th => 1, tr => 1,                ## NOTE: |<ruby><rt></ruby>|.
7713                   tbody => 1, tfoot => 1, thead => 1,                ## ISSUE: <ruby><rt></rt> will also take this code path,
7714                  }->{$self->{open_elements}->[-1]->[1]}) {                ## which seems wrong.
7715                !!!back-token;                pop @{$self->{open_elements}};
7716                $token = {type => END_TAG_TOKEN,                $node_i++;
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
7717              }              }
7718                    
7719              ## Step 2              ## Step 2
7720              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7721                        ne $token->{tag_name}) {
7722                  !!!cp ('t431');
7723                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7724                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7725                                  text => $self->{open_elements}->[-1]->[0]
7726                                      ->manakai_local_name,
7727                                  token => $token);
7728                } else {
7729                  !!!cp ('t432');
7730              }              }
7731                            
7732              ## Step 3              ## Step 3
7733              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7734    
7735              !!!next-token;              !!!next-token;
7736              last S2;              last S2;
7737            } else {            } else {
7738              ## Step 3              ## Step 3
7739              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7740                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7741                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7742                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7743                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!cp ('t433');
7744                  !!!parse-error (type => 'unmatched end tag',
7745                                  text => $token->{tag_name}, token => $token);
7746                ## Ignore the token                ## Ignore the token
7747                !!!next-token;                !!!next-token;
7748                last S2;                last S2;
7749              }              }
7750    
7751                !!!cp ('t434');
7752            }            }
7753                        
7754            ## Step 4            ## Step 4
# Line 5765  sub _tree_construction_main ($) { Line 7758  sub _tree_construction_main ($) {
7758            ## Step 5;            ## Step 5;
7759            redo S2;            redo S2;
7760          } # S2          } # S2
7761          redo B;          next B;
7762        }        }
7763      }      }
7764      redo B;      next B;
7765      } continue { # B
7766        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7767          ## NOTE: The code below is executed in cases where it does not have
7768          ## to be, but it it is harmless even in those cases.
7769          ## has an element in scope
7770          INSCOPE: {
7771            for (reverse 0..$#{$self->{open_elements}}) {
7772              my $node = $self->{open_elements}->[$_];
7773              if ($node->[1] & FOREIGN_EL) {
7774                last INSCOPE;
7775              } elsif ($node->[1] & SCOPING_EL) {
7776                last;
7777              }
7778            }
7779            
7780            ## NOTE: No foreign element in scope.
7781            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7782          } # INSCOPE
7783        }
7784    } # B    } # B
7785    
   ## 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  
   
7786    ## Stop parsing # MUST    ## Stop parsing # MUST
7787        
7788    ## TODO: script stuffs    ## TODO: script stuffs
7789  } # _tree_construct_main  } # _tree_construct_main
7790    
7791  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7792    my $class = shift;    my $class = shift;
7793    my $node = shift;    my $node = shift;
7794    my $s = \$_[0];    #my $s = \$_[0];
7795    my $onerror = $_[1];    my $onerror = $_[1];
7796      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7797    
7798    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7799    
# Line 5804  sub set_inner_html ($$$) { Line 7812  sub set_inner_html ($$$) {
7812      }      }
7813    
7814      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7815      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7816    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7817      ## TODO: If non-html element      ## TODO: If non-html element
7818    
7819      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7820    
7821    ## TODO: Support for $get_wrapper
7822    
7823      ## Step 1 # MUST      ## Step 1 # MUST
7824      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7825      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 5817  sub set_inner_html ($$$) { Line 7827  sub set_inner_html ($$$) {
7827      my $p = $class->new;      my $p = $class->new;
7828      $p->{document} = $doc;      $p->{document} = $doc;
7829    
7830      ## Step 9 # MUST      ## Step 8 # MUST
7831      my $i = 0;      my $i = 0;
7832      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7833      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7834      $p->{set_next_char} = sub {      require Whatpm::Charset::DecodeHandle;
7835        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7836        $input = $get_wrapper->($input);
7837        $p->{set_nc} = sub {
7838        my $self = shift;        my $self = shift;
7839    
7840        pop @{$self->{prev_char}};        my $char = '';
7841        unshift @{$self->{prev_char}}, $self->{next_char};        if (defined $self->{next_nc}) {
7842            $char = $self->{next_nc};
7843            delete $self->{next_nc};
7844            $self->{nc} = ord $char;
7845          } else {
7846            $self->{char_buffer} = '';
7847            $self->{char_buffer_pos} = 0;
7848            
7849            my $count = $input->manakai_read_until
7850                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7851                 $self->{char_buffer_pos});
7852            if ($count) {
7853              $self->{line_prev} = $self->{line};
7854              $self->{column_prev} = $self->{column};
7855              $self->{column}++;
7856              $self->{nc}
7857                  = ord substr ($self->{char_buffer},
7858                                $self->{char_buffer_pos}++, 1);
7859              return;
7860            }
7861            
7862            if ($input->read ($char, 1)) {
7863              $self->{nc} = ord $char;
7864            } else {
7865              $self->{nc} = -1;
7866              return;
7867            }
7868          }
7869    
7870        $self->{next_char} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7871        $self->{next_char} = ord substr $$s, $i++, 1;        $p->{column}++;
7872        $column++;  
7873          if ($self->{nc} == 0x000A) { # LF
7874        if ($self->{next_char} == 0x000A) { # LF          $p->{line}++;
7875          $line++;          $p->{column} = 0;
7876          $column = 0;          !!!cp ('i1');
7877        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{nc} == 0x000D) { # CR
7878          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7879          $self->{next_char} = 0x000A; # LF # MUST          my $next = '';
7880          $line++;          if ($input->read ($next, 1) and $next ne "\x0A") {
7881          $column = 0;            $self->{next_nc} = $next;
7882        } elsif ($self->{next_char} > 0x10FFFF) {          }
7883          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0x000A; # LF # MUST
7884        } elsif ($self->{next_char} == 0x0000) { # NULL          $p->{line}++;
7885            $p->{column} = 0;
7886            !!!cp ('i2');
7887          } elsif ($self->{nc} == 0x0000) { # NULL
7888            !!!cp ('i4');
7889          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7890          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7891        }        }
7892      };      };
7893      $p->{prev_char} = [-1, -1, -1];  
7894      $p->{next_char} = -1;      $p->{read_until} = sub {
7895              #my ($scalar, $specials_range, $offset) = @_;
7896          return 0 if defined $p->{next_nc};
7897    
7898          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7899          my $offset = $_[2] || 0;
7900          
7901          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7902            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7903            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7904              substr ($_[0], $offset)
7905                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7906              my $count = $+[0] - $-[0];
7907              if ($count) {
7908                $p->{column} += $count;
7909                $p->{char_buffer_pos} += $count;
7910                $p->{line_prev} = $p->{line};
7911                $p->{column_prev} = $p->{column} - 1;
7912                $p->{nc} = -1;
7913              }
7914              return $count;
7915            } else {
7916              return 0;
7917            }
7918          } else {
7919            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7920            if ($count) {
7921              $p->{column} += $count;
7922              $p->{column_prev} += $count;
7923              $p->{nc} = -1;
7924            }
7925            return $count;
7926          }
7927        }; # $p->{read_until}
7928    
7929      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7930        my (%opt) = @_;        my (%opt) = @_;
7931        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7932          my $column = $opt{column};
7933          if (defined $opt{token} and defined $opt{token}->{line}) {
7934            $line = $opt{token}->{line};
7935            $column = $opt{token}->{column};
7936          }
7937          warn "Parse error ($opt{type}) at line $line column $column\n";
7938      };      };
7939      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7940        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7941      };      };
7942            
7943        my $char_onerror = sub {
7944          my (undef, $type, %opt) = @_;
7945          $ponerror->(layer => 'encode',
7946                      line => $p->{line}, column => $p->{column} + 1,
7947                      %opt, type => $type);
7948        }; # $char_onerror
7949        $input->onerror ($char_onerror);
7950    
7951      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7952      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7953    
# Line 5878  sub set_inner_html ($$$) { Line 7969  sub set_inner_html ($$$) {
7969          unless defined $p->{content_model};          unless defined $p->{content_model};
7970          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7971    
7972      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7973          ## TODO: Foreign element OK?
7974    
7975      ## Step 4      ## Step 3
7976      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7977        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7978    
7979      ## Step 5 # MUST      ## Step 4 # MUST
7980      $doc->append_child ($root);      $doc->append_child ($root);
7981    
7982      ## Step 6 # MUST      ## Step 5 # MUST
7983      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7984    
7985      undef $p->{head_element};      undef $p->{head_element};
7986    
7987      ## Step 7 # MUST      ## Step 6 # MUST
7988      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7989    
7990      ## Step 8 # MUST      ## Step 7 # MUST
7991      my $anode = $node;      my $anode = $node;
7992      AN: while (defined $anode) {      AN: while (defined $anode) {
7993        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
7994          my $nsuri = $anode->namespace_uri;          my $nsuri = $anode->namespace_uri;
7995          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {          if (defined $nsuri and $nsuri eq 'http://www.w3.org/1999/xhtml') {
7996            if ($anode->manakai_local_name eq 'form') {            if ($anode->manakai_local_name eq 'form') {
7997                !!!cp ('i5');
7998              $p->{form_element} = $anode;              $p->{form_element} = $anode;
7999              last AN;              last AN;
8000            }            }
# Line 5910  sub set_inner_html ($$$) { Line 8003  sub set_inner_html ($$$) {
8003        $anode = $anode->parent_node;        $anode = $anode->parent_node;
8004      } # AN      } # AN
8005            
8006      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
8007      {      {
8008        my $self = $p;        my $self = $p;
8009        !!!next-token;        !!!next-token;
8010      }      }
8011      $p->_tree_construction_main;      $p->_tree_construction_main;
8012    
8013      ## Step 11 # MUST      ## Step 10 # MUST
8014      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
8015      for (@cn) {      for (@cn) {
8016        $node->remove_child ($_);        $node->remove_child ($_);
8017      }      }
8018      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
8019    
8020      ## Step 12 # MUST      ## Step 11 # MUST
8021      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
8022      for (@cn) {      for (@cn) {
8023        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 5934  sub set_inner_html ($$$) { Line 8026  sub set_inner_html ($$$) {
8026      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8027    
8028      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8029    
8030        delete $p->{parse_error}; # delete loop
8031    } else {    } else {
8032      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";
8033    }    }

Legend:
Removed from v.1.78  
changed lines
  Added in v.1.184

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24