/[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.85 by wakaba, Thu Mar 6 15:29:39 2008 UTC revision 1.183 by wakaba, Mon Sep 15 08:09:39 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      $self->{next_char} = -1 and return if $i >= length $$s;        if ($input->read ($char, 1)) {
679      $self->{next_char} = ord substr $$s, $i++, 1;          $self->{nc} = ord $char;
680      $column++;        } else {
681            $self->{nc} = -1;
682            return;
683          }
684        }
685    
686        ($self->{line_prev}, $self->{column_prev})
687            = ($self->{line}, $self->{column});
688        $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->{prev_char} = [-1, -1, -1];
730              $self->{nc} = -1;
731            }
732            return $count;
733          } else {
734            return 0;
735          }
736        } else {
737          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
738          if ($count) {
739            $self->{column} += $count;
740            $self->{line_prev} = $self->{line};
741            $self->{column_prev} = $self->{column} - 1;
742            $self->{prev_char} = [-1, -1, -1];
743            $self->{nc} = -1;
744          }
745          return $count;
746        }
747      }; # $self->{read_until}
748    
749    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
750      my (%opt) = @_;      my (%opt) = @_;
751      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
752        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
753        warn "Parse error ($opt{type}) at line $line column $column\n";
754    };    };
755    $self->{parse_error} = sub {    $self->{parse_error} = sub {
756      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
757    };    };
758    
759      my $char_onerror = sub {
760        my (undef, $type, %opt) = @_;
761        !!!parse-error (layer => 'encode',
762                        line => $self->{line}, column => $self->{column} + 1,
763                        %opt, type => $type);
764      }; # $char_onerror
765    
766      if ($_[3]) {
767        $input = $_[3]->($input);
768        $input->onerror ($char_onerror);
769      } else {
770        $input->onerror ($char_onerror) unless defined $input->onerror;
771      }
772    
773    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
774    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
775    $self->_construct_tree;    $self->_construct_tree;
776    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
777    
778      delete $self->{parse_error}; # remove loop
779    
780    return $self->{document};    return $self->{document};
781  } # parse_string  } # parse_char_stream
782    
783  sub new ($) {  sub new ($) {
784    my $class = shift;    my $class = shift;
785    my $self = bless {}, $class;    my $self = bless {
786    $self->{set_next_char} = sub {      level => {must => 'm',
787      $self->{next_char} = -1;                should => 's',
788                  warn => 'w',
789                  info => 'i',
790                  uncertain => 'u'},
791      }, $class;
792      $self->{set_nc} = sub {
793        $self->{nc} = -1;
794    };    };
795    $self->{parse_error} = sub {    $self->{parse_error} = sub {
796      #      #
# Line 254  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 817  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
817  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
818    
819  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
820  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
821  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
822  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
823  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 265  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 828  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
828  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
829  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
830  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
831  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
832  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
833  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
834  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 850  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
850  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
851  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
852  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
853    sub SELF_CLOSING_START_TAG_STATE () { 34 }
854    sub CDATA_SECTION_STATE () { 35 }
855    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
856    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
857    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
858    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
859    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
860    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
861    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
862    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
863    ## NOTE: "Entity data state", "entity in attribute value state", and
864    ## "consume a character reference" algorithm are jointly implemented
865    ## using the following six states:
866    sub ENTITY_STATE () { 44 }
867    sub ENTITY_HASH_STATE () { 45 }
868    sub NCR_NUM_STATE () { 46 }
869    sub HEXREF_X_STATE () { 47 }
870    sub HEXREF_HEX_STATE () { 48 }
871    sub ENTITY_NAME_STATE () { 49 }
872    
873  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
874  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 885  sub TABLE_IMS ()      { 0b1000000 }
885  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
886  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
887  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
888    sub SELECT_IMS ()     { 0b10000000000 }
889    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
890        ## NOTE: "in foreign content" insertion mode is special; it is combined
891        ## with the secondary insertion mode.  In this parser, they are stored
892        ## together in the bit-or'ed form.
893    
894  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
895    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 912  sub IN_TABLE_IM () { TABLE_IMS }
912  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
913  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
914  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
915  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
916    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
917  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
918    
919  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 333  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 921  sub IN_COLUMN_GROUP_IM () { 0b10 }
921  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
922    my $self = shift;    my $self = shift;
923    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
924      #$self->{s_kwd}; # state keyword - initialized when used
925      #$self->{entity__value}; # initialized when used
926      #$self->{entity__match}; # initialized when used
927    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
928    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{ct}; # current token
929    undef $self->{current_attribute};    undef $self->{ca}; # current attribute
930    undef $self->{last_emitted_start_tag_name};    undef $self->{last_stag_name}; # last emitted start tag name
931    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
932    $self->{char} = [];    delete $self->{self_closing};
933    # $self->{next_char}    $self->{char_buffer} = '';
934      $self->{char_buffer_pos} = 0;
935      $self->{prev_char} = [-1, -1, -1];
936      $self->{nc} = -1; # next input character
937      #$self->{next_nc}
938    !!!next-input-character;    !!!next-input-character;
939    $self->{token} = [];    $self->{token} = [];
940    # $self->{escape}    # $self->{escape}
# Line 350  sub _initialize_tokenizer ($) { Line 945  sub _initialize_tokenizer ($) {
945  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
946  ##   ->{name} (DOCTYPE_TOKEN)  ##   ->{name} (DOCTYPE_TOKEN)
947  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
948  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{pubid} (DOCTYPE_TOKEN)
949  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{sysid} (DOCTYPE_TOKEN)
950  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
951  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
952  ##        ->{name}  ##        ->{name}
953  ##        ->{value}  ##        ->{value}
954  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
955  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
956    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
957    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
958    ##     while the token is pushed back to the stack.
959    
960  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
961    
# Line 367  sub _initialize_tokenizer ($) { Line 965  sub _initialize_tokenizer ($) {
965  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
966  ## and removed from the list.  ## and removed from the list.
967    
968  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
969  ## 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,  
970    
971  sub _get_next_token ($) {  sub _get_next_token ($) {
972    my $self = shift;    my $self = shift;
973    
974      if ($self->{self_closing}) {
975        !!!parse-error (type => 'nestc', token => $self->{ct});
976        ## NOTE: The |self_closing| flag is only set by start tag token.
977        ## In addition, when a start tag token is emitted, it is always set to
978        ## |ct|.
979        delete $self->{self_closing};
980      }
981    
982    if (@{$self->{token}}) {    if (@{$self->{token}}) {
983        $self->{self_closing} = $self->{token}->[0]->{self_closing};
984      return shift @{$self->{token}};      return shift @{$self->{token}};
985    }    }
986    
987    A: {    A: {
988      if ($self->{state} == DATA_STATE) {      if ($self->{state} == DATA_STATE) {
989        if ($self->{next_char} == 0x0026) { # &        if ($self->{nc} == 0x0026) { # &
990          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
991              not $self->{escape}) {              not $self->{escape}) {
992            !!!cp (1);            !!!cp (1);
993            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
994              ## "entity data state".  In this implementation, the tokenizer
995              ## is switched to the |ENTITY_STATE|, which is an implementation
996              ## of the "consume a character reference" algorithm.
997              $self->{entity_add} = -1;
998              $self->{prev_state} = DATA_STATE;
999              $self->{state} = ENTITY_STATE;
1000            !!!next-input-character;            !!!next-input-character;
1001            redo A;            redo A;
1002          } else {          } else {
1003            !!!cp (2);            !!!cp (2);
1004            #            #
1005          }          }
1006        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
1007          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1008            unless ($self->{escape}) {            unless ($self->{escape}) {
1009              if ($self->{prev_char}->[0] == 0x002D and # -              if ($self->{prev_char}->[0] == 0x002D and # -
# Line 418  sub _get_next_token ($) { Line 1020  sub _get_next_token ($) {
1020          }          }
1021                    
1022          #          #
1023        } elsif ($self->{next_char} == 0x003C) { # <        } elsif ($self->{nc} == 0x003C) { # <
1024          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1025              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1026               not $self->{escape})) {               not $self->{escape})) {
# Line 430  sub _get_next_token ($) { Line 1032  sub _get_next_token ($) {
1032            !!!cp (7);            !!!cp (7);
1033            #            #
1034          }          }
1035        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1036          if ($self->{escape} and          if ($self->{escape} and
1037              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1038            if ($self->{prev_char}->[0] == 0x002D and # -            if ($self->{prev_char}->[0] == 0x002D and # -
# Line 445  sub _get_next_token ($) { Line 1047  sub _get_next_token ($) {
1047          }          }
1048                    
1049          #          #
1050        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1051          !!!cp (11);          !!!cp (11);
1052          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
1053                      line => $self->{line}, column => $self->{column}});
1054          last A; ## TODO: ok?          last A; ## TODO: ok?
1055        } else {        } else {
1056          !!!cp (12);          !!!cp (12);
1057        }        }
1058        # Anything else        # Anything else
1059        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1060                     data => chr $self->{next_char}};                     data => chr $self->{nc},
1061                       line => $self->{line}, column => $self->{column},
1062                      };
1063          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1064    
1065        ## Stay in the data state        ## Stay in the data state
1066        !!!next-input-character;        !!!next-input-character;
1067    
1068        !!!emit ($token);        !!!emit ($token);
1069    
1070        redo A;        redo A;
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!cp (13);  
         !!!emit ({type => CHARACTER_TOKEN, data => '&'});  
       } else {  
         !!!cp (14);  
         !!!emit ($token);  
       }  
   
       redo A;  
1071      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1072        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1073          if ($self->{next_char} == 0x002F) { # /          if ($self->{nc} == 0x002F) { # /
1074            !!!cp (15);            !!!cp (15);
1075            !!!next-input-character;            !!!next-input-character;
1076            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
# Line 490  sub _get_next_token ($) { Line 1080  sub _get_next_token ($) {
1080            ## reconsume            ## reconsume
1081            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1082    
1083            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1084                        line => $self->{line_prev},
1085                        column => $self->{column_prev},
1086                       });
1087    
1088            redo A;            redo A;
1089          }          }
1090        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1091          if ($self->{next_char} == 0x0021) { # !          if ($self->{nc} == 0x0021) { # !
1092            !!!cp (17);            !!!cp (17);
1093            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1094            !!!next-input-character;            !!!next-input-character;
1095            redo A;            redo A;
1096          } elsif ($self->{next_char} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1097            !!!cp (18);            !!!cp (18);
1098            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1099            !!!next-input-character;            !!!next-input-character;
1100            redo A;            redo A;
1101          } elsif (0x0041 <= $self->{next_char} and          } elsif (0x0041 <= $self->{nc} and
1102                   $self->{next_char} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1103            !!!cp (19);            !!!cp (19);
1104            $self->{current_token}            $self->{ct}
1105              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1106                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{nc} + 0x0020),
1107                   line => $self->{line_prev},
1108                   column => $self->{column_prev}};
1109            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1110            !!!next-input-character;            !!!next-input-character;
1111            redo A;            redo A;
1112          } elsif (0x0061 <= $self->{next_char} and          } elsif (0x0061 <= $self->{nc} and
1113                   $self->{next_char} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1114            !!!cp (20);            !!!cp (20);
1115            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{ct} = {type => START_TAG_TOKEN,
1116                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{nc}),
1117                                        line => $self->{line_prev},
1118                                        column => $self->{column_prev}};
1119            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1120            !!!next-input-character;            !!!next-input-character;
1121            redo A;            redo A;
1122          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1123            !!!cp (21);            !!!cp (21);
1124            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1125                              line => $self->{line_prev},
1126                              column => $self->{column_prev});
1127            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1128            !!!next-input-character;            !!!next-input-character;
1129    
1130            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1131                        line => $self->{line_prev},
1132                        column => $self->{column_prev},
1133                       });
1134    
1135            redo A;            redo A;
1136          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1137            !!!cp (22);            !!!cp (22);
1138            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1139                              line => $self->{line_prev},
1140                              column => $self->{column_prev});
1141            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1142            ## $self->{next_char} is intentionally left as is            $self->{ct} = {type => COMMENT_TOKEN, data => '',
1143                                        line => $self->{line_prev},
1144                                        column => $self->{column_prev},
1145                                       };
1146              ## $self->{nc} is intentionally left as is
1147            redo A;            redo A;
1148          } else {          } else {
1149            !!!cp (23);            !!!cp (23);
1150            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1151                              line => $self->{line_prev},
1152                              column => $self->{column_prev});
1153            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1154            ## reconsume            ## reconsume
1155    
1156            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1157                        line => $self->{line_prev},
1158                        column => $self->{column_prev},
1159                       });
1160    
1161            redo A;            redo A;
1162          }          }
# Line 551  sub _get_next_token ($) { Line 1164  sub _get_next_token ($) {
1164          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1165        }        }
1166      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1167        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        ## NOTE: The "close tag open state" in the spec is implemented as
1168          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;  
1169    
1170                !!!emit ({type => CHARACTER_TOKEN, data => '</'});        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1171            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1172                redo A;          if (defined $self->{last_stag_name}) {
1173              }            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1174            }            $self->{s_kwd} = '';
1175            push @next_char, $self->{next_char};            ## Reconsume.
1176                    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...  
           }  
1177          } else {          } else {
1178            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1179              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1180            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1181            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1182            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1183              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1184                        line => $l, column => $c,
1185                       });
1186            redo A;            redo A;
1187          }          }
1188        }        }
1189          
1190        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{nc} and
1191            $self->{next_char} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1192          !!!cp (29);          !!!cp (29);
1193          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{ct}
1194                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1195                   tag_name => chr ($self->{nc} + 0x0020),
1196                   line => $l, column => $c};
1197          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1198          !!!next-input-character;          !!!next-input-character;
1199          redo A;          redo A;
1200        } elsif (0x0061 <= $self->{next_char} and        } elsif (0x0061 <= $self->{nc} and
1201                 $self->{next_char} <= 0x007A) { # a..z                 $self->{nc} <= 0x007A) { # a..z
1202          !!!cp (30);          !!!cp (30);
1203          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{ct} = {type => END_TAG_TOKEN,
1204                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{nc}),
1205                                      line => $l, column => $c};
1206          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1207          !!!next-input-character;          !!!next-input-character;
1208          redo A;          redo A;
1209        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1210          !!!cp (31);          !!!cp (31);
1211          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1212                            line => $self->{line_prev}, ## "<" in "</>"
1213                            column => $self->{column_prev} - 1);
1214          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1215          !!!next-input-character;          !!!next-input-character;
1216          redo A;          redo A;
1217        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1218          !!!cp (32);          !!!cp (32);
1219          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1220          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1221          # reconsume          # reconsume
1222    
1223          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1224                      line => $l, column => $c,
1225                     });
1226    
1227          redo A;          redo A;
1228        } else {        } else {
1229          !!!cp (33);          !!!cp (33);
1230          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1231          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1232          ## $self->{next_char} is intentionally left as is          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1233          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1234                                      column => $self->{column_prev} - 1,
1235                                     };
1236            ## NOTE: $self->{nc} is intentionally left as is.
1237            ## Although the "anything else" case of the spec not explicitly
1238            ## states that the next input character is to be reconsumed,
1239            ## it will be included to the |data| of the comment token
1240            ## generated from the bogus end tag, as defined in the
1241            ## "bogus comment state" entry.
1242            redo A;
1243          }
1244        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1245          my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1246          if (length $ch) {
1247            my $CH = $ch;
1248            $ch =~ tr/a-z/A-Z/;
1249            my $nch = chr $self->{nc};
1250            if ($nch eq $ch or $nch eq $CH) {
1251              !!!cp (24);
1252              ## Stay in the state.
1253              $self->{s_kwd} .= $nch;
1254              !!!next-input-character;
1255              redo A;
1256            } else {
1257              !!!cp (25);
1258              $self->{state} = DATA_STATE;
1259              ## Reconsume.
1260              !!!emit ({type => CHARACTER_TOKEN,
1261                        data => '</' . $self->{s_kwd},
1262                        line => $self->{line_prev},
1263                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1264                       });
1265              redo A;
1266            }
1267          } else { # after "<{tag-name}"
1268            unless ({
1269                     0x0009 => 1, # HT
1270                     0x000A => 1, # LF
1271                     0x000B => 1, # VT
1272                     0x000C => 1, # FF
1273                     0x0020 => 1, # SP
1274                     0x003E => 1, # >
1275                     0x002F => 1, # /
1276                     -1 => 1, # EOF
1277                    }->{$self->{nc}}) {
1278              !!!cp (26);
1279              ## Reconsume.
1280              $self->{state} = DATA_STATE;
1281              !!!emit ({type => CHARACTER_TOKEN,
1282                        data => '</' . $self->{s_kwd},
1283                        line => $self->{line_prev},
1284                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1285                       });
1286              redo A;
1287            } else {
1288              !!!cp (27);
1289              $self->{ct}
1290                  = {type => END_TAG_TOKEN,
1291                     tag_name => $self->{last_stag_name},
1292                     line => $self->{line_prev},
1293                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1294              $self->{state} = TAG_NAME_STATE;
1295              ## Reconsume.
1296              redo A;
1297            }
1298        }        }
1299      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1300        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1301            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1302            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1303            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1304            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1305          !!!cp (34);          !!!cp (34);
1306          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1307          !!!next-input-character;          !!!next-input-character;
1308          redo A;          redo A;
1309        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1310          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1311            !!!cp (35);            !!!cp (35);
1312            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1313                = 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) {  
1314            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1315            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1316            #  ## NOTE: This should never be reached.            #  ## NOTE: This should never be reached.
1317            #  !!! cp (36);            #  !!! cp (36);
1318            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 670  sub _get_next_token ($) { Line 1320  sub _get_next_token ($) {
1320              !!!cp (37);              !!!cp (37);
1321            #}            #}
1322          } else {          } else {
1323            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1324          }          }
1325          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1326          !!!next-input-character;          !!!next-input-character;
1327    
1328          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1329    
1330          redo A;          redo A;
1331        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1332                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1333          !!!cp (38);          !!!cp (38);
1334          $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);          $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1335            # start tag or end tag            # start tag or end tag
1336          ## Stay in this state          ## Stay in this state
1337          !!!next-input-character;          !!!next-input-character;
1338          redo A;          redo A;
1339        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1340          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1341          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1342            !!!cp (39);            !!!cp (39);
1343            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1344                = 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) {  
1345            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1346            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1347            #  ## NOTE: This state should never be reached.            #  ## NOTE: This state should never be reached.
1348            #  !!! cp (40);            #  !!! cp (40);
1349            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 703  sub _get_next_token ($) { Line 1351  sub _get_next_token ($) {
1351              !!!cp (41);              !!!cp (41);
1352            #}            #}
1353          } else {          } else {
1354            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1355          }          }
1356          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1357          # reconsume          # reconsume
1358    
1359          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1360    
1361          redo A;          redo A;
1362        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1363            !!!cp (42);
1364            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1365          !!!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  
1366          redo A;          redo A;
1367        } else {        } else {
1368          !!!cp (44);          !!!cp (44);
1369          $self->{current_token}->{tag_name} .= chr $self->{next_char};          $self->{ct}->{tag_name} .= chr $self->{nc};
1370            # start tag or end tag            # start tag or end tag
1371          ## Stay in the state          ## Stay in the state
1372          !!!next-input-character;          !!!next-input-character;
1373          redo A;          redo A;
1374        }        }
1375      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1376        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1377            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1378            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1379            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1380            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1381          !!!cp (45);          !!!cp (45);
1382          ## Stay in the state          ## Stay in the state
1383          !!!next-input-character;          !!!next-input-character;
1384          redo A;          redo A;
1385        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1386          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1387            !!!cp (46);            !!!cp (46);
1388            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1389                = 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) {  
1390            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1391            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1392              !!!cp (47);              !!!cp (47);
1393              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1394            } else {            } else {
1395              !!!cp (48);              !!!cp (48);
1396            }            }
1397          } else {          } else {
1398            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1399          }          }
1400          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1401          !!!next-input-character;          !!!next-input-character;
1402    
1403          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1404    
1405          redo A;          redo A;
1406        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1407                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1408          !!!cp (49);          !!!cp (49);
1409          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{ca}
1410                                value => ''};              = {name => chr ($self->{nc} + 0x0020),
1411                   value => '',
1412                   line => $self->{line}, column => $self->{column}};
1413          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1414          !!!next-input-character;          !!!next-input-character;
1415          redo A;          redo A;
1416        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1417            !!!cp (50);
1418            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1419          !!!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  
1420          redo A;          redo A;
1421        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1422          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1423          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1424            !!!cp (52);            !!!cp (52);
1425            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1426                = 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) {  
1427            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1428            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1429              !!!cp (53);              !!!cp (53);
1430              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1431            } else {            } else {
1432              !!!cp (54);              !!!cp (54);
1433            }            }
1434          } else {          } else {
1435            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1436          }          }
1437          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1438          # reconsume          # reconsume
1439    
1440          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1441    
1442          redo A;          redo A;
1443        } else {        } else {
# Line 819  sub _get_next_token ($) { Line 1445  sub _get_next_token ($) {
1445               0x0022 => 1, # "               0x0022 => 1, # "
1446               0x0027 => 1, # '               0x0027 => 1, # '
1447               0x003D => 1, # =               0x003D => 1, # =
1448              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1449            !!!cp (55);            !!!cp (55);
1450            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1451          } else {          } else {
1452            !!!cp (56);            !!!cp (56);
1453          }          }
1454          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{ca}
1455                                value => ''};              = {name => chr ($self->{nc}),
1456                   value => '',
1457                   line => $self->{line}, column => $self->{column}};
1458          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1459          !!!next-input-character;          !!!next-input-character;
1460          redo A;          redo A;
1461        }        }
1462      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1463        my $before_leave = sub {        my $before_leave = sub {
1464          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1465              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1466            !!!cp (57);            !!!cp (57);
1467            !!!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});
1468            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{ca} # MUST
1469          } else {          } else {
1470            !!!cp (58);            !!!cp (58);
1471            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{ct}->{attributes}->{$self->{ca}->{name}}
1472              = $self->{current_attribute};              = $self->{ca};
1473          }          }
1474        }; # $before_leave        }; # $before_leave
1475    
1476        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1477            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1478            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1479            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1480            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1481          !!!cp (59);          !!!cp (59);
1482          $before_leave->();          $before_leave->();
1483          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1484          !!!next-input-character;          !!!next-input-character;
1485          redo A;          redo A;
1486        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1487          !!!cp (60);          !!!cp (60);
1488          $before_leave->();          $before_leave->();
1489          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1490          !!!next-input-character;          !!!next-input-character;
1491          redo A;          redo A;
1492        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1493          $before_leave->();          $before_leave->();
1494          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1495            !!!cp (61);            !!!cp (61);
1496            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1497                = 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) {  
1498            !!!cp (62);            !!!cp (62);
1499            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1500            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1501              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1502            }            }
1503          } else {          } else {
1504            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1505          }          }
1506          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1507          !!!next-input-character;          !!!next-input-character;
1508    
1509          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1510    
1511          redo A;          redo A;
1512        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1513                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1514          !!!cp (63);          !!!cp (63);
1515          $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);          $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1516          ## Stay in the state          ## Stay in the state
1517          !!!next-input-character;          !!!next-input-character;
1518          redo A;          redo A;
1519        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1520            !!!cp (64);
1521          $before_leave->();          $before_leave->();
1522            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1523          !!!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  
1524          redo A;          redo A;
1525        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1526          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1527          $before_leave->();          $before_leave->();
1528          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1529            !!!cp (66);            !!!cp (66);
1530            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1531                = 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) {  
1532            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1533            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1534              !!!cp (67);              !!!cp (67);
1535              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1536            } else {            } else {
# Line 924  sub _get_next_token ($) { Line 1538  sub _get_next_token ($) {
1538              !!!cp (68);              !!!cp (68);
1539            }            }
1540          } else {          } else {
1541            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1542          }          }
1543          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1544          # reconsume          # reconsume
1545    
1546          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1547    
1548          redo A;          redo A;
1549        } else {        } else {
1550          if ($self->{next_char} == 0x0022 or # "          if ($self->{nc} == 0x0022 or # "
1551              $self->{next_char} == 0x0027) { # '              $self->{nc} == 0x0027) { # '
1552            !!!cp (69);            !!!cp (69);
1553            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1554          } else {          } else {
1555            !!!cp (70);            !!!cp (70);
1556          }          }
1557          $self->{current_attribute}->{name} .= chr ($self->{next_char});          $self->{ca}->{name} .= chr ($self->{nc});
1558          ## Stay in the state          ## Stay in the state
1559          !!!next-input-character;          !!!next-input-character;
1560          redo A;          redo A;
1561        }        }
1562      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1563        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1564            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1565            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1566            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1567            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1568          !!!cp (71);          !!!cp (71);
1569          ## Stay in the state          ## Stay in the state
1570          !!!next-input-character;          !!!next-input-character;
1571          redo A;          redo A;
1572        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1573          !!!cp (72);          !!!cp (72);
1574          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1575          !!!next-input-character;          !!!next-input-character;
1576          redo A;          redo A;
1577        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1578          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1579            !!!cp (73);            !!!cp (73);
1580            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1581                = 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) {  
1582            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1583            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1584              !!!cp (74);              !!!cp (74);
1585              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1586            } else {            } else {
# Line 976  sub _get_next_token ($) { Line 1588  sub _get_next_token ($) {
1588              !!!cp (75);              !!!cp (75);
1589            }            }
1590          } else {          } else {
1591            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1592          }          }
1593          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1594          !!!next-input-character;          !!!next-input-character;
1595    
1596          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1597    
1598          redo A;          redo A;
1599        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1600                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1601          !!!cp (76);          !!!cp (76);
1602          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{ca}
1603                                value => ''};              = {name => chr ($self->{nc} + 0x0020),
1604                   value => '',
1605                   line => $self->{line}, column => $self->{column}};
1606          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1607          !!!next-input-character;          !!!next-input-character;
1608          redo A;          redo A;
1609        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1610            !!!cp (77);
1611            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1612          !!!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  
1613          redo A;          redo A;
1614        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1615          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1616          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1617            !!!cp (79);            !!!cp (79);
1618            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1619                = 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) {  
1620            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1621            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1622              !!!cp (80);              !!!cp (80);
1623              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1624            } else {            } else {
# Line 1025  sub _get_next_token ($) { Line 1626  sub _get_next_token ($) {
1626              !!!cp (81);              !!!cp (81);
1627            }            }
1628          } else {          } else {
1629            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1630          }          }
1631          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1632          # reconsume          # reconsume
1633    
1634          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1635    
1636          redo A;          redo A;
1637        } else {        } else {
1638          !!!cp (82);          if ($self->{nc} == 0x0022 or # "
1639          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{nc} == 0x0027) { # '
1640                                value => ''};            !!!cp (78);
1641              !!!parse-error (type => 'bad attribute name');
1642            } else {
1643              !!!cp (82);
1644            }
1645            $self->{ca}
1646                = {name => chr ($self->{nc}),
1647                   value => '',
1648                   line => $self->{line}, column => $self->{column}};
1649          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1650          !!!next-input-character;          !!!next-input-character;
1651          redo A;                  redo A;        
1652        }        }
1653      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1654        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1655            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1656            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1657            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1658            $self->{next_char} == 0x0020) { # SP                  $self->{nc} == 0x0020) { # SP      
1659          !!!cp (83);          !!!cp (83);
1660          ## Stay in the state          ## Stay in the state
1661          !!!next-input-character;          !!!next-input-character;
1662          redo A;          redo A;
1663        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
1664          !!!cp (84);          !!!cp (84);
1665          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1666          !!!next-input-character;          !!!next-input-character;
1667          redo A;          redo A;
1668        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1669          !!!cp (85);          !!!cp (85);
1670          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1671          ## reconsume          ## reconsume
1672          redo A;          redo A;
1673        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
1674          !!!cp (86);          !!!cp (86);
1675          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1676          !!!next-input-character;          !!!next-input-character;
1677          redo A;          redo A;
1678        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1679          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          !!!parse-error (type => 'empty unquoted attribute value');
1680            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1681            !!!cp (87);            !!!cp (87);
1682            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1683                = 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) {  
1684            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1685            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1686              !!!cp (88);              !!!cp (88);
1687              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1688            } else {            } else {
# Line 1082  sub _get_next_token ($) { Line 1690  sub _get_next_token ($) {
1690              !!!cp (89);              !!!cp (89);
1691            }            }
1692          } else {          } else {
1693            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1694          }          }
1695          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1696          !!!next-input-character;          !!!next-input-character;
1697    
1698          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1699    
1700          redo A;          redo A;
1701        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1702          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1703          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1704            !!!cp (90);            !!!cp (90);
1705            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1706                = 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) {  
1707            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1708            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1709              !!!cp (91);              !!!cp (91);
1710              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1711            } else {            } else {
# Line 1107  sub _get_next_token ($) { Line 1713  sub _get_next_token ($) {
1713              !!!cp (92);              !!!cp (92);
1714            }            }
1715          } else {          } else {
1716            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1717          }          }
1718          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1719          ## reconsume          ## reconsume
1720    
1721          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1722    
1723          redo A;          redo A;
1724        } else {        } else {
1725          if ($self->{next_char} == 0x003D) { # =          if ($self->{nc} == 0x003D) { # =
1726            !!!cp (93);            !!!cp (93);
1727            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1728          } else {          } else {
1729            !!!cp (94);            !!!cp (94);
1730          }          }
1731          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1732          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1733          !!!next-input-character;          !!!next-input-character;
1734          redo A;          redo A;
1735        }        }
1736      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1737        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
1738          !!!cp (95);          !!!cp (95);
1739          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1743          !!!cp (96);          !!!cp (96);
1744          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1745          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1746            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1747            ## implementation of the "consume a character reference" algorithm.
1748            $self->{prev_state} = $self->{state};
1749            $self->{entity_add} = 0x0022; # "
1750            $self->{state} = ENTITY_STATE;
1751          !!!next-input-character;          !!!next-input-character;
1752          redo A;          redo A;
1753        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1754          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1755          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1756            !!!cp (97);            !!!cp (97);
1757            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1758                = 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) {  
1759            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1760            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1761              !!!cp (98);              !!!cp (98);
1762              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1763            } else {            } else {
# Line 1156  sub _get_next_token ($) { Line 1765  sub _get_next_token ($) {
1765              !!!cp (99);              !!!cp (99);
1766            }            }
1767          } else {          } else {
1768            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1769          }          }
1770          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1771          ## reconsume          ## reconsume
1772    
1773          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1774    
1775          redo A;          redo A;
1776        } else {        } else {
1777          !!!cp (100);          !!!cp (100);
1778          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1779            $self->{read_until}->($self->{ca}->{value},
1780                                  q["&],
1781                                  length $self->{ca}->{value});
1782    
1783          ## Stay in the state          ## Stay in the state
1784          !!!next-input-character;          !!!next-input-character;
1785          redo A;          redo A;
1786        }        }
1787      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1788        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
1789          !!!cp (101);          !!!cp (101);
1790          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1791          !!!next-input-character;          !!!next-input-character;
1792          redo A;          redo A;
1793        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1794          !!!cp (102);          !!!cp (102);
1795          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1796          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1797            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1798            ## implementation of the "consume a character reference" algorithm.
1799            $self->{entity_add} = 0x0027; # '
1800            $self->{prev_state} = $self->{state};
1801            $self->{state} = ENTITY_STATE;
1802          !!!next-input-character;          !!!next-input-character;
1803          redo A;          redo A;
1804        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1805          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1806          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1807            !!!cp (103);            !!!cp (103);
1808            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1809                = 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) {  
1810            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1811            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1812              !!!cp (104);              !!!cp (104);
1813              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1814            } else {            } else {
# Line 1200  sub _get_next_token ($) { Line 1816  sub _get_next_token ($) {
1816              !!!cp (105);              !!!cp (105);
1817            }            }
1818          } else {          } else {
1819            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1820          }          }
1821          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1822          ## reconsume          ## reconsume
1823    
1824          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1825    
1826          redo A;          redo A;
1827        } else {        } else {
1828          !!!cp (106);          !!!cp (106);
1829          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1830            $self->{read_until}->($self->{ca}->{value},
1831                                  q['&],
1832                                  length $self->{ca}->{value});
1833    
1834          ## Stay in the state          ## Stay in the state
1835          !!!next-input-character;          !!!next-input-character;
1836          redo A;          redo A;
1837        }        }
1838      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1839        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1840            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1841            $self->{next_char} == 0x000B or # HT            $self->{nc} == 0x000B or # HT
1842            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1843            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1844          !!!cp (107);          !!!cp (107);
1845          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1846          !!!next-input-character;          !!!next-input-character;
1847          redo A;          redo A;
1848        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1849          !!!cp (108);          !!!cp (108);
1850          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1851          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1852            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1853            ## implementation of the "consume a character reference" algorithm.
1854            $self->{entity_add} = -1;
1855            $self->{prev_state} = $self->{state};
1856            $self->{state} = ENTITY_STATE;
1857          !!!next-input-character;          !!!next-input-character;
1858          redo A;          redo A;
1859        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1860          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1861            !!!cp (109);            !!!cp (109);
1862            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1863                = 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) {  
1864            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1865            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1866              !!!cp (110);              !!!cp (110);
1867              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1868            } else {            } else {
# Line 1247  sub _get_next_token ($) { Line 1870  sub _get_next_token ($) {
1870              !!!cp (111);              !!!cp (111);
1871            }            }
1872          } else {          } else {
1873            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1874          }          }
1875          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1876          !!!next-input-character;          !!!next-input-character;
1877    
1878          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1879    
1880          redo A;          redo A;
1881        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1882          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1883          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1884            !!!cp (112);            !!!cp (112);
1885            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1886                = 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) {  
1887            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1888            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1889              !!!cp (113);              !!!cp (113);
1890              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1891            } else {            } else {
# Line 1272  sub _get_next_token ($) { Line 1893  sub _get_next_token ($) {
1893              !!!cp (114);              !!!cp (114);
1894            }            }
1895          } else {          } else {
1896            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1897          }          }
1898          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1899          ## reconsume          ## reconsume
1900    
1901          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1902    
1903          redo A;          redo A;
1904        } else {        } else {
# Line 1285  sub _get_next_token ($) { Line 1906  sub _get_next_token ($) {
1906               0x0022 => 1, # "               0x0022 => 1, # "
1907               0x0027 => 1, # '               0x0027 => 1, # '
1908               0x003D => 1, # =               0x003D => 1, # =
1909              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1910            !!!cp (115);            !!!cp (115);
1911            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1912          } else {          } else {
1913            !!!cp (116);            !!!cp (116);
1914          }          }
1915          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1916            $self->{read_until}->($self->{ca}->{value},
1917                                  q["'=& >],
1918                                  length $self->{ca}->{value});
1919    
1920          ## Stay in the state          ## Stay in the state
1921          !!!next-input-character;          !!!next-input-character;
1922          redo A;          redo A;
1923        }        }
     } 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;  
