/[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.99 by wakaba, Sun Mar 9 03:46:43 2008 UTC revision 1.182 by wakaba, Mon Sep 15 07:19:03 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_next_char} = 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_next_char}) {
660          $char = $self->{next_next_char};
661          delete $self->{next_next_char};
662          $self->{next_char} = 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->{next_char}
674                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
675            return;
676          }
677    
678          if ($input->read ($char, 1)) {
679            $self->{next_char} = ord $char;
680          } else {
681            $self->{next_char} = -1;
682            return;
683          }
684        }
685    
686      $self->{next_char} = -1 and return if $i >= length $$s;      ($self->{line_prev}, $self->{column_prev})
687      $self->{next_char} = ord substr $$s, $i++, 1;          = ($self->{line}, $self->{column});
688      $column++;      $self->{column}++;
689            
690      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
691        $line++;        !!!cp ('j1');
692        $column = 0;        $self->{line}++;
693          $self->{column} = 0;
694      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
695        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
696    ## TODO: support for abort/streaming
697          my $next = '';
698          if ($input->read ($next, 1) and $next ne "\x0A") {
699            $self->{next_next_char} = $next;
700          }
701        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
702        $line++;        $self->{line}++;
703        $column = 0;        $self->{column} = 0;
     } elsif ($self->{next_char} > 0x10FFFF) {  
       $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
704      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
705          !!!cp ('j4');
706        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
707        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 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_next_char};
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->{next_char} = -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->{next_char} = -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        level => {must => 'm',
787                  should => 's',
788                  warn => 'w',
789                  info => 'i',
790                  uncertain => 'u'},
791      }, $class;
792    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
793      $self->{next_char} = -1;      $self->{next_char} = -1;
794    };    };
# 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->{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->{current_token};
929    undef $self->{current_attribute};    undef $self->{current_attribute};
930    undef $self->{last_emitted_start_tag_name};    undef $self->{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->{next_char} = -1;
937    !!!next-input-character;    !!!next-input-character;
938    $self->{token} = [];    $self->{token} = [];
939    # $self->{escape}    # $self->{escape}
# Line 358  sub _initialize_tokenizer ($) { Line 952  sub _initialize_tokenizer ($) {
952  ##        ->{value}  ##        ->{value}
953  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
954  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
955    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
956    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
957    ##     while the token is pushed back to the stack.
958    
959  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
960    
# Line 367  sub _initialize_tokenizer ($) { Line 964  sub _initialize_tokenizer ($) {
964  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
965  ## and removed from the list.  ## and removed from the list.
966    
967  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
968  ## 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,  
969    
970  sub _get_next_token ($) {  sub _get_next_token ($) {
971    my $self = shift;    my $self = shift;
972    
973      if ($self->{self_closing}) {
974        !!!parse-error (type => 'nestc', token => $self->{current_token});
975        ## NOTE: The |self_closing| flag is only set by start tag token.
976        ## In addition, when a start tag token is emitted, it is always set to
977        ## |current_token|.
978        delete $self->{self_closing};
979      }
980    
981    if (@{$self->{token}}) {    if (@{$self->{token}}) {
982        $self->{self_closing} = $self->{token}->[0]->{self_closing};
983      return shift @{$self->{token}};      return shift @{$self->{token}};
984    }    }
985    
# Line 394  sub _get_next_token ($) { Line 989  sub _get_next_token ($) {
989          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
990              not $self->{escape}) {              not $self->{escape}) {
991            !!!cp (1);            !!!cp (1);
992            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
993              ## "entity data state".  In this implementation, the tokenizer
994              ## is switched to the |ENTITY_STATE|, which is an implementation
995              ## of the "consume a character reference" algorithm.
996              $self->{entity_additional} = -1;
997              $self->{prev_state} = DATA_STATE;
998              $self->{state} = ENTITY_STATE;
999            !!!next-input-character;            !!!next-input-character;
1000            redo A;            redo A;
1001          } else {          } else {
# Line 447  sub _get_next_token ($) { Line 1048  sub _get_next_token ($) {
1048          #          #
1049        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1050          !!!cp (11);          !!!cp (11);
1051          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
1052                      line => $self->{line}, column => $self->{column}});
1053          last A; ## TODO: ok?          last A; ## TODO: ok?
1054        } else {        } else {
1055          !!!cp (12);          !!!cp (12);
1056        }        }
1057        # Anything else        # Anything else
1058        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1059                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
1060                       line => $self->{line}, column => $self->{column},
1061                      };
1062          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1063    
1064        ## Stay in the data state        ## Stay in the data state
1065        !!!next-input-character;        !!!next-input-character;
1066    
1067        !!!emit ($token);        !!!emit ($token);
1068    
1069        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;  
1070      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1071        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1072          if ($self->{next_char} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
# Line 490  sub _get_next_token ($) { Line 1079  sub _get_next_token ($) {
1079            ## reconsume            ## reconsume
1080            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1081    
1082            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1083                        line => $self->{line_prev},
1084                        column => $self->{column_prev},
1085                       });
1086    
1087            redo A;            redo A;
1088          }          }
# Line 510  sub _get_next_token ($) { Line 1102  sub _get_next_token ($) {
1102            !!!cp (19);            !!!cp (19);
1103            $self->{current_token}            $self->{current_token}
1104              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1105                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1106                   line => $self->{line_prev},
1107                   column => $self->{column_prev}};
1108            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1109            !!!next-input-character;            !!!next-input-character;
1110            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 1112  sub _get_next_token ($) {
1112                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1113            !!!cp (20);            !!!cp (20);
1114            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1115                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1116                                        line => $self->{line_prev},
1117                                        column => $self->{column_prev}};
1118            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1119            !!!next-input-character;            !!!next-input-character;
1120            redo A;            redo A;
1121          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1122            !!!cp (21);            !!!cp (21);
1123            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1124                              line => $self->{line_prev},
1125                              column => $self->{column_prev});
1126            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1127            !!!next-input-character;            !!!next-input-character;
1128    
1129            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1130                        line => $self->{line_prev},
1131                        column => $self->{column_prev},
1132                       });
1133    
1134            redo A;            redo A;
1135          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1136            !!!cp (22);            !!!cp (22);
1137            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1138                              line => $self->{line_prev},
1139                              column => $self->{column_prev});
1140            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1141              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1142                                        line => $self->{line_prev},
1143                                        column => $self->{column_prev},
1144                                       };
1145            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1146            redo A;            redo A;
1147          } else {          } else {
1148            !!!cp (23);            !!!cp (23);
1149            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1150                              line => $self->{line_prev},
1151                              column => $self->{column_prev});
1152            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1153            ## reconsume            ## reconsume
1154    
1155            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1156                        line => $self->{line_prev},
1157                        column => $self->{column_prev},
1158                       });
1159    
1160            redo A;            redo A;
1161          }          }
# Line 551  sub _get_next_token ($) { Line 1163  sub _get_next_token ($) {
1163          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1164        }        }
1165      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1166          ## NOTE: The "close tag open state" in the spec is implemented as
1167          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1168    
1169          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1170        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1171          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1172            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1173            my @next_char;            $self->{state_keyword} = '';
1174            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            ## Reconsume.
1175              push @next_char, $self->{next_char};            redo A;
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_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;  
   
               !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_char};  
         
           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...  
           }  
1176          } else {          } else {
1177            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1178              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1179            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1180            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1181            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1182              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1183                        line => $l, column => $c,
1184                       });
1185            redo A;            redo A;
1186          }          }
1187        }        }
1188          
1189        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1190            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1191          !!!cp (29);          !!!cp (29);
1192          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1193                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1194                   tag_name => chr ($self->{next_char} + 0x0020),
1195                   line => $l, column => $c};
1196          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1197          !!!next-input-character;          !!!next-input-character;
1198          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 1200  sub _get_next_token ($) {
1200                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1201          !!!cp (30);          !!!cp (30);
1202          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1203                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1204                                      line => $l, column => $c};
1205          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1206          !!!next-input-character;          !!!next-input-character;
1207          redo A;          redo A;
1208        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1209          !!!cp (31);          !!!cp (31);
1210          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1211                            line => $self->{line_prev}, ## "<" in "</>"
1212                            column => $self->{column_prev} - 1);
1213          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1214          !!!next-input-character;          !!!next-input-character;
1215          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 1219  sub _get_next_token ($) {
1219          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1220          # reconsume          # reconsume
1221    
1222          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1223                      line => $l, column => $c,
1224                     });
1225    
1226          redo A;          redo A;
1227        } else {        } else {
1228          !!!cp (33);          !!!cp (33);
1229          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1230          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1231          ## $self->{next_char} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1232          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1233                                      column => $self->{column_prev} - 1,
1234                                     };
1235            ## NOTE: $self->{next_char} is intentionally left as is.
1236            ## Although the "anything else" case of the spec not explicitly
1237            ## states that the next input character is to be reconsumed,
1238            ## it will be included to the |data| of the comment token
1239            ## generated from the bogus end tag, as defined in the
1240            ## "bogus comment state" entry.
1241            redo A;
1242          }
1243        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1244          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1245          if (length $ch) {
1246            my $CH = $ch;
1247            $ch =~ tr/a-z/A-Z/;
1248            my $nch = chr $self->{next_char};
1249            if ($nch eq $ch or $nch eq $CH) {
1250              !!!cp (24);
1251              ## Stay in the state.
1252              $self->{state_keyword} .= $nch;
1253              !!!next-input-character;
1254              redo A;
1255            } else {
1256              !!!cp (25);
1257              $self->{state} = DATA_STATE;
1258              ## Reconsume.
1259              !!!emit ({type => CHARACTER_TOKEN,
1260                        data => '</' . $self->{state_keyword},
1261                        line => $self->{line_prev},
1262                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1263                       });
1264              redo A;
1265            }
1266          } else { # after "<{tag-name}"
1267            unless ({
1268                     0x0009 => 1, # HT
1269                     0x000A => 1, # LF
1270                     0x000B => 1, # VT
1271                     0x000C => 1, # FF
1272                     0x0020 => 1, # SP
1273                     0x003E => 1, # >
1274                     0x002F => 1, # /
1275                     -1 => 1, # EOF
1276                    }->{$self->{next_char}}) {
1277              !!!cp (26);
1278              ## Reconsume.
1279              $self->{state} = DATA_STATE;
1280              !!!emit ({type => CHARACTER_TOKEN,
1281                        data => '</' . $self->{state_keyword},
1282                        line => $self->{line_prev},
1283                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1284                       });
1285              redo A;
1286            } else {
1287              !!!cp (27);
1288              $self->{current_token}
1289                  = {type => END_TAG_TOKEN,
1290                     tag_name => $self->{last_emitted_start_tag_name},
1291                     line => $self->{line_prev},
1292                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1293              $self->{state} = TAG_NAME_STATE;
1294              ## Reconsume.
1295              redo A;
1296            }
1297        }        }
1298      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1299        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 657  sub _get_next_token ($) { Line 1308  sub _get_next_token ($) {
1308        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1309          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1310            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1311            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1312          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1313            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1339  sub _get_next_token ($) {
1339          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1340          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1341            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1342            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1343          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1344            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1359  sub _get_next_token ($) {
1359    
1360          redo A;          redo A;
1361        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1362            !!!cp (42);
1363            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1364          !!!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  
1365          redo A;          redo A;
1366        } else {        } else {
1367          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1384  sub _get_next_token ($) {
1384        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1385          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1386            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1387            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1388          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1389            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1405  sub _get_next_token ($) {
1405        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1406                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1407          !!!cp (49);          !!!cp (49);
1408          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1409                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1410                   value => '',
1411                   line => $self->{line}, column => $self->{column}};
1412          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1413          !!!next-input-character;          !!!next-input-character;
1414          redo A;          redo A;
1415        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1416            !!!cp (50);
1417            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1418          !!!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  
1419          redo A;          redo A;
1420        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1421          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1422          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1423            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1424            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1425          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1426            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1450  sub _get_next_token ($) {
1450          } else {          } else {
1451            !!!cp (56);            !!!cp (56);
1452          }          }
1453          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1454                                value => ''};              = {name => chr ($self->{next_char}),
1455                   value => '',
1456                   line => $self->{line}, column => $self->{column}};
1457          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1458          !!!next-input-character;          !!!next-input-character;
1459          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1463  sub _get_next_token ($) {
1463          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1464              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1465            !!!cp (57);            !!!cp (57);
1466            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1467            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1468          } else {          } else {
1469            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1492  sub _get_next_token ($) {
1492          $before_leave->();          $before_leave->();
1493          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1494            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1495            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1496          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1497            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1516  sub _get_next_token ($) {
1516          !!!next-input-character;          !!!next-input-character;
1517          redo A;          redo A;
1518        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1519            !!!cp (64);
1520          $before_leave->();          $before_leave->();
1521            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1522          !!!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  
1523          redo A;          redo A;
1524        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1525          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1526          $before_leave->();          $before_leave->();
1527          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1528            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1529            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1530          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1531            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1576  sub _get_next_token ($) {
1576        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1577          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1578            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1579            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1580          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1581            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1598  sub _get_next_token ($) {
1598        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1599                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1600          !!!cp (76);          !!!cp (76);
1601          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1602                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1603                   value => '',
1604                   line => $self->{line}, column => $self->{column}};
1605          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1606          !!!next-input-character;          !!!next-input-character;
1607          redo A;          redo A;
1608        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1609            !!!cp (77);
1610            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1611          !!!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  
1612          redo A;          redo A;
1613        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1614          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1615          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1616            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1617            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1618          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1619            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1034  sub _get_next_token ($) { Line 1634  sub _get_next_token ($) {
1634    
1635          redo A;          redo A;
1636        } else {        } else {
1637          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1638          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1639                                value => ''};            !!!cp (78);
1640              !!!parse-error (type => 'bad attribute name');
1641            } else {
1642              !!!cp (82);
1643            }
1644            $self->{current_attribute}
1645                = {name => chr ($self->{next_char}),
1646                   value => '',
1647                   line => $self->{line}, column => $self->{column}};
1648          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1649          !!!next-input-character;          !!!next-input-character;
1650          redo A;                  redo A;        
# Line 1067  sub _get_next_token ($) { Line 1675  sub _get_next_token ($) {
1675          !!!next-input-character;          !!!next-input-character;
1676          redo A;          redo A;
1677        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1678            !!!parse-error (type => 'empty unquoted attribute value');
1679          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1680            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1681            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1682          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1683            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1701  sub _get_next_token ($) {
1701          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1702          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1703            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1704            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1705          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1706            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1135  sub _get_next_token ($) { Line 1740  sub _get_next_token ($) {
1740          redo A;          redo A;
1741        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1742          !!!cp (96);          !!!cp (96);
1743          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1744          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1745            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1746            ## implementation of the "consume a character reference" algorithm.
1747            $self->{prev_state} = $self->{state};
1748            $self->{entity_additional} = 0x0022; # "
1749            $self->{state} = ENTITY_STATE;
1750          !!!next-input-character;          !!!next-input-character;
1751          redo A;          redo A;
1752        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1753          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1754          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1755            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1756            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1757          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1758            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1167  sub _get_next_token ($) { Line 1775  sub _get_next_token ($) {
1775        } else {        } else {
1776          !!!cp (100);          !!!cp (100);
1777          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1778            $self->{read_until}->($self->{current_attribute}->{value},
1779                                  q["&],
1780                                  length $self->{current_attribute}->{value});
1781    
1782          ## Stay in the state          ## Stay in the state
1783          !!!next-input-character;          !!!next-input-character;
1784          redo A;          redo A;
# Line 1179  sub _get_next_token ($) { Line 1791  sub _get_next_token ($) {
1791          redo A;          redo A;
1792        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1793          !!!cp (102);          !!!cp (102);
1794          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1795          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1796            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1797            ## implementation of the "consume a character reference" algorithm.
1798            $self->{entity_additional} = 0x0027; # '
1799            $self->{prev_state} = $self->{state};
1800            $self->{state} = ENTITY_STATE;
1801          !!!next-input-character;          !!!next-input-character;
1802          redo A;          redo A;
1803        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1804          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1805          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1806            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1807            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1808          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1809            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1211  sub _get_next_token ($) { Line 1826  sub _get_next_token ($) {
1826        } else {        } else {
1827          !!!cp (106);          !!!cp (106);
1828          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1829            $self->{read_until}->($self->{current_attribute}->{value},
1830                                  q['&],
1831                                  length $self->{current_attribute}->{value});
1832    
1833          ## Stay in the state          ## Stay in the state
1834          !!!next-input-character;          !!!next-input-character;
1835          redo A;          redo A;
# Line 1227  sub _get_next_token ($) { Line 1846  sub _get_next_token ($) {
1846          redo A;          redo A;
1847        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1848          !!!cp (108);          !!!cp (108);
1849          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1850          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1851            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1852            ## implementation of the "consume a character reference" algorithm.
1853            $self->{entity_additional} = -1;
1854            $self->{prev_state} = $self->{state};
1855            $self->{state} = ENTITY_STATE;
1856          !!!next-input-character;          !!!next-input-character;
1857          redo A;          redo A;
1858        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1859          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1860            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1861            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1862          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1863            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1881  sub _get_next_token ($) {
1881          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1882          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1883            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1884            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1885          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1886            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1292  sub _get_next_token ($) { Line 1912  sub _get_next_token ($) {
1912            !!!cp (116);            !!!cp (116);
1913          }          }
1914          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1915            $self->{read_until}->($self->{current_attribute}->{value},
1916                                  q["'=& >],
1917                                  length $self->{current_attribute}->{value});
1918    
1919          ## Stay in the state          ## Stay in the state
1920          !!!next-input-character;          !!!next-input-character;
1921          redo A;          redo A;
1922        }        }
     } 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;  
1923      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1924        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1925            $self->{next_char} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
# Line 1331  sub _get_next_token ($) { Line 1933  sub _get_next_token ($) {
1933        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1934          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1935            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1936            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1937          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1938            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1953  sub _get_next_token ($) {
1953    
1954          redo A;          redo A;
1955        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1956            !!!cp (122);
1957            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1958          !!!next-input-character;          !!!next-input-character;
1959          if ($self->{next_char} == 0x003E and # >          redo A;
1960              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1961              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1962            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1963            !!!cp (122);            !!!cp (122.3);
1964            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1965            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1966              if ($self->{current_token}->{attributes}) {
1967                !!!cp (122.1);
1968                !!!parse-error (type => 'end tag attribute');
1969              } else {
1970                ## NOTE: This state should never be reached.
1971                !!!cp (122.2);
1972              }
1973          } else {          } else {
1974            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1975          }          }
1976          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1977          # next-input-character is already done          ## Reconsume.
1978            !!!emit ($self->{current_token}); # start tag or end tag
1979          redo A;          redo A;
1980        } else {        } else {
1981          !!!cp (124);          !!!cp ('124.1');
1982          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1983          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1984          ## reconsume          ## reconsume
1985          redo A;          redo A;
1986        }        }
1987      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1988        ## (only happen if PCDATA state)        if ($self->{next_char} == 0x003E) { # >
1989                  if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1990        my $token = {type => COMMENT_TOKEN, data => ''};            !!!cp ('124.2');
1991              !!!parse-error (type => 'nestc', token => $self->{current_token});
1992        BC: {            ## TODO: Different type than slash in start tag
1993          if ($self->{next_char} == 0x003E) { # >            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1994            !!!cp (124);            if ($self->{current_token}->{attributes}) {
1995            $self->{state} = DATA_STATE;              !!!cp ('124.4');
1996            !!!next-input-character;              !!!parse-error (type => 'end tag attribute');
1997              } else {
1998            !!!emit ($token);              !!!cp ('124.5');
1999              }
2000              ## TODO: Test |<title></title/>|
2001            } else {
2002              !!!cp ('124.3');
2003              $self->{self_closing} = 1;
2004            }
2005    
2006            redo A;          $self->{state} = DATA_STATE;
2007          } elsif ($self->{next_char} == -1) {          !!!next-input-character;
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
2008    
2009            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
2010    
2011            redo A;          redo A;
2012          } elsif ($self->{next_char} == -1) {
2013            !!!parse-error (type => 'unclosed tag');
2014            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
2015              !!!cp (124.7);
2016              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
2017            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
2018              if ($self->{current_token}->{attributes}) {
2019                !!!cp (124.5);
2020                !!!parse-error (type => 'end tag attribute');
2021              } else {
2022                ## NOTE: This state should never be reached.
2023                !!!cp (124.6);
2024              }
2025          } else {          } else {
2026            !!!cp (126);            die "$0: $self->{current_token}->{type}: Unknown token type";
           $token->{data} .= chr ($self->{next_char});  
           !!!next-input-character;  
           redo BC;  
2027          }          }
2028        } # BC          $self->{state} = DATA_STATE;
2029            ## Reconsume.
2030            !!!emit ($self->{current_token}); # start tag or end tag
2031            redo A;
2032          } else {
2033            !!!cp ('124.4');
2034            !!!parse-error (type => 'nestc');
2035            ## TODO: This error type is wrong.
2036            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2037            ## Reconsume.
2038            redo A;
2039          }
2040        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2041          ## (only happen if PCDATA state)
2042    
2043          ## NOTE: Unlike spec's "bogus comment state", this implementation
2044          ## consumes characters one-by-one basis.
2045          
2046          if ($self->{next_char} == 0x003E) { # >
2047            !!!cp (124);
2048            $self->{state} = DATA_STATE;
2049            !!!next-input-character;
2050    
2051        die "$0: _get_next_token: unexpected case [BC]";          !!!emit ($self->{current_token}); # comment
2052            redo A;
2053          } elsif ($self->{next_char} == -1) {
2054            !!!cp (125);
2055            $self->{state} = DATA_STATE;
2056            ## reconsume
2057    
2058            !!!emit ($self->{current_token}); # comment
2059            redo A;
2060          } else {
2061            !!!cp (126);
2062            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2063            $self->{read_until}->($self->{current_token}->{data},
2064                                  q[>],
2065                                  length $self->{current_token}->{data});
2066    
2067            ## Stay in the state.
2068            !!!next-input-character;
2069            redo A;
2070          }
2071      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2072        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my @next_char;  
       push @next_char, $self->{next_char};  
2073                
2074        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2075            !!!cp (133);
2076            $self->{state} = MD_HYPHEN_STATE;
2077          !!!next-input-character;          !!!next-input-character;
2078          push @next_char, $self->{next_char};          redo A;
         if ($self->{next_char} == 0x002D) { # -  
           !!!cp (127);  
           $self->{current_token} = {type => COMMENT_TOKEN, data => ''};  
           $self->{state} = COMMENT_START_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (128);  
         }  
2079        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2080                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2081            ## ASCII case-insensitive.
2082            !!!cp (130);
2083            $self->{state} = MD_DOCTYPE_STATE;
2084            $self->{state_keyword} = chr $self->{next_char};
2085          !!!next-input-character;          !!!next-input-character;
2086          push @next_char, $self->{next_char};          redo A;
2087          if ($self->{next_char} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2088              $self->{next_char} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2089            !!!next-input-character;                 $self->{next_char} == 0x005B) { # [
2090            push @next_char, $self->{next_char};          !!!cp (135.4);                
2091            if ($self->{next_char} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2092                $self->{next_char} == 0x0063) { # c          $self->{state_keyword} = '[';
2093              !!!next-input-character;          !!!next-input-character;
2094              push @next_char, $self->{next_char};          redo A;
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_char};  
               if ($self->{next_char} == 0x0059 or # Y  
                   $self->{next_char} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_char};  
                 if ($self->{next_char} == 0x0050 or # P  
                     $self->{next_char} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_char};  
                   if ($self->{next_char} == 0x0045 or # E  
                       $self->{next_char} == 0x0065) { # e  
                     !!!cp (129);  
                     ## TODO: What a stupid code this is!  
                     $self->{state} = DOCTYPE_STATE;  
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (130);  
                   }  
                 } else {  
                   !!!cp (131);  
                 }  
               } else {  
                 !!!cp (132);  
               }  
             } else {  
               !!!cp (133);  
             }  
           } else {  
             !!!cp (134);  
           }  
         } else {  
           !!!cp (135);  
         }  
2095        } else {        } else {
2096          !!!cp (136);          !!!cp (136);
2097        }        }
2098    
2099        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2100        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2101        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2102          ## Reconsume.
2103        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2104          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2105                                    line => $self->{line_prev},
2106                                    column => $self->{column_prev} - 1,
2107                                   };
2108        redo A;        redo A;
2109              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2110        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2111        ## 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);
2112            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2113                                      line => $self->{line_prev},
2114                                      column => $self->{column_prev} - 2,
2115                                     };
2116            $self->{state} = COMMENT_START_STATE;
2117            !!!next-input-character;
2118            redo A;
2119          } else {
2120            !!!cp (128);
2121            !!!parse-error (type => 'bogus comment',
2122                            line => $self->{line_prev},
2123                            column => $self->{column_prev} - 2);
2124            $self->{state} = BOGUS_COMMENT_STATE;
2125            ## Reconsume.
2126            $self->{current_token} = {type => COMMENT_TOKEN,
2127                                      data => '-',
2128                                      line => $self->{line_prev},
2129                                      column => $self->{column_prev} - 2,
2130                                     };
2131            redo A;
2132          }
2133        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2134          ## ASCII case-insensitive.
2135          if ($self->{next_char} == [
2136                undef,
2137                0x004F, # O
2138                0x0043, # C
2139                0x0054, # T
2140                0x0059, # Y
2141                0x0050, # P
2142              ]->[length $self->{state_keyword}] or
2143              $self->{next_char} == [
2144                undef,
2145                0x006F, # o
2146                0x0063, # c
2147                0x0074, # t
2148                0x0079, # y
2149                0x0070, # p
2150              ]->[length $self->{state_keyword}]) {
2151            !!!cp (131);
2152            ## Stay in the state.
2153            $self->{state_keyword} .= chr $self->{next_char};
2154            !!!next-input-character;
2155            redo A;
2156          } elsif ((length $self->{state_keyword}) == 6 and
2157                   ($self->{next_char} == 0x0045 or # E
2158                    $self->{next_char} == 0x0065)) { # e
2159            !!!cp (129);
2160            $self->{state} = DOCTYPE_STATE;
2161            $self->{current_token} = {type => DOCTYPE_TOKEN,
2162                                      quirks => 1,
2163                                      line => $self->{line_prev},
2164                                      column => $self->{column_prev} - 7,
2165                                     };
2166            !!!next-input-character;
2167            redo A;
2168          } else {
2169            !!!cp (132);        
2170            !!!parse-error (type => 'bogus comment',
2171                            line => $self->{line_prev},
2172                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2173            $self->{state} = BOGUS_COMMENT_STATE;
2174            ## Reconsume.
2175            $self->{current_token} = {type => COMMENT_TOKEN,
2176                                      data => $self->{state_keyword},
2177                                      line => $self->{line_prev},
2178                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2179                                     };
2180            redo A;
2181          }
2182        } elsif ($self->{state} == MD_CDATA_STATE) {
2183          if ($self->{next_char} == {
2184                '[' => 0x0043, # C
2185                '[C' => 0x0044, # D
2186                '[CD' => 0x0041, # A
2187                '[CDA' => 0x0054, # T
2188                '[CDAT' => 0x0041, # A
2189              }->{$self->{state_keyword}}) {
2190            !!!cp (135.1);
2191            ## Stay in the state.
2192            $self->{state_keyword} .= chr $self->{next_char};
2193            !!!next-input-character;
2194            redo A;
2195          } elsif ($self->{state_keyword} eq '[CDATA' and
2196                   $self->{next_char} == 0x005B) { # [
2197            !!!cp (135.2);
2198            $self->{current_token} = {type => CHARACTER_TOKEN,
2199                                      data => '',
2200                                      line => $self->{line_prev},
2201                                      column => $self->{column_prev} - 7};
2202            $self->{state} = CDATA_SECTION_STATE;
2203            !!!next-input-character;
2204            redo A;
2205          } else {
2206            !!!cp (135.3);
2207            !!!parse-error (type => 'bogus comment',
2208                            line => $self->{line_prev},
2209                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2210            $self->{state} = BOGUS_COMMENT_STATE;
2211            ## Reconsume.
2212            $self->{current_token} = {type => COMMENT_TOKEN,
2213                                      data => $self->{state_keyword},
2214                                      line => $self->{line_prev},
2215                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2216                                     };
2217            redo A;
2218          }
2219      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2220        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2221          !!!cp (137);          !!!cp (137);
# Line 1566  sub _get_next_token ($) { Line 2298  sub _get_next_token ($) {
2298        } else {        } else {
2299          !!!cp (147);          !!!cp (147);
2300          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2301            $self->{read_until}->($self->{current_token}->{data},
2302                                  q[-],
2303                                  length $self->{current_token}->{data});
2304    
2305          ## Stay in the state          ## Stay in the state
2306          !!!next-input-character;          !!!next-input-character;
2307          redo A;          redo A;
# Line 1603  sub _get_next_token ($) { Line 2339  sub _get_next_token ($) {
2339          redo A;          redo A;
2340        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2341          !!!cp (152);          !!!cp (152);
2342          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2343                            line => $self->{line_prev},
2344                            column => $self->{column_prev});
2345          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2346          ## Stay in the state          ## Stay in the state
2347          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 2357  sub _get_next_token ($) {
2357          redo A;          redo A;
2358        } else {        } else {
2359          !!!cp (154);          !!!cp (154);
2360          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2361                            line => $self->{line_prev},
2362                            column => $self->{column_prev});
2363          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2364          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2365          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2398  sub _get_next_token ($) {
2398          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2399          !!!next-input-character;          !!!next-input-character;
2400    
2401          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2402    
2403          redo A;          redo A;
2404        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2407  sub _get_next_token ($) {
2407          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2408          ## reconsume          ## reconsume
2409    
2410          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2411    
2412          redo A;          redo A;
2413        } else {        } else {
2414          !!!cp (160);          !!!cp (160);
2415          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2416              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2417  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2418          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2419          !!!next-input-character;          !!!next-input-character;
# Line 1749  sub _get_next_token ($) { Line 2486  sub _get_next_token ($) {
2486          redo A;          redo A;
2487        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2488                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2489            $self->{state} = PUBLIC_STATE;
2490            $self->{state_keyword} = chr $self->{next_char};
2491          !!!next-input-character;          !!!next-input-character;
2492          if ($self->{next_char} == 0x0055 or # U          redo A;
             $self->{next_char} == 0x0075) { # u  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0042 or # B  
               $self->{next_char} == 0x0062) { # b  
             !!!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);  
         }  
   
         #  
2493        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2494                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2495            $self->{state} = SYSTEM_STATE;
2496            $self->{state_keyword} = chr $self->{next_char};
2497          !!!next-input-character;          !!!next-input-character;
2498          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);  
         }  
   
         #  