1924      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1925        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
1926            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
1927            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
1928            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
1929            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
1930          !!!cp (118);          !!!cp (118);
1931          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1932          !!!next-input-character;          !!!next-input-character;
1933          redo A;          redo A;
1934        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1935          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1936            !!!cp (119);            !!!cp (119);
1937            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1938                = 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) {  
1939            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1940            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1941              !!!cp (120);              !!!cp (120);
1942              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1943            } else {            } else {
# Line 1344  sub _get_next_token ($) { Line 1945  sub _get_next_token ($) {
1945              !!!cp (121);              !!!cp (121);
1946            }            }
1947          } else {          } else {
1948            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1949          }          }
1950          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1951          !!!next-input-character;          !!!next-input-character;
1952    
1953          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1954    
1955          redo A;          redo A;
1956        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1957            !!!cp (122);
1958            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1959          !!!next-input-character;          !!!next-input-character;
1960          if ($self->{next_char} == 0x003E and # >          redo A;
1961              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{nc} == -1) {
1962              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1963            # permitted slash          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1964            !!!cp (122);            !!!cp (122.3);
1965            #            $self->{last_stag_name} = $self->{ct}->{tag_name};
1966            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
1967              if ($self->{ct}->{attributes}) {
1968                !!!cp (122.1);
1969                !!!parse-error (type => 'end tag attribute');
1970              } else {
1971                ## NOTE: This state should never be reached.
1972                !!!cp (122.2);
1973              }
1974          } else {          } else {
1975            !!!cp (123);            die "$0: $self->{ct}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1976          }          }
1977          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1978          # next-input-character is already done          ## Reconsume.
1979            !!!emit ($self->{ct}); # start tag or end tag
1980          redo A;          redo A;
1981        } else {        } else {
1982          !!!cp (124);          !!!cp ('124.1');
1983          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1984          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1985          ## reconsume          ## reconsume
1986          redo A;          redo A;
1987        }        }
1988      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1989        ## (only happen if PCDATA state)        if ($self->{nc} == 0x003E) { # >
1990                  if ($self->{ct}->{type} == END_TAG_TOKEN) {
1991        my $token = {type => COMMENT_TOKEN, data => ''};            !!!cp ('124.2');
1992              !!!parse-error (type => 'nestc', token => $self->{ct});
1993        BC: {            ## TODO: Different type than slash in start tag
1994          if ($self->{next_char} == 0x003E) { # >            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1995            !!!cp (124);            if ($self->{ct}->{attributes}) {
1996            $self->{state} = DATA_STATE;              !!!cp ('124.4');
1997            !!!next-input-character;              !!!parse-error (type => 'end tag attribute');
1998              } else {
1999            !!!emit ($token);              !!!cp ('124.5');
2000              }
2001              ## TODO: Test |<title></title/>|
2002            } else {
2003              !!!cp ('124.3');
2004              $self->{self_closing} = 1;
2005            }
2006    
2007            redo A;          $self->{state} = DATA_STATE;
2008          } elsif ($self->{next_char} == -1) {          !!!next-input-character;
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
2009    
2010            !!!emit ($token);          !!!emit ($self->{ct}); # start tag or end tag
2011    
2012            redo A;          redo A;
2013          } elsif ($self->{nc} == -1) {
2014            !!!parse-error (type => 'unclosed tag');
2015            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2016              !!!cp (124.7);
2017              $self->{last_stag_name} = $self->{ct}->{tag_name};
2018            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2019              if ($self->{ct}->{attributes}) {
2020                !!!cp (124.5);
2021                !!!parse-error (type => 'end tag attribute');
2022              } else {
2023                ## NOTE: This state should never be reached.
2024                !!!cp (124.6);
2025              }
2026          } else {          } else {
2027            !!!cp (126);            die "$0: $self->{ct}->{type}: Unknown token type";
           $token->{data} .= chr ($self->{next_char});  
           !!!next-input-character;  
           redo BC;  
2028          }          }
2029        } # BC          $self->{state} = DATA_STATE;
2030            ## Reconsume.
2031        die "$0: _get_next_token: unexpected case [BC]";          !!!emit ($self->{ct}); # start tag or end tag
2032      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {          redo A;
2033          } else {
2034            !!!cp ('124.4');
2035            !!!parse-error (type => 'nestc');
2036            ## TODO: This error type is wrong.
2037            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2038            ## Reconsume.
2039            redo A;
2040          }
2041        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2042        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2043    
2044        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2045        push @next_char, $self->{next_char};        ## consumes characters one-by-one basis.
2046                
2047        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x003E) { # >
2048            !!!cp (124);
2049            $self->{state} = DATA_STATE;
2050          !!!next-input-character;          !!!next-input-character;
2051          push @next_char, $self->{next_char};  
2052          if ($self->{next_char} == 0x002D) { # -          !!!emit ($self->{ct}); # comment
2053            !!!cp (127);          redo A;
2054            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};        } elsif ($self->{nc} == -1) {
2055            $self->{state} = COMMENT_START_STATE;          !!!cp (125);
2056            !!!next-input-character;          $self->{state} = DATA_STATE;
2057            redo A;          ## reconsume
2058          } else {  
2059            !!!cp (128);          !!!emit ($self->{ct}); # comment
2060          }          redo A;
2061        } elsif ($self->{next_char} == 0x0044 or # D        } else {
2062                 $self->{next_char} == 0x0064) { # d          !!!cp (126);
2063            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2064            $self->{read_until}->($self->{ct}->{data},
2065                                  q[>],
2066                                  length $self->{ct}->{data});
2067    
2068            ## Stay in the state.
2069          !!!next-input-character;          !!!next-input-character;
2070          push @next_char, $self->{next_char};          redo A;
2071          if ($self->{next_char} == 0x004F or # O        }
2072              $self->{next_char} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2073            !!!next-input-character;        ## (only happen if PCDATA state)
2074            push @next_char, $self->{next_char};        
2075            if ($self->{next_char} == 0x0043 or # C        if ($self->{nc} == 0x002D) { # -
2076                $self->{next_char} == 0x0063) { # c          !!!cp (133);
2077              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2078              push @next_char, $self->{next_char};          !!!next-input-character;
2079              if ($self->{next_char} == 0x0054 or # T          redo A;
2080                  $self->{next_char} == 0x0074) { # t        } elsif ($self->{nc} == 0x0044 or # D
2081                !!!next-input-character;                 $self->{nc} == 0x0064) { # d
2082                push @next_char, $self->{next_char};          ## ASCII case-insensitive.
2083                if ($self->{next_char} == 0x0059 or # Y          !!!cp (130);
2084                    $self->{next_char} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2085                  !!!next-input-character;          $self->{s_kwd} = chr $self->{nc};
2086                  push @next_char, $self->{next_char};          !!!next-input-character;
2087                  if ($self->{next_char} == 0x0050 or # P          redo A;
2088                      $self->{next_char} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2089                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2090                    push @next_char, $self->{next_char};                 $self->{nc} == 0x005B) { # [
2091                    if ($self->{next_char} == 0x0045 or # E          !!!cp (135.4);                
2092                        $self->{next_char} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2093                      !!!cp (129);          $self->{s_kwd} = '[';
2094                      ## TODO: What a stupid code this is!          !!!next-input-character;
2095                      $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);  
         }  
2096        } else {        } else {
2097          !!!cp (136);          !!!cp (136);
2098        }        }
2099    
2100        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2101        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2102        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2103          ## Reconsume.
2104        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2105          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2106                                    line => $self->{line_prev},
2107                                    column => $self->{column_prev} - 1,
2108                                   };
2109        redo A;        redo A;
2110              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2111        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2112        ## 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);
2113            $self->{ct} = {type => COMMENT_TOKEN, data => '',
2114                                      line => $self->{line_prev},
2115                                      column => $self->{column_prev} - 2,
2116                                     };
2117            $self->{state} = COMMENT_START_STATE;
2118            !!!next-input-character;
2119            redo A;
2120          } else {
2121            !!!cp (128);
2122            !!!parse-error (type => 'bogus comment',
2123                            line => $self->{line_prev},
2124                            column => $self->{column_prev} - 2);
2125            $self->{state} = BOGUS_COMMENT_STATE;
2126            ## Reconsume.
2127            $self->{ct} = {type => COMMENT_TOKEN,
2128                                      data => '-',
2129                                      line => $self->{line_prev},
2130                                      column => $self->{column_prev} - 2,
2131                                     };
2132            redo A;
2133          }
2134        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2135          ## ASCII case-insensitive.
2136          if ($self->{nc} == [
2137                undef,
2138                0x004F, # O
2139                0x0043, # C
2140                0x0054, # T
2141                0x0059, # Y
2142                0x0050, # P
2143              ]->[length $self->{s_kwd}] or
2144              $self->{nc} == [
2145                undef,
2146                0x006F, # o
2147                0x0063, # c
2148                0x0074, # t
2149                0x0079, # y
2150                0x0070, # p
2151              ]->[length $self->{s_kwd}]) {
2152            !!!cp (131);
2153            ## Stay in the state.
2154            $self->{s_kwd} .= chr $self->{nc};
2155            !!!next-input-character;
2156            redo A;
2157          } elsif ((length $self->{s_kwd}) == 6 and
2158                   ($self->{nc} == 0x0045 or # E
2159                    $self->{nc} == 0x0065)) { # e
2160            !!!cp (129);
2161            $self->{state} = DOCTYPE_STATE;
2162            $self->{ct} = {type => DOCTYPE_TOKEN,
2163                                      quirks => 1,
2164                                      line => $self->{line_prev},
2165                                      column => $self->{column_prev} - 7,
2166                                     };
2167            !!!next-input-character;
2168            redo A;
2169          } else {
2170            !!!cp (132);        
2171            !!!parse-error (type => 'bogus comment',
2172                            line => $self->{line_prev},
2173                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2174            $self->{state} = BOGUS_COMMENT_STATE;
2175            ## Reconsume.
2176            $self->{ct} = {type => COMMENT_TOKEN,
2177                                      data => $self->{s_kwd},
2178                                      line => $self->{line_prev},
2179                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2180                                     };
2181            redo A;
2182          }
2183        } elsif ($self->{state} == MD_CDATA_STATE) {
2184          if ($self->{nc} == {
2185                '[' => 0x0043, # C
2186                '[C' => 0x0044, # D
2187                '[CD' => 0x0041, # A
2188                '[CDA' => 0x0054, # T
2189                '[CDAT' => 0x0041, # A
2190              }->{$self->{s_kwd}}) {
2191            !!!cp (135.1);
2192            ## Stay in the state.
2193            $self->{s_kwd} .= chr $self->{nc};
2194            !!!next-input-character;
2195            redo A;
2196          } elsif ($self->{s_kwd} eq '[CDATA' and
2197                   $self->{nc} == 0x005B) { # [
2198            !!!cp (135.2);
2199            $self->{ct} = {type => CHARACTER_TOKEN,
2200                                      data => '',
2201                                      line => $self->{line_prev},
2202                                      column => $self->{column_prev} - 7};
2203            $self->{state} = CDATA_SECTION_STATE;
2204            !!!next-input-character;
2205            redo A;
2206          } else {
2207            !!!cp (135.3);
2208            !!!parse-error (type => 'bogus comment',
2209                            line => $self->{line_prev},
2210                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2211            $self->{state} = BOGUS_COMMENT_STATE;
2212            ## Reconsume.
2213            $self->{ct} = {type => COMMENT_TOKEN,
2214                                      data => $self->{s_kwd},
2215                                      line => $self->{line_prev},
2216                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2217                                     };
2218            redo A;
2219          }
2220      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2221        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2222          !!!cp (137);          !!!cp (137);
2223          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2224          !!!next-input-character;          !!!next-input-character;
2225          redo A;          redo A;
2226        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2227          !!!cp (138);          !!!cp (138);
2228          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2229          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2230          !!!next-input-character;          !!!next-input-character;
2231    
2232          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2233    
2234          redo A;          redo A;
2235        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2236          !!!cp (139);          !!!cp (139);
2237          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2238          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2239          ## reconsume          ## reconsume
2240    
2241          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2242    
2243          redo A;          redo A;
2244        } else {        } else {
2245          !!!cp (140);          !!!cp (140);
2246          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2247              .= chr ($self->{next_char});              .= chr ($self->{nc});
2248          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2249          !!!next-input-character;          !!!next-input-character;
2250          redo A;          redo A;
2251        }        }
2252      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2253        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2254          !!!cp (141);          !!!cp (141);
2255          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2256          !!!next-input-character;          !!!next-input-character;
2257          redo A;          redo A;
2258        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2259          !!!cp (142);          !!!cp (142);
2260          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2261          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2262          !!!next-input-character;          !!!next-input-character;
2263    
2264          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2265    
2266          redo A;          redo A;
2267        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2268          !!!cp (143);          !!!cp (143);
2269          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2270          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2271          ## reconsume          ## reconsume
2272    
2273          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2274    
2275          redo A;          redo A;
2276        } else {        } else {
2277          !!!cp (144);          !!!cp (144);
2278          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2279              .= '-' . chr ($self->{next_char});              .= '-' . chr ($self->{nc});
2280          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2281          !!!next-input-character;          !!!next-input-character;
2282          redo A;          redo A;
2283        }        }
2284      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2285        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2286          !!!cp (145);          !!!cp (145);
2287          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2288          !!!next-input-character;          !!!next-input-character;
2289          redo A;          redo A;
2290        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2291          !!!cp (146);          !!!cp (146);
2292          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2293          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2294          ## reconsume          ## reconsume
2295    
2296          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2297    
2298          redo A;          redo A;
2299        } else {        } else {
2300          !!!cp (147);          !!!cp (147);
2301          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{ct}->{data} .= chr ($self->{nc}); # comment
2302            $self->{read_until}->($self->{ct}->{data},
2303                                  q[-],
2304                                  length $self->{ct}->{data});
2305    
2306          ## Stay in the state          ## Stay in the state
2307          !!!next-input-character;          !!!next-input-character;
2308          redo A;          redo A;
2309        }        }
2310      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2311        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2312          !!!cp (148);          !!!cp (148);
2313          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2314          !!!next-input-character;          !!!next-input-character;
2315          redo A;          redo A;
2316        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2317          !!!cp (149);          !!!cp (149);
2318          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2319          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2320          ## reconsume          ## reconsume
2321    
2322          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2323    
2324          redo A;          redo A;
2325        } else {        } else {
2326          !!!cp (150);          !!!cp (150);
2327          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment          $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2328          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2329          !!!next-input-character;          !!!next-input-character;
2330          redo A;          redo A;
2331        }        }
2332      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2333        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2334          !!!cp (151);          !!!cp (151);
2335          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2336          !!!next-input-character;          !!!next-input-character;
2337    
2338          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2339    
2340          redo A;          redo A;
2341        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2342          !!!cp (152);          !!!cp (152);
2343          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2344          $self->{current_token}->{data} .= '-'; # comment                          line => $self->{line_prev},
2345                            column => $self->{column_prev});
2346            $self->{ct}->{data} .= '-'; # comment
2347          ## Stay in the state          ## Stay in the state
2348          !!!next-input-character;          !!!next-input-character;
2349          redo A;          redo A;
2350        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2351          !!!cp (153);          !!!cp (153);
2352          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2353          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2354          ## reconsume          ## reconsume
2355    
2356          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2357    
2358          redo A;          redo A;
2359        } else {        } else {
2360          !!!cp (154);          !!!cp (154);
2361          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2362          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment                          line => $self->{line_prev},
2363                            column => $self->{column_prev});
2364            $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2365          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2366          !!!next-input-character;          !!!next-input-character;
2367          redo A;          redo A;
2368        }        }
2369      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2370        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2371            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2372            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2373            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2374            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2375          !!!cp (155);          !!!cp (155);
2376          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2377          !!!next-input-character;          !!!next-input-character;
# Line 1643  sub _get_next_token ($) { Line 2384  sub _get_next_token ($) {
2384          redo A;          redo A;
2385        }        }
2386      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2387        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2388            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2389            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2390            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2391            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2392          !!!cp (157);          !!!cp (157);
2393          ## Stay in the state          ## Stay in the state
2394          !!!next-input-character;          !!!next-input-character;
2395          redo A;          redo A;
2396        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2397          !!!cp (158);          !!!cp (158);
2398          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2399          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2400          !!!next-input-character;          !!!next-input-character;
2401    
2402          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2403    
2404          redo A;          redo A;
2405        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2406          !!!cp (159);          !!!cp (159);
2407          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2408          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2409          ## reconsume          ## reconsume
2410    
2411          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2412    
2413          redo A;          redo A;
2414        } else {        } else {
2415          !!!cp (160);          !!!cp (160);
2416          $self->{current_token}          $self->{ct}->{name} = chr $self->{nc};
2417              = {type => DOCTYPE_TOKEN,          delete $self->{ct}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2418  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2419          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2420          !!!next-input-character;          !!!next-input-character;
# Line 1684  sub _get_next_token ($) { Line 2422  sub _get_next_token ($) {
2422        }        }
2423      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2424  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2425        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2426            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2427            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2428            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2429            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2430          !!!cp (161);          !!!cp (161);
2431          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2432          !!!next-input-character;          !!!next-input-character;
2433          redo A;          redo A;
2434        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2435          !!!cp (162);          !!!cp (162);
2436          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2437          !!!next-input-character;          !!!next-input-character;
2438    
2439          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2440    
2441          redo A;          redo A;
2442        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2443          !!!cp (163);          !!!cp (163);
2444          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2445          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2446          ## reconsume          ## reconsume
2447    
2448          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2449          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2450    
2451          redo A;          redo A;
2452        } else {        } else {
2453          !!!cp (164);          !!!cp (164);
2454          $self->{current_token}->{name}          $self->{ct}->{name}
2455            .= chr ($self->{next_char}); # DOCTYPE            .= chr ($self->{nc}); # DOCTYPE
2456          ## Stay in the state          ## Stay in the state
2457          !!!next-input-character;          !!!next-input-character;
2458          redo A;          redo A;
2459        }        }
2460      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2461        if ($self->{next_char} == 0x0009 or # HT        if ($self->{nc} == 0x0009 or # HT
2462            $self->{next_char} == 0x000A or # LF            $self->{nc} == 0x000A or # LF
2463            $self->{next_char} == 0x000B or # VT            $self->{nc} == 0x000B or # VT
2464            $self->{next_char} == 0x000C or # FF            $self->{nc} == 0x000C or # FF
2465            $self->{next_char} == 0x0020) { # SP            $self->{nc} == 0x0020) { # SP
2466          !!!cp (165);          !!!cp (165);
2467          ## Stay in the state          ## Stay in the state
2468          !!!next-input-character;          !!!next-input-character;
2469          redo A;          redo A;
2470        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2471          !!!cp (166);          !!!cp (166);
2472          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2473          !!!next-input-character;          !!!next-input-character;
2474    
2475          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2476    
2477          redo A;          redo A;
2478        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2479          !!!cp (167);          !!!cp (167);
2480          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2481          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2482          ## reconsume          ## reconsume
2483    
2484          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2485          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2486    
2487          redo A;          redo A;
2488        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{nc} == 0x0050 or # P
2489                 $self->{next_char} == 0x0070) { # p                 $self->{nc} == 0x0070) { # p
2490            $self->{state} = PUBLIC_STATE;
2491            $self->{s_kwd} = chr $self->{nc};
2492          !!!next-input-character;          !!!next-input-character;
2493          if ($self->{next_char} == 0x0055 or # U          redo A;
2494              $self->{next_char} == 0x0075) { # u        } elsif ($self->{nc} == 0x0053 or # S
2495            !!!next-input-character;                 $self->{nc} == 0x0073) { # s
2496            if ($self->{next_char} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2497                $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  
2498          !!!next-input-character;          !!!next-input-character;
2499          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);  
         }  
   
         #  
2500        } else {        } else {
2501          !!!cp (180);          !!!cp (180);
2502            !!!parse-error (type => 'string after DOCTYPE name');
2503            $self->{ct}->{quirks} = 1;
2504    
2505            $self->{state} = BOGUS_DOCTYPE_STATE;
2506          !!!next-input-character;          !!!next-input-character;
2507          #          redo A;
2508        }        }
2509        } elsif ($self->{state} == PUBLIC_STATE) {
2510          ## ASCII case-insensitive
2511          if ($self->{nc} == [
2512                undef,
2513                0x0055, # U
2514                0x0042, # B
2515                0x004C, # L
2516                0x0049, # I
2517              ]->[length $self->{s_kwd}] or
2518              $self->{nc} == [
2519                undef,
2520                0x0075, # u
2521                0x0062, # b
2522                0x006C, # l
2523                0x0069, # i
2524              ]->[length $self->{s_kwd}]) {
2525            !!!cp (175);
2526            ## Stay in the state.
2527            $self->{s_kwd} .= chr $self->{nc};
2528            !!!next-input-character;
2529            redo A;
2530          } elsif ((length $self->{s_kwd}) == 5 and
2531                   ($self->{nc} == 0x0043 or # C
2532                    $self->{nc} == 0x0063)) { # c
2533            !!!cp (168);
2534            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2535            !!!next-input-character;
2536            redo A;
2537          } else {
2538            !!!cp (169);
2539            !!!parse-error (type => 'string after DOCTYPE name',
2540                            line => $self->{line_prev},
2541                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2542            $self->{ct}->{quirks} = 1;
2543    
2544        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2545        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2546            redo A;
2547          }
2548        } elsif ($self->{state} == SYSTEM_STATE) {
2549          ## ASCII case-insensitive
2550          if ($self->{nc} == [
2551                undef,
2552                0x0059, # Y
2553                0x0053, # S
2554                0x0054, # T
2555                0x0045, # E
2556              ]->[length $self->{s_kwd}] or
2557              $self->{nc} == [
2558                undef,
2559                0x0079, # y
2560                0x0073, # s
2561                0x0074, # t
2562                0x0065, # e
2563              ]->[length $self->{s_kwd}]) {
2564            !!!cp (170);
2565            ## Stay in the state.
2566            $self->{s_kwd} .= chr $self->{nc};
2567            !!!next-input-character;
2568            redo A;
2569          } elsif ((length $self->{s_kwd}) == 5 and
2570                   ($self->{nc} == 0x004D or # M
2571                    $self->{nc} == 0x006D)) { # m
2572            !!!cp (171);
2573            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2574            !!!next-input-character;
2575            redo A;
2576          } else {
2577            !!!cp (172);
2578            !!!parse-error (type => 'string after DOCTYPE name',
2579                            line => $self->{line_prev},
2580                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2581            $self->{ct}->{quirks} = 1;
2582    
2583        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2584        # next-input-character is already done          ## Reconsume.
2585        redo A;          redo A;
2586          }
2587      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2588        if ({        if ({
2589              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2590              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2591            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2592          !!!cp (181);          !!!cp (181);
2593          ## Stay in the state          ## Stay in the state
2594          !!!next-input-character;          !!!next-input-character;
2595          redo A;          redo A;
2596        } elsif ($self->{next_char} eq 0x0022) { # "        } elsif ($self->{nc} eq 0x0022) { # "
2597          !!!cp (182);          !!!cp (182);
2598          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2599          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2600          !!!next-input-character;          !!!next-input-character;
2601          redo A;          redo A;
2602        } elsif ($self->{next_char} eq 0x0027) { # '        } elsif ($self->{nc} eq 0x0027) { # '
2603          !!!cp (183);          !!!cp (183);
2604          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2605          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2606          !!!next-input-character;          !!!next-input-character;
2607          redo A;          redo A;
2608        } elsif ($self->{next_char} eq 0x003E) { # >        } elsif ($self->{nc} eq 0x003E) { # >
2609          !!!cp (184);          !!!cp (184);
2610          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2611    
2612          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2613          !!!next-input-character;          !!!next-input-character;
2614    
2615          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2616          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2617    
2618          redo A;          redo A;
2619        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2620          !!!cp (185);          !!!cp (185);
2621          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2622    
2623          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2624          ## reconsume          ## reconsume
2625    
2626          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2627          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2628    
2629          redo A;          redo A;
2630        } else {        } else {
2631          !!!cp (186);          !!!cp (186);
2632          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2633          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2634    
2635          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2636          !!!next-input-character;          !!!next-input-character;
2637          redo A;          redo A;
2638        }        }
2639      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2640        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2641          !!!cp (187);          !!!cp (187);
2642          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2643          !!!next-input-character;          !!!next-input-character;
2644          redo A;          redo A;
2645        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2646          !!!cp (188);          !!!cp (188);
2647          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2648    
2649          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2650          !!!next-input-character;          !!!next-input-character;
2651    
2652          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2653          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2654    
2655          redo A;          redo A;
2656        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2657          !!!cp (189);          !!!cp (189);
2658          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2659    
2660          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2661          ## reconsume          ## reconsume
2662    
2663          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2664          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2665    
2666          redo A;          redo A;
2667        } else {        } else {
2668          !!!cp (190);          !!!cp (190);
2669          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2670              .= chr $self->{next_char};              .= chr $self->{nc};
2671            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2672                                  length $self->{ct}->{pubid});
2673    
2674          ## Stay in the state          ## Stay in the state
2675          !!!next-input-character;          !!!next-input-character;
2676          redo A;          redo A;
2677        }        }
2678      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2679        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2680          !!!cp (191);          !!!cp (191);
2681          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2682          !!!next-input-character;          !!!next-input-character;
2683          redo A;          redo A;
2684        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2685          !!!cp (192);          !!!cp (192);
2686          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2687    
2688          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2689          !!!next-input-character;          !!!next-input-character;
2690    
2691          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2692          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2693    
2694          redo A;          redo A;
2695        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2696          !!!cp (193);          !!!cp (193);
2697          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2698    
2699          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2700          ## reconsume          ## reconsume
2701    
2702          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2703          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2704    
2705          redo A;          redo A;
2706        } else {        } else {
2707          !!!cp (194);          !!!cp (194);
2708          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2709              .= chr $self->{next_char};              .= chr $self->{nc};
2710            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2711                                  length $self->{ct}->{pubid});
2712    
2713          ## Stay in the state          ## Stay in the state
2714          !!!next-input-character;          !!!next-input-character;
2715          redo A;          redo A;
# Line 1963  sub _get_next_token ($) { Line 2718  sub _get_next_token ($) {
2718        if ({        if ({
2719              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2720              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2721            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2722          !!!cp (195);          !!!cp (195);
2723          ## Stay in the state          ## Stay in the state
2724          !!!next-input-character;          !!!next-input-character;
2725          redo A;          redo A;
2726        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2727          !!!cp (196);          !!!cp (196);
2728          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2729          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2730          !!!next-input-character;          !!!next-input-character;
2731          redo A;          redo A;
2732        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2733          !!!cp (197);          !!!cp (197);
2734          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2735          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2736          !!!next-input-character;          !!!next-input-character;
2737          redo A;          redo A;
2738        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2739          !!!cp (198);          !!!cp (198);
2740          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2741          !!!next-input-character;          !!!next-input-character;
2742    
2743          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2744    
2745          redo A;          redo A;
2746        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2747          !!!cp (199);          !!!cp (199);
2748          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2749    
2750          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2751          ## reconsume          ## reconsume
2752    
2753          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2754          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2755    
2756          redo A;          redo A;
2757        } else {        } else {
2758          !!!cp (200);          !!!cp (200);
2759          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2760          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2761    
2762          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2763          !!!next-input-character;          !!!next-input-character;
# Line 2012  sub _get_next_token ($) { Line 2767  sub _get_next_token ($) {
2767        if ({        if ({
2768              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2769              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2770            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2771          !!!cp (201);          !!!cp (201);
2772          ## Stay in the state          ## Stay in the state
2773          !!!next-input-character;          !!!next-input-character;
2774          redo A;          redo A;
2775        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2776          !!!cp (202);          !!!cp (202);
2777          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2778          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2779          !!!next-input-character;          !!!next-input-character;
2780          redo A;          redo A;
2781        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2782          !!!cp (203);          !!!cp (203);
2783          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2784          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2785          !!!next-input-character;          !!!next-input-character;
2786          redo A;          redo A;
2787        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2788          !!!cp (204);          !!!cp (204);
2789          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2790          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2791          !!!next-input-character;          !!!next-input-character;
2792    
2793          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2794          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2795    
2796          redo A;          redo A;
2797        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2798          !!!cp (205);          !!!cp (205);
2799          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2800    
2801          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2802          ## reconsume          ## reconsume
2803    
2804          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2805          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2806    
2807          redo A;          redo A;
2808        } else {        } else {
2809          !!!cp (206);          !!!cp (206);
2810          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2811          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2812    
2813          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2814          !!!next-input-character;          !!!next-input-character;
2815          redo A;          redo A;
2816        }        }
2817      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2818        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2819          !!!cp (207);          !!!cp (207);
2820          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2821          !!!next-input-character;          !!!next-input-character;
2822          redo A;          redo A;
2823        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2824          !!!cp (208);          !!!cp (208);
2825          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2826    
2827          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2828          !!!next-input-character;          !!!next-input-character;
2829    
2830          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2831          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2832    
2833          redo A;          redo A;
2834        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2835          !!!cp (209);          !!!cp (209);
2836          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2837    
2838          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2839          ## reconsume          ## reconsume
2840    
2841          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2842          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2843    
2844          redo A;          redo A;
2845        } else {        } else {
2846          !!!cp (210);          !!!cp (210);
2847          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2848              .= chr $self->{next_char};              .= chr $self->{nc};
2849            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2850                                  length $self->{ct}->{sysid});
2851    
2852          ## Stay in the state          ## Stay in the state
2853          !!!next-input-character;          !!!next-input-character;
2854          redo A;          redo A;
2855        }        }
2856      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2857        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2858          !!!cp (211);          !!!cp (211);
2859          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2860          !!!next-input-character;          !!!next-input-character;
2861          redo A;          redo A;
2862        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2863          !!!cp (212);          !!!cp (212);
2864          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2865    
2866          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2867          !!!next-input-character;          !!!next-input-character;
2868    
2869          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2870          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2871    
2872          redo A;          redo A;
2873        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2874          !!!cp (213);          !!!cp (213);
2875          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2876    
2877          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2878          ## reconsume          ## reconsume
2879    
2880          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2881          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2882    
2883          redo A;          redo A;
2884        } else {        } else {
2885          !!!cp (214);          !!!cp (214);
2886          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2887              .= chr $self->{next_char};              .= chr $self->{nc};
2888            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2889                                  length $self->{ct}->{sysid});
2890    
2891          ## Stay in the state          ## Stay in the state
2892          !!!next-input-character;          !!!next-input-character;
2893          redo A;          redo A;
# Line 2135  sub _get_next_token ($) { Line 2896  sub _get_next_token ($) {
2896        if ({        if ({
2897              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
2898              #0x000D => 1, # HT, LF, VT, FF, SP, CR              #0x000D => 1, # HT, LF, VT, FF, SP, CR
2899            }->{$self->{next_char}}) {            }->{$self->{nc}}) {
2900          !!!cp (215);          !!!cp (215);
2901          ## Stay in the state          ## Stay in the state
2902          !!!next-input-character;          !!!next-input-character;
2903          redo A;          redo A;
2904        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2905          !!!cp (216);          !!!cp (216);
2906          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2907          !!!next-input-character;          !!!next-input-character;
2908    
2909          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2910    
2911          redo A;          redo A;
2912        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2913          !!!cp (217);          !!!cp (217);
2914          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2915          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2916          ## reconsume          ## reconsume
2917    
2918          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2919          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2920    
2921          redo A;          redo A;
2922        } else {        } else {
2923          !!!cp (218);          !!!cp (218);
2924          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2925          #$self->{current_token}->{quirks} = 1;          #$self->{ct}->{quirks} = 1;
2926    
2927          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2928          !!!next-input-character;          !!!next-input-character;
2929          redo A;          redo A;
2930        }        }
2931      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2932        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2933          !!!cp (219);          !!!cp (219);
2934          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2935          !!!next-input-character;          !!!next-input-character;
2936    
2937          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2938    
2939          redo A;          redo A;
2940        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2941          !!!cp (220);          !!!cp (220);
2942          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2943          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2944          ## reconsume          ## reconsume
2945    
2946          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2947    
2948          redo A;          redo A;
2949        } else {        } else {
2950          !!!cp (221);          !!!cp (221);
2951            my $s = '';
2952            $self->{read_until}->($s, q[>], 0);
2953    
2954          ## Stay in the state          ## Stay in the state
2955          !!!next-input-character;          !!!next-input-character;
2956          redo A;          redo A;
2957        }        }
2958      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2959        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2960      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2961    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2962          
2963    die "$0: _get_next_token: unexpected case";        if ($self->{nc} == 0x005D) { # ]
2964  } # _get_next_token          !!!cp (221.1);
2965            $self->{state} = CDATA_SECTION_MSE1_STATE;
2966            !!!next-input-character;
2967            redo A;
2968          } elsif ($self->{nc} == -1) {
2969            $self->{state} = DATA_STATE;
2970            !!!next-input-character;
2971            if (length $self->{ct}->{data}) { # character
2972              !!!cp (221.2);
2973              !!!emit ($self->{ct}); # character
2974            } else {
2975              !!!cp (221.3);
2976              ## No token to emit. $self->{ct} is discarded.
2977            }        
2978            redo A;
2979          } else {
2980            !!!cp (221.4);
2981            $self->{ct}->{data} .= chr $self->{nc};
2982            $self->{read_until}->($self->{ct}->{data},
2983                                  q<]>,
2984                                  length $self->{ct}->{data});
2985    
2986  sub _tokenize_attempt_to_consume_an_entity ($$$) {          ## Stay in the state.
2987    my ($self, $in_attr, $additional) = @_;          !!!next-input-character;
2988            redo A;
2989          }
2990    
2991    if ({        ## ISSUE: "text tokens" in spec.
2992         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2993         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{nc} == 0x005D) { # ]
2994         $additional => 1,          !!!cp (221.5);
2995        }->{$self->{next_char}}) {          $self->{state} = CDATA_SECTION_MSE2_STATE;
2996      !!!cp (1001);          !!!next-input-character;
2997      ## Don't consume          redo A;
2998      ## No error        } else {
2999      return undef;          !!!cp (221.6);
3000    } elsif ($self->{next_char} == 0x0023) { # #          $self->{ct}->{data} .= ']';
3001      !!!next-input-character;          $self->{state} = CDATA_SECTION_STATE;
3002      if ($self->{next_char} == 0x0078 or # x          ## Reconsume.
3003          $self->{next_char} == 0x0058) { # X          redo A;
3004        my $code;        }
3005        X: {      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3006          my $x_char = $self->{next_char};        if ($self->{nc} == 0x003E) { # >
3007          !!!next-input-character;          $self->{state} = DATA_STATE;
3008          if (0x0030 <= $self->{next_char} and          !!!next-input-character;
3009              $self->{next_char} <= 0x0039) { # 0..9          if (length $self->{ct}->{data}) { # character
3010            !!!cp (1002);            !!!cp (221.7);
3011            $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;  
3012          } else {          } else {
3013            !!!cp (1007);            !!!cp (221.8);
3014            !!!parse-error (type => 'no refc');            ## No token to emit. $self->{ct} is discarded.
3015          }          }
3016            redo A;
3017          } elsif ($self->{nc} == 0x005D) { # ]
3018            !!!cp (221.9); # character
3019            $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3020            ## Stay in the state.
3021            !!!next-input-character;
3022            redo A;
3023          } else {
3024            !!!cp (221.11);
3025            $self->{ct}->{data} .= ']]'; # character
3026            $self->{state} = CDATA_SECTION_STATE;
3027            ## Reconsume.
3028            redo A;
3029          }
3030        } elsif ($self->{state} == ENTITY_STATE) {
3031          if ({
3032            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3033            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3034            $self->{entity_add} => 1,
3035          }->{$self->{nc}}) {
3036            !!!cp (1001);
3037            ## Don't consume
3038            ## No error
3039            ## Return nothing.
3040            #
3041          } elsif ($self->{nc} == 0x0023) { # #
3042            !!!cp (999);
3043            $self->{state} = ENTITY_HASH_STATE;
3044            $self->{s_kwd} = '#';
3045            !!!next-input-character;
3046            redo A;
3047          } elsif ((0x0041 <= $self->{nc} and
3048                    $self->{nc} <= 0x005A) or # A..Z
3049                   (0x0061 <= $self->{nc} and
3050                    $self->{nc} <= 0x007A)) { # a..z
3051            !!!cp (998);
3052            require Whatpm::_NamedEntityList;
3053            $self->{state} = ENTITY_NAME_STATE;
3054            $self->{s_kwd} = chr $self->{nc};
3055            $self->{entity__value} = $self->{s_kwd};
3056            $self->{entity__match} = 0;
3057            !!!next-input-character;
3058            redo A;
3059          } else {
3060            !!!cp (1027);
3061            !!!parse-error (type => 'bare ero');
3062            ## Return nothing.
3063            #
3064          }
3065    
3066          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3067            !!!cp (1008);        ## reference" algorithm.  In other word, there is an "&" character
3068            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## that does not introduce a character reference, which would be
3069            $code = 0xFFFD;        ## appended to the parent element or the attribute value in later
3070          } elsif ($code > 0x10FFFF) {        ## process of the tokenizer.
3071            !!!cp (1009);  
3072            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        if ($self->{prev_state} == DATA_STATE) {
3073            $code = 0xFFFD;          !!!cp (997);
3074          } elsif ($code == 0x000D) {          $self->{state} = $self->{prev_state};
3075            !!!cp (1010);          ## Reconsume.
3076            !!!parse-error (type => 'CR character reference');          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3077            $code = 0x000A;                    line => $self->{line_prev},
3078          } elsif (0x80 <= $code and $code <= 0x9F) {                    column => $self->{column_prev},
3079            !!!cp (1011);                   });
3080            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          redo A;
3081            $code = $c1_entity_char->{$code};        } else {
3082          }          !!!cp (996);
3083            $self->{ca}->{value} .= '&';
3084          return {type => CHARACTER_TOKEN, data => chr $code,          $self->{state} = $self->{prev_state};
3085                  has_reference => 1};          ## Reconsume.
3086        } # X          redo A;
3087      } elsif (0x0030 <= $self->{next_char} and        }
3088               $self->{next_char} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3089        my $code = $self->{next_char} - 0x0030;        if ($self->{nc} == 0x0078 or # x
3090        !!!next-input-character;            $self->{nc} == 0x0058) { # X
3091                  !!!cp (995);
3092        while (0x0030 <= $self->{next_char} and          $self->{state} = HEXREF_X_STATE;
3093                  $self->{next_char} <= 0x0039) { # 0..9          $self->{s_kwd} .= chr $self->{nc};
3094            !!!next-input-character;
3095            redo A;
3096          } elsif (0x0030 <= $self->{nc} and
3097                   $self->{nc} <= 0x0039) { # 0..9
3098            !!!cp (994);
3099            $self->{state} = NCR_NUM_STATE;
3100            $self->{s_kwd} = $self->{nc} - 0x0030;
3101            !!!next-input-character;
3102            redo A;
3103          } else {
3104            !!!parse-error (type => 'bare nero',
3105                            line => $self->{line_prev},
3106                            column => $self->{column_prev} - 1);
3107    
3108            ## NOTE: According to the spec algorithm, nothing is returned,
3109            ## and then "&#" is appended to the parent element or the attribute
3110            ## value in the later processing.
3111    
3112            if ($self->{prev_state} == DATA_STATE) {
3113              !!!cp (1019);
3114              $self->{state} = $self->{prev_state};
3115              ## Reconsume.
3116              !!!emit ({type => CHARACTER_TOKEN,
3117                        data => '&#',
3118                        line => $self->{line_prev},
3119                        column => $self->{column_prev} - 1,
3120                       });
3121              redo A;
3122            } else {
3123              !!!cp (993);
3124              $self->{ca}->{value} .= '&#';
3125              $self->{state} = $self->{prev_state};
3126              ## Reconsume.
3127              redo A;
3128            }
3129          }
3130        } elsif ($self->{state} == NCR_NUM_STATE) {
3131          if (0x0030 <= $self->{nc} and
3132              $self->{nc} <= 0x0039) { # 0..9
3133          !!!cp (1012);          !!!cp (1012);
3134          $code *= 10;          $self->{s_kwd} *= 10;
3135          $code += $self->{next_char} - 0x0030;          $self->{s_kwd} += $self->{nc} - 0x0030;
3136                    
3137            ## Stay in the state.
3138          !!!next-input-character;          !!!next-input-character;
3139        }          redo A;
3140          } elsif ($self->{nc} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3141          !!!cp (1013);          !!!cp (1013);
3142          !!!next-input-character;          !!!next-input-character;
3143            #
3144        } else {        } else {
3145          !!!cp (1014);          !!!cp (1014);
3146          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3147            ## Reconsume.
3148            #
3149        }        }
3150    
3151          my $code = $self->{s_kwd};
3152          my $l = $self->{line_prev};
3153          my $c = $self->{column_prev};
3154        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3155          !!!cp (1015);          !!!cp (1015);
3156          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
3157                            text => (sprintf 'U+%04X', $code),
3158                            line => $l, column => $c);
3159          $code = 0xFFFD;          $code = 0xFFFD;
3160        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3161          !!!cp (1016);          !!!cp (1016);
3162          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
3163                            text => (sprintf 'U-%08X', $code),
3164                            line => $l, column => $c);
3165          $code = 0xFFFD;          $code = 0xFFFD;
3166        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3167          !!!cp (1017);          !!!cp (1017);
3168          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
3169                            line => $l, column => $c);
3170          $code = 0x000A;          $code = 0x000A;
3171        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3172          !!!cp (1018);          !!!cp (1018);
3173          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3174                            text => (sprintf 'U+%04X', $code),
3175                            line => $l, column => $c);
3176          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3177        }        }
3178          
3179        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        if ($self->{prev_state} == DATA_STATE) {
3180      } else {          !!!cp (992);
3181        !!!cp (1019);          $self->{state} = $self->{prev_state};
3182        !!!parse-error (type => 'bare nero');          ## Reconsume.
3183        !!!back-next-input-character ($self->{next_char});          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3184        $self->{next_char} = 0x0023; # #                    line => $l, column => $c,
3185        return undef;                   });
3186      }          redo A;
3187    } elsif ((0x0041 <= $self->{next_char} and        } else {
3188              $self->{next_char} <= 0x005A) or          !!!cp (991);
3189             (0x0061 <= $self->{next_char} and          $self->{ca}->{value} .= chr $code;
3190              $self->{next_char} <= 0x007A)) {          $self->{ca}->{has_reference} = 1;
3191      my $entity_name = chr $self->{next_char};          $self->{state} = $self->{prev_state};
3192      !!!next-input-character;          ## Reconsume.
3193            redo A;
3194      my $value = $entity_name;        }
3195      my $match = 0;      } elsif ($self->{state} == HEXREF_X_STATE) {
3196      require Whatpm::_NamedEntityList;        if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3197      our $EntityChar;            (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3198              (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3199      while (length $entity_name < 10 and          # 0..9, A..F, a..f
3200             ## NOTE: Some number greater than the maximum length of entity name          !!!cp (990);
3201             ((0x0041 <= $self->{next_char} and # a          $self->{state} = HEXREF_HEX_STATE;
3202               $self->{next_char} <= 0x005A) or # x          $self->{s_kwd} = 0;
3203              (0x0061 <= $self->{next_char} and # a          ## Reconsume.
3204               $self->{next_char} <= 0x007A) or # z          redo A;
3205              (0x0030 <= $self->{next_char} and # 0        } else {
3206               $self->{next_char} <= 0x0039) or # 9          !!!parse-error (type => 'bare hcro',
3207              $self->{next_char} == 0x003B)) { # ;                          line => $self->{line_prev},
3208        $entity_name .= chr $self->{next_char};                          column => $self->{column_prev} - 2);
3209        if (defined $EntityChar->{$entity_name}) {  
3210          if ($self->{next_char} == 0x003B) { # ;          ## NOTE: According to the spec algorithm, nothing is returned,
3211            !!!cp (1020);          ## and then "&#" followed by "X" or "x" is appended to the parent
3212            $value = $EntityChar->{$entity_name};          ## element or the attribute value in the later processing.
3213            $match = 1;  
3214            !!!next-input-character;          if ($self->{prev_state} == DATA_STATE) {
3215            last;            !!!cp (1005);
3216              $self->{state} = $self->{prev_state};
3217              ## Reconsume.
3218              !!!emit ({type => CHARACTER_TOKEN,
3219                        data => '&' . $self->{s_kwd},
3220                        line => $self->{line_prev},
3221                        column => $self->{column_prev} - length $self->{s_kwd},
3222                       });
3223              redo A;
3224          } else {          } else {
3225            !!!cp (1021);            !!!cp (989);
3226            $value = $EntityChar->{$entity_name};            $self->{ca}->{value} .= '&' . $self->{s_kwd};
3227            $match = -1;            $self->{state} = $self->{prev_state};
3228            !!!next-input-character;            ## Reconsume.
3229              redo A;
3230          }          }
3231        } else {        }
3232          !!!cp (1022);      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3233          $value .= chr $self->{next_char};        if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3234          $match *= 2;          # 0..9
3235            !!!cp (1002);
3236            $self->{s_kwd} *= 0x10;
3237            $self->{s_kwd} += $self->{nc} - 0x0030;
3238            ## Stay in the state.
3239            !!!next-input-character;
3240            redo A;
3241          } elsif (0x0061 <= $self->{nc} and
3242                   $self->{nc} <= 0x0066) { # a..f
3243            !!!cp (1003);
3244            $self->{s_kwd} *= 0x10;
3245            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3246            ## Stay in the state.
3247            !!!next-input-character;
3248            redo A;
3249          } elsif (0x0041 <= $self->{nc} and
3250                   $self->{nc} <= 0x0046) { # A..F
3251            !!!cp (1004);
3252            $self->{s_kwd} *= 0x10;
3253            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3254            ## Stay in the state.
3255          !!!next-input-character;          !!!next-input-character;
3256            redo A;
3257          } elsif ($self->{nc} == 0x003B) { # ;
3258            !!!cp (1006);
3259            !!!next-input-character;
3260            #
3261          } else {
3262            !!!cp (1007);
3263            !!!parse-error (type => 'no refc',
3264                            line => $self->{line},
3265                            column => $self->{column});
3266            ## Reconsume.
3267            #
3268        }        }
3269      }  
3270              my $code = $self->{s_kwd};
3271      if ($match > 0) {        my $l = $self->{line_prev};
3272        !!!cp (1023);        my $c = $self->{column_prev};
3273        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3274      } elsif ($match < 0) {          !!!cp (1008);
3275        !!!parse-error (type => 'no refc');          !!!parse-error (type => 'invalid character reference',
3276        if ($in_attr and $match < -1) {                          text => (sprintf 'U+%04X', $code),
3277          !!!cp (1024);                          line => $l, column => $c);
3278          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          $code = 0xFFFD;
3279          } elsif ($code > 0x10FFFF) {
3280            !!!cp (1009);
3281            !!!parse-error (type => 'invalid character reference',
3282                            text => (sprintf 'U-%08X', $code),
3283                            line => $l, column => $c);
3284            $code = 0xFFFD;
3285          } elsif ($code == 0x000D) {
3286            !!!cp (1010);
3287            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3288            $code = 0x000A;
3289          } elsif (0x80 <= $code and $code <= 0x9F) {
3290            !!!cp (1011);
3291            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3292            $code = $c1_entity_char->{$code};
3293          }
3294    
3295          if ($self->{prev_state} == DATA_STATE) {
3296            !!!cp (988);
3297            $self->{state} = $self->{prev_state};
3298            ## Reconsume.
3299            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3300                      line => $l, column => $c,
3301                     });
3302            redo A;
3303          } else {
3304            !!!cp (987);
3305            $self->{ca}->{value} .= chr $code;
3306            $self->{ca}->{has_reference} = 1;
3307            $self->{state} = $self->{prev_state};
3308            ## Reconsume.
3309            redo A;
3310          }
3311        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3312          if (length $self->{s_kwd} < 30 and
3313              ## NOTE: Some number greater than the maximum length of entity name
3314              ((0x0041 <= $self->{nc} and # a
3315                $self->{nc} <= 0x005A) or # x
3316               (0x0061 <= $self->{nc} and # a
3317                $self->{nc} <= 0x007A) or # z
3318               (0x0030 <= $self->{nc} and # 0
3319                $self->{nc} <= 0x0039) or # 9
3320               $self->{nc} == 0x003B)) { # ;
3321            our $EntityChar;
3322            $self->{s_kwd} .= chr $self->{nc};
3323            if (defined $EntityChar->{$self->{s_kwd}}) {
3324              if ($self->{nc} == 0x003B) { # ;
3325                !!!cp (1020);
3326                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3327                $self->{entity__match} = 1;
3328                !!!next-input-character;
3329                #
3330              } else {
3331                !!!cp (1021);
3332                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3333                $self->{entity__match} = -1;
3334                ## Stay in the state.
3335                !!!next-input-character;
3336                redo A;
3337              }
3338            } else {
3339              !!!cp (1022);
3340              $self->{entity__value} .= chr $self->{nc};
3341              $self->{entity__match} *= 2;
3342              ## Stay in the state.
3343              !!!next-input-character;
3344              redo A;
3345            }
3346          }
3347    
3348          my $data;
3349          my $has_ref;
3350          if ($self->{entity__match} > 0) {
3351            !!!cp (1023);
3352            $data = $self->{entity__value};
3353            $has_ref = 1;
3354            #
3355          } elsif ($self->{entity__match} < 0) {
3356            !!!parse-error (type => 'no refc');
3357            if ($self->{prev_state} != DATA_STATE and # in attribute
3358                $self->{entity__match} < -1) {
3359              !!!cp (1024);
3360              $data = '&' . $self->{s_kwd};
3361              #
3362            } else {
3363              !!!cp (1025);
3364              $data = $self->{entity__value};
3365              $has_ref = 1;
3366              #
3367            }
3368        } else {        } else {
3369          !!!cp (1025);          !!!cp (1026);
3370          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          !!!parse-error (type => 'bare ero',
3371                            line => $self->{line_prev},
3372                            column => $self->{column_prev} - length $self->{s_kwd});
3373            $data = '&' . $self->{s_kwd};
3374            #
3375          }
3376      
3377          ## NOTE: In these cases, when a character reference is found,
3378          ## it is consumed and a character token is returned, or, otherwise,
3379          ## nothing is consumed and returned, according to the spec algorithm.
3380          ## In this implementation, anything that has been examined by the
3381          ## tokenizer is appended to the parent element or the attribute value
3382          ## as string, either literal string when no character reference or
3383          ## entity-replaced string otherwise, in this stage, since any characters
3384          ## that would not be consumed are appended in the data state or in an
3385          ## appropriate attribute value state anyway.
3386    
3387          if ($self->{prev_state} == DATA_STATE) {
3388            !!!cp (986);
3389            $self->{state} = $self->{prev_state};
3390            ## Reconsume.
3391            !!!emit ({type => CHARACTER_TOKEN,
3392                      data => $data,
3393                      line => $self->{line_prev},
3394                      column => $self->{column_prev} + 1 - length $self->{s_kwd},
3395                     });
3396            redo A;
3397          } else {
3398            !!!cp (985);
3399            $self->{ca}->{value} .= $data;
3400            $self->{ca}->{has_reference} = 1 if $has_ref;
3401            $self->{state} = $self->{prev_state};
3402            ## Reconsume.
3403            redo A;
3404        }        }
3405      } else {      } else {
3406        !!!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};  
3407      }      }
3408    } else {    } # A  
3409      !!!cp (1027);  
3410      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3411      !!!parse-error (type => 'bare ero');  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3412    
3413  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3414    my $self = shift;    my $self = shift;
# Line 2400  sub _initialize_tree_constructor ($) { Line 3417  sub _initialize_tree_constructor ($) {
3417    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3418    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3419    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3420      $self->{document}->set_user_data (manakai_source_line => 1);
3421      $self->{document}->set_user_data (manakai_source_column => 1);
3422  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3423    
3424  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2454  sub _tree_construction_initial ($) { Line 3473  sub _tree_construction_initial ($) {
3473        ## language.        ## language.
3474        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3475        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3476        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3477        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3478            defined $token->{public_identifier} or            defined $token->{sysid}) {
           defined $token->{system_identifier}) {  
3479          !!!cp ('t1');          !!!cp ('t1');
3480          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3481        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3482          !!!cp ('t2');          !!!cp ('t2');
3483          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!parse-error (type => 'not HTML5', token => $token);
3484          !!!parse-error (type => 'not HTML5');        } elsif (defined $token->{pubid}) {
3485            if ($token->{pubid} eq 'XSLT-compat') {
3486              !!!cp ('t1.2');
3487              !!!parse-error (type => 'XSLT-compat', token => $token,
3488                              level => $self->{level}->{should});
3489            } else {
3490              !!!parse-error (type => 'not HTML5', token => $token);
3491            }
3492        } else {        } else {
3493          !!!cp ('t3');          !!!cp ('t3');
3494            #
3495        }        }
3496                
3497        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3498          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3499        $doctype->public_id ($token->{public_identifier})        ## NOTE: Default value for both |public_id| and |system_id| attributes
3500            if defined $token->{public_identifier};        ## are empty strings, so that we don't set any value in missing cases.
3501        $doctype->system_id ($token->{system_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3502            if defined $token->{system_identifier};        $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
3503        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
3504        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3505        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
# Line 2481  sub _tree_construction_initial ($) { Line 3507  sub _tree_construction_initial ($) {
3507        if ($token->{quirks} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3508          !!!cp ('t4');          !!!cp ('t4');
3509          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3510        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
3511          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
3512          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3513          if ({          my $prefix = [
3514            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3515            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3516            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3517            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3518            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3519            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3520            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3521            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3522            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3523            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3524            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3525            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3526            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3527            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3528            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3529            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3530            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3531            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3532            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3533            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3534            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3535            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3536            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3537            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3538            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3539            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3540            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3541            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3542            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3543            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3544            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3545            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3546            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3547            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3548            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3549            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3550            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3551            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3552            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3553            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3554            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3555            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3556            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3557            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3558            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3559            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3560            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3561            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3562            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3563            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3564            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3565            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3566            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3567            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3568            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3569            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3570            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3571            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3572            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3573            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3574            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3575            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3576            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3577            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3578            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3579            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3580            "-//W3O//DTD W3 HTML 3.0//EN//" => 1,              $pubid eq "HTML") {
           "-//W3O//DTD W3 HTML STRICT 3.0//EN//" => 1,  
           "-//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}) {  
3581            !!!cp ('t5');            !!!cp ('t5');
3582            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3583          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3584                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3585            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
3586              !!!cp ('t6');              !!!cp ('t6');
3587              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3588            } else {            } else {
3589              !!!cp ('t7');              !!!cp ('t7');
3590              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3591            }            }
3592          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3593                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3594            !!!cp ('t8');            !!!cp ('t8');
3595            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3596          } else {          } else {
# Line 2579  sub _tree_construction_initial ($) { Line 3599  sub _tree_construction_initial ($) {
3599        } else {        } else {
3600          !!!cp ('t10');          !!!cp ('t10');
3601        }        }
3602        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
3603          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
3604          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3605          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") {
3606            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3607              ## marked as quirks.
3608            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3609            !!!cp ('t11');            !!!cp ('t11');
3610          } else {          } else {
# Line 2602  sub _tree_construction_initial ($) { Line 3623  sub _tree_construction_initial ($) {
3623                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3624               }->{$token->{type}}) {               }->{$token->{type}}) {
3625        !!!cp ('t14');        !!!cp ('t14');
3626        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3627        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3628        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3629        ## reprocess        ## reprocess
3630          !!!ack-later;
3631        return;        return;
3632      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3633        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2623  sub _tree_construction_initial ($) { Line 3645  sub _tree_construction_initial ($) {
3645          !!!cp ('t17');          !!!cp ('t17');
3646        }        }
3647    
3648        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3649        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3650        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3651        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3674  sub _tree_construction_root_element ($)
3674    B: {    B: {
3675        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3676          !!!cp ('t19');          !!!cp ('t19');
3677          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3678          ## Ignore the token          ## Ignore the token
3679          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3680          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3708  sub _tree_construction_root_element ($)
3708        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3709          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3710            my $root_element;            my $root_element;
3711            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3712            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3713            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3714                  [$root_element, $el_category->{html}];
3715    
3716            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3717              !!!cp ('t24');              !!!cp ('t24');
3718              $self->{application_cache_selection}              $self->{application_cache_selection}
3719                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3720              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3721                ## According to Hixie (#whatwg 2008-03-19), it should be
3722                ## resolved against the base URI of the document in HTML
3723                ## or xml:base of the element in XHTML.
3724            } else {            } else {
3725              !!!cp ('t25');              !!!cp ('t25');
3726              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3727            }            }
3728    
3729              !!!nack ('t25c');
3730    
3731            !!!next-token;            !!!next-token;
3732            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3733          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3744  sub _tree_construction_root_element ($)
3744          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3745        }        }
3746    
3747      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3748        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3749      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3750      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3751    
3752      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3753    
3754      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3755        !!!ack-later;
3756      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3757    
3758      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 3773  sub _reset_insertion_mode ($) {
3773            
3774      ## Step 3      ## Step 3
3775      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"!?  
3776        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3777          $last = 1;          $last = 1;
3778          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3779            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3780                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3781              !!!cp ('t27');          } else {
3782              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3783          }          }
3784        }        }
3785              
3786        ## Step 4..13        ## Step 4..14
3787        my $new_mode = {        my $new_mode;
3788          if ($node->[1] & FOREIGN_EL) {
3789            !!!cp ('t28.1');
3790            ## NOTE: Strictly spaking, the line below only applies to MathML and
3791            ## SVG elements.  Currently the HTML syntax supports only MathML and
3792            ## SVG elements as foreigners.
3793            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3794          } elsif ($node->[1] & TABLE_CELL_EL) {
3795            if ($last) {
3796              !!!cp ('t28.2');
3797              #
3798            } else {
3799              !!!cp ('t28.3');
3800              $new_mode = IN_CELL_IM;
3801            }
3802          } else {
3803            !!!cp ('t28.4');
3804            $new_mode = {
3805                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3806                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3807                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3808                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3809                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3810                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2779  sub _reset_insertion_mode ($) { Line 3815  sub _reset_insertion_mode ($) {
3815                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3816                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3817                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3818                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3819          }
3820        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3821                
3822        ## Step 14        ## Step 15
3823        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3824          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3825            !!!cp ('t29');            !!!cp ('t29');
3826            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 3834  sub _reset_insertion_mode ($) {
3834          !!!cp ('t31');          !!!cp ('t31');
3835        }        }
3836                
3837        ## Step 15        ## Step 16
3838        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3839                
3840        ## Step 16        ## Step 17
3841        $i--;        $i--;
3842        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3843                
3844        ## Step 17        ## Step 18
3845        redo S3;        redo S3;
3846      } # S3      } # S3
3847    
# Line 2908  sub _tree_construction_main ($) { Line 3945  sub _tree_construction_main ($) {
3945      !!!cp ('t39');      !!!cp ('t39');
3946    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3947    
3948    my $parse_rcdata = sub ($$) {    my $insert;
3949      my ($content_model_flag, $insert) = @_;  
3950      my $parse_rcdata = sub ($) {
3951        my ($content_model_flag) = @_;
3952    
3953      ## Step 1      ## Step 1
3954      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3955      my $el;      my $el;
3956      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3957    
3958      ## Step 2      ## Step 2
3959      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3960    
3961      ## Step 3      ## Step 3
3962      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2925  sub _tree_construction_main ($) { Line 3964  sub _tree_construction_main ($) {
3964    
3965      ## Step 4      ## Step 4
3966      my $text = '';      my $text = '';
3967        !!!nack ('t40.1');
3968      !!!next-token;      !!!next-token;
3969      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3970        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3987  sub _tree_construction_main ($) {
3987          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3988        !!!cp ('t42');        !!!cp ('t42');
3989        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!cp ('t43');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!cp ('t44');  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3990      } else {      } else {
3991        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3992          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3993            !!!cp ('t43');
3994            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3995          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3996            !!!cp ('t44');
3997            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3998          } else {
3999            die "$0: $content_model_flag in parse_rcdata";
4000          }
4001      }      }
4002      !!!next-token;      !!!next-token;
4003    }; # $parse_rcdata    }; # $parse_rcdata
4004    
4005    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
4006      my $script_el;      my $script_el;
4007      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4008      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4009    
4010      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
4011      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4012            
4013      my $text = '';      my $text = '';
4014        !!!nack ('t45.1');
4015      !!!next-token;      !!!next-token;
4016      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
4017        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 4031  sub _tree_construction_main ($) {
4031        ## Ignore the token        ## Ignore the token
4032      } else {      } else {
4033        !!!cp ('t48');        !!!cp ('t48');
4034        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#eof', token => $token);
4035        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4036        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4037      }      }
# Line 3011  sub _tree_construction_main ($) { Line 4054  sub _tree_construction_main ($) {
4054      !!!next-token;      !!!next-token;
4055    }; # $script_start_tag    }; # $script_start_tag
4056    
4057      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4058      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4059      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4060    
4061    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4062      my $tag_name = shift;      my $end_tag_token = shift;
4063        my $tag_name = $end_tag_token->{tag_name};
4064    
4065        ## NOTE: The adoption agency algorithm (AAA).
4066    
4067      FET: {      FET: {
4068        ## Step 1        ## Step 1
4069        my $formatting_element;        my $formatting_element;
4070        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4071        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4072          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4073              !!!cp ('t52');
4074              last AFE;
4075            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4076                         eq $tag_name) {
4077            !!!cp ('t51');            !!!cp ('t51');
4078            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4079            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4080            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
4081          }          }
4082        } # AFE        } # AFE
4083        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4084          !!!cp ('t53');          !!!cp ('t53');
4085          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4086          ## Ignore the token          ## Ignore the token
4087          !!!next-token;          !!!next-token;
4088          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 4099  sub _tree_construction_main ($) {
4099              last INSCOPE;              last INSCOPE;
4100            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4101              !!!cp ('t55');              !!!cp ('t55');
4102              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
4103                                text => $token->{tag_name},
4104                                token => $end_tag_token);
4105              ## Ignore the token              ## Ignore the token
4106              !!!next-token;              !!!next-token;
4107              return;              return;
4108            }            }
4109          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4110            !!!cp ('t56');            !!!cp ('t56');
4111            $in_scope = 0;            $in_scope = 0;
4112          }          }
4113        } # INSCOPE        } # INSCOPE
4114        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4115          !!!cp ('t57');          !!!cp ('t57');
4116          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
4117                            text => $token->{tag_name},
4118                            token => $end_tag_token);
4119          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4120          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4121          return;          return;
4122        }        }
4123        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4124          !!!cp ('t58');          !!!cp ('t58');
4125          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
4126                            text => $self->{open_elements}->[-1]->[0]
4127                                ->manakai_local_name,
4128                            token => $end_tag_token);
4129        }        }
4130                
4131        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 4133  sub _tree_construction_main ($) {
4133        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4134        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4135          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4136          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4137              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4138              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4139               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4140            !!!cp ('t59');            !!!cp ('t59');
4141            $furthest_block = $node;            $furthest_block = $node;
4142            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 4222  sub _tree_construction_main ($) {
4222        } # S7          } # S7  
4223                
4224        ## Step 8        ## Step 8
4225        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4226            my $foster_parent_element;
4227            my $next_sibling;
4228            OE: for (reverse 0..$#{$self->{open_elements}}) {
4229              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4230                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4231                                 if (defined $parent and $parent->node_type == 1) {
4232                                   !!!cp ('t65.1');
4233                                   $foster_parent_element = $parent;
4234                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4235                                 } else {
4236                                   !!!cp ('t65.2');
4237                                   $foster_parent_element
4238                                     = $self->{open_elements}->[$_ - 1]->[0];
4239                                 }
4240                                 last OE;
4241                               }
4242                             } # OE
4243                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4244                               unless defined $foster_parent_element;
4245            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4246            $open_tables->[-1]->[1] = 1; # tainted
4247          } else {
4248            !!!cp ('t65.3');
4249            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4250          }
4251                
4252        ## Step 9        ## Step 9
4253        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 4293  sub _tree_construction_main ($) {
4293      } # FET      } # FET
4294    }; # $formatting_end_tag    }; # $formatting_end_tag
4295    
4296    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4297      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4298    }; # $insert_to_current    }; # $insert_to_current
4299    
4300    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4301                         my $child = shift;      my $child = shift;
4302                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4303                              table => 1, tbody => 1, tfoot => 1,        # MUST
4304                              thead => 1, tr => 1,        my $foster_parent_element;
4305                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4306                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4307                           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') {  
4308                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4309                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4310                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 4322  sub _tree_construction_main ($) {
4322                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4323                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4324                             ($child, $next_sibling);                             ($child, $next_sibling);
4325                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4326                           !!!cp ('t72');      } else {
4327                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
4328                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
4329        }
4330    }; # $insert_to_foster    }; # $insert_to_foster
4331    
4332    my $insert;    B: while (1) {
   
   B: {  
4333      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4334        !!!cp ('t73');        !!!cp ('t73');
4335        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4336        ## Ignore the token        ## Ignore the token
4337        ## Stay in the phase        ## Stay in the phase
4338        !!!next-token;        !!!next-token;
4339        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } 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]}) {  
           !!!cp ('t75');  
           !!!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')) {  
           !!!cp ('t76');  
           !!!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') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
4340      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4341               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4342        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4343          !!!cp ('t79');          !!!cp ('t79');
4344          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4345          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4346        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4347          !!!cp ('t80');          !!!cp ('t80');
4348          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4349          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4350        } else {        } else {
4351          !!!cp ('t81');          !!!cp ('t81');
4352        }        }
4353    
4354        !!!cp ('t82');        !!!cp ('t82');
4355        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
4356        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4357        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4358          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3321  sub _tree_construction_main ($) { Line 4362  sub _tree_construction_main ($) {
4362               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4363          }          }
4364        }        }
4365          !!!nack ('t84.1');
4366        !!!next-token;        !!!next-token;
4367        redo B;        next B;
4368      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4369        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4370        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3336  sub _tree_construction_main ($) { Line 4378  sub _tree_construction_main ($) {
4378          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4379        }        }
4380        !!!next-token;        !!!next-token;
4381        redo B;        next B;
4382      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4383          if ($token->{type} == CHARACTER_TOKEN) {
4384            !!!cp ('t87.1');
4385            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4386            !!!next-token;
4387            next B;
4388          } elsif ($token->{type} == START_TAG_TOKEN) {
4389            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4390                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4391                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4392                ($token->{tag_name} eq 'svg' and
4393                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4394              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4395              !!!cp ('t87.2');
4396              #
4397            } elsif ({
4398                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4399                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4400                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4401                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4402                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4403                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4404                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4405                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4406                     }->{$token->{tag_name}}) {
4407              !!!cp ('t87.2');
4408              !!!parse-error (type => 'not closed',
4409                              text => $self->{open_elements}->[-1]->[0]
4410                                  ->manakai_local_name,
4411                              token => $token);
4412    
4413              pop @{$self->{open_elements}}
4414                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4415    
4416              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4417              ## Reprocess.
4418              next B;
4419            } else {
4420              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4421              my $tag_name = $token->{tag_name};
4422              if ($nsuri eq $SVG_NS) {
4423                $tag_name = {
4424                   altglyph => 'altGlyph',
4425                   altglyphdef => 'altGlyphDef',
4426                   altglyphitem => 'altGlyphItem',
4427                   animatecolor => 'animateColor',
4428                   animatemotion => 'animateMotion',
4429                   animatetransform => 'animateTransform',
4430                   clippath => 'clipPath',
4431                   feblend => 'feBlend',
4432                   fecolormatrix => 'feColorMatrix',
4433                   fecomponenttransfer => 'feComponentTransfer',
4434                   fecomposite => 'feComposite',
4435                   feconvolvematrix => 'feConvolveMatrix',
4436                   fediffuselighting => 'feDiffuseLighting',
4437                   fedisplacementmap => 'feDisplacementMap',
4438                   fedistantlight => 'feDistantLight',
4439                   feflood => 'feFlood',
4440                   fefunca => 'feFuncA',
4441                   fefuncb => 'feFuncB',
4442                   fefuncg => 'feFuncG',
4443                   fefuncr => 'feFuncR',
4444                   fegaussianblur => 'feGaussianBlur',
4445                   feimage => 'feImage',
4446                   femerge => 'feMerge',
4447                   femergenode => 'feMergeNode',
4448                   femorphology => 'feMorphology',
4449                   feoffset => 'feOffset',
4450                   fepointlight => 'fePointLight',
4451                   fespecularlighting => 'feSpecularLighting',
4452                   fespotlight => 'feSpotLight',
4453                   fetile => 'feTile',
4454                   feturbulence => 'feTurbulence',
4455                   foreignobject => 'foreignObject',
4456                   glyphref => 'glyphRef',
4457                   lineargradient => 'linearGradient',
4458                   radialgradient => 'radialGradient',
4459                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4460                   textpath => 'textPath',  
4461                }->{$tag_name} || $tag_name;
4462              }
4463    
4464              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4465    
4466              ## "adjust foreign attributes" - done in insert-element-f
4467    
4468              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4469    
4470              if ($self->{self_closing}) {
4471                pop @{$self->{open_elements}};
4472                !!!ack ('t87.3');
4473              } else {
4474                !!!cp ('t87.4');
4475              }
4476    
4477              !!!next-token;
4478              next B;
4479            }
4480          } elsif ($token->{type} == END_TAG_TOKEN) {
4481            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4482            !!!cp ('t87.5');
4483            #
4484          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4485            !!!cp ('t87.6');
4486            !!!parse-error (type => 'not closed',
4487                            text => $self->{open_elements}->[-1]->[0]
4488                                ->manakai_local_name,
4489                            token => $token);
4490    
4491            pop @{$self->{open_elements}}
4492                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4493    
4494            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4495            ## Reprocess.
4496            next B;
4497          } else {
4498            die "$0: $token->{type}: Unknown token type";        
4499          }
4500        }
4501    
4502        if ($self->{insertion_mode} & HEAD_IMS) {
4503        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4504          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4505            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4506                !!!cp ('t88.2');
4507                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4508                #
4509              } else {
4510                !!!cp ('t88.1');
4511                ## Ignore the token.
4512                #
4513              }
4514            unless (length $token->{data}) {            unless (length $token->{data}) {
4515              !!!cp ('t88');              !!!cp ('t88');
4516              !!!next-token;              !!!next-token;
4517              redo B;              next B;
4518            }            }
4519    ## TODO: set $token->{column} appropriately
4520          }          }
4521    
4522          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4523            !!!cp ('t89');            !!!cp ('t89');
4524            ## As if <head>            ## As if <head>
4525            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4526            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4527            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4528                  [$self->{head_element}, $el_category->{head}];
4529    
4530            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4531            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3363  sub _tree_construction_main ($) { Line 4535  sub _tree_construction_main ($) {
4535            !!!cp ('t90');            !!!cp ('t90');
4536            ## As if </noscript>            ## As if </noscript>
4537            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4538            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4539                        
4540            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4541            ## As if </head>            ## As if </head>
# Line 3379  sub _tree_construction_main ($) { Line 4551  sub _tree_construction_main ($) {
4551            !!!cp ('t92');            !!!cp ('t92');
4552          }          }
4553    
4554              ## "after head" insertion mode          ## "after head" insertion mode
4555              ## As if <body>          ## As if <body>
4556              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4557              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4558              ## reprocess          ## reprocess
4559              redo B;          next B;
4560            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4561              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4562                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4563                  !!!cp ('t93');              !!!cp ('t93');
4564                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4565                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4566                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4567                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4568                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4569                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4570                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4571                  !!!cp ('t94');              !!!next-token;
4572                  #              next B;
4573                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4574                  !!!cp ('t95');              !!!cp ('t93.2');
4575                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4576                  ## Ignore the token                              token => $token);
4577                  !!!next-token;              ## Ignore the token
4578                  redo B;              !!!nack ('t93.3');
4579                }              !!!next-token;
4580              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4581                !!!cp ('t96');            } else {
4582                ## As if <head>              !!!cp ('t95');
4583                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4584                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4585                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4586                !!!nack ('t95.1');
4587                !!!next-token;
4588                next B;
4589              }
4590            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4591              !!!cp ('t96');
4592              ## As if <head>
4593              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4594              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4595              push @{$self->{open_elements}},
4596                  [$self->{head_element}, $el_category->{head}];
4597    
4598                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4599                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4600              } else {          } else {
4601                !!!cp ('t97');            !!!cp ('t97');
4602              }          }
4603    
4604              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4605                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4606                  !!!cp ('t98');                  !!!cp ('t98');
4607                  ## As if </noscript>                  ## As if </noscript>
4608                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4609                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4610                                    token => $token);
4611                                
4612                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4613                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3434  sub _tree_construction_main ($) { Line 4618  sub _tree_construction_main ($) {
4618                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4619                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4620                  !!!cp ('t100');                  !!!cp ('t100');
4621                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4622                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4623                    push @{$self->{open_elements}},
4624                        [$self->{head_element}, $el_category->{head}];
4625                } else {                } else {
4626                  !!!cp ('t101');                  !!!cp ('t101');
4627                }                }
4628                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4629                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4630                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4631                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4632                  !!!nack ('t101.1');
4633                !!!next-token;                !!!next-token;
4634                redo B;                next B;
4635              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4636                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4637                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4638                  !!!cp ('t102');                  !!!cp ('t102');
4639                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4640                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4641                    push @{$self->{open_elements}},
4642                        [$self->{head_element}, $el_category->{head}];
4643                } else {                } else {
4644                  !!!cp ('t103');                  !!!cp ('t103');
4645                }                }
4646                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4647                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4648                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4649                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4650                  !!!ack ('t103.1');
4651                !!!next-token;                !!!next-token;
4652                redo B;                next B;
4653              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4654                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4655                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4656                  !!!cp ('t104');                  !!!cp ('t104');
4657                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4658                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4659                    push @{$self->{open_elements}},
4660                        [$self->{head_element}, $el_category->{head}];
4661                } else {                } else {
4662                  !!!cp ('t105');                  !!!cp ('t105');
4663                }                }
4664                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4665                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.
4666    
4667                unless ($self->{confident}) {                unless ($self->{confident}) {
4668                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4669                    !!!cp ('t106');                    !!!cp ('t106');
4670                      ## NOTE: Whether the encoding is supported or not is handled
4671                      ## in the {change_encoding} callback.
4672                    $self->{change_encoding}                    $self->{change_encoding}
4673                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4674                             $token);
4675                                        
4676                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4677                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4678                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4679                                                 ->{has_reference});                                                 ->{has_reference});
4680                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4681                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4682                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4683                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4684                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4685                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4686                      !!!cp ('t107');                      !!!cp ('t107');
4687                        ## NOTE: Whether the encoding is supported or not is handled
4688                        ## in the {change_encoding} callback.
4689                      $self->{change_encoding}                      $self->{change_encoding}
4690                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4691                               $token);
4692                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4693                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4694                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3517  sub _tree_construction_main ($) { Line 4714  sub _tree_construction_main ($) {
4714                  }                  }
4715                }                }
4716    
4717                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4718                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4719                  !!!ack ('t110.1');
4720                !!!next-token;                !!!next-token;
4721                redo B;                next B;
4722              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4723                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4724                  !!!cp ('t111');                  !!!cp ('t111');
4725                  ## As if </noscript>                  ## As if </noscript>
4726                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4727                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4728                                    token => $token);
4729                                
4730                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4731                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4732                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4733                  !!!cp ('t112');                  !!!cp ('t112');
4734                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4735                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4736                    push @{$self->{open_elements}},
4737                        [$self->{head_element}, $el_category->{head}];
4738                } else {                } else {
4739                  !!!cp ('t113');                  !!!cp ('t113');
4740                }                }
# Line 3541  sub _tree_construction_main ($) { Line 4742  sub _tree_construction_main ($) {
4742                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4743                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4744                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4745                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4746                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4747                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4748                redo B;                next B;
4749              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4750                         $token->{tag_name} eq 'noframes') {
4751                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4752                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4753                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4754                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4755                  !!!cp ('t114');                  !!!cp ('t114');
4756                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4757                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4758                    push @{$self->{open_elements}},
4759                        [$self->{head_element}, $el_category->{head}];
4760                } else {                } else {
4761                  !!!cp ('t115');                  !!!cp ('t115');
4762                }                }
4763                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4764                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4765                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4766                redo B;                next B;
4767              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4768                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4769                  !!!cp ('t116');                  !!!cp ('t116');
4770                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4771                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4772                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4773                    !!!nack ('t116.1');
4774                  !!!next-token;                  !!!next-token;
4775                  redo B;                  next B;
4776                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4777                  !!!cp ('t117');                  !!!cp ('t117');
4778                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
4779                                    token => $token);
4780                  ## Ignore the token                  ## Ignore the token
4781                    !!!nack ('t117.1');
4782                  !!!next-token;                  !!!next-token;
4783                  redo B;                  next B;
4784                } else {                } else {
4785                  !!!cp ('t118');                  !!!cp ('t118');
4786                  #                  #
# Line 3584  sub _tree_construction_main ($) { Line 4790  sub _tree_construction_main ($) {
4790                  !!!cp ('t119');                  !!!cp ('t119');
4791                  ## As if </noscript>                  ## As if </noscript>
4792                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4793                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4794                                    token => $token);
4795                                
4796                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4797                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4798                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4799                  !!!cp ('t120');                  !!!cp ('t120');
4800                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4801                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4802                    push @{$self->{open_elements}},
4803                        [$self->{head_element}, $el_category->{head}];
4804                } else {                } else {
4805                  !!!cp ('t121');                  !!!cp ('t121');
4806                }                }
4807    
4808                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4809                $script_start_tag->($insert_to_current);                $script_start_tag->();
4810                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4811                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4812                redo B;                next B;
4813              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4814                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4815                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4816                  !!!cp ('t122');                  !!!cp ('t122');
4817                  ## As if </noscript>                  ## As if </noscript>
4818                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4819                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4820                                    text => $token->{tag_name}, token => $token);
4821                                    
4822                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4823                  ## As if </head>                  ## As if </head>
# Line 3624  sub _tree_construction_main ($) { Line 4834  sub _tree_construction_main ($) {
4834                }                }
4835    
4836                ## "after head" insertion mode                ## "after head" insertion mode
4837                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4838                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4839                  !!!cp ('t126');                  !!!cp ('t126');
4840                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3634  sub _tree_construction_main ($) { Line 4844  sub _tree_construction_main ($) {
4844                } else {                } else {
4845                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4846                }                }
4847                  !!!nack ('t127.1');
4848                !!!next-token;                !!!next-token;
4849                redo B;                next B;
4850              } else {              } else {
4851                !!!cp ('t128');                !!!cp ('t128');
4852                #                #
# Line 3645  sub _tree_construction_main ($) { Line 4856  sub _tree_construction_main ($) {
4856                !!!cp ('t129');                !!!cp ('t129');
4857                ## As if </noscript>                ## As if </noscript>
4858                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4859                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4860                                  text => $token->{tag_name}, token => $token);
4861                                
4862                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4863                ## As if </head>                ## As if </head>
# Line 3664  sub _tree_construction_main ($) { Line 4876  sub _tree_construction_main ($) {
4876    
4877              ## "after head" insertion mode              ## "after head" insertion mode
4878              ## As if <body>              ## As if <body>
4879              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4880              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4881              ## reprocess              ## reprocess
4882              redo B;              !!!ack-later;
4883                next B;
4884            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4885              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4886                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4887                  !!!cp ('t132');                  !!!cp ('t132');
4888                  ## As if <head>                  ## As if <head>
4889                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4890                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4891                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4892                        [$self->{head_element}, $el_category->{head}];
4893    
4894                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4895                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4896                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4897                  !!!next-token;                  !!!next-token;
4898                  redo B;                  next B;
4899                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4900                  !!!cp ('t133');                  !!!cp ('t133');
4901                  ## As if </noscript>                  ## As if </noscript>
4902                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4903                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/',
4904                                    text => 'head', token => $token);
4905                                    
4906                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4907                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4908                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4909                  !!!next-token;                  !!!next-token;
4910                  redo B;                  next B;
4911                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4912                  !!!cp ('t134');                  !!!cp ('t134');
4913                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4914                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4915                  !!!next-token;                  !!!next-token;
4916                  redo B;                  next B;
4917                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4918                    !!!cp ('t134.1');
4919                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4920                                    token => $token);
4921                    ## Ignore the token
4922                    !!!next-token;
4923                    next B;
4924                } else {                } else {
4925                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4926                }                }
4927              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4928                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3709  sub _tree_construction_main ($) { Line 4930  sub _tree_construction_main ($) {
4930                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4931                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4932                  !!!next-token;                  !!!next-token;
4933                  redo B;                  next B;
4934                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4935                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4936                  !!!cp ('t137');                  !!!cp ('t137');
4937                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
4938                                    text => 'noscript', token => $token);
4939                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4940                  !!!next-token;                  !!!next-token;
4941                  redo B;                  next B;
4942                } else {                } else {
4943                  !!!cp ('t138');                  !!!cp ('t138');
4944                  #                  #
# Line 3723  sub _tree_construction_main ($) { Line 4946  sub _tree_construction_main ($) {
4946              } elsif ({              } elsif ({
4947                        body => 1, html => 1,                        body => 1, html => 1,
4948                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4949                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4950                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4951                  ## As if <head>                    $self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
                 !!!create-element ($self->{head_element}, 'head');  
                 $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});  
                 push @{$self->{open_elements}}, [$self->{head_element}, 'head'];  
   
                 $self->{insertion_mode} = IN_HEAD_IM;  
                 ## Reprocess in the "in head" insertion mode...  
               } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {  
4952                  !!!cp ('t140');                  !!!cp ('t140');
4953                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4954                                    text => $token->{tag_name}, token => $token);
4955                  ## Ignore the token                  ## Ignore the token
4956                  !!!next-token;                  !!!next-token;
4957                  redo B;                  next B;
4958                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4959                    !!!cp ('t140.1');
4960                    !!!parse-error (type => 'unmatched end tag',
4961                                    text => $token->{tag_name}, token => $token);
4962                    ## Ignore the token
4963                    !!!next-token;
4964                    next B;
4965                } else {                } else {
4966                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4967                }                }
4968                              } elsif ($token->{tag_name} eq 'p') {
4969                #                !!!cp ('t142');
4970              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4971                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4972                       }->{$token->{tag_name}}) {                ## Ignore the token
4973                  !!!next-token;
4974                  next B;
4975                } elsif ($token->{tag_name} eq 'br') {
4976                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4977                  !!!cp ('t142');                  !!!cp ('t142.2');
4978                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4979                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4980                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4981                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4982      
4983                    ## Reprocess in the "after head" insertion mode...
4984                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4985                    !!!cp ('t143.2');
4986                    ## As if </head>
4987                    pop @{$self->{open_elements}};
4988                    $self->{insertion_mode} = AFTER_HEAD_IM;
4989      
4990                    ## Reprocess in the "after head" insertion mode...
4991                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4992                    !!!cp ('t143.3');
4993                    ## ISSUE: Two parse errors for <head><noscript></br>
4994                    !!!parse-error (type => 'unmatched end tag',
4995                                    text => 'br', token => $token);
4996                    ## As if </noscript>
4997                    pop @{$self->{open_elements}};
4998                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4999    
5000                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
5001                } else {                  ## As if </head>
5002                  !!!cp ('t143');                  pop @{$self->{open_elements}};
5003                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
5004    
5005                #                  ## Reprocess in the "after head" insertion mode...
5006              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5007                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
5008                  #                  #
5009                } else {                } else {
5010                  !!!cp ('t145');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                 ## Ignore the token  
                 !!!next-token;  
                 redo B;  
5011                }                }
5012    
5013                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5014                  !!!parse-error (type => 'unmatched end tag',
5015                                  text => 'br', token => $token);
5016                  ## Ignore the token
5017                  !!!next-token;
5018                  next B;
5019                } else {
5020                  !!!cp ('t145');
5021                  !!!parse-error (type => 'unmatched end tag',
5022                                  text => $token->{tag_name}, token => $token);
5023                  ## Ignore the token
5024                  !!!next-token;
5025                  next B;
5026              }              }
5027    
5028              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5029                !!!cp ('t146');                !!!cp ('t146');
5030                ## As if </noscript>                ## As if </noscript>
5031                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5032                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
5033                                  text => $token->{tag_name}, token => $token);
5034                                
5035                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5036                ## As if </head>                ## As if </head>
# Line 3793  sub _tree_construction_main ($) { Line 5046  sub _tree_construction_main ($) {
5046              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5047  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
5048                !!!cp ('t148');                !!!cp ('t148');
5049                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5050                                  text => $token->{tag_name}, token => $token);
5051                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5052                !!!next-token;                !!!next-token;
5053                redo B;                next B;
5054              } else {              } else {
5055                !!!cp ('t149');                !!!cp ('t149');
5056              }              }
5057    
5058              ## "after head" insertion mode              ## "after head" insertion mode
5059              ## As if <body>              ## As if <body>
5060              !!!insert-element ('body');              !!!insert-element ('body',, $token);
5061              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
5062              ## reprocess              ## reprocess
5063              redo B;              next B;
5064            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5065              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5066            }            !!!cp ('t149.1');
5067    
5068              ## NOTE: As if <head>
5069              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5070              $self->{open_elements}->[-1]->[0]->append_child
5071                  ($self->{head_element});
5072              #push @{$self->{open_elements}},
5073              #    [$self->{head_element}, $el_category->{head}];
5074              #$self->{insertion_mode} = IN_HEAD_IM;
5075              ## NOTE: Reprocess.
5076    
5077              ## NOTE: As if </head>
5078              #pop @{$self->{open_elements}};
5079              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5080              ## NOTE: Reprocess.
5081              
5082              #
5083            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5084              !!!cp ('t149.2');
5085    
5086              ## NOTE: As if </head>
5087              pop @{$self->{open_elements}};
5088              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5089              ## NOTE: Reprocess.
5090    
5091              #
5092            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5093              !!!cp ('t149.3');
5094    
5095              !!!parse-error (type => 'in noscript:#eof', token => $token);
5096    
5097              ## As if </noscript>
5098              pop @{$self->{open_elements}};
5099              #$self->{insertion_mode} = IN_HEAD_IM;
5100              ## NOTE: Reprocess.
5101    
5102              ## NOTE: As if </head>
5103              pop @{$self->{open_elements}};
5104              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5105              ## NOTE: Reprocess.
5106    
5107              #
5108            } else {
5109              !!!cp ('t149.4');
5110              #
5111            }
5112    
5113            ## NOTE: As if <body>
5114            !!!insert-element ('body',, $token);
5115            $self->{insertion_mode} = IN_BODY_IM;
5116            ## NOTE: Reprocess.
5117            next B;
5118          } else {
5119            die "$0: $token->{type}: Unknown token type";
5120          }
5121    
5122            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5123      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3821  sub _tree_construction_main ($) { Line 5129  sub _tree_construction_main ($) {
5129              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5130    
5131              !!!next-token;              !!!next-token;
5132              redo B;              next B;
5133            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5134              if ({              if ({
5135                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3829  sub _tree_construction_main ($) { Line 5137  sub _tree_construction_main ($) {
5137                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5138                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5139                  ## have an element in table scope                  ## have an element in table scope
5140                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5141                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5142                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5143                      !!!cp ('t151');                      !!!cp ('t151');
5144                      $tn = $node->[1];  
5145                      last INSCOPE;                      ## Close the cell
5146                    } elsif ({                      !!!back-token; # <x>
5147                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
5148                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
5149                                  line => $token->{line},
5150                                  column => $token->{column}};
5151                        next B;
5152                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5153                      !!!cp ('t152');                      !!!cp ('t152');
5154                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
5155                        last;
5156                    }                    }
5157                  } # INSCOPE                  }
5158                    unless (defined $tn) {  
5159                      !!!cp ('t153');                  !!!cp ('t153');
5160  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
5161                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      text => $token->{tag_name}, token => $token);
5162                      ## Ignore the token                  ## Ignore the token
5163                      !!!next-token;                  !!!nack ('t153.1');
5164                      redo B;                  !!!next-token;
5165                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
5166                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5167                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5168                                    token => $token);
5169                                    
5170                  ## As if </caption>                  ## NOTE: As if </caption>.
5171                  ## have a table element in table scope                  ## have a table element in table scope
5172                  my $i;                  my $i;
5173                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5174                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5175                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5176                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
5177                      $i = $_;                        !!!cp ('t155');
5178                      last INSCOPE;                        $i = $_;
5179                    } elsif ({                        last INSCOPE;
5180                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5181                             }->{$node->[1]}) {                        !!!cp ('t156');
5182                      !!!cp ('t156');                        last;
5183                      last INSCOPE;                      }
5184                    }                    }
5185    
5186                      !!!cp ('t157');
5187                      !!!parse-error (type => 'start tag not allowed',
5188                                      text => $token->{tag_name}, token => $token);
5189                      ## Ignore the token
5190                      !!!nack ('t157.1');
5191                      !!!next-token;
5192                      next B;
5193                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5194                                    
5195                  ## generate implied end tags                  ## generate implied end tags
5196                  if ({                  while ($self->{open_elements}->[-1]->[1]
5197                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
   
                      ## NOTE: Maybe the following elements never appear here.  
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
5198                    !!!cp ('t158');                    !!!cp ('t158');
5199                    !!!back-token; # <?>                    pop @{$self->{open_elements}};
                   $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;  
5200                  }                  }
5201    
5202                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5203                    !!!cp ('t159');                    !!!cp ('t159');
5204                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5205                                      text => $self->{open_elements}->[-1]->[0]
5206                                          ->manakai_local_name,
5207                                      token => $token);
5208                  } else {                  } else {
5209                    !!!cp ('t160');                    !!!cp ('t160');
5210                  }                  }
# Line 3916  sub _tree_construction_main ($) { Line 5216  sub _tree_construction_main ($) {
5216                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5217                                    
5218                  ## reprocess                  ## reprocess
5219                  redo B;                  !!!ack-later;
5220                    next B;
5221                } else {                } else {
5222                  !!!cp ('t161');                  !!!cp ('t161');
5223                  #                  #
# Line 3932  sub _tree_construction_main ($) { Line 5233  sub _tree_construction_main ($) {
5233                  my $i;                  my $i;
5234                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5235                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5236                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5237                      !!!cp ('t163');                      !!!cp ('t163');
5238                      $i = $_;                      $i = $_;
5239                      last INSCOPE;                      last INSCOPE;
5240                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5241                      !!!cp ('t164');                      !!!cp ('t164');
5242                      last INSCOPE;                      last INSCOPE;
5243                    }                    }
5244                  } # INSCOPE                  } # INSCOPE
5245                    unless (defined $i) {                    unless (defined $i) {
5246                      !!!cp ('t165');                      !!!cp ('t165');
5247                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5248                                        text => $token->{tag_name},
5249                                        token => $token);
5250                      ## Ignore the token                      ## Ignore the token
5251                      !!!next-token;                      !!!next-token;
5252                      redo B;                      next B;
5253                    }                    }
5254                                    
5255                  ## generate implied end tags                  ## generate implied end tags
5256                  if ({                  while ($self->{open_elements}->[-1]->[1]
5257                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => ($token->{tag_name} eq 'th'),  
                      th => ($token->{tag_name} eq 'td'),  
   
                      ## NOTE: Maybe the following elements never appear here.  
                      tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
5258                    !!!cp ('t166');                    !!!cp ('t166');
5259                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5260                  }                  }
5261                    
5262                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5263                            ne $token->{tag_name}) {
5264                    !!!cp ('t167');                    !!!cp ('t167');
5265                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5266                                      text => $self->{open_elements}->[-1]->[0]
5267                                          ->manakai_local_name,
5268                                      token => $token);
5269                  } else {                  } else {
5270                    !!!cp ('t168');                    !!!cp ('t168');
5271                  }                  }
# Line 3982  sub _tree_construction_main ($) { Line 5277  sub _tree_construction_main ($) {
5277                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5278                                    
5279                  !!!next-token;                  !!!next-token;
5280                  redo B;                  next B;
5281                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5282                  !!!cp ('t169');                  !!!cp ('t169');
5283                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5284                                    text => $token->{tag_name}, token => $token);
5285                  ## Ignore the token                  ## Ignore the token
5286                  !!!next-token;                  !!!next-token;
5287                  redo B;                  next B;
5288                } else {                } else {
5289                  !!!cp ('t170');                  !!!cp ('t170');
5290                  #                  #
# Line 3997  sub _tree_construction_main ($) { Line 5293  sub _tree_construction_main ($) {
5293                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5294                  ## have a table element in table scope                  ## have a table element in table scope
5295                  my $i;                  my $i;
5296                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5297                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5298                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5299                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
5300                      $i = $_;                        !!!cp ('t171');
5301                      last INSCOPE;                        $i = $_;
5302                    } elsif ({                        last INSCOPE;
5303                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5304                             }->{$node->[1]}) {                        !!!cp ('t172');
5305                      !!!cp ('t172');                        last;
5306                      last INSCOPE;                      }
5307                    }                    }
5308    
5309                      !!!cp ('t173');
5310                      !!!parse-error (type => 'unmatched end tag',
5311                                      text => $token->{tag_name}, token => $token);
5312                      ## Ignore the token
5313                      !!!next-token;
5314                      next B;
5315                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5316                                    
5317                  ## generate implied end tags                  ## generate implied end tags
5318                  if ({                  while ($self->{open_elements}->[-1]->[1]
5319                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
   
                      ## NOTE: The following elements never appear here, maybe.  
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot => 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
5320                    !!!cp ('t174');                    !!!cp ('t174');
5321                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5322                  }                  }
5323                                    
5324                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5325                    !!!cp ('t175');                    !!!cp ('t175');
5326                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5327                                      text => $self->{open_elements}->[-1]->[0]
5328                                          ->manakai_local_name,
5329                                      token => $token);
5330                  } else {                  } else {
5331                    !!!cp ('t176');                    !!!cp ('t176');
5332                  }                  }
# Line 4047  sub _tree_construction_main ($) { Line 5338  sub _tree_construction_main ($) {
5338                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5339                                    
5340                  !!!next-token;                  !!!next-token;
5341                  redo B;                  next B;
5342                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5343                  !!!cp ('t177');                  !!!cp ('t177');
5344                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5345                                    text => $token->{tag_name}, token => $token);
5346                  ## Ignore the token                  ## Ignore the token
5347                  !!!next-token;                  !!!next-token;
5348                  redo B;                  next B;
5349                } else {                } else {
5350                  !!!cp ('t178');                  !!!cp ('t178');
5351                  #                  #
# Line 4066  sub _tree_construction_main ($) { Line 5358  sub _tree_construction_main ($) {
5358                ## have an element in table scope                ## have an element in table scope
5359                my $i;                my $i;
5360                my $tn;                my $tn;
5361                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5362                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5363                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5364                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5365                    $i = $_;                      !!!cp ('t179');
5366                    last INSCOPE;                      $i = $_;
5367                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
5368                    !!!cp ('t180');                      ## Close the cell
5369                    $tn = $node->[1];                      !!!back-token; # </x>
5370                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5371                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
5372                  } elsif ({                                column => $token->{column}};
5373                            table => 1, html => 1,                      next B;
5374                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5375                    !!!cp ('t181');                      !!!cp ('t180');
5376                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
5377                        ## NOTE: There is exactly one |td| or |th| element
5378                        ## in scope in the stack of open elements by definition.
5379                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5380                        ## ISSUE: Can this be reached?
5381                        !!!cp ('t181');
5382                        last;
5383                      }
5384                  }                  }
5385                } # INSCOPE  
               unless (defined $i) {  
5386                  !!!cp ('t182');                  !!!cp ('t182');
5387                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5388                        text => $token->{tag_name}, token => $token);
5389                  ## Ignore the token                  ## Ignore the token
5390                  !!!next-token;                  !!!next-token;
5391                  redo B;                  next B;
5392                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5393              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5394                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5395                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5396                                  token => $token);
5397    
5398                ## As if </caption>                ## As if </caption>
5399                ## have a table element in table scope                ## have a table element in table scope
5400                my $i;                my $i;
5401                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5402                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5403                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5404                    !!!cp ('t184');                    !!!cp ('t184');
5405                    $i = $_;                    $i = $_;
5406                    last INSCOPE;                    last INSCOPE;
5407                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5408                    !!!cp ('t185');                    !!!cp ('t185');
5409                    last INSCOPE;                    last INSCOPE;
5410                  }                  }
5411                } # INSCOPE                } # INSCOPE
5412                unless (defined $i) {                unless (defined $i) {
5413                  !!!cp ('t186');                  !!!cp ('t186');
5414                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag',
5415                                    text => 'caption', token => $token);
5416                  ## Ignore the token                  ## Ignore the token
5417                  !!!next-token;                  !!!next-token;
5418                  redo B;                  next B;
5419                }                }
5420                                
5421                ## generate implied end tags                ## generate implied end tags
5422                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
   
                    ## NOTE: The following elements never appear, maybe.  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot => 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