2499        } else {        } else {
2500          !!!cp (180);          !!!cp (180);
2501            !!!parse-error (type => 'string after DOCTYPE name');
2502            $self->{current_token}->{quirks} = 1;
2503    
2504            $self->{state} = BOGUS_DOCTYPE_STATE;
2505          !!!next-input-character;          !!!next-input-character;
2506          #          redo A;
2507        }        }
2508        } elsif ($self->{state} == PUBLIC_STATE) {
2509          ## ASCII case-insensitive
2510          if ($self->{next_char} == [
2511                undef,
2512                0x0055, # U
2513                0x0042, # B
2514                0x004C, # L
2515                0x0049, # I
2516              ]->[length $self->{state_keyword}] or
2517              $self->{next_char} == [
2518                undef,
2519                0x0075, # u
2520                0x0062, # b
2521                0x006C, # l
2522                0x0069, # i
2523              ]->[length $self->{state_keyword}]) {
2524            !!!cp (175);
2525            ## Stay in the state.
2526            $self->{state_keyword} .= chr $self->{next_char};
2527            !!!next-input-character;
2528            redo A;
2529          } elsif ((length $self->{state_keyword}) == 5 and
2530                   ($self->{next_char} == 0x0043 or # C
2531                    $self->{next_char} == 0x0063)) { # c
2532            !!!cp (168);
2533            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2534            !!!next-input-character;
2535            redo A;
2536          } else {
2537            !!!cp (169);
2538            !!!parse-error (type => 'string after DOCTYPE name',
2539                            line => $self->{line_prev},
2540                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2541            $self->{current_token}->{quirks} = 1;
2542    
2543        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2544        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2545            redo A;
2546          }
2547        } elsif ($self->{state} == SYSTEM_STATE) {
2548          ## ASCII case-insensitive
2549          if ($self->{next_char} == [
2550                undef,
2551                0x0059, # Y
2552                0x0053, # S
2553                0x0054, # T
2554                0x0045, # E
2555              ]->[length $self->{state_keyword}] or
2556              $self->{next_char} == [
2557                undef,
2558                0x0079, # y
2559                0x0073, # s
2560                0x0074, # t
2561                0x0065, # e
2562              ]->[length $self->{state_keyword}]) {
2563            !!!cp (170);
2564            ## Stay in the state.
2565            $self->{state_keyword} .= chr $self->{next_char};
2566            !!!next-input-character;
2567            redo A;
2568          } elsif ((length $self->{state_keyword}) == 5 and
2569                   ($self->{next_char} == 0x004D or # M
2570                    $self->{next_char} == 0x006D)) { # m
2571            !!!cp (171);
2572            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2573            !!!next-input-character;
2574            redo A;
2575          } else {
2576            !!!cp (172);
2577            !!!parse-error (type => 'string after DOCTYPE name',
2578                            line => $self->{line_prev},
2579                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2580            $self->{current_token}->{quirks} = 1;
2581    
2582        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2583        # next-input-character is already done          ## Reconsume.
2584        redo A;          redo A;
2585          }
2586      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2587        if ({        if ({
2588              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 1919  sub _get_next_token ($) { Line 2667  sub _get_next_token ($) {
2667          !!!cp (190);          !!!cp (190);
2668          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2669              .= chr $self->{next_char};              .= chr $self->{next_char};
2670            $self->{read_until}->($self->{current_token}->{public_identifier},
2671                                  q[">],
2672                                  length $self->{current_token}->{public_identifier});
2673    
2674          ## Stay in the state          ## Stay in the state
2675          !!!next-input-character;          !!!next-input-character;
2676          redo A;          redo A;
# Line 1955  sub _get_next_token ($) { Line 2707  sub _get_next_token ($) {
2707          !!!cp (194);          !!!cp (194);
2708          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2709              .= chr $self->{next_char};              .= chr $self->{next_char};
2710            $self->{read_until}->($self->{current_token}->{public_identifier},
2711                                  q['>],
2712                                  length $self->{current_token}->{public_identifier});
2713    
2714          ## Stay in the state          ## Stay in the state
2715          !!!next-input-character;          !!!next-input-character;
2716          redo A;          redo A;
# Line 2067  sub _get_next_token ($) { Line 2823  sub _get_next_token ($) {
2823          redo A;          redo A;
2824        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2825          !!!cp (208);          !!!cp (208);
2826          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2827    
2828          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2829          !!!next-input-character;          !!!next-input-character;
# Line 2091  sub _get_next_token ($) { Line 2847  sub _get_next_token ($) {
2847          !!!cp (210);          !!!cp (210);
2848          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2849              .= chr $self->{next_char};              .= chr $self->{next_char};
2850            $self->{read_until}->($self->{current_token}->{system_identifier},
2851                                  q[">],
2852                                  length $self->{current_token}->{system_identifier});
2853    
2854          ## Stay in the state          ## Stay in the state
2855          !!!next-input-character;          !!!next-input-character;
2856          redo A;          redo A;
# Line 2103  sub _get_next_token ($) { Line 2863  sub _get_next_token ($) {
2863          redo A;          redo A;
2864        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2865          !!!cp (212);          !!!cp (212);
2866          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2867    
2868          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2869          !!!next-input-character;          !!!next-input-character;
# Line 2127  sub _get_next_token ($) { Line 2887  sub _get_next_token ($) {
2887          !!!cp (214);          !!!cp (214);
2888          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2889              .= chr $self->{next_char};              .= chr $self->{next_char};
2890            $self->{read_until}->($self->{current_token}->{system_identifier},
2891                                  q['>],
2892                                  length $self->{current_token}->{system_identifier});
2893    
2894          ## Stay in the state          ## Stay in the state
2895          !!!next-input-character;          !!!next-input-character;
2896          redo A;          redo A;
# Line 2151  sub _get_next_token ($) { Line 2915  sub _get_next_token ($) {
2915        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2916          !!!cp (217);          !!!cp (217);
2917          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2918          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2919          ## reconsume          ## reconsume
2920    
# Line 2188  sub _get_next_token ($) { Line 2951  sub _get_next_token ($) {
2951          redo A;          redo A;
2952        } else {        } else {
2953          !!!cp (221);          !!!cp (221);
2954            my $s = '';
2955            $self->{read_until}->($s, q[>], 0);
2956    
2957          ## Stay in the state          ## Stay in the state
2958          !!!next-input-character;          !!!next-input-character;
2959          redo A;          redo A;
2960        }        }
2961      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2962        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2963      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2964    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2965          
2966    die "$0: _get_next_token: unexpected case";        if ($self->{next_char} == 0x005D) { # ]
2967  } # _get_next_token          !!!cp (221.1);
2968            $self->{state} = CDATA_SECTION_MSE1_STATE;
2969            !!!next-input-character;
2970            redo A;
2971          } elsif ($self->{next_char} == -1) {
2972            $self->{state} = DATA_STATE;
2973            !!!next-input-character;
2974            if (length $self->{current_token}->{data}) { # character
2975              !!!cp (221.2);
2976              !!!emit ($self->{current_token}); # character
2977            } else {
2978              !!!cp (221.3);
2979              ## No token to emit. $self->{current_token} is discarded.
2980            }        
2981            redo A;
2982          } else {
2983            !!!cp (221.4);
2984            $self->{current_token}->{data} .= chr $self->{next_char};
2985            $self->{read_until}->($self->{current_token}->{data},
2986                                  q<]>,
2987                                  length $self->{current_token}->{data});
2988    
2989  sub _tokenize_attempt_to_consume_an_entity ($$$) {          ## Stay in the state.
2990    my ($self, $in_attr, $additional) = @_;          !!!next-input-character;
2991            redo A;
2992          }
2993    
2994    if ({        ## ISSUE: "text tokens" in spec.
2995         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2996         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{next_char} == 0x005D) { # ]
2997         $additional => 1,          !!!cp (221.5);
2998        }->{$self->{next_char}}) {          $self->{state} = CDATA_SECTION_MSE2_STATE;
2999      !!!cp (1001);          !!!next-input-character;
3000      ## Don't consume          redo A;
3001      ## No error        } else {
3002      return undef;          !!!cp (221.6);
3003    } elsif ($self->{next_char} == 0x0023) { # #          $self->{current_token}->{data} .= ']';
3004      !!!next-input-character;          $self->{state} = CDATA_SECTION_STATE;
3005      if ($self->{next_char} == 0x0078 or # x          ## Reconsume.
3006          $self->{next_char} == 0x0058) { # X          redo A;
3007        my $code;        }
3008        X: {      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3009          my $x_char = $self->{next_char};        if ($self->{next_char} == 0x003E) { # >
3010          !!!next-input-character;          $self->{state} = DATA_STATE;
3011          if (0x0030 <= $self->{next_char} and          !!!next-input-character;
3012              $self->{next_char} <= 0x0039) { # 0..9          if (length $self->{current_token}->{data}) { # character
3013            !!!cp (1002);            !!!cp (221.7);
3014            $code ||= 0;            !!!emit ($self->{current_token}); # 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;  
3015          } else {          } else {
3016            !!!cp (1007);            !!!cp (221.8);
3017            !!!parse-error (type => 'no refc');            ## No token to emit. $self->{current_token} is discarded.
3018          }          }
3019            redo A;
3020          } elsif ($self->{next_char} == 0x005D) { # ]
3021            !!!cp (221.9); # character
3022            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
3023            ## Stay in the state.
3024            !!!next-input-character;
3025            redo A;
3026          } else {
3027            !!!cp (221.11);
3028            $self->{current_token}->{data} .= ']]'; # character
3029            $self->{state} = CDATA_SECTION_STATE;
3030            ## Reconsume.
3031            redo A;
3032          }
3033        } elsif ($self->{state} == ENTITY_STATE) {
3034          if ({
3035            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3036            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3037            $self->{entity_additional} => 1,
3038          }->{$self->{next_char}}) {
3039            !!!cp (1001);
3040            ## Don't consume
3041            ## No error
3042            ## Return nothing.
3043            #
3044          } elsif ($self->{next_char} == 0x0023) { # #
3045            !!!cp (999);
3046            $self->{state} = ENTITY_HASH_STATE;
3047            $self->{state_keyword} = '#';
3048            !!!next-input-character;
3049            redo A;
3050          } elsif ((0x0041 <= $self->{next_char} and
3051                    $self->{next_char} <= 0x005A) or # A..Z
3052                   (0x0061 <= $self->{next_char} and
3053                    $self->{next_char} <= 0x007A)) { # a..z
3054            !!!cp (998);
3055            require Whatpm::_NamedEntityList;
3056            $self->{state} = ENTITY_NAME_STATE;
3057            $self->{state_keyword} = chr $self->{next_char};
3058            $self->{entity__value} = $self->{state_keyword};
3059            $self->{entity__match} = 0;
3060            !!!next-input-character;
3061            redo A;
3062          } else {
3063            !!!cp (1027);
3064            !!!parse-error (type => 'bare ero');
3065            ## Return nothing.
3066            #
3067          }
3068    
3069          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3070            !!!cp (1008);        ## reference" algorithm.  In other word, there is an "&" character
3071            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## that does not introduce a character reference, which would be
3072            $code = 0xFFFD;        ## appended to the parent element or the attribute value in later
3073          } elsif ($code > 0x10FFFF) {        ## process of the tokenizer.
3074            !!!cp (1009);  
3075            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        if ($self->{prev_state} == DATA_STATE) {
3076            $code = 0xFFFD;          !!!cp (997);
3077          } elsif ($code == 0x000D) {          $self->{state} = $self->{prev_state};
3078            !!!cp (1010);          ## Reconsume.
3079            !!!parse-error (type => 'CR character reference');          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3080            $code = 0x000A;                    line => $self->{line_prev},
3081          } elsif (0x80 <= $code and $code <= 0x9F) {                    column => $self->{column_prev},
3082            !!!cp (1011);                   });
3083            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          redo A;
3084            $code = $c1_entity_char->{$code};        } else {
3085          }          !!!cp (996);
3086            $self->{current_attribute}->{value} .= '&';
3087          return {type => CHARACTER_TOKEN, data => chr $code,          $self->{state} = $self->{prev_state};
3088                  has_reference => 1};          ## Reconsume.
3089        } # X          redo A;
3090      } elsif (0x0030 <= $self->{next_char} and        }
3091               $self->{next_char} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3092        my $code = $self->{next_char} - 0x0030;        if ($self->{next_char} == 0x0078 or # x
3093        !!!next-input-character;            $self->{next_char} == 0x0058) { # X
3094                  !!!cp (995);
3095        while (0x0030 <= $self->{next_char} and          $self->{state} = HEXREF_X_STATE;
3096                  $self->{next_char} <= 0x0039) { # 0..9          $self->{state_keyword} .= chr $self->{next_char};
3097            !!!next-input-character;
3098            redo A;
3099          } elsif (0x0030 <= $self->{next_char} and
3100                   $self->{next_char} <= 0x0039) { # 0..9
3101            !!!cp (994);
3102            $self->{state} = NCR_NUM_STATE;
3103            $self->{state_keyword} = $self->{next_char} - 0x0030;
3104            !!!next-input-character;
3105            redo A;
3106          } else {
3107            !!!parse-error (type => 'bare nero',
3108                            line => $self->{line_prev},
3109                            column => $self->{column_prev} - 1);
3110    
3111            ## NOTE: According to the spec algorithm, nothing is returned,
3112            ## and then "&#" is appended to the parent element or the attribute
3113            ## value in the later processing.
3114    
3115            if ($self->{prev_state} == DATA_STATE) {
3116              !!!cp (1019);
3117              $self->{state} = $self->{prev_state};
3118              ## Reconsume.
3119              !!!emit ({type => CHARACTER_TOKEN,
3120                        data => '&#',
3121                        line => $self->{line_prev},
3122                        column => $self->{column_prev} - 1,
3123                       });
3124              redo A;
3125            } else {
3126              !!!cp (993);
3127              $self->{current_attribute}->{value} .= '&#';
3128              $self->{state} = $self->{prev_state};
3129              ## Reconsume.
3130              redo A;
3131            }
3132          }
3133        } elsif ($self->{state} == NCR_NUM_STATE) {
3134          if (0x0030 <= $self->{next_char} and
3135              $self->{next_char} <= 0x0039) { # 0..9
3136          !!!cp (1012);          !!!cp (1012);
3137          $code *= 10;          $self->{state_keyword} *= 10;
3138          $code += $self->{next_char} - 0x0030;          $self->{state_keyword} += $self->{next_char} - 0x0030;
3139                    
3140            ## Stay in the state.
3141          !!!next-input-character;          !!!next-input-character;
3142        }          redo A;
3143          } elsif ($self->{next_char} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3144          !!!cp (1013);          !!!cp (1013);
3145          !!!next-input-character;          !!!next-input-character;
3146            #
3147        } else {        } else {
3148          !!!cp (1014);          !!!cp (1014);
3149          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3150            ## Reconsume.
3151            #
3152        }        }
3153    
3154          my $code = $self->{state_keyword};
3155          my $l = $self->{line_prev};
3156          my $c = $self->{column_prev};
3157        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3158          !!!cp (1015);          !!!cp (1015);
3159          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
3160                            text => (sprintf 'U+%04X', $code),
3161                            line => $l, column => $c);
3162          $code = 0xFFFD;          $code = 0xFFFD;
3163        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3164          !!!cp (1016);          !!!cp (1016);
3165          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
3166                            text => (sprintf 'U-%08X', $code),
3167                            line => $l, column => $c);
3168          $code = 0xFFFD;          $code = 0xFFFD;
3169        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3170          !!!cp (1017);          !!!cp (1017);
3171          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
3172                            line => $l, column => $c);
3173          $code = 0x000A;          $code = 0x000A;
3174        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3175          !!!cp (1018);          !!!cp (1018);
3176          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3177                            text => (sprintf 'U+%04X', $code),
3178                            line => $l, column => $c);
3179          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3180        }        }
3181          
3182        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        if ($self->{prev_state} == DATA_STATE) {
3183      } else {          !!!cp (992);
3184        !!!cp (1019);          $self->{state} = $self->{prev_state};
3185        !!!parse-error (type => 'bare nero');          ## Reconsume.
3186        !!!back-next-input-character ($self->{next_char});          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3187        $self->{next_char} = 0x0023; # #                    line => $l, column => $c,
3188        return undef;                   });
3189      }          redo A;
3190    } elsif ((0x0041 <= $self->{next_char} and        } else {
3191              $self->{next_char} <= 0x005A) or          !!!cp (991);
3192             (0x0061 <= $self->{next_char} and          $self->{current_attribute}->{value} .= chr $code;
3193              $self->{next_char} <= 0x007A)) {          $self->{current_attribute}->{has_reference} = 1;
3194      my $entity_name = chr $self->{next_char};          $self->{state} = $self->{prev_state};
3195      !!!next-input-character;          ## Reconsume.
3196            redo A;
3197      my $value = $entity_name;        }
3198      my $match = 0;      } elsif ($self->{state} == HEXREF_X_STATE) {
3199      require Whatpm::_NamedEntityList;        if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3200      our $EntityChar;            (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3201              (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3202      while (length $entity_name < 10 and          # 0..9, A..F, a..f
3203             ## NOTE: Some number greater than the maximum length of entity name          !!!cp (990);
3204             ((0x0041 <= $self->{next_char} and # a          $self->{state} = HEXREF_HEX_STATE;
3205               $self->{next_char} <= 0x005A) or # x          $self->{state_keyword} = 0;
3206              (0x0061 <= $self->{next_char} and # a          ## Reconsume.
3207               $self->{next_char} <= 0x007A) or # z          redo A;
3208              (0x0030 <= $self->{next_char} and # 0        } else {
3209               $self->{next_char} <= 0x0039) or # 9          !!!parse-error (type => 'bare hcro',
3210              $self->{next_char} == 0x003B)) { # ;                          line => $self->{line_prev},
3211        $entity_name .= chr $self->{next_char};                          column => $self->{column_prev} - 2);
3212        if (defined $EntityChar->{$entity_name}) {  
3213          if ($self->{next_char} == 0x003B) { # ;          ## NOTE: According to the spec algorithm, nothing is returned,
3214            !!!cp (1020);          ## and then "&#" followed by "X" or "x" is appended to the parent
3215            $value = $EntityChar->{$entity_name};          ## element or the attribute value in the later processing.
3216            $match = 1;  
3217            !!!next-input-character;          if ($self->{prev_state} == DATA_STATE) {
3218            last;            !!!cp (1005);
3219              $self->{state} = $self->{prev_state};
3220              ## Reconsume.
3221              !!!emit ({type => CHARACTER_TOKEN,
3222                        data => '&' . $self->{state_keyword},
3223                        line => $self->{line_prev},
3224                        column => $self->{column_prev} - length $self->{state_keyword},
3225                       });
3226              redo A;
3227          } else {          } else {
3228            !!!cp (1021);            !!!cp (989);
3229            $value = $EntityChar->{$entity_name};            $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3230            $match = -1;            $self->{state} = $self->{prev_state};
3231            !!!next-input-character;            ## Reconsume.
3232              redo A;
3233          }          }
3234        } else {        }
3235          !!!cp (1022);      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3236          $value .= chr $self->{next_char};        if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3237          $match *= 2;          # 0..9
3238            !!!cp (1002);
3239            $self->{state_keyword} *= 0x10;
3240            $self->{state_keyword} += $self->{next_char} - 0x0030;
3241            ## Stay in the state.
3242            !!!next-input-character;
3243            redo A;
3244          } elsif (0x0061 <= $self->{next_char} and
3245                   $self->{next_char} <= 0x0066) { # a..f
3246            !!!cp (1003);
3247            $self->{state_keyword} *= 0x10;
3248            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3249            ## Stay in the state.
3250          !!!next-input-character;          !!!next-input-character;
3251            redo A;
3252          } elsif (0x0041 <= $self->{next_char} and
3253                   $self->{next_char} <= 0x0046) { # A..F
3254            !!!cp (1004);
3255            $self->{state_keyword} *= 0x10;
3256            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3257            ## Stay in the state.
3258            !!!next-input-character;
3259            redo A;
3260          } elsif ($self->{next_char} == 0x003B) { # ;
3261            !!!cp (1006);
3262            !!!next-input-character;
3263            #
3264          } else {
3265            !!!cp (1007);
3266            !!!parse-error (type => 'no refc',
3267                            line => $self->{line},
3268                            column => $self->{column});
3269            ## Reconsume.
3270            #
3271        }        }
3272      }  
3273              my $code = $self->{state_keyword};
3274      if ($match > 0) {        my $l = $self->{line_prev};
3275        !!!cp (1023);        my $c = $self->{column_prev};
3276        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3277      } elsif ($match < 0) {          !!!cp (1008);
3278        !!!parse-error (type => 'no refc');          !!!parse-error (type => 'invalid character reference',
3279        if ($in_attr and $match < -1) {                          text => (sprintf 'U+%04X', $code),
3280          !!!cp (1024);                          line => $l, column => $c);
3281          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          $code = 0xFFFD;
3282          } elsif ($code > 0x10FFFF) {
3283            !!!cp (1009);
3284            !!!parse-error (type => 'invalid character reference',
3285                            text => (sprintf 'U-%08X', $code),
3286                            line => $l, column => $c);
3287            $code = 0xFFFD;
3288          } elsif ($code == 0x000D) {
3289            !!!cp (1010);
3290            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3291            $code = 0x000A;
3292          } elsif (0x80 <= $code and $code <= 0x9F) {
3293            !!!cp (1011);
3294            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3295            $code = $c1_entity_char->{$code};
3296          }
3297    
3298          if ($self->{prev_state} == DATA_STATE) {
3299            !!!cp (988);
3300            $self->{state} = $self->{prev_state};
3301            ## Reconsume.
3302            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3303                      line => $l, column => $c,
3304                     });
3305            redo A;
3306          } else {
3307            !!!cp (987);
3308            $self->{current_attribute}->{value} .= chr $code;
3309            $self->{current_attribute}->{has_reference} = 1;
3310            $self->{state} = $self->{prev_state};
3311            ## Reconsume.
3312            redo A;
3313          }
3314        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3315          if (length $self->{state_keyword} < 30 and
3316              ## NOTE: Some number greater than the maximum length of entity name
3317              ((0x0041 <= $self->{next_char} and # a
3318                $self->{next_char} <= 0x005A) or # x
3319               (0x0061 <= $self->{next_char} and # a
3320                $self->{next_char} <= 0x007A) or # z
3321               (0x0030 <= $self->{next_char} and # 0
3322                $self->{next_char} <= 0x0039) or # 9
3323               $self->{next_char} == 0x003B)) { # ;
3324            our $EntityChar;
3325            $self->{state_keyword} .= chr $self->{next_char};
3326            if (defined $EntityChar->{$self->{state_keyword}}) {
3327              if ($self->{next_char} == 0x003B) { # ;
3328                !!!cp (1020);
3329                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3330                $self->{entity__match} = 1;
3331                !!!next-input-character;
3332                #
3333              } else {
3334                !!!cp (1021);
3335                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3336                $self->{entity__match} = -1;
3337                ## Stay in the state.
3338                !!!next-input-character;
3339                redo A;
3340              }
3341            } else {
3342              !!!cp (1022);
3343              $self->{entity__value} .= chr $self->{next_char};
3344              $self->{entity__match} *= 2;
3345              ## Stay in the state.
3346              !!!next-input-character;
3347              redo A;
3348            }
3349          }
3350    
3351          my $data;
3352          my $has_ref;
3353          if ($self->{entity__match} > 0) {
3354            !!!cp (1023);
3355            $data = $self->{entity__value};
3356            $has_ref = 1;
3357            #
3358          } elsif ($self->{entity__match} < 0) {
3359            !!!parse-error (type => 'no refc');
3360            if ($self->{prev_state} != DATA_STATE and # in attribute
3361                $self->{entity__match} < -1) {
3362              !!!cp (1024);
3363              $data = '&' . $self->{state_keyword};
3364              #
3365            } else {
3366              !!!cp (1025);
3367              $data = $self->{entity__value};
3368              $has_ref = 1;
3369              #
3370            }
3371        } else {        } else {
3372          !!!cp (1025);          !!!cp (1026);
3373          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          !!!parse-error (type => 'bare ero',
3374                            line => $self->{line_prev},
3375                            column => $self->{column_prev} - length $self->{state_keyword});
3376            $data = '&' . $self->{state_keyword};
3377            #
3378          }
3379      
3380          ## NOTE: In these cases, when a character reference is found,
3381          ## it is consumed and a character token is returned, or, otherwise,
3382          ## nothing is consumed and returned, according to the spec algorithm.
3383          ## In this implementation, anything that has been examined by the
3384          ## tokenizer is appended to the parent element or the attribute value
3385          ## as string, either literal string when no character reference or
3386          ## entity-replaced string otherwise, in this stage, since any characters
3387          ## that would not be consumed are appended in the data state or in an
3388          ## appropriate attribute value state anyway.
3389    
3390          if ($self->{prev_state} == DATA_STATE) {
3391            !!!cp (986);
3392            $self->{state} = $self->{prev_state};
3393            ## Reconsume.
3394            !!!emit ({type => CHARACTER_TOKEN,
3395                      data => $data,
3396                      line => $self->{line_prev},
3397                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3398                     });
3399            redo A;
3400          } else {
3401            !!!cp (985);
3402            $self->{current_attribute}->{value} .= $data;
3403            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3404            $self->{state} = $self->{prev_state};
3405            ## Reconsume.
3406            redo A;
3407        }        }
3408      } else {      } else {
3409        !!!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};  
3410      }      }
3411    } else {    } # A  
3412      !!!cp (1027);  
3413      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3414      !!!parse-error (type => 'bare ero');  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3415    
3416  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3417    my $self = shift;    my $self = shift;
# Line 2400  sub _initialize_tree_constructor ($) { Line 3420  sub _initialize_tree_constructor ($) {
3420    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3421    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3422    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3423      $self->{document}->set_user_data (manakai_source_line => 1);
3424      $self->{document}->set_user_data (manakai_source_column => 1);
3425  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3426    
3427  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2454  sub _tree_construction_initial ($) { Line 3476  sub _tree_construction_initial ($) {
3476        ## language.        ## language.
3477        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3478        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3479        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3480        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3481            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3482          !!!cp ('t1');          !!!cp ('t1');
3483          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3484        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3485          !!!cp ('t2');          !!!cp ('t2');
3486          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!parse-error (type => 'not HTML5', token => $token);
3487          !!!parse-error (type => 'not HTML5');        } elsif (defined $token->{public_identifier}) {
3488            if ($token->{public_identifier} eq 'XSLT-compat') {
3489              !!!cp ('t1.2');
3490              !!!parse-error (type => 'XSLT-compat', token => $token,
3491                              level => $self->{level}->{should});
3492            } else {
3493              !!!parse-error (type => 'not HTML5', token => $token);
3494            }
3495        } else {        } else {
3496          !!!cp ('t3');          !!!cp ('t3');
3497            #
3498        }        }
3499                
3500        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3501          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3502          ## NOTE: Default value for both |public_id| and |system_id| attributes
3503          ## are empty strings, so that we don't set any value in missing cases.
3504        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3505            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3506        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2484  sub _tree_construction_initial ($) { Line 3515  sub _tree_construction_initial ($) {
3515        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3516          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3517          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3518          if ({          my $prefix = [
3519            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3520            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3521            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3522            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3523            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3524            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3525            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3526            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3527            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3528            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3529            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3530            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3531            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3532            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3533            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3534            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3535            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3536            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3537            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3538            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3539            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3540            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3541            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3542            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3543            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3544            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3545            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3546            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3547            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3548            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3549            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3550            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3551            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3552            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3553            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3554            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3555            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3556            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3557            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3558            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3559            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3560            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3561            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3562            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3563            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3564            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3565            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3566            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3567            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3568            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3569            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3570            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3571            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3572            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3573            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3574            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3575            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3576            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3577            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3578            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3579            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3580            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3581            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3582            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3583            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3584            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3585            "-//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}) {  
3586            !!!cp ('t5');            !!!cp ('t5');
3587            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3588          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3589                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3590            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3591              !!!cp ('t6');              !!!cp ('t6');
3592              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2569  sub _tree_construction_initial ($) { Line 3594  sub _tree_construction_initial ($) {
3594              !!!cp ('t7');              !!!cp ('t7');
3595              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3596            }            }
3597          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3598                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3599            !!!cp ('t8');            !!!cp ('t8');
3600            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3601          } else {          } else {
# Line 2583  sub _tree_construction_initial ($) { Line 3608  sub _tree_construction_initial ($) {
3608          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3609          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3610          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") {
3611            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3612              ## marked as quirks.
3613            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3614            !!!cp ('t11');            !!!cp ('t11');
3615          } else {          } else {
# Line 2602  sub _tree_construction_initial ($) { Line 3628  sub _tree_construction_initial ($) {
3628                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3629               }->{$token->{type}}) {               }->{$token->{type}}) {
3630        !!!cp ('t14');        !!!cp ('t14');
3631        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3632        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3633        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3634        ## reprocess        ## reprocess
3635          !!!ack-later;
3636        return;        return;
3637      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3638        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 3650  sub _tree_construction_initial ($) {
3650          !!!cp ('t17');          !!!cp ('t17');
3651        }        }
3652    
3653        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3654        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3655        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3656        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3679  sub _tree_construction_root_element ($)
3679    B: {    B: {
3680        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3681          !!!cp ('t19');          !!!cp ('t19');
3682          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3683          ## Ignore the token          ## Ignore the token
3684          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3685          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3713  sub _tree_construction_root_element ($)
3713        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3714          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3715            my $root_element;            my $root_element;
3716            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3717            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3718            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3719                  [$root_element, $el_category->{html}];
3720    
3721            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3722              !!!cp ('t24');              !!!cp ('t24');
3723              $self->{application_cache_selection}              $self->{application_cache_selection}
3724                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3725              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3726                ## According to Hixie (#whatwg 2008-03-19), it should be
3727                ## resolved against the base URI of the document in HTML
3728                ## or xml:base of the element in XHTML.
3729            } else {            } else {
3730              !!!cp ('t25');              !!!cp ('t25');
3731              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3732            }            }
3733    
3734              !!!nack ('t25c');
3735    
3736            !!!next-token;            !!!next-token;
3737            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3738          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3749  sub _tree_construction_root_element ($)
3749          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3750        }        }
3751    
3752      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3753        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3754      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3755      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3756    
3757      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3758    
3759      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3760        !!!ack-later;
3761      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3762    
3763      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2746  sub _reset_insertion_mode ($) { Line 3781  sub _reset_insertion_mode ($) {
3781        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3782          $last = 1;          $last = 1;
3783          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3784            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3785                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3786              !!!cp ('t27');          } else {
3787              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3788          }          }
3789        }        }
3790              
3791        ## Step 4..13        ## Step 4..14
3792        my $new_mode = {        my $new_mode;
3793          if ($node->[1] & FOREIGN_EL) {
3794            !!!cp ('t28.1');
3795            ## NOTE: Strictly spaking, the line below only applies to MathML and
3796            ## SVG elements.  Currently the HTML syntax supports only MathML and
3797            ## SVG elements as foreigners.
3798            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3799          } elsif ($node->[1] & TABLE_CELL_EL) {
3800            if ($last) {
3801              !!!cp ('t28.2');
3802              #
3803            } else {
3804              !!!cp ('t28.3');
3805              $new_mode = IN_CELL_IM;
3806            }
3807          } else {
3808            !!!cp ('t28.4');
3809            $new_mode = {
3810                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3811                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3812                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3813                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3814                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3815                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2774  sub _reset_insertion_mode ($) { Line 3820  sub _reset_insertion_mode ($) {
3820                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3821                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3822                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3823                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3824          }
3825        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3826                
3827        ## Step 14        ## Step 15
3828        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3829          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3830            !!!cp ('t29');            !!!cp ('t29');
3831            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2792  sub _reset_insertion_mode ($) { Line 3839  sub _reset_insertion_mode ($) {
3839          !!!cp ('t31');          !!!cp ('t31');
3840        }        }
3841                
3842        ## Step 15        ## Step 16
3843        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3844                
3845        ## Step 16        ## Step 17
3846        $i--;        $i--;
3847        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3848                
3849        ## Step 17        ## Step 18
3850        redo S3;        redo S3;
3851      } # S3      } # S3
3852    
# Line 2911  sub _tree_construction_main ($) { Line 3958  sub _tree_construction_main ($) {
3958      ## Step 1      ## Step 1
3959      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3960      my $el;      my $el;
3961      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3962    
3963      ## Step 2      ## Step 2
3964      $insert->($el);      $insert->($el);
# Line 2922  sub _tree_construction_main ($) { Line 3969  sub _tree_construction_main ($) {
3969    
3970      ## Step 4      ## Step 4
3971      my $text = '';      my $text = '';
3972        !!!nack ('t40.1');
3973      !!!next-token;      !!!next-token;
3974      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3975        !!!cp ('t40');        !!!cp ('t40');
# Line 2948  sub _tree_construction_main ($) { Line 3996  sub _tree_construction_main ($) {
3996        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3997        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3998          !!!cp ('t43');          !!!cp ('t43');
3999          !!!parse-error (type => 'in CDATA:#'.$token->{type});          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4000        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4001          !!!cp ('t44');          !!!cp ('t44');
4002          !!!parse-error (type => 'in RCDATA:#'.$token->{type});          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4003        } else {        } else {
4004          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
4005        }        }
# Line 2961  sub _tree_construction_main ($) { Line 4009  sub _tree_construction_main ($) {
4009    
4010    my $script_start_tag = sub () {    my $script_start_tag = sub () {
4011      my $script_el;      my $script_el;
4012      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4013      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4014    
4015      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
4016      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4017            
4018      my $text = '';      my $text = '';
4019        !!!nack ('t45.1');
4020      !!!next-token;      !!!next-token;
4021      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
4022        !!!cp ('t45');        !!!cp ('t45');
# Line 2987  sub _tree_construction_main ($) { Line 4036  sub _tree_construction_main ($) {
4036        ## Ignore the token        ## Ignore the token
4037      } else {      } else {
4038        !!!cp ('t48');        !!!cp ('t48');
4039        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#eof', token => $token);
4040        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4041        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4042      }      }
# Line 3010  sub _tree_construction_main ($) { Line 4059  sub _tree_construction_main ($) {
4059      !!!next-token;      !!!next-token;
4060    }; # $script_start_tag    }; # $script_start_tag
4061    
4062      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4063      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4064      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4065    
4066    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4067      my $tag_name = shift;      my $end_tag_token = shift;
4068        my $tag_name = $end_tag_token->{tag_name};
4069    
4070        ## NOTE: The adoption agency algorithm (AAA).
4071    
4072      FET: {      FET: {
4073        ## Step 1        ## Step 1
4074        my $formatting_element;        my $formatting_element;
4075        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4076        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4077          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4078              !!!cp ('t52');
4079              last AFE;
4080            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4081                         eq $tag_name) {
4082            !!!cp ('t51');            !!!cp ('t51');
4083            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4084            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4085            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
4086          }          }
4087        } # AFE        } # AFE
4088        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4089          !!!cp ('t53');          !!!cp ('t53');
4090          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4091          ## Ignore the token          ## Ignore the token
4092          !!!next-token;          !!!next-token;
4093          return;          return;
# Line 3047  sub _tree_construction_main ($) { Line 4104  sub _tree_construction_main ($) {
4104              last INSCOPE;              last INSCOPE;
4105            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4106              !!!cp ('t55');              !!!cp ('t55');
4107              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
4108                                text => $token->{tag_name},
4109                                token => $end_tag_token);
4110              ## Ignore the token              ## Ignore the token
4111              !!!next-token;              !!!next-token;
4112              return;              return;
4113            }            }
4114          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4115            !!!cp ('t56');            !!!cp ('t56');
4116            $in_scope = 0;            $in_scope = 0;
4117          }          }
4118        } # INSCOPE        } # INSCOPE
4119        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4120          !!!cp ('t57');          !!!cp ('t57');
4121          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
4122                            text => $token->{tag_name},
4123                            token => $end_tag_token);
4124          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4125          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4126          return;          return;
4127        }        }
4128        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4129          !!!cp ('t58');          !!!cp ('t58');
4130          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
4131                            text => $self->{open_elements}->[-1]->[0]
4132                                ->manakai_local_name,
4133                            token => $end_tag_token);
4134        }        }
4135                
4136        ## Step 2        ## Step 2
# Line 3077  sub _tree_construction_main ($) { Line 4138  sub _tree_construction_main ($) {
4138        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4139        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4140          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4141          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4142              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4143              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4144               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4145            !!!cp ('t59');            !!!cp ('t59');
4146            $furthest_block = $node;            $furthest_block = $node;
4147            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3166  sub _tree_construction_main ($) { Line 4227  sub _tree_construction_main ($) {
4227        } # S7          } # S7  
4228                
4229        ## Step 8        ## Step 8
4230        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4231            my $foster_parent_element;
4232            my $next_sibling;
4233            OE: for (reverse 0..$#{$self->{open_elements}}) {
4234              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4235                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4236                                 if (defined $parent and $parent->node_type == 1) {
4237                                   !!!cp ('t65.1');
4238                                   $foster_parent_element = $parent;
4239                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4240                                 } else {
4241                                   !!!cp ('t65.2');
4242                                   $foster_parent_element
4243                                     = $self->{open_elements}->[$_ - 1]->[0];
4244                                 }
4245                                 last OE;
4246                               }
4247                             } # OE
4248                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4249                               unless defined $foster_parent_element;
4250            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4251            $open_tables->[-1]->[1] = 1; # tainted
4252          } else {
4253            !!!cp ('t65.3');
4254            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4255          }
4256                
4257        ## Step 9        ## Step 9
4258        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3212  sub _tree_construction_main ($) { Line 4298  sub _tree_construction_main ($) {
4298      } # FET      } # FET
4299    }; # $formatting_end_tag    }; # $formatting_end_tag
4300    
   ## NOTE: $open_tables->[-1]->[0] is the "current table".  
   ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.  
   my $open_tables = [[$self->{open_elements}->[0]->[0]]];  
   