5423                  !!!cp ('t187');                  !!!cp ('t187');
5424                  !!!back-token; # </table>                  pop @{$self->{open_elements}};
                 $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;  
5425                }                }
5426    
5427                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5428                  !!!cp ('t188');                  !!!cp ('t188');
5429                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5430                                    text => $self->{open_elements}->[-1]->[0]
5431                                        ->manakai_local_name,
5432                                    token => $token);
5433                } else {                } else {
5434                  !!!cp ('t189');                  !!!cp ('t189');
5435                }                }
# Line 4157  sub _tree_construction_main ($) { Line 5441  sub _tree_construction_main ($) {
5441                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5442    
5443                ## reprocess                ## reprocess
5444                redo B;                next B;
5445              } elsif ({              } elsif ({
5446                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5447                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5448                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5449                  !!!cp ('t190');                  !!!cp ('t190');
5450                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5451                                    text => $token->{tag_name}, token => $token);
5452                  ## Ignore the token                  ## Ignore the token
5453                  !!!next-token;                  !!!next-token;
5454                  redo B;                  next B;
5455                } else {                } else {
5456                  !!!cp ('t191');                  !!!cp ('t191');
5457                  #                  #
# Line 4177  sub _tree_construction_main ($) { Line 5462  sub _tree_construction_main ($) {
5462                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5463                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5464                !!!cp ('t192');                !!!cp ('t192');
5465                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5466                                  text => $token->{tag_name}, token => $token);
5467                ## Ignore the token                ## Ignore the token
5468                !!!next-token;                !!!next-token;
5469                redo B;                next B;
5470              } else {              } else {
5471                !!!cp ('t193');                !!!cp ('t193');
5472                #                #
5473              }              }
5474          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5475            for my $entry (@{$self->{open_elements}}) {
5476              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5477                !!!cp ('t75');
5478                !!!parse-error (type => 'in body:#eof', token => $token);
5479                last;
5480              }
5481            }
5482    
5483            ## Stop parsing.
5484            last B;
5485        } else {        } else {
5486          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5487        }        }
# Line 4193  sub _tree_construction_main ($) { Line 5490  sub _tree_construction_main ($) {
5490        #        #
5491      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5492        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5493              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5494                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5495              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5496                                
5497                unless (length $token->{data}) {            unless (length $token->{data}) {
5498                  !!!cp ('t194');              !!!cp ('t194');
5499                  !!!next-token;              !!!next-token;
5500                  redo B;              next B;
5501                } else {            } else {
5502                  !!!cp ('t195');              !!!cp ('t195');
5503                }            }
5504              }          }
5505    
5506              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5507    
5508              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5509              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4213  sub _tree_construction_main ($) { Line 5511  sub _tree_construction_main ($) {
5511              ## result in a new Text node.              ## result in a new Text node.
5512              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5513                            
5514              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]}) {  
5515                # MUST                # MUST
5516                my $foster_parent_element;                my $foster_parent_element;
5517                my $next_sibling;                my $next_sibling;
5518                my $prev_sibling;                my $prev_sibling;
5519                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5520                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5521                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5522                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5523                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4250  sub _tree_construction_main ($) { Line 5545  sub _tree_construction_main ($) {
5545                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5546                     $next_sibling);                     $next_sibling);
5547                }                }
5548              } else {            $open_tables->[-1]->[1] = 1; # tainted
5549                !!!cp ('t200');          } else {
5550                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5551              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5552            }
5553                            
5554              !!!next-token;          !!!next-token;
5555              redo B;          next B;
5556        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5557              if ({          if ({
5558                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5559                   th => 1, td => 1,               th => 1, td => 1,
5560                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5561                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5562                  ## Clear back to table context              ## Clear back to table context
5563                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5564                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5565                    !!!cp ('t201');                !!!cp ('t201');
5566                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                pop @{$self->{open_elements}};
5567                    pop @{$self->{open_elements}};              }
5568                  }              
5569                                !!!insert-element ('tbody',, $token);
5570                  !!!insert-element ('tbody');              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5571                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              ## reprocess in the "in table body" insertion mode...
5572                  ## reprocess in the "in table body" insertion mode...            }
5573                }            
5574              if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5575                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {              unless ($token->{tag_name} eq 'tr') {
5576                  unless ($token->{tag_name} eq 'tr') {                !!!cp ('t202');
5577                    !!!cp ('t202');                !!!parse-error (type => 'missing start tag:tr', token => $token);
5578                    !!!parse-error (type => 'missing start tag:tr');              }
                 }  
5579                                    
5580                  ## Clear back to table body context              ## Clear back to table body context
5581                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5582                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5583                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5584                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5585                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5586                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              }
                   pop @{$self->{open_elements}};  
                 }  