4301    $insert = my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4302      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4303    }; # $insert_to_current    }; # $insert_to_current
4304    
4305    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4306      my $child = shift;      my $child = shift;
4307      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]}) {  
4308        # MUST        # MUST
4309        my $foster_parent_element;        my $foster_parent_element;
4310        my $next_sibling;        my $next_sibling;
4311                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4312                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4313                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4314                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4315                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3254  sub _tree_construction_main ($) { Line 4334  sub _tree_construction_main ($) {
4334      }      }
4335    }; # $insert_to_foster    }; # $insert_to_foster
4336    
4337    B: {    B: while (1) {
4338      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4339        !!!cp ('t73');        !!!cp ('t73');
4340        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4341        ## Ignore the token        ## Ignore the token
4342        ## Stay in the phase        ## Stay in the phase
4343        !!!next-token;        !!!next-token;
4344        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  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         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;  
4345      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4346               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4347        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4348          !!!cp ('t79');          !!!cp ('t79');
4349          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4350          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4351        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4352          !!!cp ('t80');          !!!cp ('t80');
4353          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4354          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4355        } else {        } else {
4356          !!!cp ('t81');          !!!cp ('t81');
4357        }        }
4358    
4359        !!!cp ('t82');        !!!cp ('t82');
4360        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
4361        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4362        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4363          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3319  sub _tree_construction_main ($) { Line 4367  sub _tree_construction_main ($) {
4367               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4368          }          }
4369        }        }
4370          !!!nack ('t84.1');
4371        !!!next-token;        !!!next-token;
4372        redo B;        next B;
4373      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4374        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4375        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3334  sub _tree_construction_main ($) { Line 4383  sub _tree_construction_main ($) {
4383          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4384        }        }
4385        !!!next-token;        !!!next-token;
4386        redo B;        next B;
4387      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4388          if ($token->{type} == CHARACTER_TOKEN) {
4389            !!!cp ('t87.1');
4390            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4391            !!!next-token;
4392            next B;
4393          } elsif ($token->{type} == START_TAG_TOKEN) {
4394            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4395                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4396                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4397                ($token->{tag_name} eq 'svg' and
4398                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4399              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4400              !!!cp ('t87.2');
4401              #
4402            } elsif ({
4403                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4404                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4405                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4406                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4407                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4408                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4409                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4410                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4411                     }->{$token->{tag_name}}) {
4412              !!!cp ('t87.2');
4413              !!!parse-error (type => 'not closed',
4414                              text => $self->{open_elements}->[-1]->[0]
4415                                  ->manakai_local_name,
4416                              token => $token);
4417    
4418              pop @{$self->{open_elements}}
4419                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4420    
4421              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4422              ## Reprocess.
4423              next B;
4424            } else {
4425              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4426              my $tag_name = $token->{tag_name};
4427              if ($nsuri eq $SVG_NS) {
4428                $tag_name = {
4429                   altglyph => 'altGlyph',
4430                   altglyphdef => 'altGlyphDef',
4431                   altglyphitem => 'altGlyphItem',
4432                   animatecolor => 'animateColor',
4433                   animatemotion => 'animateMotion',
4434                   animatetransform => 'animateTransform',
4435                   clippath => 'clipPath',
4436                   feblend => 'feBlend',
4437                   fecolormatrix => 'feColorMatrix',
4438                   fecomponenttransfer => 'feComponentTransfer',
4439                   fecomposite => 'feComposite',
4440                   feconvolvematrix => 'feConvolveMatrix',
4441                   fediffuselighting => 'feDiffuseLighting',
4442                   fedisplacementmap => 'feDisplacementMap',
4443                   fedistantlight => 'feDistantLight',
4444                   feflood => 'feFlood',
4445                   fefunca => 'feFuncA',
4446                   fefuncb => 'feFuncB',
4447                   fefuncg => 'feFuncG',
4448                   fefuncr => 'feFuncR',
4449                   fegaussianblur => 'feGaussianBlur',
4450                   feimage => 'feImage',
4451                   femerge => 'feMerge',
4452                   femergenode => 'feMergeNode',
4453                   femorphology => 'feMorphology',
4454                   feoffset => 'feOffset',
4455                   fepointlight => 'fePointLight',
4456                   fespecularlighting => 'feSpecularLighting',
4457                   fespotlight => 'feSpotLight',
4458                   fetile => 'feTile',
4459                   feturbulence => 'feTurbulence',
4460                   foreignobject => 'foreignObject',
4461                   glyphref => 'glyphRef',
4462                   lineargradient => 'linearGradient',
4463                   radialgradient => 'radialGradient',
4464                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4465                   textpath => 'textPath',  
4466                }->{$tag_name} || $tag_name;
4467              }
4468    
4469              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4470    
4471              ## "adjust foreign attributes" - done in insert-element-f
4472    
4473              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4474    
4475              if ($self->{self_closing}) {
4476                pop @{$self->{open_elements}};
4477                !!!ack ('t87.3');
4478              } else {
4479                !!!cp ('t87.4');
4480              }
4481    
4482              !!!next-token;
4483              next B;
4484            }
4485          } elsif ($token->{type} == END_TAG_TOKEN) {
4486            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4487            !!!cp ('t87.5');
4488            #
4489          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4490            !!!cp ('t87.6');
4491            !!!parse-error (type => 'not closed',
4492                            text => $self->{open_elements}->[-1]->[0]
4493                                ->manakai_local_name,
4494                            token => $token);
4495    
4496            pop @{$self->{open_elements}}
4497                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4498    
4499            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4500            ## Reprocess.
4501            next B;
4502          } else {
4503            die "$0: $token->{type}: Unknown token type";        
4504          }
4505        }
4506    
4507        if ($self->{insertion_mode} & HEAD_IMS) {
4508        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4509          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4510            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4511              !!!cp ('t88.2');              !!!cp ('t88.2');
4512              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4513                #
4514            } else {            } else {
4515              !!!cp ('t88.1');              !!!cp ('t88.1');
4516              ## Ignore the token.              ## Ignore the token.
4517              !!!next-token;              #
             redo B;  
4518            }            }
4519            unless (length $token->{data}) {            unless (length $token->{data}) {
4520              !!!cp ('t88');              !!!cp ('t88');
4521              !!!next-token;              !!!next-token;
4522              redo B;              next B;
4523            }            }
4524    ## TODO: set $token->{column} appropriately
4525          }          }
4526    
4527          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4528            !!!cp ('t89');            !!!cp ('t89');
4529            ## As if <head>            ## As if <head>
4530            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4531            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4532            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4533                  [$self->{head_element}, $el_category->{head}];
4534    
4535            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4536            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3369  sub _tree_construction_main ($) { Line 4540  sub _tree_construction_main ($) {
4540            !!!cp ('t90');            !!!cp ('t90');
4541            ## As if </noscript>            ## As if </noscript>
4542            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4543            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4544                        
4545            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4546            ## As if </head>            ## As if </head>
# Line 3385  sub _tree_construction_main ($) { Line 4556  sub _tree_construction_main ($) {
4556            !!!cp ('t92');            !!!cp ('t92');
4557          }          }
4558    
4559              ## "after head" insertion mode          ## "after head" insertion mode
4560              ## As if <body>          ## As if <body>
4561              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4562              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4563              ## reprocess          ## reprocess
4564              redo B;          next B;
4565            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4566              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4567                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4568                  !!!cp ('t93');              !!!cp ('t93');
4569                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4570                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4571                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4572                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4573                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4574                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4575                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4576                  !!!cp ('t94');              !!!next-token;
4577                  #              next B;
4578                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4579                  !!!cp ('t95');              !!!cp ('t93.2');
4580                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4581                  ## Ignore the token                              token => $token);
4582                  !!!next-token;              ## Ignore the token
4583                  redo B;              !!!nack ('t93.3');
4584                }              !!!next-token;
4585              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4586                !!!cp ('t96');            } else {
4587                ## As if <head>              !!!cp ('t95');
4588                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4589                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4590                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4591                !!!nack ('t95.1');
4592                !!!next-token;
4593                next B;
4594              }
4595            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4596              !!!cp ('t96');
4597              ## As if <head>
4598              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4599              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4600              push @{$self->{open_elements}},
4601                  [$self->{head_element}, $el_category->{head}];
4602    
4603                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4604                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4605              } else {          } else {
4606                !!!cp ('t97');            !!!cp ('t97');
4607              }          }
4608    
4609              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4610                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4611                  !!!cp ('t98');                  !!!cp ('t98');
4612                  ## As if </noscript>                  ## As if </noscript>
4613                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4614                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4615                                    token => $token);
4616                                
4617                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4618                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3440  sub _tree_construction_main ($) { Line 4623  sub _tree_construction_main ($) {
4623                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4624                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4625                  !!!cp ('t100');                  !!!cp ('t100');
4626                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4627                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4628                    push @{$self->{open_elements}},
4629                        [$self->{head_element}, $el_category->{head}];
4630                } else {                } else {
4631                  !!!cp ('t101');                  !!!cp ('t101');
4632                }                }
4633                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4634                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4635                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4636                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4637                  !!!nack ('t101.1');
4638                !!!next-token;                !!!next-token;
4639                redo B;                next B;
4640              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4641                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4642                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4643                  !!!cp ('t102');                  !!!cp ('t102');
4644                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4645                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4646                    push @{$self->{open_elements}},
4647                        [$self->{head_element}, $el_category->{head}];
4648                } else {                } else {
4649                  !!!cp ('t103');                  !!!cp ('t103');
4650                }                }
4651                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4652                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4653                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4654                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4655                  !!!ack ('t103.1');
4656                !!!next-token;                !!!next-token;
4657                redo B;                next B;
4658              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4659                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4660                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4661                  !!!cp ('t104');                  !!!cp ('t104');
4662                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4663                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4664                    push @{$self->{open_elements}},
4665                        [$self->{head_element}, $el_category->{head}];
4666                } else {                } else {
4667                  !!!cp ('t105');                  !!!cp ('t105');
4668                }                }
4669                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4670                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.
4671    
4672                unless ($self->{confident}) {                unless ($self->{confident}) {
4673                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4674                    !!!cp ('t106');                    !!!cp ('t106');
4675                      ## NOTE: Whether the encoding is supported or not is handled
4676                      ## in the {change_encoding} callback.
4677                    $self->{change_encoding}                    $self->{change_encoding}
4678                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4679                             $token);
4680                                        
4681                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4682                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4683                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4684                                                 ->{has_reference});                                                 ->{has_reference});
4685                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4686                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4687                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4688                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4689                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4690                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4691                      !!!cp ('t107');                      !!!cp ('t107');
4692                        ## NOTE: Whether the encoding is supported or not is handled
4693                        ## in the {change_encoding} callback.
4694                      $self->{change_encoding}                      $self->{change_encoding}
4695                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4696                               $token);
4697                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4698                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4699                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3523  sub _tree_construction_main ($) { Line 4719  sub _tree_construction_main ($) {
4719                  }                  }
4720                }                }
4721    
4722                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4723                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4724                  !!!ack ('t110.1');
4725                !!!next-token;                !!!next-token;
4726                redo B;                next B;
4727              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4728                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4729                  !!!cp ('t111');                  !!!cp ('t111');
4730                  ## As if </noscript>                  ## As if </noscript>
4731                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4732                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4733                                    token => $token);
4734                                
4735                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4736                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4737                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4738                  !!!cp ('t112');                  !!!cp ('t112');
4739                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4740                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4741                    push @{$self->{open_elements}},
4742                        [$self->{head_element}, $el_category->{head}];
4743                } else {                } else {
4744                  !!!cp ('t113');                  !!!cp ('t113');
4745                }                }
# Line 3548  sub _tree_construction_main ($) { Line 4748  sub _tree_construction_main ($) {
4748                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4749                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4750                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4751                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4752                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4753                redo B;                next B;
4754              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4755                         $token->{tag_name} eq 'noframes') {
4756                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4757                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4758                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4759                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4760                  !!!cp ('t114');                  !!!cp ('t114');
4761                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4762                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4763                    push @{$self->{open_elements}},
4764                        [$self->{head_element}, $el_category->{head}];
4765                } else {                } else {
4766                  !!!cp ('t115');                  !!!cp ('t115');
4767                }                }
4768                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4769                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4770                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4771                redo B;                next B;
4772              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4773                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4774                  !!!cp ('t116');                  !!!cp ('t116');
4775                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4776                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4777                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4778                    !!!nack ('t116.1');
4779                  !!!next-token;                  !!!next-token;
4780                  redo B;                  next B;
4781                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4782                  !!!cp ('t117');                  !!!cp ('t117');
4783                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
4784                                    token => $token);
4785                  ## Ignore the token                  ## Ignore the token
4786                    !!!nack ('t117.1');
4787                  !!!next-token;                  !!!next-token;
4788                  redo B;                  next B;
4789                } else {                } else {
4790                  !!!cp ('t118');                  !!!cp ('t118');
4791                  #                  #
# Line 3589  sub _tree_construction_main ($) { Line 4795  sub _tree_construction_main ($) {
4795                  !!!cp ('t119');                  !!!cp ('t119');
4796                  ## As if </noscript>                  ## As if </noscript>
4797                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4798                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4799                                    token => $token);
4800                                
4801                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4802                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4803                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4804                  !!!cp ('t120');                  !!!cp ('t120');
4805                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4806                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4807                    push @{$self->{open_elements}},
4808                        [$self->{head_element}, $el_category->{head}];
4809                } else {                } else {
4810                  !!!cp ('t121');                  !!!cp ('t121');
4811                }                }
4812    
4813                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4814                $script_start_tag->($insert_to_current);                $script_start_tag->();
4815                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4816                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4817                redo B;                next B;
4818              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4819                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4820                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4821                  !!!cp ('t122');                  !!!cp ('t122');
4822                  ## As if </noscript>                  ## As if </noscript>
4823                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4824                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4825                                    text => $token->{tag_name}, token => $token);
4826                                    
4827                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4828                  ## As if </head>                  ## As if </head>
# Line 3629  sub _tree_construction_main ($) { Line 4839  sub _tree_construction_main ($) {
4839                }                }
4840    
4841                ## "after head" insertion mode                ## "after head" insertion mode
4842                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4843                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4844                  !!!cp ('t126');                  !!!cp ('t126');
4845                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3639  sub _tree_construction_main ($) { Line 4849  sub _tree_construction_main ($) {
4849                } else {                } else {
4850                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4851                }                }
4852                  !!!nack ('t127.1');
4853                !!!next-token;                !!!next-token;
4854                redo B;                next B;
4855              } else {              } else {
4856                !!!cp ('t128');                !!!cp ('t128');
4857                #                #
# Line 3650  sub _tree_construction_main ($) { Line 4861  sub _tree_construction_main ($) {
4861                !!!cp ('t129');                !!!cp ('t129');
4862                ## As if </noscript>                ## As if </noscript>
4863                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4864                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4865                                  text => $token->{tag_name}, token => $token);
4866                                
4867                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4868                ## As if </head>                ## As if </head>
# Line 3669  sub _tree_construction_main ($) { Line 4881  sub _tree_construction_main ($) {
4881    
4882              ## "after head" insertion mode              ## "after head" insertion mode
4883              ## As if <body>              ## As if <body>
4884              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4885              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4886              ## reprocess              ## reprocess
4887              redo B;              !!!ack-later;
4888                next B;
4889            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4890              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4891                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4892                  !!!cp ('t132');                  !!!cp ('t132');
4893                  ## As if <head>                  ## As if <head>
4894                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4895                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4896                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4897                        [$self->{head_element}, $el_category->{head}];
4898    
4899                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4900                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4901                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4902                  !!!next-token;                  !!!next-token;
4903                  redo B;                  next B;
4904                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4905                  !!!cp ('t133');                  !!!cp ('t133');
4906                  ## As if </noscript>                  ## As if </noscript>
4907                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4908                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/',
4909                                    text => 'head', token => $token);
4910                                    
4911                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4912                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4913                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4914                  !!!next-token;                  !!!next-token;
4915                  redo B;                  next B;
4916                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4917                  !!!cp ('t134');                  !!!cp ('t134');
4918                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4919                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4920                  !!!next-token;                  !!!next-token;
4921                  redo B;                  next B;
4922                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4923                    !!!cp ('t134.1');
4924                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4925                                    token => $token);
4926                    ## Ignore the token
4927                    !!!next-token;
4928                    next B;
4929                } else {                } else {
4930                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4931                }                }
4932              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4933                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3714  sub _tree_construction_main ($) { Line 4935  sub _tree_construction_main ($) {
4935                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4936                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4937                  !!!next-token;                  !!!next-token;
4938                  redo B;                  next B;
4939                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4940                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4941                  !!!cp ('t137');                  !!!cp ('t137');
4942                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
4943                                    text => 'noscript', token => $token);
4944                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4945                  !!!next-token;                  !!!next-token;
4946                  redo B;                  next B;
4947                } else {                } else {
4948                  !!!cp ('t138');                  !!!cp ('t138');
4949                  #                  #
# Line 3728  sub _tree_construction_main ($) { Line 4951  sub _tree_construction_main ($) {
4951              } elsif ({              } elsif ({
4952                        body => 1, html => 1,                        body => 1, html => 1,
4953                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4954                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4955                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4956                  ## 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) {  
4957                  !!!cp ('t140');                  !!!cp ('t140');
4958                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4959                                    text => $token->{tag_name}, token => $token);
4960                    ## Ignore the token
4961                    !!!next-token;
4962                    next B;
4963                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4964                    !!!cp ('t140.1');
4965                    !!!parse-error (type => 'unmatched end tag',
4966                                    text => $token->{tag_name}, token => $token);
4967                  ## Ignore the token                  ## Ignore the token
4968                  !!!next-token;                  !!!next-token;
4969                  redo B;                  next B;
4970                } else {                } else {
4971                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4972                }                }
4973                              } elsif ($token->{tag_name} eq 'p') {
4974                #                !!!cp ('t142');
4975              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4976                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4977                       }->{$token->{tag_name}}) {                ## Ignore the token
4978                  !!!next-token;
4979                  next B;
4980                } elsif ($token->{tag_name} eq 'br') {
4981                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4982                  !!!cp ('t142');                  !!!cp ('t142.2');
4983                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4984                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4985                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4986                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4987      
4988                    ## Reprocess in the "after head" insertion mode...
4989                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4990                    !!!cp ('t143.2');
4991                    ## As if </head>
4992                    pop @{$self->{open_elements}};
4993                    $self->{insertion_mode} = AFTER_HEAD_IM;
4994      
4995                    ## Reprocess in the "after head" insertion mode...
4996                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4997                    !!!cp ('t143.3');
4998                    ## ISSUE: Two parse errors for <head><noscript></br>
4999                    !!!parse-error (type => 'unmatched end tag',
5000                                    text => 'br', token => $token);
5001                    ## As if </noscript>
5002                    pop @{$self->{open_elements}};
5003                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
5004    
5005                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
5006                } else {                  ## As if </head>
5007                  !!!cp ('t143');                  pop @{$self->{open_elements}};
5008                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
5009    
5010                #                  ## Reprocess in the "after head" insertion mode...
5011              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5012                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
5013                  #                  #
5014                } else {                } else {
5015                  !!!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;  
5016                }                }
5017    
5018                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5019                  !!!parse-error (type => 'unmatched end tag',
5020                                  text => 'br', token => $token);
5021                  ## Ignore the token
5022                  !!!next-token;
5023                  next B;
5024                } else {
5025                  !!!cp ('t145');
5026                  !!!parse-error (type => 'unmatched end tag',
5027                                  text => $token->{tag_name}, token => $token);
5028                  ## Ignore the token
5029                  !!!next-token;
5030                  next B;
5031              }              }
5032    
5033              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5034                !!!cp ('t146');                !!!cp ('t146');
5035                ## As if </noscript>                ## As if </noscript>
5036                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5037                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
5038                                  text => $token->{tag_name}, token => $token);
5039                                
5040                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5041                ## As if </head>                ## As if </head>
# Line 3798  sub _tree_construction_main ($) { Line 5051  sub _tree_construction_main ($) {
5051              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5052  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
5053                !!!cp ('t148');                !!!cp ('t148');
5054                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5055                                  text => $token->{tag_name}, token => $token);
5056                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5057                !!!next-token;                !!!next-token;
5058                redo B;                next B;
5059              } else {              } else {
5060                !!!cp ('t149');                !!!cp ('t149');
5061              }              }
5062    
5063              ## "after head" insertion mode              ## "after head" insertion mode
5064              ## As if <body>              ## As if <body>
5065              !!!insert-element ('body');              !!!insert-element ('body',, $token);
5066              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
5067              ## reprocess              ## reprocess
5068              redo B;              next B;
5069            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5070              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5071            }            !!!cp ('t149.1');
5072    
5073              ## NOTE: As if <head>
5074              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5075              $self->{open_elements}->[-1]->[0]->append_child
5076                  ($self->{head_element});
5077              #push @{$self->{open_elements}},
5078              #    [$self->{head_element}, $el_category->{head}];
5079              #$self->{insertion_mode} = IN_HEAD_IM;
5080              ## NOTE: Reprocess.
5081    
5082              ## NOTE: As if </head>
5083              #pop @{$self->{open_elements}};
5084              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5085              ## NOTE: Reprocess.
5086              
5087              #
5088            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5089              !!!cp ('t149.2');
5090    
5091              ## NOTE: As if </head>
5092              pop @{$self->{open_elements}};
5093              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5094              ## NOTE: Reprocess.
5095    
5096              #
5097            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5098              !!!cp ('t149.3');
5099    
5100              !!!parse-error (type => 'in noscript:#eof', token => $token);
5101    
5102              ## As if </noscript>
5103              pop @{$self->{open_elements}};
5104              #$self->{insertion_mode} = IN_HEAD_IM;
5105              ## NOTE: Reprocess.
5106    
5107              ## NOTE: As if </head>
5108              pop @{$self->{open_elements}};
5109              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5110              ## NOTE: Reprocess.
5111    
5112              #
5113            } else {
5114              !!!cp ('t149.4');
5115              #
5116            }
5117    
5118            ## NOTE: As if <body>
5119            !!!insert-element ('body',, $token);
5120            $self->{insertion_mode} = IN_BODY_IM;
5121            ## NOTE: Reprocess.
5122            next B;
5123          } else {
5124            die "$0: $token->{type}: Unknown token type";
5125          }
5126    
5127            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5128      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3826  sub _tree_construction_main ($) { Line 5134  sub _tree_construction_main ($) {
5134              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5135    
5136              !!!next-token;              !!!next-token;
5137              redo B;              next B;
5138            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5139              if ({              if ({
5140                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3834  sub _tree_construction_main ($) { Line 5142  sub _tree_construction_main ($) {
5142                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5143                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5144                  ## have an element in table scope                  ## have an element in table scope
5145                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5146                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5147                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5148                      !!!cp ('t151');                      !!!cp ('t151');
5149                      $tn = $node->[1];  
5150                      last INSCOPE;                      ## Close the cell
5151                    } elsif ({                      !!!back-token; # <x>
5152                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
5153                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
5154                                  line => $token->{line},
5155                                  column => $token->{column}};
5156                        next B;
5157                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5158                      !!!cp ('t152');                      !!!cp ('t152');
5159                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
5160                    }                      last;
                 } # INSCOPE  
                   unless (defined $tn) {  
                     !!!cp ('t153');  
 ## TODO: This error type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
5161                    }                    }
5162                                    }
5163                  !!!cp ('t154');  
5164                  ## Close the cell                  !!!cp ('t153');
5165                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
5166                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      text => $token->{tag_name}, token => $token);
5167                  redo B;                  ## Ignore the token
5168                    !!!nack ('t153.1');
5169                    !!!next-token;
5170                    next B;
5171                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5172                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5173                                    token => $token);
5174                                    
5175                  ## As if </caption>                  ## NOTE: As if </caption>.
5176                  ## have a table element in table scope                  ## have a table element in table scope
5177                  my $i;                  my $i;
5178                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5179                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5180                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5181                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
5182                      $i = $_;                        !!!cp ('t155');
5183                      last INSCOPE;                        $i = $_;
5184                    } elsif ({                        last INSCOPE;
5185                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5186                             }->{$node->[1]}) {                        !!!cp ('t156');
5187                      !!!cp ('t156');                        last;
5188                      last INSCOPE;                      }
5189                    }                    }
5190    
5191                      !!!cp ('t157');
5192                      !!!parse-error (type => 'start tag not allowed',
5193                                      text => $token->{tag_name}, token => $token);
5194                      ## Ignore the token
5195                      !!!nack ('t157.1');
5196                      !!!next-token;
5197                      next B;
5198                  } # 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;  
                   }  
5199                                    
5200                  ## generate implied end tags                  ## generate implied end tags
5201                  while ({                  while ($self->{open_elements}->[-1]->[1]
5202                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5203                    !!!cp ('t158');                    !!!cp ('t158');
5204                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5205                  }                  }
5206    
5207                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5208                    !!!cp ('t159');                    !!!cp ('t159');
5209                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5210                                      text => $self->{open_elements}->[-1]->[0]
5211                                          ->manakai_local_name,
5212                                      token => $token);
5213                  } else {                  } else {
5214                    !!!cp ('t160');                    !!!cp ('t160');
5215                  }                  }
# Line 3912  sub _tree_construction_main ($) { Line 5221  sub _tree_construction_main ($) {
5221                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5222                                    
5223                  ## reprocess                  ## reprocess
5224                  redo B;                  !!!ack-later;
5225                    next B;
5226                } else {                } else {
5227                  !!!cp ('t161');                  !!!cp ('t161');
5228                  #                  #
# Line 3928  sub _tree_construction_main ($) { Line 5238  sub _tree_construction_main ($) {
5238                  my $i;                  my $i;
5239                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5240                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5241                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5242                      !!!cp ('t163');                      !!!cp ('t163');
5243                      $i = $_;                      $i = $_;
5244                      last INSCOPE;                      last INSCOPE;
5245                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5246                      !!!cp ('t164');                      !!!cp ('t164');
5247                      last INSCOPE;                      last INSCOPE;
5248                    }                    }
5249                  } # INSCOPE                  } # INSCOPE
5250                    unless (defined $i) {                    unless (defined $i) {
5251                      !!!cp ('t165');                      !!!cp ('t165');
5252                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5253                                        text => $token->{tag_name},
5254                                        token => $token);
5255                      ## Ignore the token                      ## Ignore the token
5256                      !!!next-token;                      !!!next-token;
5257                      redo B;                      next B;
5258                    }                    }
5259                                    
5260                  ## generate implied end tags                  ## generate implied end tags
5261                  while ({                  while ($self->{open_elements}->[-1]->[1]
5262                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5263                    !!!cp ('t166');                    !!!cp ('t166');
5264                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5265                  }                  }
5266    
5267                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5268                            ne $token->{tag_name}) {
5269                    !!!cp ('t167');                    !!!cp ('t167');
5270                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5271                                      text => $self->{open_elements}->[-1]->[0]
5272                                          ->manakai_local_name,
5273                                      token => $token);
5274                  } else {                  } else {
5275                    !!!cp ('t168');                    !!!cp ('t168');
5276                  }                  }
# Line 3969  sub _tree_construction_main ($) { Line 5282  sub _tree_construction_main ($) {
5282                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5283                                    
5284                  !!!next-token;                  !!!next-token;
5285                  redo B;                  next B;
5286                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5287                  !!!cp ('t169');                  !!!cp ('t169');
5288                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5289                                    text => $token->{tag_name}, token => $token);
5290                  ## Ignore the token                  ## Ignore the token
5291                  !!!next-token;                  !!!next-token;
5292                  redo B;                  next B;
5293                } else {                } else {
5294                  !!!cp ('t170');                  !!!cp ('t170');
5295                  #                  #
# Line 3984  sub _tree_construction_main ($) { Line 5298  sub _tree_construction_main ($) {
5298                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5299                  ## have a table element in table scope                  ## have a table element in table scope
5300                  my $i;                  my $i;
5301                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5302                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5303                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5304                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
5305                      $i = $_;                        !!!cp ('t171');
5306                      last INSCOPE;                        $i = $_;
5307                    } elsif ({                        last INSCOPE;
5308                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5309                             }->{$node->[1]}) {                        !!!cp ('t172');
5310                      !!!cp ('t172');                        last;
5311                      last INSCOPE;                      }
5312                    }                    }
5313    
5314                      !!!cp ('t173');
5315                      !!!parse-error (type => 'unmatched end tag',
5316                                      text => $token->{tag_name}, token => $token);
5317                      ## Ignore the token
5318                      !!!next-token;
5319                      next B;
5320                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5321                                    
5322                  ## generate implied end tags                  ## generate implied end tags
5323                  while ({                  while ($self->{open_elements}->[-1]->[1]
5324                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5325                    !!!cp ('t174');                    !!!cp ('t174');
5326                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5327                  }                  }
5328                                    
5329                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5330                    !!!cp ('t175');                    !!!cp ('t175');
5331                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5332                                      text => $self->{open_elements}->[-1]->[0]
5333                                          ->manakai_local_name,
5334                                      token => $token);
5335                  } else {                  } else {
5336                    !!!cp ('t176');                    !!!cp ('t176');
5337                  }                  }
# Line 4027  sub _tree_construction_main ($) { Line 5343  sub _tree_construction_main ($) {
5343                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5344                                    
5345                  !!!next-token;                  !!!next-token;
5346                  redo B;                  next B;
5347                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5348                  !!!cp ('t177');                  !!!cp ('t177');
5349                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5350                                    text => $token->{tag_name}, token => $token);
5351                  ## Ignore the token                  ## Ignore the token
5352                  !!!next-token;                  !!!next-token;
5353                  redo B;                  next B;
5354                } else {                } else {
5355                  !!!cp ('t178');                  !!!cp ('t178');
5356                  #                  #
# Line 4046  sub _tree_construction_main ($) { Line 5363  sub _tree_construction_main ($) {
5363                ## have an element in table scope                ## have an element in table scope
5364                my $i;                my $i;
5365                my $tn;                my $tn;
5366                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5367                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5368                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5369                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5370                    $i = $_;                      !!!cp ('t179');
5371                    last INSCOPE;                      $i = $_;
5372                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
5373                    !!!cp ('t180');                      ## Close the cell
5374                    $tn = $node->[1];                      !!!back-token; # </x>
5375                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5376                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
5377                  } elsif ({                                column => $token->{column}};
5378                            table => 1, html => 1,                      next B;
5379                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5380                    !!!cp ('t181');                      !!!cp ('t180');
5381                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
5382                        ## NOTE: There is exactly one |td| or |th| element
5383                        ## in scope in the stack of open elements by definition.
5384                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5385                        ## ISSUE: Can this be reached?
5386                        !!!cp ('t181');
5387                        last;
5388                      }
5389                  }                  }
5390                } # INSCOPE  
               unless (defined $i) {  
5391                  !!!cp ('t182');                  !!!cp ('t182');
5392                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5393                        text => $token->{tag_name}, token => $token);
5394                  ## Ignore the token                  ## Ignore the token
5395                  !!!next-token;                  !!!next-token;
5396                  redo B;                  next B;
5397                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5398              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5399                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5400                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5401                                  token => $token);
5402    
5403                ## As if </caption>                ## As if </caption>
5404                ## have a table element in table scope                ## have a table element in table scope
5405                my $i;                my $i;
5406                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5407                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5408                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5409                    !!!cp ('t184');                    !!!cp ('t184');
5410                    $i = $_;                    $i = $_;
5411                    last INSCOPE;                    last INSCOPE;
5412                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5413                    !!!cp ('t185');                    !!!cp ('t185');
5414                    last INSCOPE;                    last INSCOPE;
5415                  }                  }
5416                } # INSCOPE                } # INSCOPE
5417                unless (defined $i) {                unless (defined $i) {
5418                  !!!cp ('t186');                  !!!cp ('t186');
5419                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag',
5420                                    text => 'caption', token => $token);
5421                  ## Ignore the token                  ## Ignore the token
5422                  !!!next-token;                  !!!next-token;
5423                  redo B;                  next B;
5424                }                }
5425                                
5426                ## generate implied end tags                ## generate implied end tags
5427                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5428                  !!!cp ('t187');                  !!!cp ('t187');
5429                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5430                }                }
5431    
5432                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5433                  !!!cp ('t188');                  !!!cp ('t188');
5434                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5435                                    text => $self->{open_elements}->[-1]->[0]
5436                                        ->manakai_local_name,
5437                                    token => $token);
5438                } else {                } else {
5439                  !!!cp ('t189');                  !!!cp ('t189');
5440                }                }
# Line 4128  sub _tree_construction_main ($) { Line 5446  sub _tree_construction_main ($) {
5446                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5447    
5448                ## reprocess                ## reprocess
5449                redo B;                next B;
5450              } elsif ({              } elsif ({
5451                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5452                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5453                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5454                  !!!cp ('t190');                  !!!cp ('t190');
5455                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5456                                    text => $token->{tag_name}, token => $token);
5457                  ## Ignore the token                  ## Ignore the token
5458                  !!!next-token;                  !!!next-token;
5459                  redo B;                  next B;
5460                } else {                } else {
5461                  !!!cp ('t191');                  !!!cp ('t191');
5462                  #                  #
# Line 4148  sub _tree_construction_main ($) { Line 5467  sub _tree_construction_main ($) {
5467                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5468                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5469                !!!cp ('t192');                !!!cp ('t192');
5470                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5471                                  text => $token->{tag_name}, token => $token);
5472                ## Ignore the token                ## Ignore the token
5473                !!!next-token;                !!!next-token;
5474                redo B;                next B;
5475              } else {              } else {
5476                !!!cp ('t193');                !!!cp ('t193');
5477                #                #
5478              }              }
5479          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5480            for my $entry (@{$self->{open_elements}}) {
5481              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5482                !!!cp ('t75');
5483                !!!parse-error (type => 'in body:#eof', token => $token);
5484                last;
5485              }
5486            }
5487    
5488            ## Stop parsing.
5489            last B;
5490        } else {        } else {
5491          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5492        }        }
# Line 4171  sub _tree_construction_main ($) { Line 5502  sub _tree_construction_main ($) {
5502            unless (length $token->{data}) {            unless (length $token->{data}) {
5503              !!!cp ('t194');              !!!cp ('t194');
5504              !!!next-token;              !!!next-token;
5505              redo B;              next B;
5506            } else {            } else {
5507              !!!cp ('t195');              !!!cp ('t195');
5508            }            }
5509          }          }
5510    
5511              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5512    
5513              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5514              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4185  sub _tree_construction_main ($) { Line 5516  sub _tree_construction_main ($) {
5516              ## result in a new Text node.              ## result in a new Text node.
5517              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5518                            
5519              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]}) {  
5520                # MUST                # MUST
5521                my $foster_parent_element;                my $foster_parent_element;
5522                my $next_sibling;                my $next_sibling;
5523                my $prev_sibling;                my $prev_sibling;
5524                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5525                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5526                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5527                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5528                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4229  sub _tree_construction_main ($) { Line 5557  sub _tree_construction_main ($) {
5557          }          }
5558                            
5559          !!!next-token;          !!!next-token;
5560          redo B;          next B;
5561        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5562              if ({          if ({
5563                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5564                   th => 1, td => 1,               th => 1, td => 1,
5565                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5566                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5567                  ## Clear back to table context              ## Clear back to table context
5568                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5569                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5570                    !!!cp ('t201');                !!!cp ('t201');
5571                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5572                  }              }
5573                                
5574                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5575                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5576                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5577                }            }
5578              
5579                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5580                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5581                    !!!cp ('t202');                !!!cp ('t202');
5582                    !!!parse-error (type => 'missing start tag:tr');                !!!parse-error (type => 'missing start tag:tr', token => $token);
5583                  }              }
5584                                    
5585                  ## Clear back to table body context              ## Clear back to table body context
5586                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5587                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5588                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5589                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5590                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5591                    pop @{$self->{open_elements}};              }
                 }  