5587                                    
5588                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5589                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5590                    !!!cp ('t204');                    !!!cp ('t204');
5591                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5592                      !!!nack ('t204');
5593                    !!!next-token;                    !!!next-token;
5594                    redo B;                    next B;
5595                  } else {                  } else {
5596                    !!!cp ('t205');                    !!!cp ('t205');
5597                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5598                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5599                  }                  }
5600                } else {                } else {
# Line 4308  sub _tree_construction_main ($) { Line 5602  sub _tree_construction_main ($) {
5602                }                }
5603    
5604                ## Clear back to table row context                ## Clear back to table row context
5605                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5606                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5607                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5608                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5609                }                }
5610                                
5611                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5612                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5613    
5614                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5615                                
5616                  !!!nack ('t207.1');
5617                !!!next-token;                !!!next-token;
5618                redo B;                next B;
5619              } elsif ({              } elsif ({
5620                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5621                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4334  sub _tree_construction_main ($) { Line 5627  sub _tree_construction_main ($) {
5627                  my $i;                  my $i;
5628                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5629                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5630                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5631                      !!!cp ('t208');                      !!!cp ('t208');
5632                      $i = $_;                      $i = $_;
5633                      last INSCOPE;                      last INSCOPE;
5634                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5635                      !!!cp ('t209');                      !!!cp ('t209');
5636                      last INSCOPE;                      last INSCOPE;
5637                    }                    }
5638                  } # INSCOPE                  } # INSCOPE
5639                  unless (defined $i) {                  unless (defined $i) {
5640                   !!!cp ('t210');                    !!!cp ('t210');
5641  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5642                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag',
5643                                      text => $token->{tag_name}, token => $token);
5644                    ## Ignore the token                    ## Ignore the token
5645                      !!!nack ('t210.1');
5646                    !!!next-token;                    !!!next-token;
5647                    redo B;                    next B;
5648                  }                  }
5649                                    
5650                  ## Clear back to table row context                  ## Clear back to table row context
5651                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5652                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5653                    !!!cp ('t211');                    !!!cp ('t211');
5654                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5655                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5656                  }                  }
5657                                    
# Line 4372  sub _tree_construction_main ($) { Line 5660  sub _tree_construction_main ($) {
5660                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5661                    !!!cp ('t212');                    !!!cp ('t212');
5662                    ## reprocess                    ## reprocess
5663                    redo B;                    !!!ack-later;
5664                      next B;
5665                  } else {                  } else {
5666                    !!!cp ('t213');                    !!!cp ('t213');
5667                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4384  sub _tree_construction_main ($) { Line 5673  sub _tree_construction_main ($) {
5673                  my $i;                  my $i;
5674                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5675                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5676                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5677                      !!!cp ('t214');                      !!!cp ('t214');
5678                      $i = $_;                      $i = $_;
5679                      last INSCOPE;                      last INSCOPE;
5680                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5681                      !!!cp ('t215');                      !!!cp ('t215');
5682                      last INSCOPE;                      last INSCOPE;
5683                    }                    }
5684                  } # INSCOPE                  } # INSCOPE
5685                  unless (defined $i) {                  unless (defined $i) {
5686                    !!!cp ('t216');                    !!!cp ('t216');
5687  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5688                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5689                                      text => $token->{tag_name}, token => $token);
5690                    ## Ignore the token                    ## Ignore the token
5691                      !!!nack ('t216.1');
5692                    !!!next-token;                    !!!next-token;
5693                    redo B;                    next B;
5694                  }                  }
5695    
5696                  ## Clear back to table body context                  ## Clear back to table body context
5697                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5698                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5699                    !!!cp ('t217');                    !!!cp ('t217');
5700                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5701                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5702                  }                  }
5703                                    
# Line 4432  sub _tree_construction_main ($) { Line 5717  sub _tree_construction_main ($) {
5717    
5718                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5719                  ## Clear back to table context                  ## Clear back to table context
5720                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5721                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5722                    !!!cp ('t219');                    !!!cp ('t219');
5723                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5724                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5725                  }                  }
5726                                    
5727                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5728                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5729                  ## reprocess                  ## reprocess
5730                  redo B;                  !!!ack-later;
5731                    next B;
5732                } elsif ({                } elsif ({
5733                          caption => 1,                          caption => 1,
5734                          colgroup => 1,                          colgroup => 1,
5735                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5736                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5737                  ## Clear back to table context                  ## Clear back to table context
5738                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5739                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5740                    !!!cp ('t220');                    !!!cp ('t220');
5741                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5742                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5743                  }                  }
5744                                    
5745                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5746                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5747                                    
5748                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5749                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5750                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5751                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4470  sub _tree_construction_main ($) { Line 5754  sub _tree_construction_main ($) {
5754                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5755                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5756                  !!!next-token;                  !!!next-token;
5757                  redo B;                  !!!nack ('t220.1');
5758                    next B;
5759                } else {                } else {
5760                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5761                }                }
5762              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5763                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5764                                  text => $self->{open_elements}->[-1]->[0]
5765                                      ->manakai_local_name,
5766                                  token => $token);
5767    
5768                ## As if </table>                ## As if </table>
5769                ## have a table element in table scope                ## have a table element in table scope
5770                my $i;                my $i;
5771                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5772                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5773                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5774                    !!!cp ('t221');                    !!!cp ('t221');
5775                    $i = $_;                    $i = $_;
5776                    last INSCOPE;                    last INSCOPE;
5777                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5778                    !!!cp ('t222');                    !!!cp ('t222');
5779                    last INSCOPE;                    last INSCOPE;
5780                  }                  }
# Line 4497  sub _tree_construction_main ($) { Line 5782  sub _tree_construction_main ($) {
5782                unless (defined $i) {                unless (defined $i) {
5783                  !!!cp ('t223');                  !!!cp ('t223');
5784  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5785                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5786                                    token => $token);
5787                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5788                    !!!nack ('t223.1');
5789                  !!!next-token;                  !!!next-token;
5790                  redo B;                  next B;
5791                }                }
5792                                
5793    ## TODO: Followings are removed from the latest spec.
5794                ## generate implied end tags                ## generate implied end tags
5795                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    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]}) {  
5796                  !!!cp ('t224');                  !!!cp ('t224');
5797                  !!!back-token; # <table>                  pop @{$self->{open_elements}};
                 $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;  
5798                }                }
5799    
5800                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5801                  !!!cp ('t225');                  !!!cp ('t225');
5802  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5803                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5804                                    text => $self->{open_elements}->[-1]->[0]
5805                                        ->manakai_local_name,
5806                                    token => $token);
5807                } else {                } else {
5808                  !!!cp ('t226');                  !!!cp ('t226');
5809                }                }
5810    
5811                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5812                  pop @{$open_tables};
5813    
5814                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5815    
5816                ## reprocess            ## reprocess
5817                redo B;            !!!ack-later;
5818              next B;
5819            } elsif ($token->{tag_name} eq 'style') {
5820              if (not $open_tables->[-1]->[1]) { # tainted
5821                !!!cp ('t227.8');
5822                ## NOTE: This is a "as if in head" code clone.
5823                $parse_rcdata->(CDATA_CONTENT_MODEL);
5824                next B;
5825              } else {
5826                !!!cp ('t227.7');
5827                #
5828              }
5829            } elsif ($token->{tag_name} eq 'script') {
5830              if (not $open_tables->[-1]->[1]) { # tainted
5831                !!!cp ('t227.6');
5832                ## NOTE: This is a "as if in head" code clone.
5833                $script_start_tag->();
5834                next B;
5835              } else {
5836                !!!cp ('t227.5');
5837                #
5838              }
5839            } elsif ($token->{tag_name} eq 'input') {
5840              if (not $open_tables->[-1]->[1]) { # tainted
5841                if ($token->{attributes}->{type}) { ## TODO: case
5842                  my $type = lc $token->{attributes}->{type}->{value};
5843                  if ($type eq 'hidden') {
5844                    !!!cp ('t227.3');
5845                    !!!parse-error (type => 'in table',
5846                                    text => $token->{tag_name}, token => $token);
5847    
5848                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5849    
5850                    ## TODO: form element pointer
5851    
5852                    pop @{$self->{open_elements}};
5853    
5854                    !!!next-token;
5855                    !!!ack ('t227.2.1');
5856                    next B;
5857                  } else {
5858                    !!!cp ('t227.2');
5859                    #
5860                  }
5861                } else {
5862                  !!!cp ('t227.1');
5863                  #
5864                }
5865              } else {
5866                !!!cp ('t227.4');
5867                #
5868              }
5869          } else {          } else {
5870            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5871            #            #
5872          }          }
5873    
5874            !!!parse-error (type => 'in table', text => $token->{tag_name},
5875                            token => $token);
5876    
5877            $insert = $insert_to_foster;
5878            #
5879        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5880              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5881                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4546  sub _tree_construction_main ($) { Line 5883  sub _tree_construction_main ($) {
5883                my $i;                my $i;
5884                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5885                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5886                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5887                    !!!cp ('t228');                    !!!cp ('t228');
5888                    $i = $_;                    $i = $_;
5889                    last INSCOPE;                    last INSCOPE;
5890                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5891                    !!!cp ('t229');                    !!!cp ('t229');
5892                    last INSCOPE;                    last INSCOPE;
5893                  }                  }
5894                } # INSCOPE                } # INSCOPE
5895                unless (defined $i) {                unless (defined $i) {
5896                  !!!cp ('t230');                  !!!cp ('t230');
5897                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5898                                    text => $token->{tag_name}, token => $token);
5899                  ## Ignore the token                  ## Ignore the token
5900                    !!!nack ('t230.1');
5901                  !!!next-token;                  !!!next-token;
5902                  redo B;                  next B;
5903                } else {                } else {
5904                  !!!cp ('t232');                  !!!cp ('t232');
5905                }                }
5906    
5907                ## Clear back to table row context                ## Clear back to table row context
5908                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5909                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5910                  !!!cp ('t231');                  !!!cp ('t231');
5911  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5912                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5913                }                }
5914    
5915                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5916                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5917                !!!next-token;                !!!next-token;
5918                redo B;                !!!nack ('t231.1');
5919                  next B;
5920              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5921                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5922                  ## As if </tr>                  ## As if </tr>
# Line 4588  sub _tree_construction_main ($) { Line 5924  sub _tree_construction_main ($) {
5924                  my $i;                  my $i;
5925                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5926                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5927                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5928                      !!!cp ('t233');                      !!!cp ('t233');
5929                      $i = $_;                      $i = $_;
5930                      last INSCOPE;                      last INSCOPE;
5931                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5932                      !!!cp ('t234');                      !!!cp ('t234');
5933                      last INSCOPE;                      last INSCOPE;
5934                    }                    }
# Line 4602  sub _tree_construction_main ($) { Line 5936  sub _tree_construction_main ($) {
5936                  unless (defined $i) {                  unless (defined $i) {
5937                    !!!cp ('t235');                    !!!cp ('t235');
5938  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5939                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag',
5940                                      text => $token->{type}, token => $token);
5941                    ## Ignore the token                    ## Ignore the token
5942                      !!!nack ('t236.1');
5943                    !!!next-token;                    !!!next-token;
5944                    redo B;                    next B;
5945                  }                  }
5946                                    
5947                  ## Clear back to table row context                  ## Clear back to table row context
5948                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5949                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5950                    !!!cp ('t236');                    !!!cp ('t236');
5951  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5952                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5953                  }                  }
5954                                    
# Line 4628  sub _tree_construction_main ($) { Line 5962  sub _tree_construction_main ($) {
5962                  my $i;                  my $i;
5963                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5964                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5965                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5966                      !!!cp ('t237');                      !!!cp ('t237');
5967                      $i = $_;                      $i = $_;
5968                      last INSCOPE;                      last INSCOPE;
5969                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5970                      !!!cp ('t238');                      !!!cp ('t238');
5971                      last INSCOPE;                      last INSCOPE;
5972                    }                    }
5973                  } # INSCOPE                  } # INSCOPE
5974                  unless (defined $i) {                  unless (defined $i) {
5975                    !!!cp ('t239');                    !!!cp ('t239');
5976                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5977                                      text => $token->{tag_name}, token => $token);
5978                    ## Ignore the token                    ## Ignore the token
5979                      !!!nack ('t239.1');
5980                    !!!next-token;                    !!!next-token;
5981                    redo B;                    next B;
5982                  }                  }
5983                                    
5984                  ## Clear back to table body context                  ## Clear back to table body context
5985                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5986                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5987                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5988                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5989                  }                  }
5990                                    
# Line 4670  sub _tree_construction_main ($) { Line 6000  sub _tree_construction_main ($) {
6000                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
6001                }                }
6002    
6003                  ## NOTE: </table> in the "in table" insertion mode.
6004                  ## When you edit the code fragment below, please ensure that
6005                  ## the code for <table> in the "in table" insertion mode
6006                  ## is synced with it.
6007    
6008                ## have a table element in table scope                ## have a table element in table scope
6009                my $i;                my $i;
6010                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6011                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6012                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
6013                    !!!cp ('t241');                    !!!cp ('t241');
6014                    $i = $_;                    $i = $_;
6015                    last INSCOPE;                    last INSCOPE;
6016                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6017                    !!!cp ('t242');                    !!!cp ('t242');
6018                    last INSCOPE;                    last INSCOPE;
6019                  }                  }
6020                } # INSCOPE                } # INSCOPE
6021                unless (defined $i) {                unless (defined $i) {
6022                  !!!cp ('t243');                  !!!cp ('t243');
6023                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6024                                    text => $token->{tag_name}, token => $token);
6025                  ## Ignore the token                  ## Ignore the token
6026                    !!!nack ('t243.1');
6027                  !!!next-token;                  !!!next-token;
6028                  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]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 !!!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') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
6029                }                }
6030                                    
6031                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
6032                  pop @{$open_tables};
6033                                
6034                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6035                                
6036                !!!next-token;                !!!next-token;
6037                redo B;                next B;
6038              } elsif ({              } elsif ({
6039                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6040                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4729  sub _tree_construction_main ($) { Line 6044  sub _tree_construction_main ($) {
6044                  my $i;                  my $i;
6045                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6046                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6047                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6048                      !!!cp ('t247');                      !!!cp ('t247');
6049                      $i = $_;                      $i = $_;
6050                      last INSCOPE;                      last INSCOPE;
6051                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6052                      !!!cp ('t248');                      !!!cp ('t248');
6053                      last INSCOPE;                      last INSCOPE;
6054                    }                    }
6055                  } # INSCOPE                  } # INSCOPE
6056                    unless (defined $i) {                    unless (defined $i) {
6057                      !!!cp ('t249');                      !!!cp ('t249');
6058                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
6059                                        text => $token->{tag_name}, token => $token);
6060                      ## Ignore the token                      ## Ignore the token
6061                        !!!nack ('t249.1');
6062                      !!!next-token;                      !!!next-token;
6063                      redo B;                      next B;
6064                    }                    }
6065                                    
6066                  ## As if </tr>                  ## As if </tr>
# Line 4753  sub _tree_construction_main ($) { Line 6068  sub _tree_construction_main ($) {
6068                  my $i;                  my $i;
6069                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6070                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6071                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6072                      !!!cp ('t250');                      !!!cp ('t250');
6073                      $i = $_;                      $i = $_;
6074                      last INSCOPE;                      last INSCOPE;
6075                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6076                      !!!cp ('t251');                      !!!cp ('t251');
6077                      last INSCOPE;                      last INSCOPE;
6078                    }                    }
6079                  } # INSCOPE                  } # INSCOPE
6080                    unless (defined $i) {                    unless (defined $i) {
6081                      !!!cp ('t252');                      !!!cp ('t252');
6082                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
6083                                        text => 'tr', token => $token);
6084                      ## Ignore the token                      ## Ignore the token
6085                        !!!nack ('t252.1');
6086                      !!!next-token;                      !!!next-token;
6087                      redo B;                      next B;
6088                    }                    }
6089                                    
6090                  ## Clear back to table row context                  ## Clear back to table row context
6091                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6092                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6093                    !!!cp ('t253');                    !!!cp ('t253');
6094  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6095                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6096                  }                  }
6097                                    
# Line 4791  sub _tree_construction_main ($) { Line 6104  sub _tree_construction_main ($) {
6104                my $i;                my $i;
6105                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6106                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6107                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6108                    !!!cp ('t254');                    !!!cp ('t254');
6109                    $i = $_;                    $i = $_;
6110                    last INSCOPE;                    last INSCOPE;
6111                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6112                    !!!cp ('t255');                    !!!cp ('t255');
6113                    last INSCOPE;                    last INSCOPE;
6114                  }                  }
6115                } # INSCOPE                } # INSCOPE
6116                unless (defined $i) {                unless (defined $i) {
6117                  !!!cp ('t256');                  !!!cp ('t256');
6118                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6119                                    text => $token->{tag_name}, token => $token);
6120                  ## Ignore the token                  ## Ignore the token
6121                    !!!nack ('t256.1');
6122                  !!!next-token;                  !!!next-token;
6123                  redo B;                  next B;
6124                }                }
6125    
6126                ## Clear back to table body context                ## Clear back to table body context
6127                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6128                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
6129                  !!!cp ('t257');                  !!!cp ('t257');
6130  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6131                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6132                }                }
6133    
6134                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6135                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6136                  !!!nack ('t257.1');
6137                !!!next-token;                !!!next-token;
6138                redo B;                next B;
6139              } elsif ({              } elsif ({
6140                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6141                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6142                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6143                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6144                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6145                !!!cp ('t258');            !!!cp ('t258');
6146                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6147                ## Ignore the token                            text => $token->{tag_name}, token => $token);
6148                !!!next-token;            ## Ignore the token
6149                redo B;            !!!nack ('t258.1');
6150               !!!next-token;
6151              next B;
6152          } else {          } else {
6153            !!!cp ('t259');            !!!cp ('t259');
6154            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
6155                              text => $token->{tag_name}, token => $token);
6156    
6157            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6158            #            #
6159          }          }
6160          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6161            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6162                    @{$self->{open_elements}} == 1) { # redundant, maybe
6163              !!!parse-error (type => 'in body:#eof', token => $token);
6164              !!!cp ('t259.1');
6165              #
6166            } else {
6167              !!!cp ('t259.2');
6168              #
6169            }
6170    
6171            ## Stop parsing
6172            last B;
6173        } else {        } else {
6174          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6175        }        }
# Line 4852  sub _tree_construction_main ($) { Line 6180  sub _tree_construction_main ($) {
6180                unless (length $token->{data}) {                unless (length $token->{data}) {
6181                  !!!cp ('t260');                  !!!cp ('t260');
6182                  !!!next-token;                  !!!next-token;
6183                  redo B;                  next B;
6184                }                }
6185              }              }
6186                            
# Line 4861  sub _tree_construction_main ($) { Line 6189  sub _tree_construction_main ($) {
6189            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6190              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6191                !!!cp ('t262');                !!!cp ('t262');
6192                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6193                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6194                  !!!ack ('t262.1');
6195                !!!next-token;                !!!next-token;
6196                redo B;                next B;
6197              } else {              } else {
6198                !!!cp ('t263');                !!!cp ('t263');
6199                #                #
6200              }              }
6201            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6202              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6203                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6204                  !!!cp ('t264');                  !!!cp ('t264');
6205                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
6206                                    text => 'colgroup', token => $token);
6207                  ## Ignore the token                  ## Ignore the token
6208                  !!!next-token;                  !!!next-token;
6209                  redo B;                  next B;
6210                } else {                } else {
6211                  !!!cp ('t265');                  !!!cp ('t265');
6212                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6213                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6214                  !!!next-token;                  !!!next-token;
6215                  redo B;                              next B;            
6216                }                }
6217              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6218                !!!cp ('t266');                !!!cp ('t266');
6219                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
6220                                  text => 'col', token => $token);
6221                ## Ignore the token                ## Ignore the token
6222                !!!next-token;                !!!next-token;
6223                redo B;                next B;
6224              } else {              } else {
6225                !!!cp ('t267');                !!!cp ('t267');
6226                #                #
6227              }              }
6228            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6229              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6230            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6231              !!!cp ('t270.2');
6232              ## Stop parsing.
6233              last B;
6234            } else {
6235              ## NOTE: As if </colgroup>.
6236              !!!cp ('t270.1');
6237              pop @{$self->{open_elements}}; # colgroup
6238              $self->{insertion_mode} = IN_TABLE_IM;
6239              ## Reprocess.
6240              next B;
6241            }
6242          } else {
6243            die "$0: $token->{type}: Unknown token type";
6244          }
6245    
6246            ## As if </colgroup>            ## As if </colgroup>
6247            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6248              !!!cp ('t269');              !!!cp ('t269');
6249              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
6250                !!!parse-error (type => 'unmatched end tag',
6251                                text => 'colgroup', token => $token);
6252              ## Ignore the token              ## Ignore the token
6253                !!!nack ('t269.1');
6254              !!!next-token;              !!!next-token;
6255              redo B;              next B;
6256            } else {            } else {
6257              !!!cp ('t270');              !!!cp ('t270');
6258              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6259              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6260                !!!ack-later;
6261              ## reprocess              ## reprocess
6262              redo B;              next B;
6263            }            }
6264      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6265        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6266          !!!cp ('t271');          !!!cp ('t271');
6267          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6268          !!!next-token;          !!!next-token;
6269          redo B;          next B;
6270        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6271              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6272                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6273                  !!!cp ('t272');              !!!cp ('t272');
6274                  ## As if </option>              ## As if </option>
6275                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6276                } else {            } else {
6277                  !!!cp ('t273');              !!!cp ('t273');
6278                }            }
6279    
6280                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6281                !!!next-token;            !!!nack ('t273.1');
6282                redo B;            !!!next-token;
6283              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6284                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6285                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6286                  ## As if </option>              !!!cp ('t274');
6287                  pop @{$self->{open_elements}};              ## As if </option>
6288                } else {              pop @{$self->{open_elements}};
6289                  !!!cp ('t275');            } else {
6290                }              !!!cp ('t275');
6291              }
6292    
6293                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6294                  !!!cp ('t276');              !!!cp ('t276');
6295                  ## As if </optgroup>              ## As if </optgroup>
6296                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6297                } else {            } else {
6298                  !!!cp ('t277');              !!!cp ('t277');
6299                }            }
6300    
6301                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6302                !!!next-token;            !!!nack ('t277.1');
6303                redo B;            !!!next-token;
6304              } elsif ($token->{tag_name} eq 'select') {            next B;
6305  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ({
6306                !!!parse-error (type => 'not closed:select');                     select => 1, input => 1, textarea => 1,
6307                ## As if </select> instead                   }->{$token->{tag_name}} or
6308                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6309                my $i;                    {
6310                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
6311                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
6312                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
6313                    !!!cp ('t278');                    }->{$token->{tag_name}})) {
6314                    $i = $_;            ## TODO: The type below is not good - <select> is replaced by </select>
6315                    last INSCOPE;            !!!parse-error (type => 'not closed', text => 'select',
6316                  } elsif ({                            token => $token);
6317                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
6318                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
6319                    !!!cp ('t279');            ## have an element in table scope
6320                    last INSCOPE;            my $i;
6321                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6322                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6323                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6324                  !!!cp ('t280');                !!!cp ('t278');
6325                  !!!parse-error (type => 'unmatched end tag:select');                $i = $_;
6326                  ## Ignore the token                last INSCOPE;
6327                  !!!next-token;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6328                  redo B;                !!!cp ('t279');
6329                }                last INSCOPE;
6330                }
6331              } # INSCOPE
6332              unless (defined $i) {
6333                !!!cp ('t280');
6334                !!!parse-error (type => 'unmatched end tag',
6335                                text => 'select', token => $token);
6336                ## Ignore the token
6337                !!!nack ('t280.1');
6338                !!!next-token;
6339                next B;
6340              }
6341                                
6342                !!!cp ('t281');            !!!cp ('t281');
6343                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6344    
6345                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6346    
6347                !!!next-token;            if ($token->{tag_name} eq 'select') {
6348                redo B;              !!!nack ('t281.2');
6349                !!!next-token;
6350                next B;
6351              } else {
6352                !!!cp ('t281.1');
6353                !!!ack-later;
6354                ## Reprocess the token.
6355                next B;
6356              }
6357          } else {          } else {
6358            !!!cp ('t282');            !!!cp ('t282');
6359            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
6360                              text => $token->{tag_name}, token => $token);
6361            ## Ignore the token            ## Ignore the token
6362              !!!nack ('t282.1');
6363            !!!next-token;            !!!next-token;
6364            redo B;            next B;
6365          }          }
6366        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6367              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6368                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6369                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6370                  !!!cp ('t283');              !!!cp ('t283');
6371                  ## As if </option>              ## As if </option>
6372                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6373                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6374                  !!!cp ('t284');              !!!cp ('t284');
6375                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6376                } else {            } else {
6377                  !!!cp ('t285');              !!!cp ('t285');
6378                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6379                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6380                }              ## Ignore the token
6381                !!!next-token;            }
6382                redo B;            !!!nack ('t285.1');
6383              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6384                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6385                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6386                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6387                } else {              !!!cp ('t286');
6388                  !!!cp ('t287');              pop @{$self->{open_elements}};
6389                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
6390                  ## Ignore the token              !!!cp ('t287');
6391                }              !!!parse-error (type => 'unmatched end tag',
6392                !!!next-token;                              text => $token->{tag_name}, token => $token);
6393                redo B;              ## Ignore the token
6394              } elsif ($token->{tag_name} eq 'select') {            }
6395                ## have an element in table scope            !!!nack ('t287.1');
6396                my $i;            !!!next-token;
6397                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6398                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6399                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6400                    !!!cp ('t288');            my $i;
6401                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6402                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6403                  } elsif ({              if ($node->[1] & SELECT_EL) {
6404                            table => 1, html => 1,                !!!cp ('t288');
6405                           }->{$node->[1]}) {                $i = $_;
6406                    !!!cp ('t289');                last INSCOPE;
6407                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6408                  }                !!!cp ('t289');
6409                } # INSCOPE                last INSCOPE;
6410                unless (defined $i) {              }
6411                  !!!cp ('t290');            } # INSCOPE
6412                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
6413                  ## Ignore the token              !!!cp ('t290');
6414                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6415                  redo B;                              text => $token->{tag_name}, token => $token);
6416                }              ## Ignore the token
6417                !!!nack ('t290.1');
6418                !!!next-token;
6419                next B;
6420              }
6421                                
6422                !!!cp ('t291');            !!!cp ('t291');
6423                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6424    
6425                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6426    
6427                !!!next-token;            !!!nack ('t291.1');
6428                redo B;            !!!next-token;
6429              } elsif ({            next B;
6430                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6431                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6432                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6433                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6434                     }->{$token->{tag_name}}) {
6435  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6436                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6437                              text => $token->{tag_name}, token => $token);
6438                                
6439                ## have an element in table scope            ## have an element in table scope
6440                my $i;            my $i;
6441                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6442                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6443                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6444                    !!!cp ('t292');                !!!cp ('t292');
6445                    $i = $_;                $i = $_;
6446                    last INSCOPE;                last INSCOPE;
6447                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6448                            table => 1, html => 1,                !!!cp ('t293');
6449                           }->{$node->[1]}) {                last INSCOPE;
6450                    !!!cp ('t293');              }
6451                    last INSCOPE;            } # INSCOPE
6452                  }            unless (defined $i) {
6453                } # INSCOPE              !!!cp ('t294');
6454                unless (defined $i) {              ## Ignore the token
6455                  !!!cp ('t294');              !!!nack ('t294.1');
6456                  ## Ignore the token              !!!next-token;
6457                  !!!next-token;              next B;
6458                  redo B;            }
               }  
6459                                
6460                ## As if </select>            ## As if </select>
6461                ## have an element in table scope            ## have an element in table scope
6462                undef $i;            undef $i;
6463                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6464                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6465                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6466                    !!!cp ('t295');                !!!cp ('t295');
6467                    $i = $_;                $i = $_;
6468                    last INSCOPE;                last INSCOPE;
6469                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6470  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6471                    !!!cp ('t296');                !!!cp ('t296');
6472                    last INSCOPE;                last INSCOPE;
6473                  }              }
6474                } # INSCOPE            } # INSCOPE
6475                unless (defined $i) {            unless (defined $i) {
6476                  !!!cp ('t297');              !!!cp ('t297');
6477  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6478                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
6479                  ## Ignore the </select> token                              text => 'select', token => $token);
6480                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6481                  redo B;              !!!nack ('t297.1');
6482                }              !!!next-token; ## TODO: ok?
6483                next B;
6484              }
6485                                
6486                !!!cp ('t298');            !!!cp ('t298');
6487                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6488    
6489                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6490    
6491                ## reprocess            !!!ack-later;
6492                redo B;            ## reprocess
6493              next B;
6494          } else {          } else {
6495            !!!cp ('t299');            !!!cp ('t299');
6496            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
6497                              text => $token->{tag_name}, token => $token);
6498            ## Ignore the token            ## Ignore the token
6499              !!!nack ('t299.3');
6500            !!!next-token;            !!!next-token;
6501            redo B;            next B;
6502            }
6503          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6504            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6505                    @{$self->{open_elements}} == 1) { # redundant, maybe
6506              !!!cp ('t299.1');
6507              !!!parse-error (type => 'in body:#eof', token => $token);
6508            } else {
6509              !!!cp ('t299.2');
6510          }          }
6511    
6512            ## Stop parsing.
6513            last B;
6514        } else {        } else {
6515          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6516        }        }
# Line 5135  sub _tree_construction_main ($) { Line 6526  sub _tree_construction_main ($) {
6526            unless (length $token->{data}) {            unless (length $token->{data}) {
6527              !!!cp ('t300');              !!!cp ('t300');
6528              !!!next-token;              !!!next-token;
6529              redo B;              next B;
6530            }            }
6531          }          }
6532                    
6533          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6534            !!!cp ('t301');            !!!cp ('t301');
6535            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
6536    
6537            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6538          } else {          } else {
# Line 5149  sub _tree_construction_main ($) { Line 6540  sub _tree_construction_main ($) {
6540          }          }
6541                    
6542          ## "after body" insertion mode          ## "after body" insertion mode
6543          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6544    
6545          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6546          ## reprocess          ## reprocess
6547          redo B;          next B;
6548        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6549          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6550            !!!cp ('t303');            !!!cp ('t303');
6551            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6552                              text => $token->{tag_name}, token => $token);
6553                        
6554            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6555          } else {          } else {
# Line 5165  sub _tree_construction_main ($) { Line 6557  sub _tree_construction_main ($) {
6557          }          }
6558    
6559          ## "after body" insertion mode          ## "after body" insertion mode
6560          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6561                            text => $token->{tag_name}, token => $token);
6562    
6563          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6564            !!!ack-later;
6565          ## reprocess          ## reprocess
6566          redo B;          next B;
6567        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6568          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6569            !!!cp ('t305');            !!!cp ('t305');
6570            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6571                              text => $token->{tag_name}, token => $token);
6572                        
6573            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6574            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5185  sub _tree_construction_main ($) { Line 6580  sub _tree_construction_main ($) {
6580          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6581            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6582              !!!cp ('t307');              !!!cp ('t307');
6583              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
6584                                text => 'html', token => $token);
6585              ## Ignore the token              ## Ignore the token
6586              !!!next-token;              !!!next-token;
6587              redo B;              next B;
6588            } else {            } else {
6589              !!!cp ('t308');              !!!cp ('t308');
6590              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6591              !!!next-token;              !!!next-token;
6592              redo B;              next B;
6593            }            }
6594          } else {          } else {
6595            !!!cp ('t309');            !!!cp ('t309');
6596            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
6597                              text => $token->{tag_name}, token => $token);
6598    
6599            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6600            ## reprocess            ## reprocess
6601            redo B;            next B;
6602          }          }
6603          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6604            !!!cp ('t309.2');
6605            ## Stop parsing
6606            last B;
6607        } else {        } else {
6608          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6609        }        }
# Line 5214  sub _tree_construction_main ($) { Line 6615  sub _tree_construction_main ($) {
6615            unless (length $token->{data}) {            unless (length $token->{data}) {
6616              !!!cp ('t310');              !!!cp ('t310');
6617              !!!next-token;              !!!next-token;
6618              redo B;              next B;
6619            }            }
6620          }          }
6621                    
6622          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6623            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6624              !!!cp ('t311');              !!!cp ('t311');
6625              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
6626            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6627              !!!cp ('t312');              !!!cp ('t312');
6628              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6629            } else { # "after html frameset"            } else { # "after after frameset"
6630              !!!cp ('t313');              !!!cp ('t313');
6631              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "after frameset" insertion mode.  
             !!!parse-error (type => 'after frameset:#character');  
6632            }            }
6633                        
6634            ## Ignore the token.            ## Ignore the token.
# Line 5242  sub _tree_construction_main ($) { Line 6639  sub _tree_construction_main ($) {
6639              !!!cp ('t315');              !!!cp ('t315');
6640              !!!next-token;              !!!next-token;
6641            }            }
6642            redo B;            next B;
6643          }          }
6644                    
6645          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6646        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t316');  
           !!!parse-error (type => 'after html:'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
6647          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6648              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6649            !!!cp ('t318');            !!!cp ('t318');
6650            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6651              !!!nack ('t318.1');
6652            !!!next-token;            !!!next-token;
6653            redo B;            next B;
6654          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6655                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6656            !!!cp ('t319');            !!!cp ('t319');
6657            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6658            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6659              !!!ack ('t319.1');
6660            !!!next-token;            !!!next-token;
6661            redo B;            next B;
6662          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6663            !!!cp ('t320');            !!!cp ('t320');
6664            ## NOTE: As if in body.            ## NOTE: As if in head.
6665            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6666            redo B;            next B;
6667    
6668              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6669              ## has no parse error.
6670          } else {          } else {
6671            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6672              !!!cp ('t321');              !!!cp ('t321');
6673              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
6674            } else {                              text => $token->{tag_name}, token => $token);
6675              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6676              !!!cp ('t322');              !!!cp ('t322');
6677              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
6678                                text => $token->{tag_name}, token => $token);
6679              } else { # "after after frameset"
6680                !!!cp ('t322.2');
6681                !!!parse-error (type => 'after after frameset',
6682                                text => $token->{tag_name}, token => $token);
6683            }            }
6684            ## Ignore the token            ## Ignore the token
6685              !!!nack ('t322.1');
6686            !!!next-token;            !!!next-token;
6687            redo B;            next B;
6688          }          }
6689        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t323');  
           !!!parse-error (type => 'after html:/'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
6690          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6691              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6692            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6693                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6694              !!!cp ('t325');              !!!cp ('t325');
6695              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6696                                text => $token->{tag_name}, token => $token);
6697              ## Ignore the token              ## Ignore the token
6698              !!!next-token;              !!!next-token;
6699            } else {            } else {
# Line 5313  sub _tree_construction_main ($) { Line 6703  sub _tree_construction_main ($) {
6703            }            }
6704    
6705            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6706                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6707              !!!cp ('t327');              !!!cp ('t327');
6708              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6709            } else {            } else {
6710              !!!cp ('t328');              !!!cp ('t328');
6711            }            }
6712            redo B;            next B;
6713          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6714                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6715            !!!cp ('t329');            !!!cp ('t329');
6716            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6717            !!!next-token;            !!!next-token;
6718            redo B;            next B;
6719          } else {          } else {
6720            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6721              !!!cp ('t330');              !!!cp ('t330');
6722              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/',
6723            } else {                              text => $token->{tag_name}, token => $token);
6724              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6725                !!!cp ('t330.1');
6726                !!!parse-error (type => 'after frameset:/',
6727                                text => $token->{tag_name}, token => $token);
6728              } else { # "after after html"
6729              !!!cp ('t331');              !!!cp ('t331');
6730              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after after frameset:/',
6731                                text => $token->{tag_name}, token => $token);
6732            }            }
6733            ## Ignore the token            ## Ignore the token
6734            !!!next-token;            !!!next-token;
6735            redo B;            next B;
6736          }          }
6737          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6738            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6739                    @{$self->{open_elements}} == 1) { # redundant, maybe
6740              !!!cp ('t331.1');
6741              !!!parse-error (type => 'in body:#eof', token => $token);
6742            } else {
6743              !!!cp ('t331.2');
6744            }
6745            
6746            ## Stop parsing
6747            last B;
6748        } else {        } else {
6749          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6750        }        }
# Line 5352  sub _tree_construction_main ($) { Line 6759  sub _tree_construction_main ($) {
6759        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6760          !!!cp ('t332');          !!!cp ('t332');
6761          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6762          $script_start_tag->($insert);          $script_start_tag->();
6763          redo B;          next B;
6764        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6765          !!!cp ('t333');          !!!cp ('t333');
6766          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6767          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6768          redo B;          next B;
6769        } elsif ({        } elsif ({
6770                  base => 1, link => 1,                  base => 1, link => 1,
6771                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6772          !!!cp ('t334');          !!!cp ('t334');
6773          ## 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
6774          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6775          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6776            !!!ack ('t334.1');
6777          !!!next-token;          !!!next-token;
6778          redo B;          next B;
6779        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6780          ## 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
6781          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6782          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.
6783    
6784          unless ($self->{confident}) {          unless ($self->{confident}) {
6785            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6786              !!!cp ('t335');              !!!cp ('t335');
6787                ## NOTE: Whether the encoding is supported or not is handled
6788                ## in the {change_encoding} callback.
6789              $self->{change_encoding}              $self->{change_encoding}
6790                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6791                            
6792              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6793                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6794                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6795                                           ->{has_reference});                                           ->{has_reference});
6796            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6797              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6798                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6799                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6800                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6801                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6802                !!!cp ('t336');                !!!cp ('t336');
6803                  ## NOTE: Whether the encoding is supported or not is handled
6804                  ## in the {change_encoding} callback.
6805                $self->{change_encoding}                $self->{change_encoding}
6806                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6807                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6808                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6809                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5416  sub _tree_construction_main ($) { Line 6827  sub _tree_construction_main ($) {
6827            }            }
6828          }          }
6829    
6830            !!!ack ('t338.1');
6831          !!!next-token;          !!!next-token;
6832          redo B;          next B;
6833        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6834          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6835          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6836          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6837            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6838        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6839          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6840                                
6841          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6842              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6843            !!!cp ('t342');            !!!cp ('t342');
6844            ## Ignore the token            ## Ignore the token
6845          } else {          } else {
# Line 5450  sub _tree_construction_main ($) { Line 6853  sub _tree_construction_main ($) {
6853              }              }
6854            }            }
6855          }          }
6856            !!!nack ('t343.1');
6857          !!!next-token;          !!!next-token;
6858          redo B;          next B;
6859        } elsif ({        } elsif ({
6860                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6861                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6862                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6863                  listing => 1, menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6864                  pre => 1,                  pre => 1, listing => 1,
6865                    form => 1,
6866                    table => 1,
6867                    hr => 1,
6868                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6869            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6870              !!!cp ('t350');
6871              !!!parse-error (type => 'in form:form', token => $token);
6872              ## Ignore the token
6873              !!!nack ('t350.1');
6874              !!!next-token;
6875              next B;
6876            }
6877    
6878          ## has a p element in scope          ## has a p element in scope
6879          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6880            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6881              !!!cp ('t344');              !!!cp ('t344');
6882              !!!back-token;              !!!back-token; # <form>
6883              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6884              redo B;                        line => $token->{line}, column => $token->{column}};
6885            } elsif ({              next B;
6886                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6887              !!!cp ('t345');              !!!cp ('t345');
6888              last INSCOPE;              last INSCOPE;
6889            }            }
6890          } # INSCOPE          } # INSCOPE
6891                        
6892          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6893          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6894              !!!nack ('t346.1');
6895            !!!next-token;            !!!next-token;
6896            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6897              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5489  sub _tree_construction_main ($) { Line 6904  sub _tree_construction_main ($) {
6904            } else {            } else {
6905              !!!cp ('t348');              !!!cp ('t348');
6906            }            }
6907          } else {          } elsif ($token->{tag_name} eq 'form') {
6908            !!!cp ('t347');            !!!cp ('t347.1');
6909              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6910    
6911              !!!nack ('t347.2');
6912            !!!next-token;            !!!next-token;
6913          }          } elsif ($token->{tag_name} eq 'table') {
6914          redo B;            !!!cp ('t382');
6915        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6916          if (defined $self->{form_element}) {            
6917            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6918            !!!parse-error (type => 'in form:form');  
6919            ## Ignore the token            !!!nack ('t382.1');
6920              !!!next-token;
6921            } elsif ($token->{tag_name} eq 'hr') {
6922              !!!cp ('t386');
6923              pop @{$self->{open_elements}};
6924            
6925              !!!nack ('t386.1');
6926            !!!next-token;            !!!next-token;
           redo B;  
6927          } else {          } else {
6928            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!cp ('t351');  
               !!!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]}) {  
               !!!cp ('t352');  
               last INSCOPE;  
             }  
           } # INSCOPE  
               
           !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
           $self->{form_element} = $self->{open_elements}->[-1]->[0];  