5592                                    
5593                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5594                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5595                    !!!cp ('t204');                    !!!cp ('t204');
5596                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5597                      !!!nack ('t204');
5598                    !!!next-token;                    !!!next-token;
5599                    redo B;                    next B;
5600                  } else {                  } else {
5601                    !!!cp ('t205');                    !!!cp ('t205');
5602                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5603                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5604                  }                  }
5605                } else {                } else {
# Line 4279  sub _tree_construction_main ($) { Line 5607  sub _tree_construction_main ($) {
5607                }                }
5608    
5609                ## Clear back to table row context                ## Clear back to table row context
5610                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5611                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5612                  !!!cp ('t207');                  !!!cp ('t207');
5613                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5614                }                }
5615                                
5616                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5617                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5618    
5619                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5620                                
5621                  !!!nack ('t207.1');
5622                !!!next-token;                !!!next-token;
5623                redo B;                next B;
5624              } elsif ({              } elsif ({
5625                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5626                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4304  sub _tree_construction_main ($) { Line 5632  sub _tree_construction_main ($) {
5632                  my $i;                  my $i;
5633                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5634                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5635                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5636                      !!!cp ('t208');                      !!!cp ('t208');
5637                      $i = $_;                      $i = $_;
5638                      last INSCOPE;                      last INSCOPE;
5639                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5640                      !!!cp ('t209');                      !!!cp ('t209');
5641                      last INSCOPE;                      last INSCOPE;
5642                    }                    }
5643                  } # INSCOPE                  } # INSCOPE
5644                  unless (defined $i) {                  unless (defined $i) {
5645                   !!!cp ('t210');                    !!!cp ('t210');
5646  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5647                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag',
5648                                      text => $token->{tag_name}, token => $token);
5649                    ## Ignore the token                    ## Ignore the token
5650                      !!!nack ('t210.1');
5651                    !!!next-token;                    !!!next-token;
5652                    redo B;                    next B;
5653                  }                  }
5654                                    
5655                  ## Clear back to table row context                  ## Clear back to table row context
5656                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5657                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5658                    !!!cp ('t211');                    !!!cp ('t211');
5659                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5660                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4341  sub _tree_construction_main ($) { Line 5665  sub _tree_construction_main ($) {
5665                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5666                    !!!cp ('t212');                    !!!cp ('t212');
5667                    ## reprocess                    ## reprocess
5668                    redo B;                    !!!ack-later;
5669                      next B;
5670                  } else {                  } else {
5671                    !!!cp ('t213');                    !!!cp ('t213');
5672                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4353  sub _tree_construction_main ($) { Line 5678  sub _tree_construction_main ($) {
5678                  my $i;                  my $i;
5679                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5680                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5681                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5682                      !!!cp ('t214');                      !!!cp ('t214');
5683                      $i = $_;                      $i = $_;
5684                      last INSCOPE;                      last INSCOPE;
5685                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5686                      !!!cp ('t215');                      !!!cp ('t215');
5687                      last INSCOPE;                      last INSCOPE;
5688                    }                    }
5689                  } # INSCOPE                  } # INSCOPE
5690                  unless (defined $i) {                  unless (defined $i) {
5691                    !!!cp ('t216');                    !!!cp ('t216');
5692  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5693                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5694                                      text => $token->{tag_name}, token => $token);
5695                    ## Ignore the token                    ## Ignore the token
5696                      !!!nack ('t216.1');
5697                    !!!next-token;                    !!!next-token;
5698                    redo B;                    next B;
5699                  }                  }
5700    
5701                  ## Clear back to table body context                  ## Clear back to table body context
5702                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5703                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5704                    !!!cp ('t217');                    !!!cp ('t217');
5705                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5706                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4400  sub _tree_construction_main ($) { Line 5722  sub _tree_construction_main ($) {
5722    
5723                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5724                  ## Clear back to table context                  ## Clear back to table context
5725                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5726                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5727                    !!!cp ('t219');                    !!!cp ('t219');
5728                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5729                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5730                  }                  }
5731                                    
5732                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5733                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5734                  ## reprocess                  ## reprocess
5735                  redo B;                  !!!ack-later;
5736                    next B;
5737                } elsif ({                } elsif ({
5738                          caption => 1,                          caption => 1,
5739                          colgroup => 1,                          colgroup => 1,
5740                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5741                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5742                  ## Clear back to table context                  ## Clear back to table context
5743                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5744                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5745                    !!!cp ('t220');                    !!!cp ('t220');
5746                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5747                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4427  sub _tree_construction_main ($) { Line 5750  sub _tree_construction_main ($) {
5750                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5751                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5752                                    
5753                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5754                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5755                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5756                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4436  sub _tree_construction_main ($) { Line 5759  sub _tree_construction_main ($) {
5759                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5760                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5761                  !!!next-token;                  !!!next-token;
5762                  redo B;                  !!!nack ('t220.1');
5763                    next B;
5764                } else {                } else {
5765                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5766                }                }
5767              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5768                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5769                                  text => $self->{open_elements}->[-1]->[0]
5770                                      ->manakai_local_name,
5771                                  token => $token);
5772    
5773                ## As if </table>                ## As if </table>
5774                ## have a table element in table scope                ## have a table element in table scope
5775                my $i;                my $i;
5776                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5777                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5778                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5779                    !!!cp ('t221');                    !!!cp ('t221');
5780                    $i = $_;                    $i = $_;
5781                    last INSCOPE;                    last INSCOPE;
5782                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5783                    !!!cp ('t222');                    !!!cp ('t222');
5784                    last INSCOPE;                    last INSCOPE;
5785                  }                  }
# Line 4463  sub _tree_construction_main ($) { Line 5787  sub _tree_construction_main ($) {
5787                unless (defined $i) {                unless (defined $i) {
5788                  !!!cp ('t223');                  !!!cp ('t223');
5789  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5790                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5791                                    token => $token);
5792                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5793                    !!!nack ('t223.1');
5794                  !!!next-token;                  !!!next-token;
5795                  redo B;                  next B;
5796                }                }
5797                                
5798    ## TODO: Followings are removed from the latest spec.
5799                ## generate implied end tags                ## generate implied end tags
5800                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5801                  !!!cp ('t224');                  !!!cp ('t224');
5802                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5803                }                }
5804    
5805                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5806                  !!!cp ('t225');                  !!!cp ('t225');
5807  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5808                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5809                                    text => $self->{open_elements}->[-1]->[0]
5810                                        ->manakai_local_name,
5811                                    token => $token);
5812                } else {                } else {
5813                  !!!cp ('t226');                  !!!cp ('t226');
5814                }                }
# Line 4490  sub _tree_construction_main ($) { Line 5818  sub _tree_construction_main ($) {
5818    
5819                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5820    
5821                ## reprocess            ## reprocess
5822                redo B;            !!!ack-later;
5823              next B;
5824            } elsif ($token->{tag_name} eq 'style') {
5825              if (not $open_tables->[-1]->[1]) { # tainted
5826                !!!cp ('t227.8');
5827                ## NOTE: This is a "as if in head" code clone.
5828                $parse_rcdata->(CDATA_CONTENT_MODEL);
5829                next B;
5830              } else {
5831                !!!cp ('t227.7');
5832                #
5833              }
5834            } elsif ($token->{tag_name} eq 'script') {
5835              if (not $open_tables->[-1]->[1]) { # tainted
5836                !!!cp ('t227.6');
5837                ## NOTE: This is a "as if in head" code clone.
5838                $script_start_tag->();
5839                next B;
5840              } else {
5841                !!!cp ('t227.5');
5842                #
5843              }
5844          } elsif ($token->{tag_name} eq 'input') {          } elsif ($token->{tag_name} eq 'input') {
5845            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5846              if ($token->{attributes}->{type}) { ## TODO: case              if ($token->{attributes}->{type}) { ## TODO: case
5847                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5848                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5849                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5850                  !!!parse-error (type => 'in table:'.$token->{tag_name});                  !!!parse-error (type => 'in table',
5851                                    text => $token->{tag_name}, token => $token);
5852    
5853                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5854    
5855                  ## TODO: form element pointer                  ## TODO: form element pointer
5856    
5857                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5858    
5859                  !!!next-token;                  !!!next-token;
5860                  redo B;                  !!!ack ('t227.2.1');
5861                    next B;
5862                } else {                } else {
5863                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5864                  #                  #
# Line 4525  sub _tree_construction_main ($) { Line 5876  sub _tree_construction_main ($) {
5876            #            #
5877          }          }
5878    
5879          !!!parse-error (type => 'in table:'.$token->{tag_name});          !!!parse-error (type => 'in table', text => $token->{tag_name},
5880                            token => $token);
5881    
5882          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5883          #          #
# Line 4536  sub _tree_construction_main ($) { Line 5888  sub _tree_construction_main ($) {
5888                my $i;                my $i;
5889                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5890                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5891                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5892                    !!!cp ('t228');                    !!!cp ('t228');
5893                    $i = $_;                    $i = $_;
5894                    last INSCOPE;                    last INSCOPE;
5895                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5896                    !!!cp ('t229');                    !!!cp ('t229');
5897                    last INSCOPE;                    last INSCOPE;
5898                  }                  }
5899                } # INSCOPE                } # INSCOPE
5900                unless (defined $i) {                unless (defined $i) {
5901                  !!!cp ('t230');                  !!!cp ('t230');
5902                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5903                                    text => $token->{tag_name}, token => $token);
5904                  ## Ignore the token                  ## Ignore the token
5905                    !!!nack ('t230.1');
5906                  !!!next-token;                  !!!next-token;
5907                  redo B;                  next B;
5908                } else {                } else {
5909                  !!!cp ('t232');                  !!!cp ('t232');
5910                }                }
5911    
5912                ## Clear back to table row context                ## Clear back to table row context
5913                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5914                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5915                  !!!cp ('t231');                  !!!cp ('t231');
5916  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5917                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4569  sub _tree_construction_main ($) { Line 5920  sub _tree_construction_main ($) {
5920                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5921                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5922                !!!next-token;                !!!next-token;
5923                redo B;                !!!nack ('t231.1');
5924                  next B;
5925              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5926                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5927                  ## As if </tr>                  ## As if </tr>
# Line 4577  sub _tree_construction_main ($) { Line 5929  sub _tree_construction_main ($) {
5929                  my $i;                  my $i;
5930                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5931                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5932                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5933                      !!!cp ('t233');                      !!!cp ('t233');
5934                      $i = $_;                      $i = $_;
5935                      last INSCOPE;                      last INSCOPE;
5936                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5937                      !!!cp ('t234');                      !!!cp ('t234');
5938                      last INSCOPE;                      last INSCOPE;
5939                    }                    }
# Line 4591  sub _tree_construction_main ($) { Line 5941  sub _tree_construction_main ($) {
5941                  unless (defined $i) {                  unless (defined $i) {
5942                    !!!cp ('t235');                    !!!cp ('t235');
5943  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5944                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag',
5945                                      text => $token->{type}, token => $token);
5946                    ## Ignore the token                    ## Ignore the token
5947                      !!!nack ('t236.1');
5948                    !!!next-token;                    !!!next-token;
5949                    redo B;                    next B;
5950                  }                  }
5951                                    
5952                  ## Clear back to table row context                  ## Clear back to table row context
5953                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5954                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5955                    !!!cp ('t236');                    !!!cp ('t236');
5956  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5957                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4616  sub _tree_construction_main ($) { Line 5967  sub _tree_construction_main ($) {
5967                  my $i;                  my $i;
5968                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5969                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5970                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5971                      !!!cp ('t237');                      !!!cp ('t237');
5972                      $i = $_;                      $i = $_;
5973                      last INSCOPE;                      last INSCOPE;
5974                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5975                      !!!cp ('t238');                      !!!cp ('t238');
5976                      last INSCOPE;                      last INSCOPE;
5977                    }                    }
5978                  } # INSCOPE                  } # INSCOPE
5979                  unless (defined $i) {                  unless (defined $i) {
5980                    !!!cp ('t239');                    !!!cp ('t239');
5981                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5982                                      text => $token->{tag_name}, token => $token);
5983                    ## Ignore the token                    ## Ignore the token
5984                      !!!nack ('t239.1');
5985                    !!!next-token;                    !!!next-token;
5986                    redo B;                    next B;
5987                  }                  }
5988                                    
5989                  ## Clear back to table body context                  ## Clear back to table body context
5990                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5991                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5992                    !!!cp ('t240');                    !!!cp ('t240');
5993                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5994                  }                  }
# Line 4666  sub _tree_construction_main ($) { Line 6014  sub _tree_construction_main ($) {
6014                my $i;                my $i;
6015                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6016                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6017                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
6018                    !!!cp ('t241');                    !!!cp ('t241');
6019                    $i = $_;                    $i = $_;
6020                    last INSCOPE;                    last INSCOPE;
6021                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6022                    !!!cp ('t242');                    !!!cp ('t242');
6023                    last INSCOPE;                    last INSCOPE;
6024                  }                  }
6025                } # INSCOPE                } # INSCOPE
6026                unless (defined $i) {                unless (defined $i) {
6027                  !!!cp ('t243');                  !!!cp ('t243');
6028                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6029                                    text => $token->{tag_name}, token => $token);
6030                  ## Ignore the token                  ## Ignore the token
6031                    !!!nack ('t243.1');
6032                  !!!next-token;                  !!!next-token;
6033                  redo B;                  next B;
6034                }                }
6035                                    
6036                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4691  sub _tree_construction_main ($) { Line 6039  sub _tree_construction_main ($) {
6039                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6040                                
6041                !!!next-token;                !!!next-token;
6042                redo B;                next B;
6043              } elsif ({              } elsif ({
6044                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6045                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4701  sub _tree_construction_main ($) { Line 6049  sub _tree_construction_main ($) {
6049                  my $i;                  my $i;
6050                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6051                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6052                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6053                      !!!cp ('t247');                      !!!cp ('t247');
6054                      $i = $_;                      $i = $_;
6055                      last INSCOPE;                      last INSCOPE;
6056                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6057                      !!!cp ('t248');                      !!!cp ('t248');
6058                      last INSCOPE;                      last INSCOPE;
6059                    }                    }
6060                  } # INSCOPE                  } # INSCOPE
6061                    unless (defined $i) {                    unless (defined $i) {
6062                      !!!cp ('t249');                      !!!cp ('t249');
6063                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
6064                                        text => $token->{tag_name}, token => $token);
6065                      ## Ignore the token                      ## Ignore the token
6066                        !!!nack ('t249.1');
6067                      !!!next-token;                      !!!next-token;
6068                      redo B;                      next B;
6069                    }                    }
6070                                    
6071                  ## As if </tr>                  ## As if </tr>
# Line 4725  sub _tree_construction_main ($) { Line 6073  sub _tree_construction_main ($) {
6073                  my $i;                  my $i;
6074                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6075                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6076                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6077                      !!!cp ('t250');                      !!!cp ('t250');
6078                      $i = $_;                      $i = $_;
6079                      last INSCOPE;                      last INSCOPE;
6080                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6081                      !!!cp ('t251');                      !!!cp ('t251');
6082                      last INSCOPE;                      last INSCOPE;
6083                    }                    }
6084                  } # INSCOPE                  } # INSCOPE
6085                    unless (defined $i) {                    unless (defined $i) {
6086                      !!!cp ('t252');                      !!!cp ('t252');
6087                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
6088                                        text => 'tr', token => $token);
6089                      ## Ignore the token                      ## Ignore the token
6090                        !!!nack ('t252.1');
6091                      !!!next-token;                      !!!next-token;
6092                      redo B;                      next B;
6093                    }                    }
6094                                    
6095                  ## Clear back to table row context                  ## Clear back to table row context
6096                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6097                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6098                    !!!cp ('t253');                    !!!cp ('t253');
6099  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
6100                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4762  sub _tree_construction_main ($) { Line 6109  sub _tree_construction_main ($) {
6109                my $i;                my $i;
6110                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6111                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6112                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6113                    !!!cp ('t254');                    !!!cp ('t254');
6114                    $i = $_;                    $i = $_;
6115                    last INSCOPE;                    last INSCOPE;
6116                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6117                    !!!cp ('t255');                    !!!cp ('t255');
6118                    last INSCOPE;                    last INSCOPE;
6119                  }                  }
6120                } # INSCOPE                } # INSCOPE
6121                unless (defined $i) {                unless (defined $i) {
6122                  !!!cp ('t256');                  !!!cp ('t256');
6123                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6124                                    text => $token->{tag_name}, token => $token);
6125                  ## Ignore the token                  ## Ignore the token
6126                    !!!nack ('t256.1');
6127                  !!!next-token;                  !!!next-token;
6128                  redo B;                  next B;
6129                }                }
6130    
6131                ## Clear back to table body context                ## Clear back to table body context
6132                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6133                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
6134                  !!!cp ('t257');                  !!!cp ('t257');
6135  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
6136                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4792  sub _tree_construction_main ($) { Line 6138  sub _tree_construction_main ($) {
6138    
6139                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6140                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6141                  !!!nack ('t257.1');
6142                !!!next-token;                !!!next-token;
6143                redo B;                next B;
6144              } elsif ({              } elsif ({
6145                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6146                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6147                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6148                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6149                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6150                !!!cp ('t258');            !!!cp ('t258');
6151                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6152                ## Ignore the token                            text => $token->{tag_name}, token => $token);
6153                !!!next-token;            ## Ignore the token
6154                redo B;            !!!nack ('t258.1');
6155               !!!next-token;
6156              next B;
6157          } else {          } else {
6158            !!!cp ('t259');            !!!cp ('t259');
6159            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
6160                              text => $token->{tag_name}, token => $token);
6161    
6162            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6163            #            #
6164          }          }
6165          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6166            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6167                    @{$self->{open_elements}} == 1) { # redundant, maybe
6168              !!!parse-error (type => 'in body:#eof', token => $token);
6169              !!!cp ('t259.1');
6170              #
6171            } else {
6172              !!!cp ('t259.2');
6173              #
6174            }
6175    
6176            ## Stop parsing
6177            last B;
6178        } else {        } else {
6179          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6180        }        }
# Line 4822  sub _tree_construction_main ($) { Line 6185  sub _tree_construction_main ($) {
6185                unless (length $token->{data}) {                unless (length $token->{data}) {
6186                  !!!cp ('t260');                  !!!cp ('t260');
6187                  !!!next-token;                  !!!next-token;
6188                  redo B;                  next B;
6189                }                }
6190              }              }
6191                            
# Line 4831  sub _tree_construction_main ($) { Line 6194  sub _tree_construction_main ($) {
6194            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6195              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6196                !!!cp ('t262');                !!!cp ('t262');
6197                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6198                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6199                  !!!ack ('t262.1');
6200                !!!next-token;                !!!next-token;
6201                redo B;                next B;
6202              } else {              } else {
6203                !!!cp ('t263');                !!!cp ('t263');
6204                #                #
6205              }              }
6206            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6207              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6208                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6209                  !!!cp ('t264');                  !!!cp ('t264');
6210                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
6211                                    text => 'colgroup', token => $token);
6212                  ## Ignore the token                  ## Ignore the token
6213                  !!!next-token;                  !!!next-token;
6214                  redo B;                  next B;
6215                } else {                } else {
6216                  !!!cp ('t265');                  !!!cp ('t265');
6217                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6218                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6219                  !!!next-token;                  !!!next-token;
6220                  redo B;                              next B;            
6221                }                }
6222              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6223                !!!cp ('t266');                !!!cp ('t266');
6224                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
6225                                  text => 'col', token => $token);
6226                ## Ignore the token                ## Ignore the token
6227                !!!next-token;                !!!next-token;
6228                redo B;                next B;
6229              } else {              } else {
6230                !!!cp ('t267');                !!!cp ('t267');
6231                #                #
6232              }              }
6233            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6234              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6235            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6236              !!!cp ('t270.2');
6237              ## Stop parsing.
6238              last B;
6239            } else {
6240              ## NOTE: As if </colgroup>.
6241              !!!cp ('t270.1');
6242              pop @{$self->{open_elements}}; # colgroup
6243              $self->{insertion_mode} = IN_TABLE_IM;
6244              ## Reprocess.
6245              next B;
6246            }
6247          } else {
6248            die "$0: $token->{type}: Unknown token type";
6249          }
6250    
6251            ## As if </colgroup>            ## As if </colgroup>
6252            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6253              !!!cp ('t269');              !!!cp ('t269');
6254              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
6255                !!!parse-error (type => 'unmatched end tag',
6256                                text => 'colgroup', token => $token);
6257              ## Ignore the token              ## Ignore the token
6258                !!!nack ('t269.1');
6259              !!!next-token;              !!!next-token;
6260              redo B;              next B;
6261            } else {            } else {
6262              !!!cp ('t270');              !!!cp ('t270');
6263              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6264              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6265                !!!ack-later;
6266              ## reprocess              ## reprocess
6267              redo B;              next B;
6268            }            }
6269      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6270        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6271          !!!cp ('t271');          !!!cp ('t271');
6272          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6273          !!!next-token;          !!!next-token;
6274          redo B;          next B;
6275        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6276              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6277                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6278                  !!!cp ('t272');              !!!cp ('t272');
6279                  ## As if </option>              ## As if </option>
6280                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6281                } else {            } else {
6282                  !!!cp ('t273');              !!!cp ('t273');
6283                }            }
6284    
6285                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6286                !!!next-token;            !!!nack ('t273.1');
6287                redo B;            !!!next-token;
6288              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6289                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6290                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6291                  ## As if </option>              !!!cp ('t274');
6292                  pop @{$self->{open_elements}};              ## As if </option>
6293                } else {              pop @{$self->{open_elements}};
6294                  !!!cp ('t275');            } else {
6295                }              !!!cp ('t275');
6296              }
6297    
6298                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6299                  !!!cp ('t276');              !!!cp ('t276');
6300                  ## As if </optgroup>              ## As if </optgroup>
6301                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6302                } else {            } else {
6303                  !!!cp ('t277');              !!!cp ('t277');
6304                }            }
6305    
6306                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6307                !!!next-token;            !!!nack ('t277.1');
6308                redo B;            !!!next-token;
6309              } elsif ($token->{tag_name} eq 'select') {            next B;
6310  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ({
6311                !!!parse-error (type => 'not closed:select');                     select => 1, input => 1, textarea => 1,
6312                ## As if </select> instead                   }->{$token->{tag_name}} or
6313                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6314                my $i;                    {
6315                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
6316                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
6317                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
6318                    !!!cp ('t278');                    }->{$token->{tag_name}})) {
6319                    $i = $_;            ## TODO: The type below is not good - <select> is replaced by </select>
6320                    last INSCOPE;            !!!parse-error (type => 'not closed', text => 'select',
6321                  } elsif ({                            token => $token);
6322                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
6323                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
6324                    !!!cp ('t279');            ## have an element in table scope
6325                    last INSCOPE;            my $i;
6326                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6327                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6328                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6329                  !!!cp ('t280');                !!!cp ('t278');
6330                  !!!parse-error (type => 'unmatched end tag:select');                $i = $_;
6331                  ## Ignore the token                last INSCOPE;
6332                  !!!next-token;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6333                  redo B;                !!!cp ('t279');
6334                }                last INSCOPE;
6335                }
6336              } # INSCOPE
6337              unless (defined $i) {
6338                !!!cp ('t280');
6339                !!!parse-error (type => 'unmatched end tag',
6340                                text => 'select', token => $token);
6341                ## Ignore the token
6342                !!!nack ('t280.1');
6343                !!!next-token;
6344                next B;
6345              }
6346                                
6347                !!!cp ('t281');            !!!cp ('t281');
6348                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6349    
6350                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6351    
6352                !!!next-token;            if ($token->{tag_name} eq 'select') {
6353                redo B;              !!!nack ('t281.2');
6354                !!!next-token;
6355                next B;
6356              } else {
6357                !!!cp ('t281.1');
6358                !!!ack-later;
6359                ## Reprocess the token.
6360                next B;
6361              }
6362          } else {          } else {
6363            !!!cp ('t282');            !!!cp ('t282');
6364            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
6365                              text => $token->{tag_name}, token => $token);
6366            ## Ignore the token            ## Ignore the token
6367              !!!nack ('t282.1');
6368            !!!next-token;            !!!next-token;
6369            redo B;            next B;
6370          }          }
6371        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6372              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6373                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6374                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6375                  !!!cp ('t283');              !!!cp ('t283');
6376                  ## As if </option>              ## As if </option>
6377                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6378                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6379                  !!!cp ('t284');              !!!cp ('t284');
6380                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6381                } else {            } else {
6382                  !!!cp ('t285');              !!!cp ('t285');
6383                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6384                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6385                }              ## Ignore the token
6386                !!!next-token;            }
6387                redo B;            !!!nack ('t285.1');
6388              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6389                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6390                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6391                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6392                } else {              !!!cp ('t286');
6393                  !!!cp ('t287');              pop @{$self->{open_elements}};
6394                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
6395                  ## Ignore the token              !!!cp ('t287');
6396                }              !!!parse-error (type => 'unmatched end tag',
6397                !!!next-token;                              text => $token->{tag_name}, token => $token);
6398                redo B;              ## Ignore the token
6399              } elsif ($token->{tag_name} eq 'select') {            }
6400                ## have an element in table scope            !!!nack ('t287.1');
6401                my $i;            !!!next-token;
6402                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6403                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6404                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6405                    !!!cp ('t288');            my $i;
6406                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6407                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6408                  } elsif ({              if ($node->[1] & SELECT_EL) {
6409                            table => 1, html => 1,                !!!cp ('t288');
6410                           }->{$node->[1]}) {                $i = $_;
6411                    !!!cp ('t289');                last INSCOPE;
6412                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6413                  }                !!!cp ('t289');
6414                } # INSCOPE                last INSCOPE;
6415                unless (defined $i) {              }
6416                  !!!cp ('t290');            } # INSCOPE
6417                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
6418                  ## Ignore the token              !!!cp ('t290');
6419                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6420                  redo B;                              text => $token->{tag_name}, token => $token);
6421                }              ## Ignore the token
6422                !!!nack ('t290.1');
6423                !!!next-token;
6424                next B;
6425              }
6426                                
6427                !!!cp ('t291');            !!!cp ('t291');
6428                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6429    
6430                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6431    
6432                !!!next-token;            !!!nack ('t291.1');
6433                redo B;            !!!next-token;
6434              } elsif ({            next B;
6435                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6436                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6437                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6438                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6439                     }->{$token->{tag_name}}) {
6440  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6441                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6442                              text => $token->{tag_name}, token => $token);
6443                                
6444                ## have an element in table scope            ## have an element in table scope
6445                my $i;            my $i;
6446                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6447                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6448                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6449                    !!!cp ('t292');                !!!cp ('t292');
6450                    $i = $_;                $i = $_;
6451                    last INSCOPE;                last INSCOPE;
6452                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6453                            table => 1, html => 1,                !!!cp ('t293');
6454                           }->{$node->[1]}) {                last INSCOPE;
6455                    !!!cp ('t293');              }
6456                    last INSCOPE;            } # INSCOPE
6457                  }            unless (defined $i) {
6458                } # INSCOPE              !!!cp ('t294');
6459                unless (defined $i) {              ## Ignore the token
6460                  !!!cp ('t294');              !!!nack ('t294.1');
6461                  ## Ignore the token              !!!next-token;
6462                  !!!next-token;              next B;
6463                  redo B;            }
               }  