6929            !!!next-token;            !!!next-token;
           redo B;  
6930          }          }
6931        } elsif ($token->{tag_name} eq 'li') {          next B;
6932          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6933          ## has a p element in scope          ## has a p element in scope
6934          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6935            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6936              !!!cp ('t353');              !!!cp ('t353');
6937              !!!back-token;              !!!back-token; # <x>
6938              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6939              redo B;                        line => $token->{line}, column => $token->{column}};
6940            } elsif ({              next B;
6941                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6942              !!!cp ('t354');              !!!cp ('t354');
6943              last INSCOPE;              last INSCOPE;
6944            }            }
# Line 5543  sub _tree_construction_main ($) { Line 6947  sub _tree_construction_main ($) {
6947          ## Step 1          ## Step 1
6948          my $i = -1;          my $i = -1;
6949          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6950            my $li_or_dtdd = {li => {li => 1},
6951                              dt => {dt => 1, dd => 1},
6952                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6953          LI: {          LI: {
6954            ## Step 2            ## Step 2
6955            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6956              if ($i != -1) {              if ($i != -1) {
6957                !!!cp ('t355');                !!!cp ('t355');
6958                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6959                                $self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
6960                                      ->manakai_local_name,
6961                                  token => $token);
6962              } else {              } else {
6963                !!!cp ('t356');                !!!cp ('t356');
6964              }              }
# Line 5560  sub _tree_construction_main ($) { Line 6969  sub _tree_construction_main ($) {
6969            }            }
6970                        
6971            ## Step 3            ## Step 3
6972            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6973                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6974                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6975                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6976                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6977                  not ($node->[1] & DIV_EL)) {
6978              !!!cp ('t358');              !!!cp ('t358');
6979              last LI;              last LI;
6980            }            }
# Line 5576  sub _tree_construction_main ($) { Line 6986  sub _tree_construction_main ($) {
6986            redo LI;            redo LI;
6987          } # LI          } # LI
6988                        
6989          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6990            !!!nack ('t359.1');
6991          !!!next-token;          !!!next-token;
6992          redo B;          next B;
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!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]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## 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') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
6993        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6994          ## has a p element in scope          ## has a p element in scope
6995          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6996            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6997              !!!cp ('t367');              !!!cp ('t367');
6998              !!!back-token;              !!!back-token; # <plaintext>
6999              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
7000              redo B;                        line => $token->{line}, column => $token->{column}};
7001            } elsif ({              next B;
7002                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
7003              !!!cp ('t368');              !!!cp ('t368');
7004              last INSCOPE;              last INSCOPE;
7005            }            }
7006          } # INSCOPE          } # INSCOPE
7007                        
7008          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7009                        
7010          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7011                        
7012            !!!nack ('t368.1');
7013          !!!next-token;          !!!next-token;
7014          redo B;          next B;
7015        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
7016          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7017            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
7018            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
7019              !!!cp ('t371');              !!!cp ('t371');
7020              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
7021                            
7022              !!!back-token;              !!!back-token; # <a>
7023              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
7024              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
7025                $formatting_end_tag->($token);
7026                            
7027              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
7028                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5692  sub _tree_construction_main ($) { Line 7047  sub _tree_construction_main ($) {
7047                        
7048          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7049    
7050          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7051          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7052    
7053            !!!nack ('t374.1');
7054          !!!next-token;          !!!next-token;
7055          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}}) {  
         !!!cp ('t375');  
         $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;  
7056        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
7057          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7058    
7059          ## has a |nobr| element in scope          ## has a |nobr| element in scope
7060          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7061            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7062            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
7063              !!!cp ('t376');              !!!cp ('t376');
7064              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
7065              !!!back-token;              !!!back-token; # <nobr>
7066              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7067              redo B;                        line => $token->{line}, column => $token->{column}};
7068            } elsif ({              next B;
7069                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7070              !!!cp ('t377');              !!!cp ('t377');
7071              last INSCOPE;              last INSCOPE;
7072            }            }
7073          } # INSCOPE          } # INSCOPE
7074                    
7075          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7076          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7077                    
7078            !!!nack ('t377.1');
7079          !!!next-token;          !!!next-token;
7080          redo B;          next B;
7081        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7082          ## has a button element in scope          ## has a button element in scope
7083          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7084            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7085            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7086              !!!cp ('t378');              !!!cp ('t378');
7087              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
7088              !!!back-token;              !!!back-token; # <button>
7089              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7090              redo B;                        line => $token->{line}, column => $token->{column}};
7091            } elsif ({              next B;
7092                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7093              !!!cp ('t379');              !!!cp ('t379');
7094              last INSCOPE;              last INSCOPE;
7095            }            }
# Line 5757  sub _tree_construction_main ($) { Line 7097  sub _tree_construction_main ($) {
7097                        
7098          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7099                        
7100          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7101    
7102          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
7103    
7104          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7105    
7106            !!!nack ('t379.1');
7107          !!!next-token;          !!!next-token;
7108          redo B;          next B;
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $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') {  
             !!!cp ('t382');  
             !!!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]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