6464                                
6465                ## As if </select>            ## As if </select>
6466                ## have an element in table scope            ## have an element in table scope
6467                undef $i;            undef $i;
6468                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6469                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6470                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6471                    !!!cp ('t295');                !!!cp ('t295');
6472                    $i = $_;                $i = $_;
6473                    last INSCOPE;                last INSCOPE;
6474                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6475  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6476                    !!!cp ('t296');                !!!cp ('t296');
6477                    last INSCOPE;                last INSCOPE;
6478                  }              }
6479                } # INSCOPE            } # INSCOPE
6480                unless (defined $i) {            unless (defined $i) {
6481                  !!!cp ('t297');              !!!cp ('t297');
6482  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6483                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
6484                  ## Ignore the </select> token                              text => 'select', token => $token);
6485                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6486                  redo B;              !!!nack ('t297.1');
6487                }              !!!next-token; ## TODO: ok?
6488                next B;
6489              }
6490                                
6491                !!!cp ('t298');            !!!cp ('t298');
6492                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6493    
6494                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6495    
6496                ## reprocess            !!!ack-later;
6497                redo B;            ## reprocess
6498              next B;
6499          } else {          } else {
6500            !!!cp ('t299');            !!!cp ('t299');
6501            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
6502                              text => $token->{tag_name}, token => $token);
6503            ## Ignore the token            ## Ignore the token
6504              !!!nack ('t299.3');
6505            !!!next-token;            !!!next-token;
6506            redo B;            next B;
6507          }          }
6508          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6509            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6510                    @{$self->{open_elements}} == 1) { # redundant, maybe
6511              !!!cp ('t299.1');
6512              !!!parse-error (type => 'in body:#eof', token => $token);
6513            } else {
6514              !!!cp ('t299.2');
6515            }
6516    
6517            ## Stop parsing.
6518            last B;
6519        } else {        } else {
6520          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6521        }        }
# Line 5105  sub _tree_construction_main ($) { Line 6531  sub _tree_construction_main ($) {
6531            unless (length $token->{data}) {            unless (length $token->{data}) {
6532              !!!cp ('t300');              !!!cp ('t300');
6533              !!!next-token;              !!!next-token;
6534              redo B;              next B;
6535            }            }
6536          }          }
6537                    
6538          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6539            !!!cp ('t301');            !!!cp ('t301');
6540            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
6541    
6542            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6543          } else {          } else {
# Line 5119  sub _tree_construction_main ($) { Line 6545  sub _tree_construction_main ($) {
6545          }          }
6546                    
6547          ## "after body" insertion mode          ## "after body" insertion mode
6548          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6549    
6550          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6551          ## reprocess          ## reprocess
6552          redo B;          next B;
6553        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6554          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6555            !!!cp ('t303');            !!!cp ('t303');
6556            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6557                              text => $token->{tag_name}, token => $token);
6558                        
6559            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6560          } else {          } else {
# Line 5135  sub _tree_construction_main ($) { Line 6562  sub _tree_construction_main ($) {
6562          }          }
6563    
6564          ## "after body" insertion mode          ## "after body" insertion mode
6565          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6566                            text => $token->{tag_name}, token => $token);
6567    
6568          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6569            !!!ack-later;
6570          ## reprocess          ## reprocess
6571          redo B;          next B;
6572        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6573          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6574            !!!cp ('t305');            !!!cp ('t305');
6575            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6576                              text => $token->{tag_name}, token => $token);
6577                        
6578            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6579            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5155  sub _tree_construction_main ($) { Line 6585  sub _tree_construction_main ($) {
6585          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6586            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6587              !!!cp ('t307');              !!!cp ('t307');
6588              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
6589                                text => 'html', token => $token);
6590              ## Ignore the token              ## Ignore the token
6591              !!!next-token;              !!!next-token;
6592              redo B;              next B;
6593            } else {            } else {
6594              !!!cp ('t308');              !!!cp ('t308');
6595              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6596              !!!next-token;              !!!next-token;
6597              redo B;              next B;
6598            }            }
6599          } else {          } else {
6600            !!!cp ('t309');            !!!cp ('t309');
6601            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
6602                              text => $token->{tag_name}, token => $token);
6603    
6604            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6605            ## reprocess            ## reprocess
6606            redo B;            next B;
6607          }          }
6608          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6609            !!!cp ('t309.2');
6610            ## Stop parsing
6611            last B;
6612        } else {        } else {
6613          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6614        }        }
# Line 5184  sub _tree_construction_main ($) { Line 6620  sub _tree_construction_main ($) {
6620            unless (length $token->{data}) {            unless (length $token->{data}) {
6621              !!!cp ('t310');              !!!cp ('t310');
6622              !!!next-token;              !!!next-token;
6623              redo B;              next B;
6624            }            }
6625          }          }
6626                    
6627          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6628            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6629              !!!cp ('t311');              !!!cp ('t311');
6630              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
6631            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6632              !!!cp ('t312');              !!!cp ('t312');
6633              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6634            } else { # "after html frameset"            } else { # "after after frameset"
6635              !!!cp ('t313');              !!!cp ('t313');
6636              !!!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');  
6637            }            }
6638                        
6639            ## Ignore the token.            ## Ignore the token.
# Line 5212  sub _tree_construction_main ($) { Line 6644  sub _tree_construction_main ($) {
6644              !!!cp ('t315');              !!!cp ('t315');
6645              !!!next-token;              !!!next-token;
6646            }            }
6647            redo B;            next B;
6648          }          }
6649                    
6650          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6651        } 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');  
         }  
   