7109        } elsif ({        } elsif ({
7110                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7111                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7112                  image => 1,                  noembed => 1,
7113                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7114                    noscript => 0, ## TODO: 1 if scripting is enabled
7115                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7116          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7117            !!!cp ('t384');            !!!cp ('t381');
7118            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
7119          } else {          } else {
7120            !!!cp ('t385');            !!!cp ('t399');
7121          }          }
7122            ## NOTE: There is an "as if in body" code clone.
7123          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7124          $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') {  
             !!!cp ('t386');  
             !!!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]}) {  
             !!!cp ('t387');  
             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') {  
         !!!cp ('t388');  
         $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;  
7125        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7126          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7127                    
7128          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7129            !!!cp ('t389');            !!!cp ('t389');
7130            ## Ignore the token            ## Ignore the token
7131              !!!nack ('t389'); ## NOTE: Not acknowledged.
7132            !!!next-token;            !!!next-token;
7133            redo B;            next B;
7134          } else {          } else {
7135              !!!ack ('t391.1');
7136    
7137            my $at = $token->{attributes};            my $at = $token->{attributes};
7138            my $form_attrs;            my $form_attrs;
7139            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5874  sub _tree_construction_main ($) { Line 7143  sub _tree_construction_main ($) {
7143            delete $at->{prompt};            delete $at->{prompt};
7144            my @tokens = (            my @tokens = (
7145                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7146                           attributes => $form_attrs},                           attributes => $form_attrs,
7147                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7148                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7149                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7150                            {type => START_TAG_TOKEN, tag_name => 'p',
7151                             line => $token->{line}, column => $token->{column}},
7152                            {type => START_TAG_TOKEN, tag_name => 'label',
7153                             line => $token->{line}, column => $token->{column}},
7154                         );                         );
7155            if ($prompt_attr) {            if ($prompt_attr) {
7156              !!!cp ('t390');              !!!cp ('t390');
7157              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7158                               #line => $token->{line}, column => $token->{column},
7159                              };
7160            } else {            } else {
7161              !!!cp ('t391');              !!!cp ('t391');
7162              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7163                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7164                               #line => $token->{line}, column => $token->{column},
7165                              }; # SHOULD
7166              ## TODO: make this configurable              ## TODO: make this configurable
7167            }            }
7168            push @tokens,            push @tokens,
7169                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7170                             line => $token->{line}, column => $token->{column}},
7171                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7172                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7173                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7174                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7175                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7176            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7177                             line => $token->{line}, column => $token->{column}},
7178                            {type => END_TAG_TOKEN, tag_name => 'form',
7179                             line => $token->{line}, column => $token->{column}};
7180            !!!back-token (@tokens);            !!!back-token (@tokens);
7181            redo B;            !!!next-token;
7182              next B;
7183          }          }
7184        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7185          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7186          my $el;          my $el;
7187          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7188                    
7189          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7190          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5911  sub _tree_construction_main ($) { Line 7193  sub _tree_construction_main ($) {
7193          $insert->($el);          $insert->($el);
7194                    
7195          my $text = '';          my $text = '';
7196            !!!nack ('t392.1');
7197          !!!next-token;          !!!next-token;
7198          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7199            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5941  sub _tree_construction_main ($) { Line 7224  sub _tree_construction_main ($) {
7224            ## Ignore the token            ## Ignore the token
7225          } else {          } else {
7226            !!!cp ('t398');            !!!cp ('t398');
7227            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7228          }          }
7229          !!!next-token;          !!!next-token;
7230            next B;
7231          } elsif ($token->{tag_name} eq 'rt' or
7232                   $token->{tag_name} eq 'rp') {
7233            ## has a |ruby| element in scope
7234            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7235              my $node = $self->{open_elements}->[$_];
7236              if ($node->[1] & RUBY_EL) {
7237                !!!cp ('t398.1');
7238                ## generate implied end tags
7239                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7240                  !!!cp ('t398.2');
7241                  pop @{$self->{open_elements}};
7242                }
7243                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7244                  !!!cp ('t398.3');
7245                  !!!parse-error (type => 'not closed',
7246                                  text => $self->{open_elements}->[-1]->[0]
7247                                      ->manakai_local_name,
7248                                  token => $token);
7249                  pop @{$self->{open_elements}}
7250                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7251                }
7252                last INSCOPE;
7253              } elsif ($node->[1] & SCOPING_EL) {
7254                !!!cp ('t398.4');
7255                last INSCOPE;
7256              }
7257            } # INSCOPE
7258    
7259            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7260    
7261            !!!nack ('t398.5');
7262            !!!next-token;
7263          redo B;          redo B;
7264        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7265                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
7266          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
7267    
7268          ## TODO: associate with $self->{form_element} if defined          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7269    
7270            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7271    
7272            ## "adjust foreign attributes" - done in insert-element-f
7273                    
7274          $self->{insertion_mode} = IN_SELECT_IM;          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7275            
7276            if ($self->{self_closing}) {
7277              pop @{$self->{open_elements}};
7278              !!!ack ('t398.1');
7279            } else {
7280              !!!cp ('t398.2');
7281              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7282              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7283              ## mode, "in body" (not "in foreign content") secondary insertion
7284              ## mode, maybe.
7285            }
7286    
7287          !!!next-token;          !!!next-token;
7288          redo B;          next B;
7289        } elsif ({        } elsif ({
7290                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7291                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5973  sub _tree_construction_main ($) { Line 7293  sub _tree_construction_main ($) {
7293                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7294                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7295          !!!cp ('t401');          !!!cp ('t401');
7296          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
7297                            text => $token->{tag_name}, token => $token);
7298          ## Ignore the token          ## Ignore the token
7299            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7300          !!!next-token;          !!!next-token;
7301          redo B;          next B;
7302                    
7303          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7304        } else {        } else {
7305          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
7306              !!!cp ('t384');
7307              !!!parse-error (type => 'image', token => $token);
7308              $token->{tag_name} = 'img';
7309            } else {
7310              !!!cp ('t385');
7311            }
7312    
7313            ## NOTE: There is an "as if <br>" code clone.
7314          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7315                    
7316          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7317    
7318            if ({
7319                 applet => 1, marquee => 1, object => 1,
7320                }->{$token->{tag_name}}) {
7321              !!!cp ('t380');
7322              push @$active_formatting_elements, ['#marker', ''];
7323              !!!nack ('t380.1');
7324            } elsif ({
7325                      b => 1, big => 1, em => 1, font => 1, i => 1,
7326                      s => 1, small => 1, strile => 1,
7327                      strong => 1, tt => 1, u => 1,
7328                     }->{$token->{tag_name}}) {
7329              !!!cp ('t375');
7330              push @$active_formatting_elements, $self->{open_elements}->[-1];
7331              !!!nack ('t375.1');
7332            } elsif ($token->{tag_name} eq 'input') {
7333              !!!cp ('t388');
7334              ## TODO: associate with $self->{form_element} if defined
7335              pop @{$self->{open_elements}};
7336              !!!ack ('t388.2');
7337            } elsif ({
7338                      area => 1, basefont => 1, bgsound => 1, br => 1,
7339                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7340                      #image => 1,
7341                     }->{$token->{tag_name}}) {
7342              !!!cp ('t388.1');
7343              pop @{$self->{open_elements}};
7344              !!!ack ('t388.3');
7345            } elsif ($token->{tag_name} eq 'select') {
7346              ## TODO: associate with $self->{form_element} if defined
7347            
7348              if ($self->{insertion_mode} & TABLE_IMS or
7349                  $self->{insertion_mode} & BODY_TABLE_IMS or
7350                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7351                !!!cp ('t400.1');
7352                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7353              } else {
7354                !!!cp ('t400.2');
7355                $self->{insertion_mode} = IN_SELECT_IM;
7356              }
7357              !!!nack ('t400.3');
7358            } else {
7359              !!!nack ('t402');
7360            }
7361                    
7362          !!!next-token;          !!!next-token;
7363          redo B;          next B;
7364        }        }
7365      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7366        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7367          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7368              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7369            for (@{$self->{open_elements}}) {          INSCOPE: {
7370              unless ({            for (reverse @{$self->{open_elements}}) {
7371                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7372                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7373                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7374                      }->{$_->[1]}) {                last INSCOPE;
7375                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
7376                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
7377              } else {                last;
               !!!cp ('t404');  
7378              }              }
7379            }            }
7380    
7381            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7382                              text => $token->{tag_name}, token => $token);
7383              ## NOTE: Ignore the token.
7384            !!!next-token;            !!!next-token;
7385            redo B;            next B;
7386          } else {          } # INSCOPE
7387            !!!cp ('t405');  
7388            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          for (@{$self->{open_elements}}) {
7389            ## Ignore the token            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7390            !!!next-token;              !!!cp ('t403');
7391            redo B;              !!!parse-error (type => 'not closed',
7392                                text => $_->[0]->manakai_local_name,
7393                                token => $token);
7394                last;
7395              } else {
7396                !!!cp ('t404');
7397              }
7398          }          }
7399    
7400            $self->{insertion_mode} = AFTER_BODY_IM;
7401            !!!next-token;
7402            next B;
7403        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7404          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
7405            ## up-to-date, though it has same effect as speced.
7406            if (@{$self->{open_elements}} > 1 and
7407                $self->{open_elements}->[1]->[1] & BODY_EL) {
7408            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7409            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7410              !!!cp ('t406');              !!!cp ('t406');
7411              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
7412                                text => $self->{open_elements}->[1]->[0]
7413                                    ->manakai_local_name,
7414                                token => $token);
7415            } else {            } else {
7416              !!!cp ('t407');              !!!cp ('t407');
7417            }            }
7418            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7419            ## reprocess            ## reprocess
7420            redo B;            next B;
7421          } else {          } else {
7422            !!!cp ('t408');            !!!cp ('t408');
7423            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7424                              text => $token->{tag_name}, token => $token);
7425            ## Ignore the token            ## Ignore the token
7426            !!!next-token;            !!!next-token;
7427            redo B;            next B;
7428          }          }
7429        } elsif ({        } elsif ({
7430                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7431                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7432                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7433                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7434                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7435                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7436          ## has an element in scope          ## has an element in scope
7437          my $i;          my $i;
7438          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7439            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7440            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             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]}) {  
               !!!cp ('t409');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
7441              !!!cp ('t410');              !!!cp ('t410');
7442              $i = $_;              $i = $_;
7443              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7444            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7445              !!!cp ('t411');              !!!cp ('t411');
7446              last INSCOPE;              last INSCOPE;
7447            }            }
7448          } # INSCOPE          } # INSCOPE
7449            
7450          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7451            if (defined $i) {            !!!cp ('t413');
7452              !!!parse-error (type => 'unmatched end tag',
7453                              text => $token->{tag_name}, token => $token);
7454              ## NOTE: Ignore the token.
7455            } else {
7456              ## Step 1. generate implied end tags
7457              while ({
7458                      ## END_TAG_OPTIONAL_EL
7459                      dd => ($token->{tag_name} ne 'dd'),
7460                      dt => ($token->{tag_name} ne 'dt'),
7461                      li => ($token->{tag_name} ne 'li'),
7462                      p => 1,
7463                      rt => 1,
7464                      rp => 1,
7465                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7466                !!!cp ('t409');
7467                pop @{$self->{open_elements}};
7468              }
7469    
7470              ## Step 2.
7471              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7472                      ne $token->{tag_name}) {
7473              !!!cp ('t412');              !!!cp ('t412');
7474              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7475                                text => $self->{open_elements}->[-1]->[0]
7476                                    ->manakai_local_name,
7477                                token => $token);
7478            } else {            } else {
7479              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
7480            }            }
7481          }  
7482                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
7483            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7484          } elsif ($token->{tag_name} eq 'p') {  
7485            !!!cp ('t415');            ## Step 4.
7486            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
7487            my $el;                if {
7488            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
7489            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
7490          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7491          !!!next-token;          !!!next-token;
7492          redo B;          next B;
7493        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7494            undef $self->{form_element};
7495    
7496          ## has an element in scope          ## has an element in scope
7497            my $i;
7498          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7499            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7500            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
   
                  ## NOTE: The following elements never appear here, maybe.  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
7501              !!!cp ('t418');              !!!cp ('t418');
7502                $i = $_;
7503              last INSCOPE;              last INSCOPE;
7504            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7505              !!!cp ('t419');              !!!cp ('t419');
7506              last INSCOPE;              last INSCOPE;
7507            }            }
7508          } # INSCOPE          } # INSCOPE
7509            
7510          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
7511            !!!cp ('t421');            !!!cp ('t421');
7512            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7513                              text => $token->{tag_name}, token => $token);
7514              ## NOTE: Ignore the token.
7515            } else {
7516              ## Step 1. generate implied end tags
7517              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7518                !!!cp ('t417');
7519                pop @{$self->{open_elements}};
7520              }
7521              
7522              ## Step 2.
7523              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7524                      ne $token->{tag_name}) {
7525                !!!cp ('t417.1');
7526                !!!parse-error (type => 'not closed',
7527                                text => $self->{open_elements}->[-1]->[0]
7528                                    ->manakai_local_name,
7529                                token => $token);
7530              } else {
7531                !!!cp ('t420');
7532              }  
7533              
7534              ## Step 3.
7535              splice @{$self->{open_elements}}, $i;
7536          }          }
7537    
         undef $self->{form_element};  