6652          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6653              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6654            !!!cp ('t318');            !!!cp ('t318');
6655            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6656              !!!nack ('t318.1');
6657            !!!next-token;            !!!next-token;
6658            redo B;            next B;
6659          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6660                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6661            !!!cp ('t319');            !!!cp ('t319');
6662            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6663            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6664              !!!ack ('t319.1');
6665            !!!next-token;            !!!next-token;
6666            redo B;            next B;
6667          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6668            !!!cp ('t320');            !!!cp ('t320');
6669            ## NOTE: As if in body.            ## NOTE: As if in head.
6670            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6671            redo B;            next B;
6672    
6673              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6674              ## has no parse error.
6675          } else {          } else {
6676            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6677              !!!cp ('t321');              !!!cp ('t321');
6678              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
6679            } else {                              text => $token->{tag_name}, token => $token);
6680              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6681              !!!cp ('t322');              !!!cp ('t322');
6682              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
6683                                text => $token->{tag_name}, token => $token);
6684              } else { # "after after frameset"
6685                !!!cp ('t322.2');
6686                !!!parse-error (type => 'after after frameset',
6687                                text => $token->{tag_name}, token => $token);
6688            }            }
6689            ## Ignore the token            ## Ignore the token
6690              !!!nack ('t322.1');
6691            !!!next-token;            !!!next-token;
6692            redo B;            next B;
6693          }          }
6694        } 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');  
         }  
   