7538          !!!next-token;          !!!next-token;
7539          redo B;          next B;
7540        } elsif ({        } elsif ({
7541                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7542                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6152  sub _tree_construction_main ($) { Line 7544  sub _tree_construction_main ($) {
7544          my $i;          my $i;
7545          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7546            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7547            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$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]}) {  
               !!!cp ('t422');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
   
7548              !!!cp ('t423');              !!!cp ('t423');
7549              $i = $_;              $i = $_;
7550              last INSCOPE;              last INSCOPE;
7551            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7552              !!!cp ('t424');              !!!cp ('t424');
7553              last INSCOPE;              last INSCOPE;
7554            }            }
7555          } # INSCOPE          } # INSCOPE
7556    
7557            unless (defined $i) { # has an element in scope
7558              !!!cp ('t425.1');
7559              !!!parse-error (type => 'unmatched end tag',
7560                              text => $token->{tag_name}, token => $token);
7561              ## NOTE: Ignore the token.
7562            } else {
7563              ## Step 1. generate implied end tags
7564              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7565                !!!cp ('t422');
7566                pop @{$self->{open_elements}};
7567              }
7568              
7569              ## Step 2.
7570              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7571                      ne $token->{tag_name}) {
7572                !!!cp ('t425');
7573                !!!parse-error (type => 'unmatched end tag',
7574                                text => $token->{tag_name}, token => $token);
7575              } else {
7576                !!!cp ('t426');
7577              }
7578    
7579              ## Step 3.
7580              splice @{$self->{open_elements}}, $i;
7581            }
7582                    
7583          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
7584            !!!cp ('t425');          next B;
7585            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
7586            ## has an element in scope
7587            my $i;
7588            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7589              my $node = $self->{open_elements}->[$_];
7590              if ($node->[1] & P_EL) {
7591                !!!cp ('t410.1');
7592                $i = $_;
7593                last INSCOPE;
7594              } elsif ($node->[1] & SCOPING_EL) {
7595                !!!cp ('t411.1');
7596                last INSCOPE;
7597              }
7598            } # INSCOPE
7599    
7600            if (defined $i) {
7601              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7602                      ne $token->{tag_name}) {
7603                !!!cp ('t412.1');
7604                !!!parse-error (type => 'not closed',
7605                                text => $self->{open_elements}->[-1]->[0]
7606                                    ->manakai_local_name,
7607                                token => $token);
7608              } else {
7609                !!!cp ('t414.1');
7610              }
7611    
7612              splice @{$self->{open_elements}}, $i;
7613          } else {          } else {
7614            !!!cp ('t426');            !!!cp ('t413.1');
7615              !!!parse-error (type => 'unmatched end tag',
7616                              text => $token->{tag_name}, token => $token);
7617    
7618              !!!cp ('t415.1');
7619              ## As if <p>, then reprocess the current token
7620              my $el;
7621              !!!create-element ($el, $HTML_NS, 'p',, $token);
7622              $insert->($el);
7623              ## NOTE: Not inserted into |$self->{open_elements}|.
7624          }          }
7625            
         splice @{$self->{open_elements}}, $i if defined $i;  
7626          !!!next-token;          !!!next-token;
7627          redo B;          next B;
7628        } elsif ({        } elsif ({
7629                  a => 1,                  a => 1,
7630                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6197  sub _tree_construction_main ($) { Line 7632  sub _tree_construction_main ($) {
7632                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7633                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7634          !!!cp ('t427');          !!!cp ('t427');
7635          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7636          redo B;          next B;
7637        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7638          !!!cp ('t428');          !!!cp ('t428');
7639          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
7640                            text => 'br', token => $token);
7641    
7642          ## As if <br>          ## As if <br>
7643          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7644                    
7645          my $el;          my $el;
7646          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7647          $insert->($el);          $insert->($el);
7648                    
7649          ## Ignore the token.          ## Ignore the token.
7650          !!!next-token;          !!!next-token;
7651          redo B;          next B;
7652        } elsif ({        } elsif ({
7653                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7654                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6226  sub _tree_construction_main ($) { Line 7662  sub _tree_construction_main ($) {
7662                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7663                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7664          !!!cp ('t429');          !!!cp ('t429');
7665          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
7666                            text => $token->{tag_name}, token => $token);
7667          ## Ignore the token          ## Ignore the token
7668          !!!next-token;          !!!next-token;
7669          redo B;          next B;
7670                    
7671          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7672                    
# Line 6240  sub _tree_construction_main ($) { Line 7677  sub _tree_construction_main ($) {
7677    
7678          ## Step 2          ## Step 2
7679          S2: {          S2: {
7680            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7681              ## Step 1              ## Step 1
7682              ## generate implied end tags              ## generate implied end tags
7683              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                  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]}) {  
7684                !!!cp ('t430');                !!!cp ('t430');
7685                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7686                !!!back-token;                ## ISSUE: <ruby><rt></rt> will also take this code path,
7687                $token = {type => END_TAG_TOKEN,                ## which seems wrong.
7688                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                pop @{$self->{open_elements}};
7689                redo B;                $node_i++;
7690              }              }
7691                    
7692              ## Step 2              ## Step 2
7693              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7694                        ne $token->{tag_name}) {
7695                !!!cp ('t431');                !!!cp ('t431');
7696                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7697                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7698                                  text => $self->{open_elements}->[-1]->[0]
7699                                      ->manakai_local_name,
7700                                  token => $token);
7701              } else {              } else {
7702                !!!cp ('t432');                !!!cp ('t432');
7703              }              }
7704                            
7705              ## Step 3              ## Step 3
7706              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7707    
7708              !!!next-token;              !!!next-token;
7709              last S2;              last S2;
7710            } else {            } else {
7711              ## Step 3              ## Step 3
7712              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7713                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7714                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7715                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7716                !!!cp ('t433');                !!!cp ('t433');
7717                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
7718                                  text => $token->{tag_name}, token => $token);
7719                ## Ignore the token                ## Ignore the token
7720                !!!next-token;                !!!next-token;
7721                last S2;                last S2;
# Line 6293  sub _tree_construction_main ($) { Line 7731  sub _tree_construction_main ($) {
7731            ## Step 5;            ## Step 5;
7732            redo S2;            redo S2;
7733          } # S2          } # S2
7734          redo B;          next B;
7735        }        }
7736      }      }
7737      redo B;      next B;
7738      } continue { # B
7739        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7740          ## NOTE: The code below is executed in cases where it does not have
7741          ## to be, but it it is harmless even in those cases.
7742          ## has an element in scope
7743          INSCOPE: {
7744            for (reverse 0..$#{$self->{open_elements}}) {
7745              my $node = $self->{open_elements}->[$_];
7746              if ($node->[1] & FOREIGN_EL) {
7747                last INSCOPE;
7748              } elsif ($node->[1] & SCOPING_EL) {
7749                last;
7750              }
7751            }
7752            
7753            ## NOTE: No foreign element in scope.
7754            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7755          } # INSCOPE
7756        }
7757    } # B    } # B
7758    
7759    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6304  sub _tree_construction_main ($) { Line 7761  sub _tree_construction_main ($) {
7761    ## TODO: script stuffs    ## TODO: script stuffs
7762  } # _tree_construct_main  } # _tree_construct_main
7763    
7764  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7765    my $class = shift;    my $class = shift;
7766    my $node = shift;    my $node = shift;
7767    my $s = \$_[0];    #my $s = \$_[0];
7768    my $onerror = $_[1];    my $onerror = $_[1];
7769      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7770    
7771    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7772    
# Line 6327  sub set_inner_html ($$$) { Line 7785  sub set_inner_html ($$$) {
7785      }      }
7786    
7787      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7788      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7789    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7790      ## TODO: If non-html element      ## TODO: If non-html element
7791    
7792      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7793    
7794    ## TODO: Support for $get_wrapper
7795    
7796      ## Step 1 # MUST      ## Step 1 # MUST
7797      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7798      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6342  sub set_inner_html ($$$) { Line 7802  sub set_inner_html ($$$) {
7802    
7803      ## Step 8 # MUST      ## Step 8 # MUST
7804      my $i = 0;      my $i = 0;
7805      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7806      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7807      $p->{set_next_char} = sub {      require Whatpm::Charset::DecodeHandle;
7808        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7809        $input = $get_wrapper->($input);
7810        $p->{set_nc} = sub {
7811        my $self = shift;        my $self = shift;
7812    
7813        pop @{$self->{prev_char}};        my $char = '';
7814        unshift @{$self->{prev_char}}, $self->{next_char};        if (defined $self->{next_nc}) {
7815            $char = $self->{next_nc};
7816            delete $self->{next_nc};
7817            $self->{nc} = ord $char;
7818          } else {
7819            $self->{char_buffer} = '';
7820            $self->{char_buffer_pos} = 0;
7821            
7822            my $count = $input->manakai_read_until
7823                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7824                 $self->{char_buffer_pos});
7825            if ($count) {
7826              $self->{line_prev} = $self->{line};
7827              $self->{column_prev} = $self->{column};
7828              $self->{column}++;
7829              $self->{nc}
7830                  = ord substr ($self->{char_buffer},
7831                                $self->{char_buffer_pos}++, 1);
7832              return;
7833            }
7834            
7835            if ($input->read ($char, 1)) {
7836              $self->{nc} = ord $char;
7837            } else {
7838              $self->{nc} = -1;
7839              return;
7840            }
7841          }
7842    
7843        $self->{next_char} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7844        $self->{next_char} = ord substr $$s, $i++, 1;        $p->{column}++;
7845        $column++;  
7846          if ($self->{nc} == 0x000A) { # LF
7847        if ($self->{next_char} == 0x000A) { # LF          $p->{line}++;
7848          $line++;          $p->{column} = 0;
         $column = 0;  
7849          !!!cp ('i1');          !!!cp ('i1');
7850        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{nc} == 0x000D) { # CR
7851          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7852          $self->{next_char} = 0x000A; # LF # MUST          my $next = '';
7853          $line++;          if ($input->read ($next, 1) and $next ne "\x0A") {
7854          $column = 0;            $self->{next_nc} = $next;
7855            }
7856            $self->{nc} = 0x000A; # LF # MUST
7857            $p->{line}++;
7858            $p->{column} = 0;
7859          !!!cp ('i2');          !!!cp ('i2');
7860        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{nc} == 0x0000) { # NULL
         $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
         !!!cp ('i3');  
       } elsif ($self->{next_char} == 0x0000) { # NULL  
7861          !!!cp ('i4');          !!!cp ('i4');
7862          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7863          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7864        }        }
7865      };      };
7866      $p->{prev_char} = [-1, -1, -1];  
7867      $p->{next_char} = -1;      $p->{read_until} = sub {
7868              #my ($scalar, $specials_range, $offset) = @_;
7869          return 0 if defined $p->{next_nc};
7870    
7871          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7872          my $offset = $_[2] || 0;
7873          
7874          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7875            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7876            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7877              substr ($_[0], $offset)
7878                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7879              my $count = $+[0] - $-[0];
7880              if ($count) {
7881                $p->{column} += $count;
7882                $p->{char_buffer_pos} += $count;
7883                $p->{line_prev} = $p->{line};
7884                $p->{column_prev} = $p->{column} - 1;
7885                $p->{prev_char} = [-1, -1, -1];
7886                $p->{nc} = -1;
7887              }
7888              return $count;
7889            } else {
7890              return 0;
7891            }
7892          } else {
7893            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7894            if ($count) {
7895              $p->{column} += $count;
7896              $p->{column_prev} += $count;
7897              $p->{prev_char} = [-1, -1, -1];
7898              $p->{nc} = -1;
7899            }
7900            return $count;
7901          }
7902        }; # $p->{read_until}
7903    
7904      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7905        my (%opt) = @_;        my (%opt) = @_;
7906        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7907          my $column = $opt{column};
7908          if (defined $opt{token} and defined $opt{token}->{line}) {
7909            $line = $opt{token}->{line};
7910            $column = $opt{token}->{column};
7911          }
7912          warn "Parse error ($opt{type}) at line $line column $column\n";
7913      };      };
7914      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7915        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7916      };      };
7917            
7918        my $char_onerror = sub {
7919          my (undef, $type, %opt) = @_;
7920          $ponerror->(layer => 'encode',
7921                      line => $p->{line}, column => $p->{column} + 1,
7922                      %opt, type => $type);
7923        }; # $char_onerror
7924        $input->onerror ($char_onerror);
7925    
7926      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7927      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7928    
# Line 6405  sub set_inner_html ($$$) { Line 7944  sub set_inner_html ($$$) {
7944          unless defined $p->{content_model};          unless defined $p->{content_model};
7945          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7946    
7947      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7948          ## TODO: Foreign element OK?
7949    
7950      ## Step 3      ## Step 3
7951      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6415  sub set_inner_html ($$$) { Line 7955  sub set_inner_html ($$$) {
7955      $doc->append_child ($root);      $doc->append_child ($root);
7956    
7957      ## Step 5 # MUST      ## Step 5 # MUST
7958      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7959    
7960      undef $p->{head_element};      undef $p->{head_element};
7961    
# Line 6461  sub set_inner_html ($$$) { Line 8001  sub set_inner_html ($$$) {
8001      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8002    
8003      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8004    
8005        delete $p->{parse_error}; # delete loop
8006    } else {    } else {
8007      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";
8008    }    }

Legend:
Removed from v.1.85  
changed lines
  Added in v.1.183

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24