6695          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6696              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6697            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6698                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6699              !!!cp ('t325');              !!!cp ('t325');
6700              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6701                                text => $token->{tag_name}, token => $token);
6702              ## Ignore the token              ## Ignore the token
6703              !!!next-token;              !!!next-token;
6704            } else {            } else {
# Line 5283  sub _tree_construction_main ($) { Line 6708  sub _tree_construction_main ($) {
6708            }            }
6709    
6710            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6711                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6712              !!!cp ('t327');              !!!cp ('t327');
6713              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6714            } else {            } else {
6715              !!!cp ('t328');              !!!cp ('t328');
6716            }            }
6717            redo B;            next B;
6718          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6719                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6720            !!!cp ('t329');            !!!cp ('t329');
6721            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6722            !!!next-token;            !!!next-token;
6723            redo B;            next B;
6724          } else {          } else {
6725            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6726              !!!cp ('t330');              !!!cp ('t330');
6727              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/',
6728            } else {                              text => $token->{tag_name}, token => $token);
6729              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6730                !!!cp ('t330.1');
6731                !!!parse-error (type => 'after frameset:/',
6732                                text => $token->{tag_name}, token => $token);
6733              } else { # "after after html"
6734              !!!cp ('t331');              !!!cp ('t331');
6735              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after after frameset:/',
6736                                text => $token->{tag_name}, token => $token);
6737            }            }
6738            ## Ignore the token            ## Ignore the token
6739            !!!next-token;            !!!next-token;
6740            redo B;            next B;
6741            }
6742          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6743            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6744                    @{$self->{open_elements}} == 1) { # redundant, maybe
6745              !!!cp ('t331.1');
6746              !!!parse-error (type => 'in body:#eof', token => $token);
6747            } else {
6748              !!!cp ('t331.2');
6749          }          }
6750            
6751            ## Stop parsing
6752            last B;
6753        } else {        } else {
6754          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6755        }        }
# Line 5322  sub _tree_construction_main ($) { Line 6764  sub _tree_construction_main ($) {
6764        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6765          !!!cp ('t332');          !!!cp ('t332');
6766          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6767          $script_start_tag->($insert);          $script_start_tag->();
6768          redo B;          next B;
6769        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6770          !!!cp ('t333');          !!!cp ('t333');
6771          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6772          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6773          redo B;          next B;
6774        } elsif ({        } elsif ({
6775                  base => 1, link => 1,                  base => 1, link => 1,
6776                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6777          !!!cp ('t334');          !!!cp ('t334');
6778          ## 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
6779          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6780          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6781            !!!ack ('t334.1');
6782          !!!next-token;          !!!next-token;
6783          redo B;          next B;
6784        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6785          ## 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
6786          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6787          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.
6788    
6789          unless ($self->{confident}) {          unless ($self->{confident}) {
6790            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6791              !!!cp ('t335');              !!!cp ('t335');
6792                ## NOTE: Whether the encoding is supported or not is handled
6793                ## in the {change_encoding} callback.
6794              $self->{change_encoding}              $self->{change_encoding}
6795                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6796                            
6797              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6798                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6799                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6800                                           ->{has_reference});                                           ->{has_reference});
6801            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6802              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6803                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6804                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6805                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6806                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6807                !!!cp ('t336');                !!!cp ('t336');
6808                  ## NOTE: Whether the encoding is supported or not is handled
6809                  ## in the {change_encoding} callback.
6810                $self->{change_encoding}                $self->{change_encoding}
6811                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6812                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6813                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6814                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5386  sub _tree_construction_main ($) { Line 6832  sub _tree_construction_main ($) {
6832            }            }
6833          }          }
6834    
6835            !!!ack ('t338.1');
6836          !!!next-token;          !!!next-token;
6837          redo B;          next B;
6838        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6839          !!!cp ('t341');          !!!cp ('t341');
6840          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6841          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6842          redo B;          next B;
6843        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6844          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6845                                
6846          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6847              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6848            !!!cp ('t342');            !!!cp ('t342');
6849            ## Ignore the token            ## Ignore the token
6850          } else {          } else {
# Line 5411  sub _tree_construction_main ($) { Line 6858  sub _tree_construction_main ($) {
6858              }              }
6859            }            }
6860          }          }
6861            !!!nack ('t343.1');
6862          !!!next-token;          !!!next-token;
6863          redo B;          next B;
6864        } elsif ({        } elsif ({
6865                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6866                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6867                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6868                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6869                  pre => 1, listing => 1,                  pre => 1, listing => 1,
6870                    form => 1,
6871                    table => 1,
6872                    hr => 1,
6873                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6874            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6875              !!!cp ('t350');
6876              !!!parse-error (type => 'in form:form', token => $token);
6877              ## Ignore the token
6878              !!!nack ('t350.1');
6879              !!!next-token;
6880              next B;
6881            }
6882    
6883          ## has a p element in scope          ## has a p element in scope
6884          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6885            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6886              !!!cp ('t344');              !!!cp ('t344');
6887              !!!back-token;              !!!back-token; # <form>
6888              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6889              redo B;                        line => $token->{line}, column => $token->{column}};
6890            } elsif ({              next B;
6891                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6892              !!!cp ('t345');              !!!cp ('t345');
6893              last INSCOPE;              last INSCOPE;
6894            }            }
6895          } # INSCOPE          } # INSCOPE
6896                        
6897          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6898          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6899              !!!nack ('t346.1');
6900            !!!next-token;            !!!next-token;
6901            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6902              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5450  sub _tree_construction_main ($) { Line 6909  sub _tree_construction_main ($) {
6909            } else {            } else {
6910              !!!cp ('t348');              !!!cp ('t348');
6911            }            }
6912          } else {          } elsif ($token->{tag_name} eq 'form') {
6913            !!!cp ('t347');            !!!cp ('t347.1');
6914              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6915    
6916              !!!nack ('t347.2');
6917            !!!next-token;            !!!next-token;
6918          }          } elsif ($token->{tag_name} eq 'table') {
6919          redo B;            !!!cp ('t382');
6920        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6921          if (defined $self->{form_element}) {            
6922            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6923            !!!parse-error (type => 'in form:form');  
6924            ## Ignore the token            !!!nack ('t382.1');
6925              !!!next-token;
6926            } elsif ($token->{tag_name} eq 'hr') {
6927              !!!cp ('t386');
6928              pop @{$self->{open_elements}};
6929            
6930              !!!nack ('t386.1');
6931            !!!next-token;            !!!next-token;
           redo B;  
6932          } else {          } else {
6933            ## 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];  
6934            !!!next-token;            !!!next-token;
           redo B;  
6935          }          }
6936        } elsif ($token->{tag_name} eq 'li') {          next B;
6937          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6938          ## has a p element in scope          ## has a p element in scope
6939          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6940            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6941              !!!cp ('t353');              !!!cp ('t353');
6942              !!!back-token;              !!!back-token; # <x>
6943              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6944              redo B;                        line => $token->{line}, column => $token->{column}};
6945            } elsif ({              next B;
6946                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6947              !!!cp ('t354');              !!!cp ('t354');
6948              last INSCOPE;              last INSCOPE;
6949            }            }
# Line 5504  sub _tree_construction_main ($) { Line 6952  sub _tree_construction_main ($) {
6952          ## Step 1          ## Step 1
6953          my $i = -1;          my $i = -1;
6954          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6955            my $li_or_dtdd = {li => {li => 1},
6956                              dt => {dt => 1, dd => 1},
6957                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6958          LI: {          LI: {
6959            ## Step 2            ## Step 2
6960            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6961              if ($i != -1) {              if ($i != -1) {
6962                !!!cp ('t355');                !!!cp ('t355');
6963                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6964                                $self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
6965                                      ->manakai_local_name,
6966                                  token => $token);
6967              } else {              } else {
6968                !!!cp ('t356');                !!!cp ('t356');
6969              }              }
# Line 5521  sub _tree_construction_main ($) { Line 6974  sub _tree_construction_main ($) {
6974            }            }
6975                        
6976            ## Step 3            ## Step 3
6977            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6978                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6979                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6980                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6981                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6982                  not ($node->[1] & DIV_EL)) {
6983              !!!cp ('t358');              !!!cp ('t358');
6984              last LI;              last LI;
6985            }            }
# Line 5537  sub _tree_construction_main ($) { Line 6991  sub _tree_construction_main ($) {
6991            redo LI;            redo LI;
6992          } # LI          } # LI
6993                        
6994          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6995            !!!nack ('t359.1');
6996          !!!next-token;          !!!next-token;
6997          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;  
6998        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6999          ## has a p element in scope          ## has a p element in scope
7000          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
7001            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
7002              !!!cp ('t367');              !!!cp ('t367');
7003              !!!back-token;              !!!back-token; # <plaintext>
7004              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
7005              redo B;                        line => $token->{line}, column => $token->{column}};
7006            } elsif ({              next B;
7007                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
7008              !!!cp ('t368');              !!!cp ('t368');
7009              last INSCOPE;              last INSCOPE;
7010            }            }
7011          } # INSCOPE          } # INSCOPE
7012                        
7013          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7014                        
7015          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7016                        
7017            !!!nack ('t368.1');
7018          !!!next-token;          !!!next-token;
7019          redo B;          next B;
7020        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
7021          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7022            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
7023            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
7024              !!!cp ('t371');              !!!cp ('t371');
7025              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
7026                            
7027              !!!back-token;              !!!back-token; # <a>
7028              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
7029              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
7030                $formatting_end_tag->($token);
7031                            
7032              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
7033                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5653  sub _tree_construction_main ($) { Line 7052  sub _tree_construction_main ($) {
7052                        
7053          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7054    
7055          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7056          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7057    
7058            !!!nack ('t374.1');
7059          !!!next-token;          !!!next-token;
7060          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;  
7061        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
7062          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7063    
7064          ## has a |nobr| element in scope          ## has a |nobr| element in scope
7065          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7066            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7067            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
7068              !!!cp ('t376');              !!!cp ('t376');
7069              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
7070              !!!back-token;              !!!back-token; # <nobr>
7071              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7072              redo B;                        line => $token->{line}, column => $token->{column}};
7073            } elsif ({              next B;
7074                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7075              !!!cp ('t377');              !!!cp ('t377');
7076              last INSCOPE;              last INSCOPE;
7077            }            }
7078          } # INSCOPE          } # INSCOPE
7079                    
7080          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7081          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7082                    
7083            !!!nack ('t377.1');
7084          !!!next-token;          !!!next-token;
7085          redo B;          next B;
7086        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7087          ## has a button element in scope          ## has a button element in scope
7088          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7089            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7090            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7091              !!!cp ('t378');              !!!cp ('t378');
7092              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
7093              !!!back-token;              !!!back-token; # <button>
7094              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7095              redo B;                        line => $token->{line}, column => $token->{column}};
7096            } elsif ({              next B;
7097                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7098              !!!cp ('t379');              !!!cp ('t379');
7099              last INSCOPE;              last INSCOPE;
7100            }            }
# Line 5718  sub _tree_construction_main ($) { Line 7102  sub _tree_construction_main ($) {
7102                        
7103          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7104                        
7105          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7106    
7107          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
7108    
7109          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7110    
7111            !!!nack ('t379.1');
7112          !!!next-token;          !!!next-token;
7113          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);  
         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});  
         push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];  
   
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
7114        } elsif ({        } elsif ({
7115                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7116                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7117                  image => 1,                  noembed => 1,
7118                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7119                    noscript => 0, ## TODO: 1 if scripting is enabled
7120                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7121          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7122            !!!cp ('t384');            !!!cp ('t381');
7123            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
7124          } else {          } else {
7125            !!!cp ('t385');            !!!cp ('t399');
7126          }          }
7127            ## NOTE: There is an "as if in body" code clone.
7128          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7129          $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;  
7130        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7131          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7132                    
7133          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7134            !!!cp ('t389');            !!!cp ('t389');
7135            ## Ignore the token            ## Ignore the token
7136              !!!nack ('t389'); ## NOTE: Not acknowledged.
7137            !!!next-token;            !!!next-token;
7138            redo B;            next B;
7139          } else {          } else {
7140              !!!ack ('t391.1');
7141    
7142            my $at = $token->{attributes};            my $at = $token->{attributes};
7143            my $form_attrs;            my $form_attrs;
7144            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5836  sub _tree_construction_main ($) { Line 7148  sub _tree_construction_main ($) {
7148            delete $at->{prompt};            delete $at->{prompt};
7149            my @tokens = (            my @tokens = (
7150                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7151                           attributes => $form_attrs},                           attributes => $form_attrs,
7152                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7153                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7154                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7155                            {type => START_TAG_TOKEN, tag_name => 'p',
7156                             line => $token->{line}, column => $token->{column}},
7157                            {type => START_TAG_TOKEN, tag_name => 'label',
7158                             line => $token->{line}, column => $token->{column}},
7159                         );                         );
7160            if ($prompt_attr) {            if ($prompt_attr) {
7161              !!!cp ('t390');              !!!cp ('t390');
7162              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7163                               #line => $token->{line}, column => $token->{column},
7164                              };
7165            } else {            } else {
7166              !!!cp ('t391');              !!!cp ('t391');
7167              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7168                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7169                               #line => $token->{line}, column => $token->{column},
7170                              }; # SHOULD
7171              ## TODO: make this configurable              ## TODO: make this configurable
7172            }            }
7173            push @tokens,            push @tokens,
7174                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7175                             line => $token->{line}, column => $token->{column}},
7176                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7177                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7178                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7179                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7180                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7181            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7182                             line => $token->{line}, column => $token->{column}},
7183                            {type => END_TAG_TOKEN, tag_name => 'form',
7184                             line => $token->{line}, column => $token->{column}};
7185            !!!back-token (@tokens);            !!!back-token (@tokens);
7186            redo B;            !!!next-token;
7187              next B;
7188          }          }
7189        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7190          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7191          my $el;          my $el;
7192          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7193                    
7194          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7195          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5873  sub _tree_construction_main ($) { Line 7198  sub _tree_construction_main ($) {
7198          $insert->($el);          $insert->($el);
7199                    
7200          my $text = '';          my $text = '';
7201            !!!nack ('t392.1');
7202          !!!next-token;          !!!next-token;
7203          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7204            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5903  sub _tree_construction_main ($) { Line 7229  sub _tree_construction_main ($) {
7229            ## Ignore the token            ## Ignore the token
7230          } else {          } else {
7231            !!!cp ('t398');            !!!cp ('t398');
7232            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7233          }          }
7234          !!!next-token;          !!!next-token;
7235            next B;
7236          } elsif ($token->{tag_name} eq 'rt' or
7237                   $token->{tag_name} eq 'rp') {
7238            ## has a |ruby| element in scope
7239            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7240              my $node = $self->{open_elements}->[$_];
7241              if ($node->[1] & RUBY_EL) {
7242                !!!cp ('t398.1');
7243                ## generate implied end tags
7244                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7245                  !!!cp ('t398.2');
7246                  pop @{$self->{open_elements}};
7247                }
7248                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7249                  !!!cp ('t398.3');
7250                  !!!parse-error (type => 'not closed',
7251                                  text => $self->{open_elements}->[-1]->[0]
7252                                      ->manakai_local_name,
7253                                  token => $token);
7254                  pop @{$self->{open_elements}}
7255                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7256                }
7257                last INSCOPE;
7258              } elsif ($node->[1] & SCOPING_EL) {
7259                !!!cp ('t398.4');
7260                last INSCOPE;
7261              }
7262            } # INSCOPE
7263    
7264            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7265    
7266            !!!nack ('t398.5');
7267            !!!next-token;
7268          redo B;          redo B;
7269        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7270                  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);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
7271          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
7272    
7273          ## TODO: associate with $self->{form_element} if defined          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7274    
7275            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7276    
7277            ## "adjust foreign attributes" - done in insert-element-f
7278            
7279            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7280                    
7281          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
7282              pop @{$self->{open_elements}};
7283              !!!ack ('t398.1');
7284            } else {
7285              !!!cp ('t398.2');
7286              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7287              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7288              ## mode, "in body" (not "in foreign content") secondary insertion
7289              ## mode, maybe.
7290            }
7291    
7292          !!!next-token;          !!!next-token;
7293          redo B;          next B;
7294        } elsif ({        } elsif ({
7295                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7296                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5935  sub _tree_construction_main ($) { Line 7298  sub _tree_construction_main ($) {
7298                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7299                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7300          !!!cp ('t401');          !!!cp ('t401');
7301          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
7302                            text => $token->{tag_name}, token => $token);
7303          ## Ignore the token          ## Ignore the token
7304            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7305          !!!next-token;          !!!next-token;
7306          redo B;          next B;
7307                    
7308          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7309        } else {        } else {
7310          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
7311              !!!cp ('t384');
7312              !!!parse-error (type => 'image', token => $token);
7313              $token->{tag_name} = 'img';
7314            } else {
7315              !!!cp ('t385');
7316            }
7317    
7318            ## NOTE: There is an "as if <br>" code clone.
7319          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7320                    
7321          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7322    
7323            if ({
7324                 applet => 1, marquee => 1, object => 1,
7325                }->{$token->{tag_name}}) {
7326              !!!cp ('t380');
7327              push @$active_formatting_elements, ['#marker', ''];
7328              !!!nack ('t380.1');
7329            } elsif ({
7330                      b => 1, big => 1, em => 1, font => 1, i => 1,
7331                      s => 1, small => 1, strile => 1,
7332                      strong => 1, tt => 1, u => 1,
7333                     }->{$token->{tag_name}}) {
7334              !!!cp ('t375');
7335              push @$active_formatting_elements, $self->{open_elements}->[-1];
7336              !!!nack ('t375.1');
7337            } elsif ($token->{tag_name} eq 'input') {
7338              !!!cp ('t388');
7339              ## TODO: associate with $self->{form_element} if defined
7340              pop @{$self->{open_elements}};
7341              !!!ack ('t388.2');
7342            } elsif ({
7343                      area => 1, basefont => 1, bgsound => 1, br => 1,
7344                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7345                      #image => 1,
7346                     }->{$token->{tag_name}}) {
7347              !!!cp ('t388.1');
7348              pop @{$self->{open_elements}};
7349              !!!ack ('t388.3');
7350            } elsif ($token->{tag_name} eq 'select') {
7351              ## TODO: associate with $self->{form_element} if defined
7352            
7353              if ($self->{insertion_mode} & TABLE_IMS or
7354                  $self->{insertion_mode} & BODY_TABLE_IMS or
7355                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7356                !!!cp ('t400.1');
7357                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7358              } else {
7359                !!!cp ('t400.2');
7360                $self->{insertion_mode} = IN_SELECT_IM;
7361              }
7362              !!!nack ('t400.3');
7363            } else {
7364              !!!nack ('t402');
7365            }
7366                    
7367          !!!next-token;          !!!next-token;
7368          redo B;          next B;
7369        }        }
7370      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7371        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7372          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7373              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7374            for (@{$self->{open_elements}}) {          INSCOPE: {
7375              unless ({            for (reverse @{$self->{open_elements}}) {
7376                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7377                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7378                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7379                      }->{$_->[1]}) {                last INSCOPE;
7380                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
7381                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
7382              } else {                last;
               !!!cp ('t404');  
7383              }              }
7384            }            }
7385    
7386            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7387            !!!next-token;                            text => $token->{tag_name}, token => $token);
7388            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
7389            !!!next-token;            !!!next-token;
7390            redo B;            next B;
7391            } # INSCOPE
7392    
7393            for (@{$self->{open_elements}}) {
7394              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7395                !!!cp ('t403');
7396                !!!parse-error (type => 'not closed',
7397                                text => $_->[0]->manakai_local_name,
7398                                token => $token);
7399                last;
7400              } else {
7401                !!!cp ('t404');
7402              }
7403          }          }
7404    
7405            $self->{insertion_mode} = AFTER_BODY_IM;
7406            !!!next-token;
7407            next B;
7408        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7409          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
7410            ## up-to-date, though it has same effect as speced.
7411            if (@{$self->{open_elements}} > 1 and
7412                $self->{open_elements}->[1]->[1] & BODY_EL) {
7413            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7414            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7415              !!!cp ('t406');              !!!cp ('t406');
7416              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
7417                                text => $self->{open_elements}->[1]->[0]
7418                                    ->manakai_local_name,
7419                                token => $token);
7420            } else {            } else {
7421              !!!cp ('t407');              !!!cp ('t407');
7422            }            }
7423            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7424            ## reprocess            ## reprocess
7425            redo B;            next B;
7426          } else {          } else {
7427            !!!cp ('t408');            !!!cp ('t408');
7428            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7429                              text => $token->{tag_name}, token => $token);
7430            ## Ignore the token            ## Ignore the token
7431            !!!next-token;            !!!next-token;
7432            redo B;            next B;
7433          }          }
7434        } elsif ({        } elsif ({
7435                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7436                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7437                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
7438                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7439                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7440                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7441          ## has an element in scope          ## has an element in scope
7442          my $i;          my $i;
7443          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7444            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7445            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7446              !!!cp ('t410');              !!!cp ('t410');
7447              $i = $_;              $i = $_;
7448              last INSCOPE;              last INSCOPE;
7449            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7450              !!!cp ('t411');              !!!cp ('t411');
7451              last INSCOPE;              last INSCOPE;
7452            }            }
# Line 6022  sub _tree_construction_main ($) { Line 7454  sub _tree_construction_main ($) {
7454    
7455          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7456            !!!cp ('t413');            !!!cp ('t413');
7457            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7458                              text => $token->{tag_name}, token => $token);
7459              ## NOTE: Ignore the token.
7460          } else {          } else {
7461            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7462            while ({            while ({
7463                      ## END_TAG_OPTIONAL_EL
7464                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7465                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7466                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7467                    p => 1,                    p => 1,
7468                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7469                      rp => 1,
7470                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7471              !!!cp ('t409');              !!!cp ('t409');
7472              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7473            }            }
7474    
7475            ## Step 2.            ## Step 2.
7476            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7477                      ne $token->{tag_name}) {
7478              !!!cp ('t412');              !!!cp ('t412');
7479              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7480                                text => $self->{open_elements}->[-1]->[0]
7481                                    ->manakai_local_name,
7482                                token => $token);
7483            } else {            } else {
7484              !!!cp ('t414');              !!!cp ('t414');
7485            }            }
# Line 6049  sub _tree_construction_main ($) { Line 7490  sub _tree_construction_main ($) {
7490            ## Step 4.            ## Step 4.
7491            $clear_up_to_marker->()            $clear_up_to_marker->()
7492                if {                if {
7493                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7494                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7495          }          }
7496          !!!next-token;          !!!next-token;
7497          redo B;          next B;
7498        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7499          undef $self->{form_element};          undef $self->{form_element};
7500    
# Line 6061  sub _tree_construction_main ($) { Line 7502  sub _tree_construction_main ($) {
7502          my $i;          my $i;
7503          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7504            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7505            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7506              !!!cp ('t418');              !!!cp ('t418');
7507              $i = $_;              $i = $_;
7508              last INSCOPE;              last INSCOPE;
7509            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7510              !!!cp ('t419');              !!!cp ('t419');
7511              last INSCOPE;              last INSCOPE;
7512            }            }
# Line 6076  sub _tree_construction_main ($) { Line 7514  sub _tree_construction_main ($) {
7514    
7515          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7516            !!!cp ('t421');            !!!cp ('t421');
7517            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7518                              text => $token->{tag_name}, token => $token);
7519              ## NOTE: Ignore the token.
7520          } else {          } else {
7521            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7522            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7523              !!!cp ('t417');              !!!cp ('t417');
7524              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7525            }            }
7526                        
7527            ## Step 2.            ## Step 2.
7528            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7529                      ne $token->{tag_name}) {
7530              !!!cp ('t417.1');              !!!cp ('t417.1');
7531              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7532                                text => $self->{open_elements}->[-1]->[0]
7533                                    ->manakai_local_name,
7534                                token => $token);
7535            } else {            } else {
7536              !!!cp ('t420');              !!!cp ('t420');
7537            }              }  
# Line 6099  sub _tree_construction_main ($) { Line 7541  sub _tree_construction_main ($) {
7541          }          }
7542    
7543          !!!next-token;          !!!next-token;
7544          redo B;          next B;
7545        } elsif ({        } elsif ({
7546                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7547                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6107  sub _tree_construction_main ($) { Line 7549  sub _tree_construction_main ($) {
7549          my $i;          my $i;
7550          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7551            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7552            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7553              !!!cp ('t423');              !!!cp ('t423');
7554              $i = $_;              $i = $_;
7555              last INSCOPE;              last INSCOPE;
7556            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7557              !!!cp ('t424');              !!!cp ('t424');
7558              last INSCOPE;              last INSCOPE;
7559            }            }
# Line 6124  sub _tree_construction_main ($) { Line 7561  sub _tree_construction_main ($) {
7561    
7562          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7563            !!!cp ('t425.1');            !!!cp ('t425.1');
7564            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7565                              text => $token->{tag_name}, token => $token);
7566              ## NOTE: Ignore the token.
7567          } else {          } else {
7568            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7569            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7570              !!!cp ('t422');              !!!cp ('t422');
7571              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7572            }            }
7573                        
7574            ## Step 2.            ## Step 2.
7575            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7576                      ne $token->{tag_name}) {
7577              !!!cp ('t425');              !!!cp ('t425');
7578              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
7579                                text => $token->{tag_name}, token => $token);
7580            } else {            } else {
7581              !!!cp ('t426');              !!!cp ('t426');
7582            }            }
# Line 6147  sub _tree_construction_main ($) { Line 7586  sub _tree_construction_main ($) {
7586          }          }
7587                    
7588          !!!next-token;          !!!next-token;
7589          redo B;          next B;
7590        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7591          ## has an element in scope          ## has an element in scope
7592          my $i;          my $i;
7593          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7594            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7595            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7596              !!!cp ('t410.1');              !!!cp ('t410.1');
7597              $i = $_;              $i = $_;
7598              last INSCOPE;              last INSCOPE;
7599            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7600              !!!cp ('t411.1');              !!!cp ('t411.1');
7601              last INSCOPE;              last INSCOPE;
7602            }            }
7603          } # INSCOPE          } # INSCOPE
7604    
7605          if (defined $i) {          if (defined $i) {
7606            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7607                      ne $token->{tag_name}) {
7608              !!!cp ('t412.1');              !!!cp ('t412.1');
7609              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7610                                text => $self->{open_elements}->[-1]->[0]
7611                                    ->manakai_local_name,
7612                                token => $token);
7613            } else {            } else {
7614              !!!cp ('t414.1');              !!!cp ('t414.1');
7615            }            }
# Line 6177  sub _tree_construction_main ($) { Line 7617  sub _tree_construction_main ($) {
7617            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7618          } else {          } else {
7619            !!!cp ('t413.1');            !!!cp ('t413.1');
7620            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7621                              text => $token->{tag_name}, token => $token);
7622    
7623            !!!cp ('t415.1');            !!!cp ('t415.1');
7624            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7625            my $el;            my $el;
7626            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
7627            $insert->($el);            $insert->($el);
7628            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7629          }          }
7630    
7631          !!!next-token;          !!!next-token;
7632          redo B;          next B;
7633        } elsif ({        } elsif ({
7634                  a => 1,                  a => 1,
7635                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6196  sub _tree_construction_main ($) { Line 7637  sub _tree_construction_main ($) {
7637                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7638                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7639          !!!cp ('t427');          !!!cp ('t427');
7640          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7641          redo B;          next B;
7642        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7643          !!!cp ('t428');          !!!cp ('t428');
7644          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
7645                            text => 'br', token => $token);
7646    
7647          ## As if <br>          ## As if <br>
7648          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7649                    
7650          my $el;          my $el;
7651          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7652          $insert->($el);          $insert->($el);
7653                    
7654          ## Ignore the token.          ## Ignore the token.
7655          !!!next-token;          !!!next-token;
7656          redo B;          next B;
7657        } elsif ({        } elsif ({
7658                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7659                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6225  sub _tree_construction_main ($) { Line 7667  sub _tree_construction_main ($) {
7667                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7668                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7669          !!!cp ('t429');          !!!cp ('t429');
7670          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
7671                            text => $token->{tag_name}, token => $token);
7672          ## Ignore the token          ## Ignore the token
7673          !!!next-token;          !!!next-token;
7674          redo B;          next B;
7675                    
7676          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7677                    
# Line 6239  sub _tree_construction_main ($) { Line 7682  sub _tree_construction_main ($) {
7682    
7683          ## Step 2          ## Step 2
7684          S2: {          S2: {
7685            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7686              ## Step 1              ## Step 1
7687              ## generate implied end tags              ## generate implied end tags
7688              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7689                !!!cp ('t430');                !!!cp ('t430');
7690                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7691                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7692                  ## which seems wrong.
7693                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7694                  $node_i++;
7695              }              }
7696                    
7697              ## Step 2              ## Step 2
7698              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7699                        ne $token->{tag_name}) {
7700                !!!cp ('t431');                !!!cp ('t431');
7701                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7702                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7703                                  text => $self->{open_elements}->[-1]->[0]
7704                                      ->manakai_local_name,
7705                                  token => $token);
7706              } else {              } else {
7707                !!!cp ('t432');                !!!cp ('t432');
7708              }              }
7709                            
7710              ## Step 3              ## Step 3
7711              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7712    
7713              !!!next-token;              !!!next-token;
7714              last S2;              last S2;
7715            } else {            } else {
7716              ## Step 3              ## Step 3
7717              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7718                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7719                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7720                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7721                !!!cp ('t433');                !!!cp ('t433');
7722                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
7723                                  text => $token->{tag_name}, token => $token);
7724                ## Ignore the token                ## Ignore the token
7725                !!!next-token;                !!!next-token;
7726                last S2;                last S2;
# Line 6287  sub _tree_construction_main ($) { Line 7736  sub _tree_construction_main ($) {
7736            ## Step 5;            ## Step 5;
7737            redo S2;            redo S2;
7738          } # S2          } # S2
7739          redo B;          next B;
7740        }        }
7741      }      }
7742      redo B;      next B;
7743      } continue { # B
7744        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7745          ## NOTE: The code below is executed in cases where it does not have
7746          ## to be, but it it is harmless even in those cases.
7747          ## has an element in scope
7748          INSCOPE: {
7749            for (reverse 0..$#{$self->{open_elements}}) {
7750              my $node = $self->{open_elements}->[$_];
7751              if ($node->[1] & FOREIGN_EL) {
7752                last INSCOPE;
7753              } elsif ($node->[1] & SCOPING_EL) {
7754                last;
7755              }
7756            }
7757            
7758            ## NOTE: No foreign element in scope.
7759            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7760          } # INSCOPE
7761        }
7762    } # B    } # B
7763    
7764    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6298  sub _tree_construction_main ($) { Line 7766  sub _tree_construction_main ($) {
7766    ## TODO: script stuffs    ## TODO: script stuffs
7767  } # _tree_construct_main  } # _tree_construct_main
7768    
7769  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7770    my $class = shift;    my $class = shift;
7771    my $node = shift;    my $node = shift;
7772    my $s = \$_[0];    #my $s = \$_[0];
7773    my $onerror = $_[1];    my $onerror = $_[1];
7774      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7775    
7776    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7777    
# Line 6321  sub set_inner_html ($$$) { Line 7790  sub set_inner_html ($$$) {
7790      }      }
7791    
7792      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7793      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7794    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7795      ## TODO: If non-html element      ## TODO: If non-html element
7796    
7797      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7798    
7799    ## TODO: Support for $get_wrapper
7800    
7801      ## Step 1 # MUST      ## Step 1 # MUST
7802      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7803      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6336  sub set_inner_html ($$$) { Line 7807  sub set_inner_html ($$$) {
7807    
7808      ## Step 8 # MUST      ## Step 8 # MUST
7809      my $i = 0;      my $i = 0;
7810      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7811      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7812        require Whatpm::Charset::DecodeHandle;
7813        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7814        $input = $get_wrapper->($input);
7815      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7816        my $self = shift;        my $self = shift;
7817    
7818        pop @{$self->{prev_char}};        my $char = '';
7819        unshift @{$self->{prev_char}}, $self->{next_char};        if (defined $self->{next_next_char}) {
7820            $char = $self->{next_next_char};
7821            delete $self->{next_next_char};
7822            $self->{next_char} = ord $char;
7823          } else {
7824            $self->{char_buffer} = '';
7825            $self->{char_buffer_pos} = 0;
7826            
7827            my $count = $input->manakai_read_until
7828                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7829                 $self->{char_buffer_pos});
7830            if ($count) {
7831              $self->{line_prev} = $self->{line};
7832              $self->{column_prev} = $self->{column};
7833              $self->{column}++;
7834              $self->{next_char}
7835                  = ord substr ($self->{char_buffer},
7836                                $self->{char_buffer_pos}++, 1);
7837              return;
7838            }
7839            
7840            if ($input->read ($char, 1)) {
7841              $self->{next_char} = ord $char;
7842            } else {
7843              $self->{next_char} = -1;
7844              return;
7845            }
7846          }
7847    
7848        $self->{next_char} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7849        $self->{next_char} = ord substr $$s, $i++, 1;        $p->{column}++;
       $column++;  
7850    
7851        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7852          $line++;          $p->{line}++;
7853          $column = 0;          $p->{column} = 0;
7854          !!!cp ('i1');          !!!cp ('i1');
7855        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7856          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7857            my $next = '';
7858            if ($input->read ($next, 1) and $next ne "\x0A") {
7859              $self->{next_next_char} = $next;
7860            }
7861          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7862          $line++;          $p->{line}++;
7863          $column = 0;          $p->{column} = 0;
7864          !!!cp ('i2');          !!!cp ('i2');
       } elsif ($self->{next_char} > 0x10FFFF) {  
         $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
         !!!cp ('i3');  
7865        } elsif ($self->{next_char} == 0x0000) { # NULL        } elsif ($self->{next_char} == 0x0000) { # NULL
7866          !!!cp ('i4');          !!!cp ('i4');
7867          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7868          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7869        }        }
7870      };      };
7871      $p->{prev_char} = [-1, -1, -1];  
7872      $p->{next_char} = -1;      $p->{read_until} = sub {
7873              #my ($scalar, $specials_range, $offset) = @_;
7874          return 0 if defined $p->{next_next_char};
7875    
7876          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7877          my $offset = $_[2] || 0;
7878          
7879          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7880            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7881            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7882              substr ($_[0], $offset)
7883                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7884              my $count = $+[0] - $-[0];
7885              if ($count) {
7886                $p->{column} += $count;
7887                $p->{char_buffer_pos} += $count;
7888                $p->{line_prev} = $p->{line};
7889                $p->{column_prev} = $p->{column} - 1;
7890                $p->{prev_char} = [-1, -1, -1];
7891                $p->{next_char} = -1;
7892              }
7893              return $count;
7894            } else {
7895              return 0;
7896            }
7897          } else {
7898            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7899            if ($count) {
7900              $p->{column} += $count;
7901              $p->{column_prev} += $count;
7902              $p->{prev_char} = [-1, -1, -1];
7903              $p->{next_char} = -1;
7904            }
7905            return $count;
7906          }
7907        }; # $p->{read_until}
7908    
7909      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7910        my (%opt) = @_;        my (%opt) = @_;
7911        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7912          my $column = $opt{column};
7913          if (defined $opt{token} and defined $opt{token}->{line}) {
7914            $line = $opt{token}->{line};
7915            $column = $opt{token}->{column};
7916          }
7917          warn "Parse error ($opt{type}) at line $line column $column\n";
7918      };      };
7919      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7920        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7921      };      };
7922            
7923        my $char_onerror = sub {
7924          my (undef, $type, %opt) = @_;
7925          $ponerror->(layer => 'encode',
7926                      line => $p->{line}, column => $p->{column} + 1,
7927                      %opt, type => $type);
7928        }; # $char_onerror
7929        $input->onerror ($char_onerror);
7930    
7931      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7932      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7933    
# Line 6399  sub set_inner_html ($$$) { Line 7949  sub set_inner_html ($$$) {
7949          unless defined $p->{content_model};          unless defined $p->{content_model};
7950          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7951    
7952      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7953          ## TODO: Foreign element OK?
7954    
7955      ## Step 3      ## Step 3
7956      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6409  sub set_inner_html ($$$) { Line 7960  sub set_inner_html ($$$) {
7960      $doc->append_child ($root);      $doc->append_child ($root);
7961    
7962      ## Step 5 # MUST      ## Step 5 # MUST
7963      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7964    
7965      undef $p->{head_element};      undef $p->{head_element};
7966    
# Line 6455  sub set_inner_html ($$$) { Line 8006  sub set_inner_html ($$$) {
8006      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8007    
8008      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8009    
8010        delete $p->{parse_error}; # delete loop
8011    } else {    } else {
8012      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";
8013    }    }

Legend:
Removed from v.1.99  
changed lines
  Added in v.1.182

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24