/[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.102 by wakaba, Sun Mar 9 06:29:25 2008 UTC revision 1.190 by wakaba, Sun Sep 21 05:08:16 2008 UTC
# Line 3  use strict; Line 3  use strict;
3  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};  our $VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4  use Error qw(:try);  use Error qw(:try);
5    
6    ## NOTE: This module don't check all HTML5 parse errors; character
7    ## encoding related parse errors are expected to be handled by relevant
8    ## modules.
9    ## Parse errors for control characters that are not allowed in HTML5
10    ## documents, for surrogate code points, and for noncharacter code
11    ## points, as well as U+FFFD substitions for characters whose code points
12    ## is higher than U+10FFFF may be detected by combining the parser with
13    ## the checker implemented by Whatpm::Charset::UnicodeChecker (for its
14    ## usage example, see |t/HTML-tree.t| in the Whatpm package or the
15    ## WebHACC::Language::HTML module in the WebHACC package).
16    
17  ## ISSUE:  ## ISSUE:
18  ## var doc = implementation.createDocument (null, null, null);  ## var doc = implementation.createDocument (null, null, null);
19  ## doc.write ('');  ## doc.write ('');
20  ## alert (doc.compatMode);  ## alert (doc.compatMode);
21    
22  ## 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          ($char_stream, $e_status) = $charset->get_decode_handle
405              ($byte_stream, allow_error_reporting => 1,
406               allow_fallback => 1);
407          if ($char_stream) {
408            $self->{confident} = 1;
409            last SNIFFING;
410          } else {
411            !!!parse-error (type => 'charset:not supported',
412                            layer => 'encode',
413                            line => 1, column => 1,
414                            value => $charset_name,
415                            level => $self->{level}->{uncertain});
416          }
417      }      }
418    
419      ## Step 2      ## Step 2
420      if (defined $self->{input_encoding} and      my $byte_buffer = '';
421          $self->{input_encoding} eq $charset) {      for (1..1024) {
422          my $char = $byte_stream->getc;
423          last unless defined $char;
424          $byte_buffer .= $char;
425        } ## TODO: timeout
426    
427        ## Step 3
428        if ($byte_buffer =~ /^\xFE\xFF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433        $self->{confident} = 1;        $self->{confident} = 1;
434        return;        last SNIFFING;
435        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
436          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
437          ($char_stream, $e_status) = $charset->get_decode_handle
438              ($byte_stream, allow_error_reporting => 1,
439               allow_fallback => 1, byte_buffer => \$byte_buffer);
440          $self->{confident} = 1;
441          last SNIFFING;
442        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
443          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
444          ($char_stream, $e_status) = $charset->get_decode_handle
445              ($byte_stream, allow_error_reporting => 1,
446               allow_fallback => 1, byte_buffer => \$byte_buffer);
447          $self->{confident} = 1;
448          last SNIFFING;
449      }      }
450    
451      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
452          ':'.$charset, level => 'w');      ## TODO: <meta charset>
453    
454      ## Step 3      ## Step 5
455      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
456    
457      ## Step 4      ## Step 6
458      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
459        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
460            ($byte_buffer);
461        if (defined $charset_name) {
462          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
463    
464          ## ISSUE: Unsupported encoding is not ignored according to the spec.
465          require Whatpm::Charset::DecodeHandle;
466          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
467              ($byte_stream);
468          ($char_stream, $e_status) = $charset->get_decode_handle
469              ($buffer, allow_error_reporting => 1,
470               allow_fallback => 1, byte_buffer => \$byte_buffer);
471          if ($char_stream) {
472            $buffer->{buffer} = $byte_buffer;
473            !!!parse-error (type => 'sniffing:chardet',
474                            text => $charset_name,
475                            level => $self->{level}->{info},
476                            layer => 'encode',
477                            line => 1, column => 1);
478            $self->{confident} = 0;
479            last SNIFFING;
480          }
481        }
482    
483        ## Step 7: default
484        ## TODO: Make this configurable.
485        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
486            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
487            ## detectable in the step 6.
488        require Whatpm::Charset::DecodeHandle;
489        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
490            ($byte_stream);
491        ($char_stream, $e_status)
492            = $charset->get_decode_handle ($buffer,
493                                           allow_error_reporting => 1,
494                                           allow_fallback => 1,
495                                           byte_buffer => \$byte_buffer);
496        $buffer->{buffer} = $byte_buffer;
497        !!!parse-error (type => 'sniffing:default',
498                        text => 'windows-1252',
499                        level => $self->{level}->{info},
500                        line => 1, column => 1,
501                        layer => 'encode');
502        $self->{confident} = 0;
503      } # SNIFFING
504    
505      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
506        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
507        !!!parse-error (type => 'chardecode:fallback',
508                        #text => $self->{input_encoding},
509                        level => $self->{level}->{uncertain},
510                        line => 1, column => 1,
511                        layer => 'encode');
512      } elsif (not ($e_status &
513                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
514        $self->{input_encoding} = $charset->get_iana_name;
515        !!!parse-error (type => 'chardecode:no error',
516                        text => $self->{input_encoding},
517                        level => $self->{level}->{uncertain},
518                        line => 1, column => 1,
519                        layer => 'encode');
520      } else {
521        $self->{input_encoding} = $charset->get_iana_name;
522      }
523    
524      $self->{change_encoding} = sub {
525        my $self = shift;
526        $charset_name = shift;
527        my $token = shift;
528    
529        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
530        ($char_stream, $e_status) = $charset->get_decode_handle
531            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
532             byte_buffer => \ $buffer->{buffer});
533        
534        if ($char_stream) { # if supported
535          ## "Change the encoding" algorithm:
536    
537          ## Step 1    
538          if ($charset->{category} &
539              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
540            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
541            ($char_stream, $e_status) = $charset->get_decode_handle
542                ($byte_stream,
543                 byte_buffer => \ $buffer->{buffer});
544          }
545          $charset_name = $charset->get_iana_name;
546          
547          ## Step 2
548          if (defined $self->{input_encoding} and
549              $self->{input_encoding} eq $charset_name) {
550            !!!parse-error (type => 'charset label:matching',
551                            text => $charset_name,
552                            level => $self->{level}->{info});
553            $self->{confident} = 1;
554            return;
555          }
556    
557          !!!parse-error (type => 'charset label detected',
558                          text => $self->{input_encoding},
559                          value => $charset_name,
560                          level => $self->{level}->{warn},
561                          token => $token);
562          
563          ## Step 3
564          # if (can) {
565            ## change the encoding on the fly.
566            #$self->{confident} = 1;
567            #return;
568          # }
569          
570          ## Step 4
571          throw Whatpm::HTML::RestartParser ();
572        }
573    }; # $self->{change_encoding}    }; # $self->{change_encoding}
574    
575    my @args = @_; shift @args; # $s    my $char_onerror = sub {
576        my (undef, $type, %opt) = @_;
577        !!!parse-error (layer => 'encode',
578                        line => $self->{line}, column => $self->{column} + 1,
579                        %opt, type => $type);
580        if ($opt{octets}) {
581          ${$opt{octets}} = "\x{FFFD}"; # relacement character
582        }
583      };
584    
585      my $wrapped_char_stream = $get_wrapper->($char_stream);
586      $wrapped_char_stream->onerror ($char_onerror);
587    
588      my @args = ($_[1], $_[2]); # $doc, $onerror - $get_wrapper = undef;
589    my $return;    my $return;
590    try {    try {
591      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
592    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
593      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
594      $s = \ (Encode::decode ($charset, $$bytes_s));      
595      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
596          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
597          !!!parse-error (type => 'chardecode:fallback',
598                          level => $self->{level}->{uncertain},
599                          #text => $self->{input_encoding},
600                          line => 1, column => 1,
601                          layer => 'encode');
602        } elsif (not ($e_status &
603                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
604          $self->{input_encoding} = $charset->get_iana_name;
605          !!!parse-error (type => 'chardecode:no error',
606                          text => $self->{input_encoding},
607                          level => $self->{level}->{uncertain},
608                          line => 1, column => 1,
609                          layer => 'encode');
610        } else {
611          $self->{input_encoding} = $charset->get_iana_name;
612        }
613      $self->{confident} = 1;      $self->{confident} = 1;
614      $return = $self->parse_char_string ($s, @args);  
615        $wrapped_char_stream = $get_wrapper->($char_stream);
616        $wrapped_char_stream->onerror ($char_onerror);
617    
618        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
619    };    };
620    return $return;    return $return;
621  } # parse_byte_string  } # parse_byte_stream
622    
623  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
624  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 629  sub parse_byte_string ($$$$;$) {
629  ## 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
630  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
631    
632  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
633      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
634      my $self = shift;
635      my $s = ref $_[0] ? $_[0] : \($_[0]);
636      require Whatpm::Charset::DecodeHandle;
637      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
638      return $self->parse_char_stream ($input, @_[1..$#_]);
639    } # parse_char_string
640    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
641    
642  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$$) {
643    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
644    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
645    $self->{document} = $_[1];    $self->{document} = $_[1];
646    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
647    
# Line 175  sub parse_string ($$$;$) { Line 650  sub parse_string ($$$;$) {
650    $self->{confident} = 1 unless exists $self->{confident};    $self->{confident} = 1 unless exists $self->{confident};
651    $self->{document}->input_encoding ($self->{input_encoding})    $self->{document}->input_encoding ($self->{input_encoding})
652        if defined $self->{input_encoding};        if defined $self->{input_encoding};
653    ## TODO: |{input_encoding}| is needless?
654    
655    my $i = 0;    $self->{line_prev} = $self->{line} = 1;
656    my $line = 1;    $self->{column_prev} = -1;
657    my $column = 0;    $self->{column} = 0;
658    $self->{set_next_char} = sub {    $self->{set_nc} = sub {
659      my $self = shift;      my $self = shift;
660    
661      pop @{$self->{prev_char}};      my $char = '';
662      unshift @{$self->{prev_char}}, $self->{next_char};      if (defined $self->{next_nc}) {
663          $char = $self->{next_nc};
664          delete $self->{next_nc};
665          $self->{nc} = ord $char;
666        } else {
667          $self->{char_buffer} = '';
668          $self->{char_buffer_pos} = 0;
669    
670      $self->{next_char} = -1 and return if $i >= length $$s;        my $count = $input->manakai_read_until
671      $self->{next_char} = ord substr $$s, $i++, 1;           ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/, $self->{char_buffer_pos});
672      $column++;        if ($count) {
673            $self->{line_prev} = $self->{line};
674            $self->{column_prev} = $self->{column};
675            $self->{column}++;
676            $self->{nc}
677                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
678            return;
679          }
680    
681          if ($input->read ($char, 1)) {
682            $self->{nc} = ord $char;
683          } else {
684            $self->{nc} = -1;
685            return;
686          }
687        }
688    
689        ($self->{line_prev}, $self->{column_prev})
690            = ($self->{line}, $self->{column});
691        $self->{column}++;
692            
693      if ($self->{next_char} == 0x000A) { # LF      if ($self->{nc} == 0x000A) { # LF
694        $line++;        !!!cp ('j1');
695        $column = 0;        $self->{line}++;
696      } elsif ($self->{next_char} == 0x000D) { # CR        $self->{column} = 0;
697        $i++ if substr ($$s, $i, 1) eq "\x0A";      } elsif ($self->{nc} == 0x000D) { # CR
698        $self->{next_char} = 0x000A; # LF # MUST        !!!cp ('j2');
699        $line++;  ## TODO: support for abort/streaming
700        $column = 0;        my $next = '';
701      } elsif ($self->{next_char} > 0x10FFFF) {        if ($input->read ($next, 1) and $next ne "\x0A") {
702        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_nc} = $next;
703      } elsif ($self->{next_char} == 0x0000) { # NULL        }
704          $self->{nc} = 0x000A; # LF # MUST
705          $self->{line}++;
706          $self->{column} = 0;
707        } elsif ($self->{nc} == 0x0000) { # NULL
708          !!!cp ('j4');
709        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
710        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
711      }      }
712    };    };
713    $self->{prev_char} = [-1, -1, -1];  
714    $self->{next_char} = -1;    $self->{read_until} = sub {
715        #my ($scalar, $specials_range, $offset) = @_;
716        return 0 if defined $self->{next_nc};
717    
718        my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
719        my $offset = $_[2] || 0;
720    
721        if ($self->{char_buffer_pos} < length $self->{char_buffer}) {
722          pos ($self->{char_buffer}) = $self->{char_buffer_pos};
723          if ($self->{char_buffer} =~ /\G(?>$pattern)+/) {
724            substr ($_[0], $offset)
725                = substr ($self->{char_buffer}, $-[0], $+[0] - $-[0]);
726            my $count = $+[0] - $-[0];
727            if ($count) {
728              $self->{column} += $count;
729              $self->{char_buffer_pos} += $count;
730              $self->{line_prev} = $self->{line};
731              $self->{column_prev} = $self->{column} - 1;
732              $self->{nc} = -1;
733            }
734            return $count;
735          } else {
736            return 0;
737          }
738        } else {
739          my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
740          if ($count) {
741            $self->{column} += $count;
742            $self->{line_prev} = $self->{line};
743            $self->{column_prev} = $self->{column} - 1;
744            $self->{nc} = -1;
745          }
746          return $count;
747        }
748      }; # $self->{read_until}
749    
750    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
751      my (%opt) = @_;      my (%opt) = @_;
752      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
753        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
754        warn "Parse error ($opt{type}) at line $line column $column\n";
755    };    };
756    $self->{parse_error} = sub {    $self->{parse_error} = sub {
757      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
758    };    };
759    
760      my $char_onerror = sub {
761        my (undef, $type, %opt) = @_;
762        !!!parse-error (layer => 'encode',
763                        line => $self->{line}, column => $self->{column} + 1,
764                        %opt, type => $type);
765      }; # $char_onerror
766    
767      if ($_[3]) {
768        $input = $_[3]->($input);
769        $input->onerror ($char_onerror);
770      } else {
771        $input->onerror ($char_onerror) unless defined $input->onerror;
772      }
773    
774    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
775    $self->_initialize_tree_constructor;    $self->_initialize_tree_constructor;
776    $self->_construct_tree;    $self->_construct_tree;
777    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
778    
779      delete $self->{parse_error}; # remove loop
780    
781    return $self->{document};    return $self->{document};
782  } # parse_string  } # parse_char_stream
783    
784  sub new ($) {  sub new ($) {
785    my $class = shift;    my $class = shift;
786    my $self = bless {}, $class;    my $self = bless {
787    $self->{set_next_char} = sub {      level => {must => 'm',
788      $self->{next_char} = -1;                should => 's',
789                  warn => 'w',
790                  info => 'i',
791                  uncertain => 'u'},
792      }, $class;
793      $self->{set_nc} = sub {
794        $self->{nc} = -1;
795    };    };
796    $self->{parse_error} = sub {    $self->{parse_error} = sub {
797      #      #
# Line 254  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 818  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
818  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
819    
820  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
821  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
822  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
823  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
824  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 265  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 829  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
829  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
830  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
831  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
832  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
833  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
834  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
835  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 851  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
851  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
852  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
853  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
854    sub SELF_CLOSING_START_TAG_STATE () { 34 }
855    sub CDATA_SECTION_STATE () { 35 }
856    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
857    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
858    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
859    sub CDATA_RCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
860    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
861    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
862    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
863    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
864    ## NOTE: "Entity data state", "entity in attribute value state", and
865    ## "consume a character reference" algorithm are jointly implemented
866    ## using the following six states:
867    sub ENTITY_STATE () { 44 }
868    sub ENTITY_HASH_STATE () { 45 }
869    sub NCR_NUM_STATE () { 46 }
870    sub HEXREF_X_STATE () { 47 }
871    sub HEXREF_HEX_STATE () { 48 }
872    sub ENTITY_NAME_STATE () { 49 }
873    sub PCDATA_STATE () { 50 } # "data state" in the spec
874    
875  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
876  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 304  sub ROW_IMS ()        { 0b10000000 } Line 888  sub ROW_IMS ()        { 0b10000000 }
888  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
889  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
890  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
891    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
892        ## NOTE: "in foreign content" insertion mode is special; it is combined
893        ## with the secondary insertion mode.  In this parser, they are stored
894        ## together in the bit-or'ed form.
895    
896  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
897    
# Line 335  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 923  sub IN_COLUMN_GROUP_IM () { 0b10 }
923  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
924    my $self = shift;    my $self = shift;
925    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
926      #$self->{s_kwd}; # state keyword - initialized when used
927      #$self->{entity__value}; # initialized when used
928      #$self->{entity__match}; # initialized when used
929    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
930    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{ct}; # current token
931    undef $self->{current_attribute};    undef $self->{ca}; # current attribute
932    undef $self->{last_emitted_start_tag_name};    undef $self->{last_stag_name}; # last emitted start tag name
933    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
934    $self->{char} = [];    delete $self->{self_closing};
935    # $self->{next_char}    $self->{char_buffer} = '';
936      $self->{char_buffer_pos} = 0;
937      $self->{nc} = -1; # next input character
938      #$self->{next_nc}
939    !!!next-input-character;    !!!next-input-character;
940    $self->{token} = [];    $self->{token} = [];
941    # $self->{escape}    # $self->{escape}
# Line 352  sub _initialize_tokenizer ($) { Line 946  sub _initialize_tokenizer ($) {
946  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN  ##       CHARACTER_TOKEN, or END_OF_FILE_TOKEN
947  ##   ->{name} (DOCTYPE_TOKEN)  ##   ->{name} (DOCTYPE_TOKEN)
948  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{tag_name} (START_TAG_TOKEN, END_TAG_TOKEN)
949  ##   ->{public_identifier} (DOCTYPE_TOKEN)  ##   ->{pubid} (DOCTYPE_TOKEN)
950  ##   ->{system_identifier} (DOCTYPE_TOKEN)  ##   ->{sysid} (DOCTYPE_TOKEN)
951  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag  ##   ->{quirks} == 1 or 0 (DOCTYPE_TOKEN): "force-quirks" flag
952  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)  ##   ->{attributes} isa HASH (START_TAG_TOKEN, END_TAG_TOKEN)
953  ##        ->{name}  ##        ->{name}
954  ##        ->{value}  ##        ->{value}
955  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
956  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
957    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
958    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
959    ##     while the token is pushed back to the stack.
960    
961  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
962    
# Line 369  sub _initialize_tokenizer ($) { Line 966  sub _initialize_tokenizer ($) {
966  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
967  ## and removed from the list.  ## and removed from the list.
968    
969  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
970  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
971  ## contains some requirements that are not detected by the  
972  ## parsing algorithm:  my $is_space = {
973  ## - Some requirements on character encoding declarations. ## TODO    0x0009 => 1, # CHARACTER TABULATION (HT)
974  ## - "Elements MUST NOT contain content that their content model disallows."    0x000A => 1, # LINE FEED (LF)
975  ##   ... Some are parse error, some are not (will be reported by c.c.).    #0x000B => 0, # LINE TABULATION (VT)
976  ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO    0x000C => 1, # FORM FEED (FF)
977  ## - Text (in elements, attributes, and comments) SHOULD NOT contain    #0x000D => 1, # CARRIAGE RETURN (CR)
978  ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)    0x0020 => 1, # SPACE (SP)
979    };
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
980    
981  sub _get_next_token ($) {  sub _get_next_token ($) {
982    my $self = shift;    my $self = shift;
983    
984      if ($self->{self_closing}) {
985        !!!parse-error (type => 'nestc', token => $self->{ct});
986        ## NOTE: The |self_closing| flag is only set by start tag token.
987        ## In addition, when a start tag token is emitted, it is always set to
988        ## |ct|.
989        delete $self->{self_closing};
990      }
991    
992    if (@{$self->{token}}) {    if (@{$self->{token}}) {
993        $self->{self_closing} = $self->{token}->[0]->{self_closing};
994      return shift @{$self->{token}};      return shift @{$self->{token}};
995    }    }
996    
997    A: {    A: {
998      if ($self->{state} == DATA_STATE) {      if ($self->{state} == PCDATA_STATE) {
999        if ($self->{next_char} == 0x0026) { # &        ## NOTE: Same as |DATA_STATE|, but only for |PCDATA| content model.
1000    
1001          if ($self->{nc} == 0x0026) { # &
1002            !!!cp (0.1);
1003            ## NOTE: In the spec, the tokenizer is switched to the
1004            ## "entity data state".  In this implementation, the tokenizer
1005            ## is switched to the |ENTITY_STATE|, which is an implementation
1006            ## of the "consume a character reference" algorithm.
1007            $self->{entity_add} = -1;
1008            $self->{prev_state} = DATA_STATE;
1009            $self->{state} = ENTITY_STATE;
1010            !!!next-input-character;
1011            redo A;
1012          } elsif ($self->{nc} == 0x003C) { # <
1013            !!!cp (0.2);
1014            $self->{state} = TAG_OPEN_STATE;
1015            !!!next-input-character;
1016            redo A;
1017          } elsif ($self->{nc} == -1) {
1018            !!!cp (0.3);
1019            !!!emit ({type => END_OF_FILE_TOKEN,
1020                      line => $self->{line}, column => $self->{column}});
1021            last A; ## TODO: ok?
1022          } else {
1023            !!!cp (0.4);
1024            #
1025          }
1026    
1027          # Anything else
1028          my $token = {type => CHARACTER_TOKEN,
1029                       data => chr $self->{nc},
1030                       line => $self->{line}, column => $self->{column},
1031                      };
1032          $self->{read_until}->($token->{data}, q[<&], length $token->{data});
1033    
1034          ## Stay in the state.
1035          !!!next-input-character;
1036          !!!emit ($token);
1037          redo A;
1038        } elsif ($self->{state} == DATA_STATE) {
1039          $self->{s_kwd} = '' unless defined $self->{s_kwd};
1040          if ($self->{nc} == 0x0026) { # &
1041            $self->{s_kwd} = '';
1042          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
1043              not $self->{escape}) {              not $self->{escape}) {
1044            !!!cp (1);            !!!cp (1);
1045            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
1046              ## "entity data state".  In this implementation, the tokenizer
1047              ## is switched to the |ENTITY_STATE|, which is an implementation
1048              ## of the "consume a character reference" algorithm.
1049              $self->{entity_add} = -1;
1050              $self->{prev_state} = DATA_STATE;
1051              $self->{state} = ENTITY_STATE;
1052            !!!next-input-character;            !!!next-input-character;
1053            redo A;            redo A;
1054          } else {          } else {
1055            !!!cp (2);            !!!cp (2);
1056            #            #
1057          }          }
1058        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
1059          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA          if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1060            unless ($self->{escape}) {            $self->{s_kwd} .= '-';
1061              if ($self->{prev_char}->[0] == 0x002D and # -            
1062                  $self->{prev_char}->[1] == 0x0021 and # !            if ($self->{s_kwd} eq '<!--') {
1063                  $self->{prev_char}->[2] == 0x003C) { # <              !!!cp (3);
1064                !!!cp (3);              $self->{escape} = 1; # unless $self->{escape};
1065                $self->{escape} = 1;              $self->{s_kwd} = '--';
1066              } else {              #
1067                !!!cp (4);            } elsif ($self->{s_kwd} eq '---') {
1068              }              !!!cp (4);
1069                $self->{s_kwd} = '--';
1070                #
1071            } else {            } else {
1072              !!!cp (5);              !!!cp (5);
1073                #
1074            }            }
1075          }          }
1076                    
1077          #          #
1078        } elsif ($self->{next_char} == 0x003C) { # <        } elsif ($self->{nc} == 0x0021) { # !
1079            if (length $self->{s_kwd}) {
1080              !!!cp (5.1);
1081              $self->{s_kwd} .= '!';
1082              #
1083            } else {
1084              !!!cp (5.2);
1085              #$self->{s_kwd} = '';
1086              #
1087            }
1088            #
1089          } elsif ($self->{nc} == 0x003C) { # <
1090          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA          if ($self->{content_model} & CM_FULL_MARKUP or # PCDATA
1091              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA              (($self->{content_model} & CM_LIMITED_MARKUP) and # CDATA | RCDATA
1092               not $self->{escape})) {               not $self->{escape})) {
# Line 430  sub _get_next_token ($) { Line 1096  sub _get_next_token ($) {
1096            redo A;            redo A;
1097          } else {          } else {
1098            !!!cp (7);            !!!cp (7);
1099              $self->{s_kwd} = '';
1100            #            #
1101          }          }
1102        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1103          if ($self->{escape} and          if ($self->{escape} and
1104              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA              ($self->{content_model} & CM_LIMITED_MARKUP)) { # RCDATA | CDATA
1105            if ($self->{prev_char}->[0] == 0x002D and # -            if ($self->{s_kwd} eq '--') {
               $self->{prev_char}->[1] == 0x002D) { # -  
1106              !!!cp (8);              !!!cp (8);
1107              delete $self->{escape};              delete $self->{escape};
1108            } else {            } else {
# Line 446  sub _get_next_token ($) { Line 1112  sub _get_next_token ($) {
1112            !!!cp (10);            !!!cp (10);
1113          }          }
1114                    
1115            $self->{s_kwd} = '';
1116          #          #
1117        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1118          !!!cp (11);          !!!cp (11);
1119          !!!emit ({type => END_OF_FILE_TOKEN});          $self->{s_kwd} = '';
1120            !!!emit ({type => END_OF_FILE_TOKEN,
1121                      line => $self->{line}, column => $self->{column}});
1122          last A; ## TODO: ok?          last A; ## TODO: ok?
1123        } else {        } else {
1124          !!!cp (12);          !!!cp (12);
1125            $self->{s_kwd} = '';
1126            #
1127        }        }
1128    
1129        # Anything else        # Anything else
1130        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1131                     data => chr $self->{next_char}};                     data => chr $self->{nc},
1132        ## Stay in the data state                     line => $self->{line}, column => $self->{column},
1133        !!!next-input-character;                    };
1134          if ($self->{read_until}->($token->{data}, q[-!<>&],
1135        !!!emit ($token);                                  length $token->{data})) {
1136            $self->{s_kwd} = '';
1137        redo A;        }
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
1138    
1139        unless (defined $token) {        ## Stay in the data state.
1140          if ($self->{content_model} == PCDATA_CONTENT_MODEL) {
1141          !!!cp (13);          !!!cp (13);
1142          !!!emit ({type => CHARACTER_TOKEN, data => '&'});          $self->{state} = PCDATA_STATE;
1143        } else {        } else {
1144          !!!cp (14);          !!!cp (14);
1145          !!!emit ($token);          ## Stay in the state.
1146        }        }
1147          !!!next-input-character;
1148          !!!emit ($token);
1149        redo A;        redo A;
1150      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1151        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1152          if ($self->{next_char} == 0x002F) { # /          if ($self->{nc} == 0x002F) { # /
1153            !!!cp (15);            !!!cp (15);
1154            !!!next-input-character;            !!!next-input-character;
1155            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1156            redo A;            redo A;
1157            } elsif ($self->{nc} == 0x0021) { # !
1158              !!!cp (15.1);
1159              $self->{s_kwd} = '<' unless $self->{escape};
1160              #
1161          } else {          } else {
1162            !!!cp (16);            !!!cp (16);
1163            ## reconsume            #
           $self->{state} = DATA_STATE;  
   
           !!!emit ({type => CHARACTER_TOKEN, data => '<'});  
   
           redo A;  
1164          }          }
1165    
1166            ## reconsume
1167            $self->{state} = DATA_STATE;
1168            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1169                      line => $self->{line_prev},
1170                      column => $self->{column_prev},
1171                     });
1172            redo A;
1173        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA        } elsif ($self->{content_model} & CM_FULL_MARKUP) { # PCDATA
1174          if ($self->{next_char} == 0x0021) { # !          if ($self->{nc} == 0x0021) { # !
1175            !!!cp (17);            !!!cp (17);
1176            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;            $self->{state} = MARKUP_DECLARATION_OPEN_STATE;
1177            !!!next-input-character;            !!!next-input-character;
1178            redo A;            redo A;
1179          } elsif ($self->{next_char} == 0x002F) { # /          } elsif ($self->{nc} == 0x002F) { # /
1180            !!!cp (18);            !!!cp (18);
1181            $self->{state} = CLOSE_TAG_OPEN_STATE;            $self->{state} = CLOSE_TAG_OPEN_STATE;
1182            !!!next-input-character;            !!!next-input-character;
1183            redo A;            redo A;
1184          } elsif (0x0041 <= $self->{next_char} and          } elsif (0x0041 <= $self->{nc} and
1185                   $self->{next_char} <= 0x005A) { # A..Z                   $self->{nc} <= 0x005A) { # A..Z
1186            !!!cp (19);            !!!cp (19);
1187            $self->{current_token}            $self->{ct}
1188              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1189                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{nc} + 0x0020),
1190                   line => $self->{line_prev},
1191                   column => $self->{column_prev}};
1192            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1193            !!!next-input-character;            !!!next-input-character;
1194            redo A;            redo A;
1195          } elsif (0x0061 <= $self->{next_char} and          } elsif (0x0061 <= $self->{nc} and
1196                   $self->{next_char} <= 0x007A) { # a..z                   $self->{nc} <= 0x007A) { # a..z
1197            !!!cp (20);            !!!cp (20);
1198            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{ct} = {type => START_TAG_TOKEN,
1199                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{nc}),
1200                                        line => $self->{line_prev},
1201                                        column => $self->{column_prev}};
1202            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1203            !!!next-input-character;            !!!next-input-character;
1204            redo A;            redo A;
1205          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{nc} == 0x003E) { # >
1206            !!!cp (21);            !!!cp (21);
1207            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1208                              line => $self->{line_prev},
1209                              column => $self->{column_prev});
1210            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1211            !!!next-input-character;            !!!next-input-character;
1212    
1213            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1214                        line => $self->{line_prev},
1215                        column => $self->{column_prev},
1216                       });
1217    
1218            redo A;            redo A;
1219          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{nc} == 0x003F) { # ?
1220            !!!cp (22);            !!!cp (22);
1221            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1222                              line => $self->{line_prev},
1223                              column => $self->{column_prev});
1224            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1225            ## $self->{next_char} is intentionally left as is            $self->{ct} = {type => COMMENT_TOKEN, data => '',
1226                                        line => $self->{line_prev},
1227                                        column => $self->{column_prev},
1228                                       };
1229              ## $self->{nc} is intentionally left as is
1230            redo A;            redo A;
1231          } else {          } else {
1232            !!!cp (23);            !!!cp (23);
1233            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1234                              line => $self->{line_prev},
1235                              column => $self->{column_prev});
1236            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1237            ## reconsume            ## reconsume
1238    
1239            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1240                        line => $self->{line_prev},
1241                        column => $self->{column_prev},
1242                       });
1243    
1244            redo A;            redo A;
1245          }          }
# Line 553  sub _get_next_token ($) { Line 1247  sub _get_next_token ($) {
1247          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1248        }        }
1249      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1250        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        ## NOTE: The "close tag open state" in the spec is implemented as
1251          if (defined $self->{last_emitted_start_tag_name}) {        ## |CLOSE_TAG_OPEN_STATE| and |CDATA_RCDATA_CLOSE_TAG_STATE|.
           ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>  
           my @next_char;  
           TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {  
             push @next_char, $self->{next_char};  
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_char} == $c or $self->{next_char} == $C) {  
               !!!cp (24);  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               !!!cp (25);  
               $self->{next_char} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
1252    
1253                !!!emit ({type => CHARACTER_TOKEN, data => '</'});        my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1254            if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1255                redo A;          if (defined $self->{last_stag_name}) {
1256              }            $self->{state} = CDATA_RCDATA_CLOSE_TAG_STATE;
1257            }            $self->{s_kwd} = '';
1258            push @next_char, $self->{next_char};            ## Reconsume.
1259                    redo A;
           unless ($self->{next_char} == 0x0009 or # HT  
                   $self->{next_char} == 0x000A or # LF  
                   $self->{next_char} == 0x000B or # VT  
                   $self->{next_char} == 0x000C or # FF  
                   $self->{next_char} == 0x0020 or # SP  
                   $self->{next_char} == 0x003E or # >  
                   $self->{next_char} == 0x002F or # /  
                   $self->{next_char} == -1) {  
             !!!cp (26);  
             $self->{next_char} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
             redo A;  
           } else {  
             !!!cp (27);  
             $self->{next_char} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1260          } else {          } else {
1261            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1262              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1263            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1264            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1265            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1266              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1267                        line => $l, column => $c,
1268                       });
1269            redo A;            redo A;
1270          }          }
1271        }        }
1272          
1273        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{nc} and
1274            $self->{next_char} <= 0x005A) { # A..Z            $self->{nc} <= 0x005A) { # A..Z
1275          !!!cp (29);          !!!cp (29);
1276          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{ct}
1277                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1278                   tag_name => chr ($self->{nc} + 0x0020),
1279                   line => $l, column => $c};
1280          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1281          !!!next-input-character;          !!!next-input-character;
1282          redo A;          redo A;
1283        } elsif (0x0061 <= $self->{next_char} and        } elsif (0x0061 <= $self->{nc} and
1284                 $self->{next_char} <= 0x007A) { # a..z                 $self->{nc} <= 0x007A) { # a..z
1285          !!!cp (30);          !!!cp (30);
1286          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{ct} = {type => END_TAG_TOKEN,
1287                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{nc}),
1288                                      line => $l, column => $c};
1289          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1290          !!!next-input-character;          !!!next-input-character;
1291          redo A;          redo A;
1292        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1293          !!!cp (31);          !!!cp (31);
1294          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1295                            line => $self->{line_prev}, ## "<" in "</>"
1296                            column => $self->{column_prev} - 1);
1297          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1298          !!!next-input-character;          !!!next-input-character;
1299          redo A;          redo A;
1300        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1301          !!!cp (32);          !!!cp (32);
1302          !!!parse-error (type => 'bare etago');          !!!parse-error (type => 'bare etago');
1303          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1304          # reconsume          # reconsume
1305    
1306          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1307                      line => $l, column => $c,
1308                     });
1309    
1310          redo A;          redo A;
1311        } else {        } else {
1312          !!!cp (33);          !!!cp (33);
1313          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1314          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1315          ## $self->{next_char} is intentionally left as is          $self->{ct} = {type => COMMENT_TOKEN, data => '',
1316          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1317                                      column => $self->{column_prev} - 1,
1318                                     };
1319            ## NOTE: $self->{nc} is intentionally left as is.
1320            ## Although the "anything else" case of the spec not explicitly
1321            ## states that the next input character is to be reconsumed,
1322            ## it will be included to the |data| of the comment token
1323            ## generated from the bogus end tag, as defined in the
1324            ## "bogus comment state" entry.
1325            redo A;
1326          }
1327        } elsif ($self->{state} == CDATA_RCDATA_CLOSE_TAG_STATE) {
1328          my $ch = substr $self->{last_stag_name}, length $self->{s_kwd}, 1;
1329          if (length $ch) {
1330            my $CH = $ch;
1331            $ch =~ tr/a-z/A-Z/;
1332            my $nch = chr $self->{nc};
1333            if ($nch eq $ch or $nch eq $CH) {
1334              !!!cp (24);
1335              ## Stay in the state.
1336              $self->{s_kwd} .= $nch;
1337              !!!next-input-character;
1338              redo A;
1339            } else {
1340              !!!cp (25);
1341              $self->{state} = DATA_STATE;
1342              ## Reconsume.
1343              !!!emit ({type => CHARACTER_TOKEN,
1344                        data => '</' . $self->{s_kwd},
1345                        line => $self->{line_prev},
1346                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1347                       });
1348              redo A;
1349            }
1350          } else { # after "<{tag-name}"
1351            unless ($is_space->{$self->{nc}} or
1352                    {
1353                     0x003E => 1, # >
1354                     0x002F => 1, # /
1355                     -1 => 1, # EOF
1356                    }->{$self->{nc}}) {
1357              !!!cp (26);
1358              ## Reconsume.
1359              $self->{state} = DATA_STATE;
1360              !!!emit ({type => CHARACTER_TOKEN,
1361                        data => '</' . $self->{s_kwd},
1362                        line => $self->{line_prev},
1363                        column => $self->{column_prev} - 1 - length $self->{s_kwd},
1364                       });
1365              redo A;
1366            } else {
1367              !!!cp (27);
1368              $self->{ct}
1369                  = {type => END_TAG_TOKEN,
1370                     tag_name => $self->{last_stag_name},
1371                     line => $self->{line_prev},
1372                     column => $self->{column_prev} - 1 - length $self->{s_kwd}};
1373              $self->{state} = TAG_NAME_STATE;
1374              ## Reconsume.
1375              redo A;
1376            }
1377        }        }
1378      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1379        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1380          !!!cp (34);          !!!cp (34);
1381          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1382          !!!next-input-character;          !!!next-input-character;
1383          redo A;          redo A;
1384        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1385          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1386            !!!cp (35);            !!!cp (35);
1387            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1388                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1389            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1390            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1391            #  ## NOTE: This should never be reached.            #  ## NOTE: This should never be reached.
1392            #  !!! cp (36);            #  !!! cp (36);
1393            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 672  sub _get_next_token ($) { Line 1395  sub _get_next_token ($) {
1395              !!!cp (37);              !!!cp (37);
1396            #}            #}
1397          } else {          } else {
1398            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1399          }          }
1400          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1401          !!!next-input-character;          !!!next-input-character;
1402    
1403          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1404    
1405          redo A;          redo A;
1406        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1407                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1408          !!!cp (38);          !!!cp (38);
1409          $self->{current_token}->{tag_name} .= chr ($self->{next_char} + 0x0020);          $self->{ct}->{tag_name} .= chr ($self->{nc} + 0x0020);
1410            # start tag or end tag            # start tag or end tag
1411          ## Stay in this state          ## Stay in this state
1412          !!!next-input-character;          !!!next-input-character;
1413          redo A;          redo A;
1414        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1415          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1416          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1417            !!!cp (39);            !!!cp (39);
1418            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1419                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1420            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1421            #if ($self->{current_token}->{attributes}) {            #if ($self->{ct}->{attributes}) {
1422            #  ## NOTE: This state should never be reached.            #  ## NOTE: This state should never be reached.
1423            #  !!! cp (40);            #  !!! cp (40);
1424            #  !!! parse-error (type => 'end tag attribute');            #  !!! parse-error (type => 'end tag attribute');
# Line 705  sub _get_next_token ($) { Line 1426  sub _get_next_token ($) {
1426              !!!cp (41);              !!!cp (41);
1427            #}            #}
1428          } else {          } else {
1429            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1430          }          }
1431          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1432          # reconsume          # reconsume
1433    
1434          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1435    
1436          redo A;          redo A;
1437        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1438            !!!cp (42);
1439            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1440          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_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  
1441          redo A;          redo A;
1442        } else {        } else {
1443          !!!cp (44);          !!!cp (44);
1444          $self->{current_token}->{tag_name} .= chr $self->{next_char};          $self->{ct}->{tag_name} .= chr $self->{nc};
1445            # start tag or end tag            # start tag or end tag
1446          ## Stay in the state          ## Stay in the state
1447          !!!next-input-character;          !!!next-input-character;
1448          redo A;          redo A;
1449        }        }
1450      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_NAME_STATE) {
1451        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1452          !!!cp (45);          !!!cp (45);
1453          ## Stay in the state          ## Stay in the state
1454          !!!next-input-character;          !!!next-input-character;
1455          redo A;          redo A;
1456        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1457          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1458            !!!cp (46);            !!!cp (46);
1459            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1460                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1461            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1462            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1463              !!!cp (47);              !!!cp (47);
1464              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1465            } else {            } else {
1466              !!!cp (48);              !!!cp (48);
1467            }            }
1468          } else {          } else {
1469            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1470          }          }
1471          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1472          !!!next-input-character;          !!!next-input-character;
1473    
1474          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1475    
1476          redo A;          redo A;
1477        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1478                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1479          !!!cp (49);          !!!cp (49);
1480          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{ca}
1481                                value => ''};              = {name => chr ($self->{nc} + 0x0020),
1482                   value => '',
1483                   line => $self->{line}, column => $self->{column}};
1484          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1485          !!!next-input-character;          !!!next-input-character;
1486          redo A;          redo A;
1487        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1488            !!!cp (50);
1489            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1490          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_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  
1491          redo A;          redo A;
1492        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1494          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1495            !!!cp (52);            !!!cp (52);
1496            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1497                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1498            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1499            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1500              !!!cp (53);              !!!cp (53);
1501              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1502            } else {            } else {
1503              !!!cp (54);              !!!cp (54);
1504            }            }
1505          } else {          } else {
1506            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1507          }          }
1508          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1509          # reconsume          # reconsume
1510    
1511          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1512    
1513          redo A;          redo A;
1514        } else {        } else {
# Line 821  sub _get_next_token ($) { Line 1516  sub _get_next_token ($) {
1516               0x0022 => 1, # "               0x0022 => 1, # "
1517               0x0027 => 1, # '               0x0027 => 1, # '
1518               0x003D => 1, # =               0x003D => 1, # =
1519              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1520            !!!cp (55);            !!!cp (55);
1521            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1522          } else {          } else {
1523            !!!cp (56);            !!!cp (56);
1524          }          }
1525          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{ca}
1526                                value => ''};              = {name => chr ($self->{nc}),
1527                   value => '',
1528                   line => $self->{line}, column => $self->{column}};
1529          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1530          !!!next-input-character;          !!!next-input-character;
1531          redo A;          redo A;
1532        }        }
1533      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == ATTRIBUTE_NAME_STATE) {
1534        my $before_leave = sub {        my $before_leave = sub {
1535          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{ct}->{attributes} # start tag or end tag
1536              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{ca}->{name}}) { # MUST
1537            !!!cp (57);            !!!cp (57);
1538            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute', text => $self->{ca}->{name}, line => $self->{ca}->{line}, column => $self->{ca}->{column});
1539            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{ca} # MUST
1540          } else {          } else {
1541            !!!cp (58);            !!!cp (58);
1542            $self->{current_token}->{attributes}->{$self->{current_attribute}->{name}}            $self->{ct}->{attributes}->{$self->{ca}->{name}}
1543              = $self->{current_attribute};              = $self->{ca};
1544          }          }
1545        }; # $before_leave        }; # $before_leave
1546    
1547        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1548          !!!cp (59);          !!!cp (59);
1549          $before_leave->();          $before_leave->();
1550          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;          $self->{state} = AFTER_ATTRIBUTE_NAME_STATE;
1551          !!!next-input-character;          !!!next-input-character;
1552          redo A;          redo A;
1553        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1554          !!!cp (60);          !!!cp (60);
1555          $before_leave->();          $before_leave->();
1556          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1557          !!!next-input-character;          !!!next-input-character;
1558          redo A;          redo A;
1559        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1560          $before_leave->();          $before_leave->();
1561          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1562            !!!cp (61);            !!!cp (61);
1563            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1564                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1565            !!!cp (62);            !!!cp (62);
1566            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1567            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1568              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1569            }            }
1570          } else {          } else {
1571            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1572          }          }
1573          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1574          !!!next-input-character;          !!!next-input-character;
1575    
1576          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1577    
1578          redo A;          redo A;
1579        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1580                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1581          !!!cp (63);          !!!cp (63);
1582          $self->{current_attribute}->{name} .= chr ($self->{next_char} + 0x0020);          $self->{ca}->{name} .= chr ($self->{nc} + 0x0020);
1583          ## Stay in the state          ## Stay in the state
1584          !!!next-input-character;          !!!next-input-character;
1585          redo A;          redo A;
1586        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1587            !!!cp (64);
1588          $before_leave->();          $before_leave->();
1589            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1590          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_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  
1591          redo A;          redo A;
1592        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1593          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1594          $before_leave->();          $before_leave->();
1595          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1596            !!!cp (66);            !!!cp (66);
1597            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1598                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1599            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1600            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1601              !!!cp (67);              !!!cp (67);
1602              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1603            } else {            } else {
# Line 926  sub _get_next_token ($) { Line 1605  sub _get_next_token ($) {
1605              !!!cp (68);              !!!cp (68);
1606            }            }
1607          } else {          } else {
1608            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1609          }          }
1610          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1611          # reconsume          # reconsume
1612    
1613          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1614    
1615          redo A;          redo A;
1616        } else {        } else {
1617          if ($self->{next_char} == 0x0022 or # "          if ($self->{nc} == 0x0022 or # "
1618              $self->{next_char} == 0x0027) { # '              $self->{nc} == 0x0027) { # '
1619            !!!cp (69);            !!!cp (69);
1620            !!!parse-error (type => 'bad attribute name');            !!!parse-error (type => 'bad attribute name');
1621          } else {          } else {
1622            !!!cp (70);            !!!cp (70);
1623          }          }
1624          $self->{current_attribute}->{name} .= chr ($self->{next_char});          $self->{ca}->{name} .= chr ($self->{nc});
1625          ## Stay in the state          ## Stay in the state
1626          !!!next-input-character;          !!!next-input-character;
1627          redo A;          redo A;
1628        }        }
1629      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_NAME_STATE) {
1630        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1631          !!!cp (71);          !!!cp (71);
1632          ## Stay in the state          ## Stay in the state
1633          !!!next-input-character;          !!!next-input-character;
1634          redo A;          redo A;
1635        } elsif ($self->{next_char} == 0x003D) { # =        } elsif ($self->{nc} == 0x003D) { # =
1636          !!!cp (72);          !!!cp (72);
1637          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;          $self->{state} = BEFORE_ATTRIBUTE_VALUE_STATE;
1638          !!!next-input-character;          !!!next-input-character;
1639          redo A;          redo A;
1640        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1641          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1642            !!!cp (73);            !!!cp (73);
1643            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1644                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1645            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1646            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1647              !!!cp (74);              !!!cp (74);
1648              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1649            } else {            } else {
# Line 978  sub _get_next_token ($) { Line 1651  sub _get_next_token ($) {
1651              !!!cp (75);              !!!cp (75);
1652            }            }
1653          } else {          } else {
1654            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1655          }          }
1656          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1657          !!!next-input-character;          !!!next-input-character;
1658    
1659          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1660    
1661          redo A;          redo A;
1662        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{nc} and
1663                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{nc} <= 0x005A) { # A..Z
1664          !!!cp (76);          !!!cp (76);
1665          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{ca}
1666                                value => ''};              = {name => chr ($self->{nc} + 0x0020),
1667                   value => '',
1668                   line => $self->{line}, column => $self->{column}};
1669          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1670          !!!next-input-character;          !!!next-input-character;
1671          redo A;          redo A;
1672        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
1673            !!!cp (77);
1674            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1675          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_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  
1676          redo A;          redo A;
1677        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1678          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1679          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1680            !!!cp (79);            !!!cp (79);
1681            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1682                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1683            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1684            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1685              !!!cp (80);              !!!cp (80);
1686              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1687            } else {            } else {
# Line 1027  sub _get_next_token ($) { Line 1689  sub _get_next_token ($) {
1689              !!!cp (81);              !!!cp (81);
1690            }            }
1691          } else {          } else {
1692            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1693          }          }
1694          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1695          # reconsume          # reconsume
1696    
1697          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1698    
1699          redo A;          redo A;
1700        } else {        } else {
1701          !!!cp (82);          if ($self->{nc} == 0x0022 or # "
1702          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{nc} == 0x0027) { # '
1703                                value => ''};            !!!cp (78);
1704              !!!parse-error (type => 'bad attribute name');
1705            } else {
1706              !!!cp (82);
1707            }
1708            $self->{ca}
1709                = {name => chr ($self->{nc}),
1710                   value => '',
1711                   line => $self->{line}, column => $self->{column}};
1712          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1713          !!!next-input-character;          !!!next-input-character;
1714          redo A;                  redo A;        
1715        }        }
1716      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {      } elsif ($self->{state} == BEFORE_ATTRIBUTE_VALUE_STATE) {
1717        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP        
1718          !!!cp (83);          !!!cp (83);
1719          ## Stay in the state          ## Stay in the state
1720          !!!next-input-character;          !!!next-input-character;
1721          redo A;          redo A;
1722        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
1723          !!!cp (84);          !!!cp (84);
1724          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
1725          !!!next-input-character;          !!!next-input-character;
1726          redo A;          redo A;
1727        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1728          !!!cp (85);          !!!cp (85);
1729          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1730          ## reconsume          ## reconsume
1731          redo A;          redo A;
1732        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
1733          !!!cp (86);          !!!cp (86);
1734          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
1735          !!!next-input-character;          !!!next-input-character;
1736          redo A;          redo A;
1737        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1738          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          !!!parse-error (type => 'empty unquoted attribute value');
1739            if ($self->{ct}->{type} == START_TAG_TOKEN) {
1740            !!!cp (87);            !!!cp (87);
1741            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1742                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1743            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1744            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1745              !!!cp (88);              !!!cp (88);
1746              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1747            } else {            } else {
# Line 1084  sub _get_next_token ($) { Line 1749  sub _get_next_token ($) {
1749              !!!cp (89);              !!!cp (89);
1750            }            }
1751          } else {          } else {
1752            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1753          }          }
1754          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1755          !!!next-input-character;          !!!next-input-character;
1756    
1757          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1758    
1759          redo A;          redo A;
1760        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1761          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1762          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1763            !!!cp (90);            !!!cp (90);
1764            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1765                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1766            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1767            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1768              !!!cp (91);              !!!cp (91);
1769              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1770            } else {            } else {
# Line 1109  sub _get_next_token ($) { Line 1772  sub _get_next_token ($) {
1772              !!!cp (92);              !!!cp (92);
1773            }            }
1774          } else {          } else {
1775            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1776          }          }
1777          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1778          ## reconsume          ## reconsume
1779    
1780          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1781    
1782          redo A;          redo A;
1783        } else {        } else {
1784          if ($self->{next_char} == 0x003D) { # =          if ($self->{nc} == 0x003D) { # =
1785            !!!cp (93);            !!!cp (93);
1786            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1787          } else {          } else {
1788            !!!cp (94);            !!!cp (94);
1789          }          }
1790          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1791          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;          $self->{state} = ATTRIBUTE_VALUE_UNQUOTED_STATE;
1792          !!!next-input-character;          !!!next-input-character;
1793          redo A;          redo A;
1794        }        }
1795      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE) {
1796        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
1797          !!!cp (95);          !!!cp (95);
1798          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1799          !!!next-input-character;          !!!next-input-character;
1800          redo A;          redo A;
1801        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1802          !!!cp (96);          !!!cp (96);
1803          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1804          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1805            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1806            ## implementation of the "consume a character reference" algorithm.
1807            $self->{prev_state} = $self->{state};
1808            $self->{entity_add} = 0x0022; # "
1809            $self->{state} = ENTITY_STATE;
1810          !!!next-input-character;          !!!next-input-character;
1811          redo A;          redo A;
1812        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1813          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1814          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1815            !!!cp (97);            !!!cp (97);
1816            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1817                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1818            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1819            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1820              !!!cp (98);              !!!cp (98);
1821              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1822            } else {            } else {
# Line 1158  sub _get_next_token ($) { Line 1824  sub _get_next_token ($) {
1824              !!!cp (99);              !!!cp (99);
1825            }            }
1826          } else {          } else {
1827            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1828          }          }
1829          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1830          ## reconsume          ## reconsume
1831    
1832          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1833    
1834          redo A;          redo A;
1835        } else {        } else {
1836          !!!cp (100);          !!!cp (100);
1837          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1838            $self->{read_until}->($self->{ca}->{value},
1839                                  q["&],
1840                                  length $self->{ca}->{value});
1841    
1842          ## Stay in the state          ## Stay in the state
1843          !!!next-input-character;          !!!next-input-character;
1844          redo A;          redo A;
1845        }        }
1846      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE) {
1847        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
1848          !!!cp (101);          !!!cp (101);
1849          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;          $self->{state} = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
1850          !!!next-input-character;          !!!next-input-character;
1851          redo A;          redo A;
1852        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1853          !!!cp (102);          !!!cp (102);
1854          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1855          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1856            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1857            ## implementation of the "consume a character reference" algorithm.
1858            $self->{entity_add} = 0x0027; # '
1859            $self->{prev_state} = $self->{state};
1860            $self->{state} = ENTITY_STATE;
1861          !!!next-input-character;          !!!next-input-character;
1862          redo A;          redo A;
1863        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1864          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1865          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1866            !!!cp (103);            !!!cp (103);
1867            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1868                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1869            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1870            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1871              !!!cp (104);              !!!cp (104);
1872              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1873            } else {            } else {
# Line 1202  sub _get_next_token ($) { Line 1875  sub _get_next_token ($) {
1875              !!!cp (105);              !!!cp (105);
1876            }            }
1877          } else {          } else {
1878            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1879          }          }
1880          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1881          ## reconsume          ## reconsume
1882    
1883          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1884    
1885          redo A;          redo A;
1886        } else {        } else {
1887          !!!cp (106);          !!!cp (106);
1888          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1889            $self->{read_until}->($self->{ca}->{value},
1890                                  q['&],
1891                                  length $self->{ca}->{value});
1892    
1893          ## Stay in the state          ## Stay in the state
1894          !!!next-input-character;          !!!next-input-character;
1895          redo A;          redo A;
1896        }        }
1897      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {      } elsif ($self->{state} == ATTRIBUTE_VALUE_UNQUOTED_STATE) {
1898        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # HT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1899          !!!cp (107);          !!!cp (107);
1900          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1901          !!!next-input-character;          !!!next-input-character;
1902          redo A;          redo A;
1903        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{nc} == 0x0026) { # &
1904          !!!cp (108);          !!!cp (108);
1905          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1906          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1907            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1908            ## implementation of the "consume a character reference" algorithm.
1909            $self->{entity_add} = -1;
1910            $self->{prev_state} = $self->{state};
1911            $self->{state} = ENTITY_STATE;
1912          !!!next-input-character;          !!!next-input-character;
1913          redo A;          redo A;
1914        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1915          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1916            !!!cp (109);            !!!cp (109);
1917            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1918                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1919            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1920            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1921              !!!cp (110);              !!!cp (110);
1922              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1923            } else {            } else {
# Line 1249  sub _get_next_token ($) { Line 1925  sub _get_next_token ($) {
1925              !!!cp (111);              !!!cp (111);
1926            }            }
1927          } else {          } else {
1928            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1929          }          }
1930          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1931          !!!next-input-character;          !!!next-input-character;
1932    
1933          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1934    
1935          redo A;          redo A;
1936        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
1937          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1938          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1939            !!!cp (112);            !!!cp (112);
1940            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1941                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1942            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1943            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1944              !!!cp (113);              !!!cp (113);
1945              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1946            } else {            } else {
# Line 1274  sub _get_next_token ($) { Line 1948  sub _get_next_token ($) {
1948              !!!cp (114);              !!!cp (114);
1949            }            }
1950          } else {          } else {
1951            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
1952          }          }
1953          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1954          ## reconsume          ## reconsume
1955    
1956          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
1957    
1958          redo A;          redo A;
1959        } else {        } else {
# Line 1287  sub _get_next_token ($) { Line 1961  sub _get_next_token ($) {
1961               0x0022 => 1, # "               0x0022 => 1, # "
1962               0x0027 => 1, # '               0x0027 => 1, # '
1963               0x003D => 1, # =               0x003D => 1, # =
1964              }->{$self->{next_char}}) {              }->{$self->{nc}}) {
1965            !!!cp (115);            !!!cp (115);
1966            !!!parse-error (type => 'bad attribute value');            !!!parse-error (type => 'bad attribute value');
1967          } else {          } else {
1968            !!!cp (116);            !!!cp (116);
1969          }          }
1970          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{ca}->{value} .= chr ($self->{nc});
1971            $self->{read_until}->($self->{ca}->{value},
1972                                  q["'=& >],
1973                                  length $self->{ca}->{value});
1974    
1975          ## Stay in the state          ## Stay in the state
1976          !!!next-input-character;          !!!next-input-character;
1977          redo A;          redo A;
1978        }        }
     } 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;  
1979      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1980        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
1981          !!!cp (118);          !!!cp (118);
1982          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1983          !!!next-input-character;          !!!next-input-character;
1984          redo A;          redo A;
1985        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
1986          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{ct}->{type} == START_TAG_TOKEN) {
1987            !!!cp (119);            !!!cp (119);
1988            $self->{current_token}->{first_start_tag}            $self->{last_stag_name} = $self->{ct}->{tag_name};
1989                = not defined $self->{last_emitted_start_tag_name};          } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
           $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};  
         } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {  
1990            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1991            if ($self->{current_token}->{attributes}) {            if ($self->{ct}->{attributes}) {
1992              !!!cp (120);              !!!cp (120);
1993              !!!parse-error (type => 'end tag attribute');              !!!parse-error (type => 'end tag attribute');
1994            } else {            } else {
# Line 1346  sub _get_next_token ($) { Line 1996  sub _get_next_token ($) {
1996              !!!cp (121);              !!!cp (121);
1997            }            }
1998          } else {          } else {
1999            die "$0: $self->{current_token}->{type}: Unknown token type";            die "$0: $self->{ct}->{type}: Unknown token type";
2000          }          }
2001          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2002          !!!next-input-character;          !!!next-input-character;
2003    
2004          !!!emit ($self->{current_token}); # start tag or end tag          !!!emit ($self->{ct}); # start tag or end tag
2005    
2006          redo A;          redo A;
2007        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{nc} == 0x002F) { # /
2008            !!!cp (122);
2009            $self->{state} = SELF_CLOSING_START_TAG_STATE;
2010          !!!next-input-character;          !!!next-input-character;
2011          if ($self->{next_char} == 0x003E and # >          redo A;
2012              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{nc} == -1) {
2013              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
2014            # permitted slash          if ($self->{ct}->{type} == START_TAG_TOKEN) {
2015            !!!cp (122);            !!!cp (122.3);
2016            #            $self->{last_stag_name} = $self->{ct}->{tag_name};
2017            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2018              if ($self->{ct}->{attributes}) {
2019                !!!cp (122.1);
2020                !!!parse-error (type => 'end tag attribute');
2021              } else {
2022                ## NOTE: This state should never be reached.
2023                !!!cp (122.2);
2024              }
2025          } else {          } else {
2026            !!!cp (123);            die "$0: $self->{ct}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
2027          }          }
2028          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
2029          # next-input-character is already done          ## Reconsume.
2030            !!!emit ($self->{ct}); # start tag or end tag
2031          redo A;          redo A;
2032        } else {        } else {
2033          !!!cp (124);          !!!cp ('124.1');
2034          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
2035          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2036          ## reconsume          ## reconsume
2037          redo A;          redo A;
2038        }        }
2039      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
2040        ## (only happen if PCDATA state)        if ($self->{nc} == 0x003E) { # >
2041                  if ($self->{ct}->{type} == END_TAG_TOKEN) {
2042        my $token = {type => COMMENT_TOKEN, data => ''};            !!!cp ('124.2');
2043              !!!parse-error (type => 'nestc', token => $self->{ct});
2044        BC: {            ## TODO: Different type than slash in start tag
2045          if ($self->{next_char} == 0x003E) { # >            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
2046            !!!cp (124);            if ($self->{ct}->{attributes}) {
2047            $self->{state} = DATA_STATE;              !!!cp ('124.4');
2048            !!!next-input-character;              !!!parse-error (type => 'end tag attribute');
2049              } else {
2050            !!!emit ($token);              !!!cp ('124.5');
2051              }
2052              ## TODO: Test |<title></title/>|
2053            } else {
2054              !!!cp ('124.3');
2055              $self->{self_closing} = 1;
2056            }
2057    
2058            redo A;          $self->{state} = DATA_STATE;
2059          } elsif ($self->{next_char} == -1) {          !!!next-input-character;
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
2060    
2061            !!!emit ($token);          !!!emit ($self->{ct}); # start tag or end tag
2062    
2063            redo A;          redo A;
2064          } elsif ($self->{nc} == -1) {
2065            !!!parse-error (type => 'unclosed tag');
2066            if ($self->{ct}->{type} == START_TAG_TOKEN) {
2067              !!!cp (124.7);
2068              $self->{last_stag_name} = $self->{ct}->{tag_name};
2069            } elsif ($self->{ct}->{type} == END_TAG_TOKEN) {
2070              if ($self->{ct}->{attributes}) {
2071                !!!cp (124.5);
2072                !!!parse-error (type => 'end tag attribute');
2073              } else {
2074                ## NOTE: This state should never be reached.
2075                !!!cp (124.6);
2076              }
2077          } else {          } else {
2078            !!!cp (126);            die "$0: $self->{ct}->{type}: Unknown token type";
           $token->{data} .= chr ($self->{next_char});  
           !!!next-input-character;  
           redo BC;  
2079          }          }
2080        } # BC          $self->{state} = DATA_STATE;
2081            ## Reconsume.
2082        die "$0: _get_next_token: unexpected case [BC]";          !!!emit ($self->{ct}); # start tag or end tag
2083      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {          redo A;
2084          } else {
2085            !!!cp ('124.4');
2086            !!!parse-error (type => 'nestc');
2087            ## TODO: This error type is wrong.
2088            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2089            ## Reconsume.
2090            redo A;
2091          }
2092        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2093        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
2094    
2095        my @next_char;        ## NOTE: Unlike spec's "bogus comment state", this implementation
2096        push @next_char, $self->{next_char};        ## consumes characters one-by-one basis.
2097                
2098        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x003E) { # >
2099            !!!cp (124);
2100            $self->{state} = DATA_STATE;
2101          !!!next-input-character;          !!!next-input-character;
2102          push @next_char, $self->{next_char};  
2103          if ($self->{next_char} == 0x002D) { # -          !!!emit ($self->{ct}); # comment
2104            !!!cp (127);          redo A;
2105            $self->{current_token} = {type => COMMENT_TOKEN, data => ''};        } elsif ($self->{nc} == -1) {
2106            $self->{state} = COMMENT_START_STATE;          !!!cp (125);
2107            !!!next-input-character;          $self->{state} = DATA_STATE;
2108            redo A;          ## reconsume
2109          } else {  
2110            !!!cp (128);          !!!emit ($self->{ct}); # comment
2111          }          redo A;
2112        } elsif ($self->{next_char} == 0x0044 or # D        } else {
2113                 $self->{next_char} == 0x0064) { # d          !!!cp (126);
2114            $self->{ct}->{data} .= chr ($self->{nc}); # comment
2115            $self->{read_until}->($self->{ct}->{data},
2116                                  q[>],
2117                                  length $self->{ct}->{data});
2118    
2119            ## Stay in the state.
2120          !!!next-input-character;          !!!next-input-character;
2121          push @next_char, $self->{next_char};          redo A;
2122          if ($self->{next_char} == 0x004F or # O        }
2123              $self->{next_char} == 0x006F) { # o      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2124            !!!next-input-character;        ## (only happen if PCDATA state)
2125            push @next_char, $self->{next_char};        
2126            if ($self->{next_char} == 0x0043 or # C        if ($self->{nc} == 0x002D) { # -
2127                $self->{next_char} == 0x0063) { # c          !!!cp (133);
2128              !!!next-input-character;          $self->{state} = MD_HYPHEN_STATE;
2129              push @next_char, $self->{next_char};          !!!next-input-character;
2130              if ($self->{next_char} == 0x0054 or # T          redo A;
2131                  $self->{next_char} == 0x0074) { # t        } elsif ($self->{nc} == 0x0044 or # D
2132                !!!next-input-character;                 $self->{nc} == 0x0064) { # d
2133                push @next_char, $self->{next_char};          ## ASCII case-insensitive.
2134                if ($self->{next_char} == 0x0059 or # Y          !!!cp (130);
2135                    $self->{next_char} == 0x0079) { # y          $self->{state} = MD_DOCTYPE_STATE;
2136                  !!!next-input-character;          $self->{s_kwd} = chr $self->{nc};
2137                  push @next_char, $self->{next_char};          !!!next-input-character;
2138                  if ($self->{next_char} == 0x0050 or # P          redo A;
2139                      $self->{next_char} == 0x0070) { # p        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2140                    !!!next-input-character;                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2141                    push @next_char, $self->{next_char};                 $self->{nc} == 0x005B) { # [
2142                    if ($self->{next_char} == 0x0045 or # E          !!!cp (135.4);                
2143                        $self->{next_char} == 0x0065) { # e          $self->{state} = MD_CDATA_STATE;
2144                      !!!cp (129);          $self->{s_kwd} = '[';
2145                      ## TODO: What a stupid code this is!          !!!next-input-character;
2146                      $self->{state} = DOCTYPE_STATE;          redo A;
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (130);  
                   }  
                 } else {  
                   !!!cp (131);  
                 }  
               } else {  
                 !!!cp (132);  
               }  
             } else {  
               !!!cp (133);  
             }  
           } else {  
             !!!cp (134);  
           }  
         } else {  
           !!!cp (135);  
         }  
2147        } else {        } else {
2148          !!!cp (136);          !!!cp (136);
2149        }        }
2150    
2151        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2152        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2153        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2154          ## Reconsume.
2155        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2156          $self->{ct} = {type => COMMENT_TOKEN, data => '',
2157                                    line => $self->{line_prev},
2158                                    column => $self->{column_prev} - 1,
2159                                   };
2160        redo A;        redo A;
2161              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2162        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{nc} == 0x002D) { # -
2163        ## ISSUE: spec is somewhat unclear on "is the first character that will be in the comment"; what is "that will be in the comment" is what the algorithm defines, isn't it?          !!!cp (127);
2164            $self->{ct} = {type => COMMENT_TOKEN, data => '',
2165                                      line => $self->{line_prev},
2166                                      column => $self->{column_prev} - 2,
2167                                     };
2168            $self->{state} = COMMENT_START_STATE;
2169            !!!next-input-character;
2170            redo A;
2171          } else {
2172            !!!cp (128);
2173            !!!parse-error (type => 'bogus comment',
2174                            line => $self->{line_prev},
2175                            column => $self->{column_prev} - 2);
2176            $self->{state} = BOGUS_COMMENT_STATE;
2177            ## Reconsume.
2178            $self->{ct} = {type => COMMENT_TOKEN,
2179                                      data => '-',
2180                                      line => $self->{line_prev},
2181                                      column => $self->{column_prev} - 2,
2182                                     };
2183            redo A;
2184          }
2185        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2186          ## ASCII case-insensitive.
2187          if ($self->{nc} == [
2188                undef,
2189                0x004F, # O
2190                0x0043, # C
2191                0x0054, # T
2192                0x0059, # Y
2193                0x0050, # P
2194              ]->[length $self->{s_kwd}] or
2195              $self->{nc} == [
2196                undef,
2197                0x006F, # o
2198                0x0063, # c
2199                0x0074, # t
2200                0x0079, # y
2201                0x0070, # p
2202              ]->[length $self->{s_kwd}]) {
2203            !!!cp (131);
2204            ## Stay in the state.
2205            $self->{s_kwd} .= chr $self->{nc};
2206            !!!next-input-character;
2207            redo A;
2208          } elsif ((length $self->{s_kwd}) == 6 and
2209                   ($self->{nc} == 0x0045 or # E
2210                    $self->{nc} == 0x0065)) { # e
2211            !!!cp (129);
2212            $self->{state} = DOCTYPE_STATE;
2213            $self->{ct} = {type => DOCTYPE_TOKEN,
2214                                      quirks => 1,
2215                                      line => $self->{line_prev},
2216                                      column => $self->{column_prev} - 7,
2217                                     };
2218            !!!next-input-character;
2219            redo A;
2220          } else {
2221            !!!cp (132);        
2222            !!!parse-error (type => 'bogus comment',
2223                            line => $self->{line_prev},
2224                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2225            $self->{state} = BOGUS_COMMENT_STATE;
2226            ## Reconsume.
2227            $self->{ct} = {type => COMMENT_TOKEN,
2228                                      data => $self->{s_kwd},
2229                                      line => $self->{line_prev},
2230                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2231                                     };
2232            redo A;
2233          }
2234        } elsif ($self->{state} == MD_CDATA_STATE) {
2235          if ($self->{nc} == {
2236                '[' => 0x0043, # C
2237                '[C' => 0x0044, # D
2238                '[CD' => 0x0041, # A
2239                '[CDA' => 0x0054, # T
2240                '[CDAT' => 0x0041, # A
2241              }->{$self->{s_kwd}}) {
2242            !!!cp (135.1);
2243            ## Stay in the state.
2244            $self->{s_kwd} .= chr $self->{nc};
2245            !!!next-input-character;
2246            redo A;
2247          } elsif ($self->{s_kwd} eq '[CDATA' and
2248                   $self->{nc} == 0x005B) { # [
2249            !!!cp (135.2);
2250            $self->{ct} = {type => CHARACTER_TOKEN,
2251                                      data => '',
2252                                      line => $self->{line_prev},
2253                                      column => $self->{column_prev} - 7};
2254            $self->{state} = CDATA_SECTION_STATE;
2255            !!!next-input-character;
2256            redo A;
2257          } else {
2258            !!!cp (135.3);
2259            !!!parse-error (type => 'bogus comment',
2260                            line => $self->{line_prev},
2261                            column => $self->{column_prev} - 1 - length $self->{s_kwd});
2262            $self->{state} = BOGUS_COMMENT_STATE;
2263            ## Reconsume.
2264            $self->{ct} = {type => COMMENT_TOKEN,
2265                                      data => $self->{s_kwd},
2266                                      line => $self->{line_prev},
2267                                      column => $self->{column_prev} - 1 - length $self->{s_kwd},
2268                                     };
2269            redo A;
2270          }
2271      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2272        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2273          !!!cp (137);          !!!cp (137);
2274          $self->{state} = COMMENT_START_DASH_STATE;          $self->{state} = COMMENT_START_DASH_STATE;
2275          !!!next-input-character;          !!!next-input-character;
2276          redo A;          redo A;
2277        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2278          !!!cp (138);          !!!cp (138);
2279          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2280          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2281          !!!next-input-character;          !!!next-input-character;
2282    
2283          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2284    
2285          redo A;          redo A;
2286        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2287          !!!cp (139);          !!!cp (139);
2288          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2289          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2290          ## reconsume          ## reconsume
2291    
2292          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2293    
2294          redo A;          redo A;
2295        } else {        } else {
2296          !!!cp (140);          !!!cp (140);
2297          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2298              .= chr ($self->{next_char});              .= chr ($self->{nc});
2299          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2300          !!!next-input-character;          !!!next-input-character;
2301          redo A;          redo A;
2302        }        }
2303      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {      } elsif ($self->{state} == COMMENT_START_DASH_STATE) {
2304        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2305          !!!cp (141);          !!!cp (141);
2306          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2307          !!!next-input-character;          !!!next-input-character;
2308          redo A;          redo A;
2309        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2310          !!!cp (142);          !!!cp (142);
2311          !!!parse-error (type => 'bogus comment');          !!!parse-error (type => 'bogus comment');
2312          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2313          !!!next-input-character;          !!!next-input-character;
2314    
2315          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2316    
2317          redo A;          redo A;
2318        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2319          !!!cp (143);          !!!cp (143);
2320          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2321          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2322          ## reconsume          ## reconsume
2323    
2324          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2325    
2326          redo A;          redo A;
2327        } else {        } else {
2328          !!!cp (144);          !!!cp (144);
2329          $self->{current_token}->{data} # comment          $self->{ct}->{data} # comment
2330              .= '-' . chr ($self->{next_char});              .= '-' . chr ($self->{nc});
2331          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2332          !!!next-input-character;          !!!next-input-character;
2333          redo A;          redo A;
2334        }        }
2335      } elsif ($self->{state} == COMMENT_STATE) {      } elsif ($self->{state} == COMMENT_STATE) {
2336        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2337          !!!cp (145);          !!!cp (145);
2338          $self->{state} = COMMENT_END_DASH_STATE;          $self->{state} = COMMENT_END_DASH_STATE;
2339          !!!next-input-character;          !!!next-input-character;
2340          redo A;          redo A;
2341        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2342          !!!cp (146);          !!!cp (146);
2343          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2344          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2345          ## reconsume          ## reconsume
2346    
2347          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2348    
2349          redo A;          redo A;
2350        } else {        } else {
2351          !!!cp (147);          !!!cp (147);
2352          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{ct}->{data} .= chr ($self->{nc}); # comment
2353            $self->{read_until}->($self->{ct}->{data},
2354                                  q[-],
2355                                  length $self->{ct}->{data});
2356    
2357          ## Stay in the state          ## Stay in the state
2358          !!!next-input-character;          !!!next-input-character;
2359          redo A;          redo A;
2360        }        }
2361      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {      } elsif ($self->{state} == COMMENT_END_DASH_STATE) {
2362        if ($self->{next_char} == 0x002D) { # -        if ($self->{nc} == 0x002D) { # -
2363          !!!cp (148);          !!!cp (148);
2364          $self->{state} = COMMENT_END_STATE;          $self->{state} = COMMENT_END_STATE;
2365          !!!next-input-character;          !!!next-input-character;
2366          redo A;          redo A;
2367        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2368          !!!cp (149);          !!!cp (149);
2369          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2370          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2371          ## reconsume          ## reconsume
2372    
2373          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2374    
2375          redo A;          redo A;
2376        } else {        } else {
2377          !!!cp (150);          !!!cp (150);
2378          $self->{current_token}->{data} .= '-' . chr ($self->{next_char}); # comment          $self->{ct}->{data} .= '-' . chr ($self->{nc}); # comment
2379          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2380          !!!next-input-character;          !!!next-input-character;
2381          redo A;          redo A;
2382        }        }
2383      } elsif ($self->{state} == COMMENT_END_STATE) {      } elsif ($self->{state} == COMMENT_END_STATE) {
2384        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2385          !!!cp (151);          !!!cp (151);
2386          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2387          !!!next-input-character;          !!!next-input-character;
2388    
2389          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2390    
2391          redo A;          redo A;
2392        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{nc} == 0x002D) { # -
2393          !!!cp (152);          !!!cp (152);
2394          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2395          $self->{current_token}->{data} .= '-'; # comment                          line => $self->{line_prev},
2396                            column => $self->{column_prev});
2397            $self->{ct}->{data} .= '-'; # comment
2398          ## Stay in the state          ## Stay in the state
2399          !!!next-input-character;          !!!next-input-character;
2400          redo A;          redo A;
2401        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2402          !!!cp (153);          !!!cp (153);
2403          !!!parse-error (type => 'unclosed comment');          !!!parse-error (type => 'unclosed comment');
2404          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2405          ## reconsume          ## reconsume
2406    
2407          !!!emit ($self->{current_token}); # comment          !!!emit ($self->{ct}); # comment
2408    
2409          redo A;          redo A;
2410        } else {        } else {
2411          !!!cp (154);          !!!cp (154);
2412          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2413          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment                          line => $self->{line_prev},
2414                            column => $self->{column_prev});
2415            $self->{ct}->{data} .= '--' . chr ($self->{nc}); # comment
2416          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2417          !!!next-input-character;          !!!next-input-character;
2418          redo A;          redo A;
2419        }        }
2420      } elsif ($self->{state} == DOCTYPE_STATE) {      } elsif ($self->{state} == DOCTYPE_STATE) {
2421        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2422          !!!cp (155);          !!!cp (155);
2423          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;          $self->{state} = BEFORE_DOCTYPE_NAME_STATE;
2424          !!!next-input-character;          !!!next-input-character;
# Line 1645  sub _get_next_token ($) { Line 2431  sub _get_next_token ($) {
2431          redo A;          redo A;
2432        }        }
2433      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_NAME_STATE) {
2434        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2435          !!!cp (157);          !!!cp (157);
2436          ## Stay in the state          ## Stay in the state
2437          !!!next-input-character;          !!!next-input-character;
2438          redo A;          redo A;
2439        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2440          !!!cp (158);          !!!cp (158);
2441          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2442          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2443          !!!next-input-character;          !!!next-input-character;
2444    
2445          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2446    
2447          redo A;          redo A;
2448        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2449          !!!cp (159);          !!!cp (159);
2450          !!!parse-error (type => 'no DOCTYPE name');          !!!parse-error (type => 'no DOCTYPE name');
2451          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2452          ## reconsume          ## reconsume
2453    
2454          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{ct}); # DOCTYPE (quirks)
2455    
2456          redo A;          redo A;
2457        } else {        } else {
2458          !!!cp (160);          !!!cp (160);
2459          $self->{current_token}          $self->{ct}->{name} = chr $self->{nc};
2460              = {type => DOCTYPE_TOKEN,          delete $self->{ct}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2461  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2462          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2463          !!!next-input-character;          !!!next-input-character;
# Line 1686  sub _get_next_token ($) { Line 2465  sub _get_next_token ($) {
2465        }        }
2466      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == DOCTYPE_NAME_STATE) {
2467  ## ISSUE: Redundant "First," in the spec.  ## ISSUE: Redundant "First," in the spec.
2468        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2469          !!!cp (161);          !!!cp (161);
2470          $self->{state} = AFTER_DOCTYPE_NAME_STATE;          $self->{state} = AFTER_DOCTYPE_NAME_STATE;
2471          !!!next-input-character;          !!!next-input-character;
2472          redo A;          redo A;
2473        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2474          !!!cp (162);          !!!cp (162);
2475          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2476          !!!next-input-character;          !!!next-input-character;
2477    
2478          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2479    
2480          redo A;          redo A;
2481        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2482          !!!cp (163);          !!!cp (163);
2483          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2484          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2485          ## reconsume          ## reconsume
2486    
2487          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2488          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2489    
2490          redo A;          redo A;
2491        } else {        } else {
2492          !!!cp (164);          !!!cp (164);
2493          $self->{current_token}->{name}          $self->{ct}->{name}
2494            .= chr ($self->{next_char}); # DOCTYPE            .= chr ($self->{nc}); # DOCTYPE
2495          ## Stay in the state          ## Stay in the state
2496          !!!next-input-character;          !!!next-input-character;
2497          redo A;          redo A;
2498        }        }
2499      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_NAME_STATE) {
2500        if ($self->{next_char} == 0x0009 or # HT        if ($is_space->{$self->{nc}}) {
           $self->{next_char} == 0x000A or # LF  
           $self->{next_char} == 0x000B or # VT  
           $self->{next_char} == 0x000C or # FF  
           $self->{next_char} == 0x0020) { # SP  
2501          !!!cp (165);          !!!cp (165);
2502          ## Stay in the state          ## Stay in the state
2503          !!!next-input-character;          !!!next-input-character;
2504          redo A;          redo A;
2505        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2506          !!!cp (166);          !!!cp (166);
2507          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2508          !!!next-input-character;          !!!next-input-character;
2509    
2510          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2511    
2512          redo A;          redo A;
2513        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2514          !!!cp (167);          !!!cp (167);
2515          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2516          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2517          ## reconsume          ## reconsume
2518    
2519          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2520          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2521    
2522          redo A;          redo A;
2523        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{nc} == 0x0050 or # P
2524                 $self->{next_char} == 0x0070) { # p                 $self->{nc} == 0x0070) { # p
2525            $self->{state} = PUBLIC_STATE;
2526            $self->{s_kwd} = chr $self->{nc};
2527          !!!next-input-character;          !!!next-input-character;
2528          if ($self->{next_char} == 0x0055 or # U          redo A;
2529              $self->{next_char} == 0x0075) { # u        } elsif ($self->{nc} == 0x0053 or # S
2530            !!!next-input-character;                 $self->{nc} == 0x0073) { # s
2531            if ($self->{next_char} == 0x0042 or # B          $self->{state} = SYSTEM_STATE;
2532                $self->{next_char} == 0x0062) { # b          $self->{s_kwd} = chr $self->{nc};
             !!!next-input-character;  
             if ($self->{next_char} == 0x004C or # L  
                 $self->{next_char} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0049 or # I  
                   $self->{next_char} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x0043 or # C  
                     $self->{next_char} == 0x0063) { # c  
                   !!!cp (168);  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (169);  
                 }  
               } else {  
                 !!!cp (170);  
               }  
             } else {  
               !!!cp (171);  
             }  
           } else {  
             !!!cp (172);  
           }  
         } else {  
           !!!cp (173);  
         }  
   
         #  
       } elsif ($self->{next_char} == 0x0053 or # S  
                $self->{next_char} == 0x0073) { # s  
2533          !!!next-input-character;          !!!next-input-character;
2534          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);  
         }  
   
         #  
2535        } else {        } else {
2536          !!!cp (180);          !!!cp (180);
2537            !!!parse-error (type => 'string after DOCTYPE name');
2538            $self->{ct}->{quirks} = 1;
2539    
2540            $self->{state} = BOGUS_DOCTYPE_STATE;
2541          !!!next-input-character;          !!!next-input-character;
2542          #          redo A;
2543        }        }
2544        } elsif ($self->{state} == PUBLIC_STATE) {
2545          ## ASCII case-insensitive
2546          if ($self->{nc} == [
2547                undef,
2548                0x0055, # U
2549                0x0042, # B
2550                0x004C, # L
2551                0x0049, # I
2552              ]->[length $self->{s_kwd}] or
2553              $self->{nc} == [
2554                undef,
2555                0x0075, # u
2556                0x0062, # b
2557                0x006C, # l
2558                0x0069, # i
2559              ]->[length $self->{s_kwd}]) {
2560            !!!cp (175);
2561            ## Stay in the state.
2562            $self->{s_kwd} .= chr $self->{nc};
2563            !!!next-input-character;
2564            redo A;
2565          } elsif ((length $self->{s_kwd}) == 5 and
2566                   ($self->{nc} == 0x0043 or # C
2567                    $self->{nc} == 0x0063)) { # c
2568            !!!cp (168);
2569            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2570            !!!next-input-character;
2571            redo A;
2572          } else {
2573            !!!cp (169);
2574            !!!parse-error (type => 'string after DOCTYPE name',
2575                            line => $self->{line_prev},
2576                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2577            $self->{ct}->{quirks} = 1;
2578    
2579        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2580        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2581            redo A;
2582          }
2583        } elsif ($self->{state} == SYSTEM_STATE) {
2584          ## ASCII case-insensitive
2585          if ($self->{nc} == [
2586                undef,
2587                0x0059, # Y
2588                0x0053, # S
2589                0x0054, # T
2590                0x0045, # E
2591              ]->[length $self->{s_kwd}] or
2592              $self->{nc} == [
2593                undef,
2594                0x0079, # y
2595                0x0073, # s
2596                0x0074, # t
2597                0x0065, # e
2598              ]->[length $self->{s_kwd}]) {
2599            !!!cp (170);
2600            ## Stay in the state.
2601            $self->{s_kwd} .= chr $self->{nc};
2602            !!!next-input-character;
2603            redo A;
2604          } elsif ((length $self->{s_kwd}) == 5 and
2605                   ($self->{nc} == 0x004D or # M
2606                    $self->{nc} == 0x006D)) { # m
2607            !!!cp (171);
2608            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2609            !!!next-input-character;
2610            redo A;
2611          } else {
2612            !!!cp (172);
2613            !!!parse-error (type => 'string after DOCTYPE name',
2614                            line => $self->{line_prev},
2615                            column => $self->{column_prev} + 1 - length $self->{s_kwd});
2616            $self->{ct}->{quirks} = 1;
2617    
2618        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2619        # next-input-character is already done          ## Reconsume.
2620        redo A;          redo A;
2621          }
2622      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2623        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2624          !!!cp (181);          !!!cp (181);
2625          ## Stay in the state          ## Stay in the state
2626          !!!next-input-character;          !!!next-input-character;
2627          redo A;          redo A;
2628        } elsif ($self->{next_char} eq 0x0022) { # "        } elsif ($self->{nc} eq 0x0022) { # "
2629          !!!cp (182);          !!!cp (182);
2630          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2631          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
2632          !!!next-input-character;          !!!next-input-character;
2633          redo A;          redo A;
2634        } elsif ($self->{next_char} eq 0x0027) { # '        } elsif ($self->{nc} eq 0x0027) { # '
2635          !!!cp (183);          !!!cp (183);
2636          $self->{current_token}->{public_identifier} = ''; # DOCTYPE          $self->{ct}->{pubid} = ''; # DOCTYPE
2637          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
2638          !!!next-input-character;          !!!next-input-character;
2639          redo A;          redo A;
2640        } elsif ($self->{next_char} eq 0x003E) { # >        } elsif ($self->{nc} eq 0x003E) { # >
2641          !!!cp (184);          !!!cp (184);
2642          !!!parse-error (type => 'no PUBLIC literal');          !!!parse-error (type => 'no PUBLIC literal');
2643    
2644          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2645          !!!next-input-character;          !!!next-input-character;
2646    
2647          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2648          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2649    
2650          redo A;          redo A;
2651        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2652          !!!cp (185);          !!!cp (185);
2653          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2654    
2655          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2656          ## reconsume          ## reconsume
2657    
2658          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2659          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2660    
2661          redo A;          redo A;
2662        } else {        } else {
2663          !!!cp (186);          !!!cp (186);
2664          !!!parse-error (type => 'string after PUBLIC');          !!!parse-error (type => 'string after PUBLIC');
2665          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2666    
2667          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2668          !!!next-input-character;          !!!next-input-character;
2669          redo A;          redo A;
2670        }        }
2671      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2672        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2673          !!!cp (187);          !!!cp (187);
2674          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2675          !!!next-input-character;          !!!next-input-character;
2676          redo A;          redo A;
2677        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2678          !!!cp (188);          !!!cp (188);
2679          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2680    
2681          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2682          !!!next-input-character;          !!!next-input-character;
2683    
2684          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2685          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2686    
2687          redo A;          redo A;
2688        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2689          !!!cp (189);          !!!cp (189);
2690          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2691    
2692          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2693          ## reconsume          ## reconsume
2694    
2695          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2696          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2697    
2698          redo A;          redo A;
2699        } else {        } else {
2700          !!!cp (190);          !!!cp (190);
2701          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2702              .= chr $self->{next_char};              .= chr $self->{nc};
2703            $self->{read_until}->($self->{ct}->{pubid}, q[">],
2704                                  length $self->{ct}->{pubid});
2705    
2706          ## Stay in the state          ## Stay in the state
2707          !!!next-input-character;          !!!next-input-character;
2708          redo A;          redo A;
2709        }        }
2710      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE) {
2711        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2712          !!!cp (191);          !!!cp (191);
2713          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2714          !!!next-input-character;          !!!next-input-character;
2715          redo A;          redo A;
2716        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2717          !!!cp (192);          !!!cp (192);
2718          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2719    
2720          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2721          !!!next-input-character;          !!!next-input-character;
2722    
2723          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2724          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2725    
2726          redo A;          redo A;
2727        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2728          !!!cp (193);          !!!cp (193);
2729          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed PUBLIC literal');
2730    
2731          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2732          ## reconsume          ## reconsume
2733    
2734          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2735          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2736    
2737          redo A;          redo A;
2738        } else {        } else {
2739          !!!cp (194);          !!!cp (194);
2740          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{ct}->{pubid} # DOCTYPE
2741              .= chr $self->{next_char};              .= chr $self->{nc};
2742            $self->{read_until}->($self->{ct}->{pubid}, q['>],
2743                                  length $self->{ct}->{pubid});
2744    
2745          ## Stay in the state          ## Stay in the state
2746          !!!next-input-character;          !!!next-input-character;
2747          redo A;          redo A;
2748        }        }
2749      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2750        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2751          !!!cp (195);          !!!cp (195);
2752          ## Stay in the state          ## Stay in the state
2753          !!!next-input-character;          !!!next-input-character;
2754          redo A;          redo A;
2755        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2756          !!!cp (196);          !!!cp (196);
2757          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2758          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2759          !!!next-input-character;          !!!next-input-character;
2760          redo A;          redo A;
2761        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2762          !!!cp (197);          !!!cp (197);
2763          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2764          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2765          !!!next-input-character;          !!!next-input-character;
2766          redo A;          redo A;
2767        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2768          !!!cp (198);          !!!cp (198);
2769          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2770          !!!next-input-character;          !!!next-input-character;
2771    
2772          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2773    
2774          redo A;          redo A;
2775        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2776          !!!cp (199);          !!!cp (199);
2777          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2778    
2779          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2780          ## reconsume          ## reconsume
2781    
2782          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2783          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2784    
2785          redo A;          redo A;
2786        } else {        } else {
2787          !!!cp (200);          !!!cp (200);
2788          !!!parse-error (type => 'string after PUBLIC literal');          !!!parse-error (type => 'string after PUBLIC literal');
2789          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2790    
2791          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2792          !!!next-input-character;          !!!next-input-character;
2793          redo A;          redo A;
2794        }        }
2795      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2796        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2797          !!!cp (201);          !!!cp (201);
2798          ## Stay in the state          ## Stay in the state
2799          !!!next-input-character;          !!!next-input-character;
2800          redo A;          redo A;
2801        } elsif ($self->{next_char} == 0x0022) { # "        } elsif ($self->{nc} == 0x0022) { # "
2802          !!!cp (202);          !!!cp (202);
2803          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2804          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
2805          !!!next-input-character;          !!!next-input-character;
2806          redo A;          redo A;
2807        } elsif ($self->{next_char} == 0x0027) { # '        } elsif ($self->{nc} == 0x0027) { # '
2808          !!!cp (203);          !!!cp (203);
2809          $self->{current_token}->{system_identifier} = ''; # DOCTYPE          $self->{ct}->{sysid} = ''; # DOCTYPE
2810          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;          $self->{state} = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
2811          !!!next-input-character;          !!!next-input-character;
2812          redo A;          redo A;
2813        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2814          !!!cp (204);          !!!cp (204);
2815          !!!parse-error (type => 'no SYSTEM literal');          !!!parse-error (type => 'no SYSTEM literal');
2816          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2817          !!!next-input-character;          !!!next-input-character;
2818    
2819          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2820          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2821    
2822          redo A;          redo A;
2823        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2824          !!!cp (205);          !!!cp (205);
2825          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2826    
2827          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2828          ## reconsume          ## reconsume
2829    
2830          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2831          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2832    
2833          redo A;          redo A;
2834        } else {        } else {
2835          !!!cp (206);          !!!cp (206);
2836          !!!parse-error (type => 'string after SYSTEM');          !!!parse-error (type => 'string after SYSTEM');
2837          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2838    
2839          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2840          !!!next-input-character;          !!!next-input-character;
2841          redo A;          redo A;
2842        }        }
2843      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE) {
2844        if ($self->{next_char} == 0x0022) { # "        if ($self->{nc} == 0x0022) { # "
2845          !!!cp (207);          !!!cp (207);
2846          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2847          !!!next-input-character;          !!!next-input-character;
2848          redo A;          redo A;
2849        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2850          !!!cp (208);          !!!cp (208);
2851          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2852    
2853          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2854          !!!next-input-character;          !!!next-input-character;
2855    
2856          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2857          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2858    
2859          redo A;          redo A;
2860        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2861          !!!cp (209);          !!!cp (209);
2862          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2863    
2864          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2865          ## reconsume          ## reconsume
2866    
2867          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2868          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2869    
2870          redo A;          redo A;
2871        } else {        } else {
2872          !!!cp (210);          !!!cp (210);
2873          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2874              .= chr $self->{next_char};              .= chr $self->{nc};
2875            $self->{read_until}->($self->{ct}->{sysid}, q[">],
2876                                  length $self->{ct}->{sysid});
2877    
2878          ## Stay in the state          ## Stay in the state
2879          !!!next-input-character;          !!!next-input-character;
2880          redo A;          redo A;
2881        }        }
2882      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {      } elsif ($self->{state} == DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE) {
2883        if ($self->{next_char} == 0x0027) { # '        if ($self->{nc} == 0x0027) { # '
2884          !!!cp (211);          !!!cp (211);
2885          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;          $self->{state} = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2886          !!!next-input-character;          !!!next-input-character;
2887          redo A;          redo A;
2888        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2889          !!!cp (212);          !!!cp (212);
2890          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2891    
2892          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2893          !!!next-input-character;          !!!next-input-character;
2894    
2895          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2896          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2897    
2898          redo A;          redo A;
2899        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2900          !!!cp (213);          !!!cp (213);
2901          !!!parse-error (type => 'unclosed SYSTEM literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2902    
2903          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2904          ## reconsume          ## reconsume
2905    
2906          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2907          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2908    
2909          redo A;          redo A;
2910        } else {        } else {
2911          !!!cp (214);          !!!cp (214);
2912          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{ct}->{sysid} # DOCTYPE
2913              .= chr $self->{next_char};              .= chr $self->{nc};
2914            $self->{read_until}->($self->{ct}->{sysid}, q['>],
2915                                  length $self->{ct}->{sysid});
2916    
2917          ## Stay in the state          ## Stay in the state
2918          !!!next-input-character;          !!!next-input-character;
2919          redo A;          redo A;
2920        }        }
2921      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {      } elsif ($self->{state} == AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE) {
2922        if ({        if ($is_space->{$self->{nc}}) {
             0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,  
             #0x000D => 1, # HT, LF, VT, FF, SP, CR  
           }->{$self->{next_char}}) {  
2923          !!!cp (215);          !!!cp (215);
2924          ## Stay in the state          ## Stay in the state
2925          !!!next-input-character;          !!!next-input-character;
2926          redo A;          redo A;
2927        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{nc} == 0x003E) { # >
2928          !!!cp (216);          !!!cp (216);
2929          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2930          !!!next-input-character;          !!!next-input-character;
2931    
2932          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2933    
2934          redo A;          redo A;
2935        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2936          !!!cp (217);          !!!cp (217);
2937          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2938          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2939          ## reconsume          ## reconsume
2940    
2941          $self->{current_token}->{quirks} = 1;          $self->{ct}->{quirks} = 1;
2942          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2943    
2944          redo A;          redo A;
2945        } else {        } else {
2946          !!!cp (218);          !!!cp (218);
2947          !!!parse-error (type => 'string after SYSTEM literal');          !!!parse-error (type => 'string after SYSTEM literal');
2948          #$self->{current_token}->{quirks} = 1;          #$self->{ct}->{quirks} = 1;
2949    
2950          $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2951          !!!next-input-character;          !!!next-input-character;
2952          redo A;          redo A;
2953        }        }
2954      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {      } elsif ($self->{state} == BOGUS_DOCTYPE_STATE) {
2955        if ($self->{next_char} == 0x003E) { # >        if ($self->{nc} == 0x003E) { # >
2956          !!!cp (219);          !!!cp (219);
2957          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2958          !!!next-input-character;          !!!next-input-character;
2959    
2960          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2961    
2962          redo A;          redo A;
2963        } elsif ($self->{next_char} == -1) {        } elsif ($self->{nc} == -1) {
2964          !!!cp (220);          !!!cp (220);
2965          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
2966          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2967          ## reconsume          ## reconsume
2968    
2969          !!!emit ($self->{current_token}); # DOCTYPE          !!!emit ($self->{ct}); # DOCTYPE
2970    
2971          redo A;          redo A;
2972        } else {        } else {
2973          !!!cp (221);          !!!cp (221);
2974            my $s = '';
2975            $self->{read_until}->($s, q[>], 0);
2976    
2977          ## Stay in the state          ## Stay in the state
2978          !!!next-input-character;          !!!next-input-character;
2979          redo A;          redo A;
2980        }        }
2981      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2982        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2983      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2984    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2985          
2986    die "$0: _get_next_token: unexpected case";        if ($self->{nc} == 0x005D) { # ]
2987  } # _get_next_token          !!!cp (221.1);
2988            $self->{state} = CDATA_SECTION_MSE1_STATE;
2989            !!!next-input-character;
2990            redo A;
2991          } elsif ($self->{nc} == -1) {
2992            $self->{state} = DATA_STATE;
2993            !!!next-input-character;
2994            if (length $self->{ct}->{data}) { # character
2995              !!!cp (221.2);
2996              !!!emit ($self->{ct}); # character
2997            } else {
2998              !!!cp (221.3);
2999              ## No token to emit. $self->{ct} is discarded.
3000            }        
3001            redo A;
3002          } else {
3003            !!!cp (221.4);
3004            $self->{ct}->{data} .= chr $self->{nc};
3005            $self->{read_until}->($self->{ct}->{data},
3006                                  q<]>,
3007                                  length $self->{ct}->{data});
3008    
3009  sub _tokenize_attempt_to_consume_an_entity ($$$) {          ## Stay in the state.
3010    my ($self, $in_attr, $additional) = @_;          !!!next-input-character;
3011            redo A;
3012          }
3013    
3014    if ({        ## ISSUE: "text tokens" in spec.
3015         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
3016         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{nc} == 0x005D) { # ]
3017         $additional => 1,          !!!cp (221.5);
3018        }->{$self->{next_char}}) {          $self->{state} = CDATA_SECTION_MSE2_STATE;
3019      !!!cp (1001);          !!!next-input-character;
3020      ## Don't consume          redo A;
3021      ## No error        } else {
3022      return undef;          !!!cp (221.6);
3023    } elsif ($self->{next_char} == 0x0023) { # #          $self->{ct}->{data} .= ']';
3024      !!!next-input-character;          $self->{state} = CDATA_SECTION_STATE;
3025      if ($self->{next_char} == 0x0078 or # x          ## Reconsume.
3026          $self->{next_char} == 0x0058) { # X          redo A;
3027        my $code;        }
3028        X: {      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
3029          my $x_char = $self->{next_char};        if ($self->{nc} == 0x003E) { # >
3030          !!!next-input-character;          $self->{state} = DATA_STATE;
3031          if (0x0030 <= $self->{next_char} and          !!!next-input-character;
3032              $self->{next_char} <= 0x0039) { # 0..9          if (length $self->{ct}->{data}) { # character
3033            !!!cp (1002);            !!!cp (221.7);
3034            $code ||= 0;            !!!emit ($self->{ct}); # character
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0030;  
           redo X;  
         } elsif (0x0061 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0066) { # a..f  
           !!!cp (1003);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0046) { # A..F  
           !!!cp (1004);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!cp (1005);  
           !!!parse-error (type => 'bare hcro');  
           !!!back-next-input-character ($x_char, $self->{next_char});  
           $self->{next_char} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_char} == 0x003B) { # ;  
           !!!cp (1006);  
           !!!next-input-character;  
3035          } else {          } else {
3036            !!!cp (1007);            !!!cp (221.8);
3037            !!!parse-error (type => 'no refc');            ## No token to emit. $self->{ct} is discarded.
3038          }          }
3039            redo A;
3040          } elsif ($self->{nc} == 0x005D) { # ]
3041            !!!cp (221.9); # character
3042            $self->{ct}->{data} .= ']'; ## Add first "]" of "]]]".
3043            ## Stay in the state.
3044            !!!next-input-character;
3045            redo A;
3046          } else {
3047            !!!cp (221.11);
3048            $self->{ct}->{data} .= ']]'; # character
3049            $self->{state} = CDATA_SECTION_STATE;
3050            ## Reconsume.
3051            redo A;
3052          }
3053        } elsif ($self->{state} == ENTITY_STATE) {
3054          if ($is_space->{$self->{nc}} or
3055              {
3056                0x003C => 1, 0x0026 => 1, -1 => 1, # <, &
3057                $self->{entity_add} => 1,
3058              }->{$self->{nc}}) {
3059            !!!cp (1001);
3060            ## Don't consume
3061            ## No error
3062            ## Return nothing.
3063            #
3064          } elsif ($self->{nc} == 0x0023) { # #
3065            !!!cp (999);
3066            $self->{state} = ENTITY_HASH_STATE;
3067            $self->{s_kwd} = '#';
3068            !!!next-input-character;
3069            redo A;
3070          } elsif ((0x0041 <= $self->{nc} and
3071                    $self->{nc} <= 0x005A) or # A..Z
3072                   (0x0061 <= $self->{nc} and
3073                    $self->{nc} <= 0x007A)) { # a..z
3074            !!!cp (998);
3075            require Whatpm::_NamedEntityList;
3076            $self->{state} = ENTITY_NAME_STATE;
3077            $self->{s_kwd} = chr $self->{nc};
3078            $self->{entity__value} = $self->{s_kwd};
3079            $self->{entity__match} = 0;
3080            !!!next-input-character;
3081            redo A;
3082          } else {
3083            !!!cp (1027);
3084            !!!parse-error (type => 'bare ero');
3085            ## Return nothing.
3086            #
3087          }
3088    
3089          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3090            !!!cp (1008);        ## reference" algorithm.  In other word, there is an "&" character
3091            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## that does not introduce a character reference, which would be
3092            $code = 0xFFFD;        ## appended to the parent element or the attribute value in later
3093          } elsif ($code > 0x10FFFF) {        ## process of the tokenizer.
3094            !!!cp (1009);  
3095            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        if ($self->{prev_state} == DATA_STATE) {
3096            $code = 0xFFFD;          !!!cp (997);
3097          } elsif ($code == 0x000D) {          $self->{state} = $self->{prev_state};
3098            !!!cp (1010);          ## Reconsume.
3099            !!!parse-error (type => 'CR character reference');          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3100            $code = 0x000A;                    line => $self->{line_prev},
3101          } elsif (0x80 <= $code and $code <= 0x9F) {                    column => $self->{column_prev},
3102            !!!cp (1011);                   });
3103            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          redo A;
3104            $code = $c1_entity_char->{$code};        } else {
3105          }          !!!cp (996);
3106            $self->{ca}->{value} .= '&';
3107          return {type => CHARACTER_TOKEN, data => chr $code,          $self->{state} = $self->{prev_state};
3108                  has_reference => 1};          ## Reconsume.
3109        } # X          redo A;
3110      } elsif (0x0030 <= $self->{next_char} and        }
3111               $self->{next_char} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3112        my $code = $self->{next_char} - 0x0030;        if ($self->{nc} == 0x0078 or # x
3113        !!!next-input-character;            $self->{nc} == 0x0058) { # X
3114                  !!!cp (995);
3115        while (0x0030 <= $self->{next_char} and          $self->{state} = HEXREF_X_STATE;
3116                  $self->{next_char} <= 0x0039) { # 0..9          $self->{s_kwd} .= chr $self->{nc};
3117            !!!next-input-character;
3118            redo A;
3119          } elsif (0x0030 <= $self->{nc} and
3120                   $self->{nc} <= 0x0039) { # 0..9
3121            !!!cp (994);
3122            $self->{state} = NCR_NUM_STATE;
3123            $self->{s_kwd} = $self->{nc} - 0x0030;
3124            !!!next-input-character;
3125            redo A;
3126          } else {
3127            !!!parse-error (type => 'bare nero',
3128                            line => $self->{line_prev},
3129                            column => $self->{column_prev} - 1);
3130    
3131            ## NOTE: According to the spec algorithm, nothing is returned,
3132            ## and then "&#" is appended to the parent element or the attribute
3133            ## value in the later processing.
3134    
3135            if ($self->{prev_state} == DATA_STATE) {
3136              !!!cp (1019);
3137              $self->{state} = $self->{prev_state};
3138              ## Reconsume.
3139              !!!emit ({type => CHARACTER_TOKEN,
3140                        data => '&#',
3141                        line => $self->{line_prev},
3142                        column => $self->{column_prev} - 1,
3143                       });
3144              redo A;
3145            } else {
3146              !!!cp (993);
3147              $self->{ca}->{value} .= '&#';
3148              $self->{state} = $self->{prev_state};
3149              ## Reconsume.
3150              redo A;
3151            }
3152          }
3153        } elsif ($self->{state} == NCR_NUM_STATE) {
3154          if (0x0030 <= $self->{nc} and
3155              $self->{nc} <= 0x0039) { # 0..9
3156          !!!cp (1012);          !!!cp (1012);
3157          $code *= 10;          $self->{s_kwd} *= 10;
3158          $code += $self->{next_char} - 0x0030;          $self->{s_kwd} += $self->{nc} - 0x0030;
3159                    
3160            ## Stay in the state.
3161          !!!next-input-character;          !!!next-input-character;
3162        }          redo A;
3163          } elsif ($self->{nc} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3164          !!!cp (1013);          !!!cp (1013);
3165          !!!next-input-character;          !!!next-input-character;
3166            #
3167        } else {        } else {
3168          !!!cp (1014);          !!!cp (1014);
3169          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3170            ## Reconsume.
3171            #
3172        }        }
3173    
3174          my $code = $self->{s_kwd};
3175          my $l = $self->{line_prev};
3176          my $c = $self->{column_prev};
3177        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3178          !!!cp (1015);          !!!cp (1015);
3179          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
3180                            text => (sprintf 'U+%04X', $code),
3181                            line => $l, column => $c);
3182          $code = 0xFFFD;          $code = 0xFFFD;
3183        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3184          !!!cp (1016);          !!!cp (1016);
3185          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
3186                            text => (sprintf 'U-%08X', $code),
3187                            line => $l, column => $c);
3188          $code = 0xFFFD;          $code = 0xFFFD;
3189        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3190          !!!cp (1017);          !!!cp (1017);
3191          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
3192                            line => $l, column => $c);
3193          $code = 0x000A;          $code = 0x000A;
3194        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3195          !!!cp (1018);          !!!cp (1018);
3196          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3197                            text => (sprintf 'U+%04X', $code),
3198                            line => $l, column => $c);
3199          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3200        }        }
3201          
3202        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        if ($self->{prev_state} == DATA_STATE) {
3203      } else {          !!!cp (992);
3204        !!!cp (1019);          $self->{state} = $self->{prev_state};
3205        !!!parse-error (type => 'bare nero');          ## Reconsume.
3206        !!!back-next-input-character ($self->{next_char});          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3207        $self->{next_char} = 0x0023; # #                    line => $l, column => $c,
3208        return undef;                   });
3209      }          redo A;
3210    } elsif ((0x0041 <= $self->{next_char} and        } else {
3211              $self->{next_char} <= 0x005A) or          !!!cp (991);
3212             (0x0061 <= $self->{next_char} and          $self->{ca}->{value} .= chr $code;
3213              $self->{next_char} <= 0x007A)) {          $self->{ca}->{has_reference} = 1;
3214      my $entity_name = chr $self->{next_char};          $self->{state} = $self->{prev_state};
3215      !!!next-input-character;          ## Reconsume.
3216            redo A;
3217      my $value = $entity_name;        }
3218      my $match = 0;      } elsif ($self->{state} == HEXREF_X_STATE) {
3219      require Whatpm::_NamedEntityList;        if ((0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) or
3220      our $EntityChar;            (0x0041 <= $self->{nc} and $self->{nc} <= 0x0046) or
3221              (0x0061 <= $self->{nc} and $self->{nc} <= 0x0066)) {
3222      while (length $entity_name < 10 and          # 0..9, A..F, a..f
3223             ## NOTE: Some number greater than the maximum length of entity name          !!!cp (990);
3224             ((0x0041 <= $self->{next_char} and # a          $self->{state} = HEXREF_HEX_STATE;
3225               $self->{next_char} <= 0x005A) or # x          $self->{s_kwd} = 0;
3226              (0x0061 <= $self->{next_char} and # a          ## Reconsume.
3227               $self->{next_char} <= 0x007A) or # z          redo A;
3228              (0x0030 <= $self->{next_char} and # 0        } else {
3229               $self->{next_char} <= 0x0039) or # 9          !!!parse-error (type => 'bare hcro',
3230              $self->{next_char} == 0x003B)) { # ;                          line => $self->{line_prev},
3231        $entity_name .= chr $self->{next_char};                          column => $self->{column_prev} - 2);
3232        if (defined $EntityChar->{$entity_name}) {  
3233          if ($self->{next_char} == 0x003B) { # ;          ## NOTE: According to the spec algorithm, nothing is returned,
3234            !!!cp (1020);          ## and then "&#" followed by "X" or "x" is appended to the parent
3235            $value = $EntityChar->{$entity_name};          ## element or the attribute value in the later processing.
3236            $match = 1;  
3237            !!!next-input-character;          if ($self->{prev_state} == DATA_STATE) {
3238            last;            !!!cp (1005);
3239              $self->{state} = $self->{prev_state};
3240              ## Reconsume.
3241              !!!emit ({type => CHARACTER_TOKEN,
3242                        data => '&' . $self->{s_kwd},
3243                        line => $self->{line_prev},
3244                        column => $self->{column_prev} - length $self->{s_kwd},
3245                       });
3246              redo A;
3247          } else {          } else {
3248            !!!cp (1021);            !!!cp (989);
3249            $value = $EntityChar->{$entity_name};            $self->{ca}->{value} .= '&' . $self->{s_kwd};
3250            $match = -1;            $self->{state} = $self->{prev_state};
3251            !!!next-input-character;            ## Reconsume.
3252              redo A;
3253          }          }
3254        } else {        }
3255          !!!cp (1022);      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3256          $value .= chr $self->{next_char};        if (0x0030 <= $self->{nc} and $self->{nc} <= 0x0039) {
3257          $match *= 2;          # 0..9
3258            !!!cp (1002);
3259            $self->{s_kwd} *= 0x10;
3260            $self->{s_kwd} += $self->{nc} - 0x0030;
3261            ## Stay in the state.
3262            !!!next-input-character;
3263            redo A;
3264          } elsif (0x0061 <= $self->{nc} and
3265                   $self->{nc} <= 0x0066) { # a..f
3266            !!!cp (1003);
3267            $self->{s_kwd} *= 0x10;
3268            $self->{s_kwd} += $self->{nc} - 0x0060 + 9;
3269            ## Stay in the state.
3270            !!!next-input-character;
3271            redo A;
3272          } elsif (0x0041 <= $self->{nc} and
3273                   $self->{nc} <= 0x0046) { # A..F
3274            !!!cp (1004);
3275            $self->{s_kwd} *= 0x10;
3276            $self->{s_kwd} += $self->{nc} - 0x0040 + 9;
3277            ## Stay in the state.
3278            !!!next-input-character;
3279            redo A;
3280          } elsif ($self->{nc} == 0x003B) { # ;
3281            !!!cp (1006);
3282          !!!next-input-character;          !!!next-input-character;
3283            #
3284          } else {
3285            !!!cp (1007);
3286            !!!parse-error (type => 'no refc',
3287                            line => $self->{line},
3288                            column => $self->{column});
3289            ## Reconsume.
3290            #
3291        }        }
3292      }  
3293              my $code = $self->{s_kwd};
3294      if ($match > 0) {        my $l = $self->{line_prev};
3295        !!!cp (1023);        my $c = $self->{column_prev};
3296        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3297      } elsif ($match < 0) {          !!!cp (1008);
3298        !!!parse-error (type => 'no refc');          !!!parse-error (type => 'invalid character reference',
3299        if ($in_attr and $match < -1) {                          text => (sprintf 'U+%04X', $code),
3300          !!!cp (1024);                          line => $l, column => $c);
3301          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          $code = 0xFFFD;
3302          } elsif ($code > 0x10FFFF) {
3303            !!!cp (1009);
3304            !!!parse-error (type => 'invalid character reference',
3305                            text => (sprintf 'U-%08X', $code),
3306                            line => $l, column => $c);
3307            $code = 0xFFFD;
3308          } elsif ($code == 0x000D) {
3309            !!!cp (1010);
3310            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3311            $code = 0x000A;
3312          } elsif (0x80 <= $code and $code <= 0x9F) {
3313            !!!cp (1011);
3314            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3315            $code = $c1_entity_char->{$code};
3316          }
3317    
3318          if ($self->{prev_state} == DATA_STATE) {
3319            !!!cp (988);
3320            $self->{state} = $self->{prev_state};
3321            ## Reconsume.
3322            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3323                      line => $l, column => $c,
3324                     });
3325            redo A;
3326          } else {
3327            !!!cp (987);
3328            $self->{ca}->{value} .= chr $code;
3329            $self->{ca}->{has_reference} = 1;
3330            $self->{state} = $self->{prev_state};
3331            ## Reconsume.
3332            redo A;
3333          }
3334        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3335          if (length $self->{s_kwd} < 30 and
3336              ## NOTE: Some number greater than the maximum length of entity name
3337              ((0x0041 <= $self->{nc} and # a
3338                $self->{nc} <= 0x005A) or # x
3339               (0x0061 <= $self->{nc} and # a
3340                $self->{nc} <= 0x007A) or # z
3341               (0x0030 <= $self->{nc} and # 0
3342                $self->{nc} <= 0x0039) or # 9
3343               $self->{nc} == 0x003B)) { # ;
3344            our $EntityChar;
3345            $self->{s_kwd} .= chr $self->{nc};
3346            if (defined $EntityChar->{$self->{s_kwd}}) {
3347              if ($self->{nc} == 0x003B) { # ;
3348                !!!cp (1020);
3349                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3350                $self->{entity__match} = 1;
3351                !!!next-input-character;
3352                #
3353              } else {
3354                !!!cp (1021);
3355                $self->{entity__value} = $EntityChar->{$self->{s_kwd}};
3356                $self->{entity__match} = -1;
3357                ## Stay in the state.
3358                !!!next-input-character;
3359                redo A;
3360              }
3361            } else {
3362              !!!cp (1022);
3363              $self->{entity__value} .= chr $self->{nc};
3364              $self->{entity__match} *= 2;
3365              ## Stay in the state.
3366              !!!next-input-character;
3367              redo A;
3368            }
3369          }
3370    
3371          my $data;
3372          my $has_ref;
3373          if ($self->{entity__match} > 0) {
3374            !!!cp (1023);
3375            $data = $self->{entity__value};
3376            $has_ref = 1;
3377            #
3378          } elsif ($self->{entity__match} < 0) {
3379            !!!parse-error (type => 'no refc');
3380            if ($self->{prev_state} != DATA_STATE and # in attribute
3381                $self->{entity__match} < -1) {
3382              !!!cp (1024);
3383              $data = '&' . $self->{s_kwd};
3384              #
3385            } else {
3386              !!!cp (1025);
3387              $data = $self->{entity__value};
3388              $has_ref = 1;
3389              #
3390            }
3391        } else {        } else {
3392          !!!cp (1025);          !!!cp (1026);
3393          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          !!!parse-error (type => 'bare ero',
3394                            line => $self->{line_prev},
3395                            column => $self->{column_prev} - length $self->{s_kwd});
3396            $data = '&' . $self->{s_kwd};
3397            #
3398          }
3399      
3400          ## NOTE: In these cases, when a character reference is found,
3401          ## it is consumed and a character token is returned, or, otherwise,
3402          ## nothing is consumed and returned, according to the spec algorithm.
3403          ## In this implementation, anything that has been examined by the
3404          ## tokenizer is appended to the parent element or the attribute value
3405          ## as string, either literal string when no character reference or
3406          ## entity-replaced string otherwise, in this stage, since any characters
3407          ## that would not be consumed are appended in the data state or in an
3408          ## appropriate attribute value state anyway.
3409    
3410          if ($self->{prev_state} == DATA_STATE) {
3411            !!!cp (986);
3412            $self->{state} = $self->{prev_state};
3413            ## Reconsume.
3414            !!!emit ({type => CHARACTER_TOKEN,
3415                      data => $data,
3416                      line => $self->{line_prev},
3417                      column => $self->{column_prev} + 1 - length $self->{s_kwd},
3418                     });
3419            redo A;
3420          } else {
3421            !!!cp (985);
3422            $self->{ca}->{value} .= $data;
3423            $self->{ca}->{has_reference} = 1 if $has_ref;
3424            $self->{state} = $self->{prev_state};
3425            ## Reconsume.
3426            redo A;
3427        }        }
3428      } else {      } else {
3429        !!!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};  
3430      }      }
3431    } else {    } # A  
3432      !!!cp (1027);  
3433      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3434      !!!parse-error (type => 'bare ero');  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3435    
3436  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3437    my $self = shift;    my $self = shift;
# Line 2402  sub _initialize_tree_constructor ($) { Line 3440  sub _initialize_tree_constructor ($) {
3440    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3441    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3442    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3443      $self->{document}->set_user_data (manakai_source_line => 1);
3444      $self->{document}->set_user_data (manakai_source_column => 1);
3445  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3446    
3447  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2456  sub _tree_construction_initial ($) { Line 3496  sub _tree_construction_initial ($) {
3496        ## language.        ## language.
3497        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3498        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3499        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3500        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
3501            defined $token->{public_identifier} or            defined $token->{sysid}) {
           defined $token->{system_identifier}) {  
3502          !!!cp ('t1');          !!!cp ('t1');
3503          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3504        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3505          !!!cp ('t2');          !!!cp ('t2');
3506          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!parse-error (type => 'not HTML5', token => $token);
3507          !!!parse-error (type => 'not HTML5');        } elsif (defined $token->{pubid}) {
3508            if ($token->{pubid} eq 'XSLT-compat') {
3509              !!!cp ('t1.2');
3510              !!!parse-error (type => 'XSLT-compat', token => $token,
3511                              level => $self->{level}->{should});
3512            } else {
3513              !!!parse-error (type => 'not HTML5', token => $token);
3514            }
3515        } else {        } else {
3516          !!!cp ('t3');          !!!cp ('t3');
3517            #
3518        }        }
3519                
3520        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3521          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3522        $doctype->public_id ($token->{public_identifier})        ## NOTE: Default value for both |public_id| and |system_id| attributes
3523            if defined $token->{public_identifier};        ## are empty strings, so that we don't set any value in missing cases.
3524        $doctype->system_id ($token->{system_identifier})        $doctype->public_id ($token->{pubid}) if defined $token->{pubid};
3525            if defined $token->{system_identifier};        $doctype->system_id ($token->{sysid}) if defined $token->{sysid};
3526        ## NOTE: Other DocumentType attributes are null or empty lists.        ## NOTE: Other DocumentType attributes are null or empty lists.
3527        ## ISSUE: internalSubset = null??        ## ISSUE: internalSubset = null??
3528        $self->{document}->append_child ($doctype);        $self->{document}->append_child ($doctype);
# Line 2483  sub _tree_construction_initial ($) { Line 3530  sub _tree_construction_initial ($) {
3530        if ($token->{quirks} or $doctype_name ne 'HTML') {        if ($token->{quirks} or $doctype_name ne 'HTML') {
3531          !!!cp ('t4');          !!!cp ('t4');
3532          $self->{document}->manakai_compat_mode ('quirks');          $self->{document}->manakai_compat_mode ('quirks');
3533        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{pubid}) {
3534          my $pubid = $token->{public_identifier};          my $pubid = $token->{pubid};
3535          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3536          if ({          my $prefix = [
3537            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3538            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3539            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3540            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3541            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3542            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3543            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3544            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3545            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3546            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3547            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3548            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3549            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3550            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3551            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3552            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3553            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3554            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3555            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3556            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3557            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3558            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3559            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3560            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3561            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3562            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3563            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3564            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3565            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3566            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3567            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3568            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3569            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3570            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3571            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3572            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3573            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3574            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3575            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3576            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3577            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3578            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3579            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3580            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3581            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3582            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3583            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3584            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3585            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3586            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3587            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3588            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3589            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3590            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3591            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3592            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3593            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3594            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3595            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3596            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3597            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3598            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3599            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3600            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3601            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3602            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3603            "-//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}) {  
3604            !!!cp ('t5');            !!!cp ('t5');
3605            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3606          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3607                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3608            if (defined $token->{system_identifier}) {            if (defined $token->{sysid}) {
3609              !!!cp ('t6');              !!!cp ('t6');
3610              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
3611            } else {            } else {
3612              !!!cp ('t7');              !!!cp ('t7');
3613              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3614            }            }
3615          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3616                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3617            !!!cp ('t8');            !!!cp ('t8');
3618            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3619          } else {          } else {
# Line 2581  sub _tree_construction_initial ($) { Line 3622  sub _tree_construction_initial ($) {
3622        } else {        } else {
3623          !!!cp ('t10');          !!!cp ('t10');
3624        }        }
3625        if (defined $token->{system_identifier}) {        if (defined $token->{sysid}) {
3626          my $sysid = $token->{system_identifier};          my $sysid = $token->{sysid};
3627          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3628          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {          if ($sysid eq "http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd") {
3629            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3630              ## marked as quirks.
3631            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3632            !!!cp ('t11');            !!!cp ('t11');
3633          } else {          } else {
# Line 2604  sub _tree_construction_initial ($) { Line 3646  sub _tree_construction_initial ($) {
3646                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3647               }->{$token->{type}}) {               }->{$token->{type}}) {
3648        !!!cp ('t14');        !!!cp ('t14');
3649        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3650        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3651        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3652        ## reprocess        ## reprocess
3653          !!!ack-later;
3654        return;        return;
3655      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3656        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3657          ## Ignore the token          ## Ignore the token
3658    
3659          unless (length $token->{data}) {          unless (length $token->{data}) {
# Line 2625  sub _tree_construction_initial ($) { Line 3668  sub _tree_construction_initial ($) {
3668          !!!cp ('t17');          !!!cp ('t17');
3669        }        }
3670    
3671        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3672        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3673        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3674        ## reprocess        ## reprocess
# Line 2654  sub _tree_construction_root_element ($) Line 3697  sub _tree_construction_root_element ($)
3697    B: {    B: {
3698        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3699          !!!cp ('t19');          !!!cp ('t19');
3700          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3701          ## Ignore the token          ## Ignore the token
3702          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3703          !!!next-token;          !!!next-token;
# Line 2667  sub _tree_construction_root_element ($) Line 3710  sub _tree_construction_root_element ($)
3710          !!!next-token;          !!!next-token;
3711          redo B;          redo B;
3712        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
3713          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
3714            ## Ignore the token.            ## Ignore the token.
3715    
3716            unless (length $token->{data}) {            unless (length $token->{data}) {
# Line 2688  sub _tree_construction_root_element ($) Line 3731  sub _tree_construction_root_element ($)
3731        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3732          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3733            my $root_element;            my $root_element;
3734            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3735            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3736            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3737                  [$root_element, $el_category->{html}];
3738    
3739            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3740              !!!cp ('t24');              !!!cp ('t24');
3741              $self->{application_cache_selection}              $self->{application_cache_selection}
3742                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3743              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3744                ## According to Hixie (#whatwg 2008-03-19), it should be
3745                ## resolved against the base URI of the document in HTML
3746                ## or xml:base of the element in XHTML.
3747            } else {            } else {
3748              !!!cp ('t25');              !!!cp ('t25');
3749              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3750            }            }
3751    
3752              !!!nack ('t25c');
3753    
3754            !!!next-token;            !!!next-token;
3755            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3756          } else {          } else {
# Line 2718  sub _tree_construction_root_element ($) Line 3767  sub _tree_construction_root_element ($)
3767          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3768        }        }
3769    
3770      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3771        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3772      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3773      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3774    
3775      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3776    
3777      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3778        !!!ack-later;
3779      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3780    
3781      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2748  sub _reset_insertion_mode ($) { Line 3799  sub _reset_insertion_mode ($) {
3799        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3800          $last = 1;          $last = 1;
3801          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3802            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3803                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3804              !!!cp ('t27');          } else {
3805              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3806          }          }
3807        }        }
3808              
3809        ## Step 4..13        ## Step 4..14
3810        my $new_mode = {        my $new_mode;
3811          if ($node->[1] & FOREIGN_EL) {
3812            !!!cp ('t28.1');
3813            ## NOTE: Strictly spaking, the line below only applies to MathML and
3814            ## SVG elements.  Currently the HTML syntax supports only MathML and
3815            ## SVG elements as foreigners.
3816            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3817          } elsif ($node->[1] & TABLE_CELL_EL) {
3818            if ($last) {
3819              !!!cp ('t28.2');
3820              #
3821            } else {
3822              !!!cp ('t28.3');
3823              $new_mode = IN_CELL_IM;
3824            }
3825          } else {
3826            !!!cp ('t28.4');
3827            $new_mode = {
3828                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3829                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3830                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3831                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3832                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3833                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2776  sub _reset_insertion_mode ($) { Line 3838  sub _reset_insertion_mode ($) {
3838                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3839                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3840                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3841                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3842          }
3843        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3844                
3845        ## Step 14        ## Step 15
3846        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3847          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3848            !!!cp ('t29');            !!!cp ('t29');
3849            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2794  sub _reset_insertion_mode ($) { Line 3857  sub _reset_insertion_mode ($) {
3857          !!!cp ('t31');          !!!cp ('t31');
3858        }        }
3859                
3860        ## Step 15        ## Step 16
3861        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3862                
3863        ## Step 16        ## Step 17
3864        $i--;        $i--;
3865        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3866                
3867        ## Step 17        ## Step 18
3868        redo S3;        redo S3;
3869      } # S3      } # S3
3870    
# Line 2913  sub _tree_construction_main ($) { Line 3976  sub _tree_construction_main ($) {
3976      ## Step 1      ## Step 1
3977      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3978      my $el;      my $el;
3979      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3980    
3981      ## Step 2      ## Step 2
3982      $insert->($el);      $insert->($el);
# Line 2924  sub _tree_construction_main ($) { Line 3987  sub _tree_construction_main ($) {
3987    
3988      ## Step 4      ## Step 4
3989      my $text = '';      my $text = '';
3990        !!!nack ('t40.1');
3991      !!!next-token;      !!!next-token;
3992      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3993        !!!cp ('t40');        !!!cp ('t40');
# Line 2950  sub _tree_construction_main ($) { Line 4014  sub _tree_construction_main ($) {
4014        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
4015        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
4016          !!!cp ('t43');          !!!cp ('t43');
4017          !!!parse-error (type => 'in CDATA:#'.$token->{type});          !!!parse-error (type => 'in CDATA:#eof', token => $token);
4018        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
4019          !!!cp ('t44');          !!!cp ('t44');
4020          !!!parse-error (type => 'in RCDATA:#'.$token->{type});          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
4021        } else {        } else {
4022          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
4023        }        }
# Line 2963  sub _tree_construction_main ($) { Line 4027  sub _tree_construction_main ($) {
4027    
4028    my $script_start_tag = sub () {    my $script_start_tag = sub () {
4029      my $script_el;      my $script_el;
4030      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4031      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4032    
4033      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
4034      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4035            
4036      my $text = '';      my $text = '';
4037        !!!nack ('t45.1');
4038      !!!next-token;      !!!next-token;
4039      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
4040        !!!cp ('t45');        !!!cp ('t45');
# Line 2989  sub _tree_construction_main ($) { Line 4054  sub _tree_construction_main ($) {
4054        ## Ignore the token        ## Ignore the token
4055      } else {      } else {
4056        !!!cp ('t48');        !!!cp ('t48');
4057        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#eof', token => $token);
4058        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4059        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4060      }      }
# Line 3017  sub _tree_construction_main ($) { Line 4082  sub _tree_construction_main ($) {
4082    my $open_tables = [[$self->{open_elements}->[0]->[0]]];    my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4083    
4084    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4085      my $tag_name = shift;      my $end_tag_token = shift;
4086        my $tag_name = $end_tag_token->{tag_name};
4087    
4088      ## NOTE: The adoptioon agency algorithm (AAA).      ## NOTE: The adoption agency algorithm (AAA).
4089    
4090      FET: {      FET: {
4091        ## Step 1        ## Step 1
4092        my $formatting_element;        my $formatting_element;
4093        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4094        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4095          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4096              !!!cp ('t52');
4097              last AFE;
4098            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4099                         eq $tag_name) {
4100            !!!cp ('t51');            !!!cp ('t51');
4101            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4102            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4103            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
4104          }          }
4105        } # AFE        } # AFE
4106        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4107          !!!cp ('t53');          !!!cp ('t53');
4108          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4109          ## Ignore the token          ## Ignore the token
4110          !!!next-token;          !!!next-token;
4111          return;          return;
# Line 3055  sub _tree_construction_main ($) { Line 4122  sub _tree_construction_main ($) {
4122              last INSCOPE;              last INSCOPE;
4123            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4124              !!!cp ('t55');              !!!cp ('t55');
4125              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
4126                                text => $token->{tag_name},
4127                                token => $end_tag_token);
4128              ## Ignore the token              ## Ignore the token
4129              !!!next-token;              !!!next-token;
4130              return;              return;
4131            }            }
4132          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4133            !!!cp ('t56');            !!!cp ('t56');
4134            $in_scope = 0;            $in_scope = 0;
4135          }          }
4136        } # INSCOPE        } # INSCOPE
4137        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4138          !!!cp ('t57');          !!!cp ('t57');
4139          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
4140                            text => $token->{tag_name},
4141                            token => $end_tag_token);
4142          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4143          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4144          return;          return;
4145        }        }
4146        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4147          !!!cp ('t58');          !!!cp ('t58');
4148          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
4149                            text => $self->{open_elements}->[-1]->[0]
4150                                ->manakai_local_name,
4151                            token => $end_tag_token);
4152        }        }
4153                
4154        ## Step 2        ## Step 2
# Line 3085  sub _tree_construction_main ($) { Line 4156  sub _tree_construction_main ($) {
4156        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4157        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4158          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4159          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4160              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4161              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4162               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4163            !!!cp ('t59');            !!!cp ('t59');
4164            $furthest_block = $node;            $furthest_block = $node;
4165            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3174  sub _tree_construction_main ($) { Line 4245  sub _tree_construction_main ($) {
4245        } # S7          } # S7  
4246                
4247        ## Step 8        ## Step 8
4248        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
4249          my $foster_parent_element;          my $foster_parent_element;
4250          my $next_sibling;          my $next_sibling;
4251                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
4252                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4253                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4254                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4255                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3253  sub _tree_construction_main ($) { Line 4322  sub _tree_construction_main ($) {
4322    
4323    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4324      my $child = shift;      my $child = shift;
4325      if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
          table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
         }->{$self->{open_elements}->[-1]->[1]}) {  
4326        # MUST        # MUST
4327        my $foster_parent_element;        my $foster_parent_element;
4328        my $next_sibling;        my $next_sibling;
4329                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4330                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4331                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4332                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4333                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3285  sub _tree_construction_main ($) { Line 4352  sub _tree_construction_main ($) {
4352      }      }
4353    }; # $insert_to_foster    }; # $insert_to_foster
4354    
4355    B: {    B: while (1) {
4356      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4357        !!!cp ('t73');        !!!cp ('t73');
4358        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4359        ## Ignore the token        ## Ignore the token
4360        ## Stay in the phase        ## Stay in the phase
4361        !!!next-token;        !!!next-token;
4362        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!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;  
4363      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4364               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4365        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4366          !!!cp ('t79');          !!!cp ('t79');
4367          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4368          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4369        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4370          !!!cp ('t80');          !!!cp ('t80');
4371          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4372          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4373        } else {        } else {
4374          !!!cp ('t81');          !!!cp ('t81');
4375        }        }
4376    
4377        !!!cp ('t82');        !!!cp ('t82');
4378        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
4379        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4380        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4381          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3350  sub _tree_construction_main ($) { Line 4385  sub _tree_construction_main ($) {
4385               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4386          }          }
4387        }        }
4388          !!!nack ('t84.1');
4389        !!!next-token;        !!!next-token;
4390        redo B;        next B;
4391      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4392        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4393        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3365  sub _tree_construction_main ($) { Line 4401  sub _tree_construction_main ($) {
4401          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4402        }        }
4403        !!!next-token;        !!!next-token;
4404        redo B;        next B;
4405      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4406        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4407          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          !!!cp ('t87.1');
4408            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4409            !!!next-token;
4410            next B;
4411          } elsif ($token->{type} == START_TAG_TOKEN) {
4412            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4413                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4414                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4415                ($token->{tag_name} eq 'svg' and
4416                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4417              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4418              !!!cp ('t87.2');
4419              #
4420            } elsif ({
4421                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4422                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4423                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4424                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4425                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4426                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4427                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4428                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4429                     }->{$token->{tag_name}}) {
4430              !!!cp ('t87.2');
4431              !!!parse-error (type => 'not closed',
4432                              text => $self->{open_elements}->[-1]->[0]
4433                                  ->manakai_local_name,
4434                              token => $token);
4435    
4436              pop @{$self->{open_elements}}
4437                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4438    
4439              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4440              ## Reprocess.
4441              next B;
4442            } else {
4443              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4444              my $tag_name = $token->{tag_name};
4445              if ($nsuri eq $SVG_NS) {
4446                $tag_name = {
4447                   altglyph => 'altGlyph',
4448                   altglyphdef => 'altGlyphDef',
4449                   altglyphitem => 'altGlyphItem',
4450                   animatecolor => 'animateColor',
4451                   animatemotion => 'animateMotion',
4452                   animatetransform => 'animateTransform',
4453                   clippath => 'clipPath',
4454                   feblend => 'feBlend',
4455                   fecolormatrix => 'feColorMatrix',
4456                   fecomponenttransfer => 'feComponentTransfer',
4457                   fecomposite => 'feComposite',
4458                   feconvolvematrix => 'feConvolveMatrix',
4459                   fediffuselighting => 'feDiffuseLighting',
4460                   fedisplacementmap => 'feDisplacementMap',
4461                   fedistantlight => 'feDistantLight',
4462                   feflood => 'feFlood',
4463                   fefunca => 'feFuncA',
4464                   fefuncb => 'feFuncB',
4465                   fefuncg => 'feFuncG',
4466                   fefuncr => 'feFuncR',
4467                   fegaussianblur => 'feGaussianBlur',
4468                   feimage => 'feImage',
4469                   femerge => 'feMerge',
4470                   femergenode => 'feMergeNode',
4471                   femorphology => 'feMorphology',
4472                   feoffset => 'feOffset',
4473                   fepointlight => 'fePointLight',
4474                   fespecularlighting => 'feSpecularLighting',
4475                   fespotlight => 'feSpotLight',
4476                   fetile => 'feTile',
4477                   feturbulence => 'feTurbulence',
4478                   foreignobject => 'foreignObject',
4479                   glyphref => 'glyphRef',
4480                   lineargradient => 'linearGradient',
4481                   radialgradient => 'radialGradient',
4482                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4483                   textpath => 'textPath',  
4484                }->{$tag_name} || $tag_name;
4485              }
4486    
4487              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4488    
4489              ## "adjust foreign attributes" - done in insert-element-f
4490    
4491              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4492    
4493              if ($self->{self_closing}) {
4494                pop @{$self->{open_elements}};
4495                !!!ack ('t87.3');
4496              } else {
4497                !!!cp ('t87.4');
4498              }
4499    
4500              !!!next-token;
4501              next B;
4502            }
4503          } elsif ($token->{type} == END_TAG_TOKEN) {
4504            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4505            !!!cp ('t87.5');
4506            #
4507          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4508            !!!cp ('t87.6');
4509            !!!parse-error (type => 'not closed',
4510                            text => $self->{open_elements}->[-1]->[0]
4511                                ->manakai_local_name,
4512                            token => $token);
4513    
4514            pop @{$self->{open_elements}}
4515                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4516    
4517            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4518            ## Reprocess.
4519            next B;
4520          } else {
4521            die "$0: $token->{type}: Unknown token type";        
4522          }
4523        }
4524    
4525        if ($self->{insertion_mode} & HEAD_IMS) {
4526          if ($token->{type} == CHARACTER_TOKEN) {
4527            if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
4528            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4529              !!!cp ('t88.2');              !!!cp ('t88.2');
4530              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4531                #
4532            } else {            } else {
4533              !!!cp ('t88.1');              !!!cp ('t88.1');
4534              ## Ignore the token.              ## Ignore the token.
4535              !!!next-token;              #
             redo B;  
4536            }            }
4537            unless (length $token->{data}) {            unless (length $token->{data}) {
4538              !!!cp ('t88');              !!!cp ('t88');
4539              !!!next-token;              !!!next-token;
4540              redo B;              next B;
4541            }            }
4542    ## TODO: set $token->{column} appropriately
4543          }          }
4544    
4545          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4546            !!!cp ('t89');            !!!cp ('t89');
4547            ## As if <head>            ## As if <head>
4548            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4549            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4550            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4551                  [$self->{head_element}, $el_category->{head}];
4552    
4553            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4554            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3400  sub _tree_construction_main ($) { Line 4558  sub _tree_construction_main ($) {
4558            !!!cp ('t90');            !!!cp ('t90');
4559            ## As if </noscript>            ## As if </noscript>
4560            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4561            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4562                        
4563            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4564            ## As if </head>            ## As if </head>
# Line 3416  sub _tree_construction_main ($) { Line 4574  sub _tree_construction_main ($) {
4574            !!!cp ('t92');            !!!cp ('t92');
4575          }          }
4576    
4577              ## "after head" insertion mode          ## "after head" insertion mode
4578              ## As if <body>          ## As if <body>
4579              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4580              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4581              ## reprocess          ## reprocess
4582              redo B;          next B;
4583            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4584              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4585                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4586                  !!!cp ('t93');              !!!cp ('t93');
4587                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4588                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4589                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4590                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4591                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4592                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4593                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4594                  !!!cp ('t94');              !!!next-token;
4595                  #              next B;
4596                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4597                  !!!cp ('t95');              !!!cp ('t93.2');
4598                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4599                  ## Ignore the token                              token => $token);
4600                  !!!next-token;              ## Ignore the token
4601                  redo B;              !!!nack ('t93.3');
4602                }              !!!next-token;
4603              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4604                !!!cp ('t96');            } else {
4605                ## As if <head>              !!!cp ('t95');
4606                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4607                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4608                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4609                !!!nack ('t95.1');
4610                !!!next-token;
4611                next B;
4612              }
4613            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4614              !!!cp ('t96');
4615              ## As if <head>
4616              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4617              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4618              push @{$self->{open_elements}},
4619                  [$self->{head_element}, $el_category->{head}];
4620    
4621                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4622                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4623              } else {          } else {
4624                !!!cp ('t97');            !!!cp ('t97');
4625              }          }
4626    
4627              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4628                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4629                  !!!cp ('t98');                  !!!cp ('t98');
4630                  ## As if </noscript>                  ## As if </noscript>
4631                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4632                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4633                                    token => $token);
4634                                
4635                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4636                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3471  sub _tree_construction_main ($) { Line 4641  sub _tree_construction_main ($) {
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 ('t100');                  !!!cp ('t100');
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 ('t101');                  !!!cp ('t101');
4650                }                }
4651                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4652                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4653                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4654                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4655                  !!!nack ('t101.1');
4656                !!!next-token;                !!!next-token;
4657                redo B;                next B;
4658              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4659                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4660                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4661                  !!!cp ('t102');                  !!!cp ('t102');
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 ('t103');                  !!!cp ('t103');
4668                }                }
4669                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4670                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4671                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4672                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4673                  !!!ack ('t103.1');
4674                !!!next-token;                !!!next-token;
4675                redo B;                next B;
4676              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4677                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4678                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4679                  !!!cp ('t104');                  !!!cp ('t104');
4680                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4681                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4682                    push @{$self->{open_elements}},
4683                        [$self->{head_element}, $el_category->{head}];
4684                } else {                } else {
4685                  !!!cp ('t105');                  !!!cp ('t105');
4686                }                }
4687                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4688                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.
4689    
4690                unless ($self->{confident}) {                unless ($self->{confident}) {
4691                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4692                    !!!cp ('t106');                    !!!cp ('t106');
4693                      ## NOTE: Whether the encoding is supported or not is handled
4694                      ## in the {change_encoding} callback.
4695                    $self->{change_encoding}                    $self->{change_encoding}
4696                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4697                             $token);
4698                                        
4699                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4700                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4701                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4702                                                 ->{has_reference});                                                 ->{has_reference});
4703                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4704                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4705                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4706                            [\x09-\x0D\x20]*=                            [\x09\x0A\x0C\x0D\x20]*=
4707                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4708                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09\x0A\x0C\x0D\x20]
4709                               [^\x09\x0A\x0C\x0D\x20\x3B]*))/x) {
4710                      !!!cp ('t107');                      !!!cp ('t107');
4711                        ## NOTE: Whether the encoding is supported or not is handled
4712                        ## in the {change_encoding} callback.
4713                      $self->{change_encoding}                      $self->{change_encoding}
4714                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4715                               $token);
4716                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4717                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4718                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3556  sub _tree_construction_main ($) { Line 4740  sub _tree_construction_main ($) {
4740    
4741                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4742                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4743                  !!!ack ('t110.1');
4744                !!!next-token;                !!!next-token;
4745                redo B;                next B;
4746              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4747                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4748                  !!!cp ('t111');                  !!!cp ('t111');
4749                  ## As if </noscript>                  ## As if </noscript>
4750                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4751                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4752                                    token => $token);
4753                                
4754                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4755                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4756                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4757                  !!!cp ('t112');                  !!!cp ('t112');
4758                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4759                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4760                    push @{$self->{open_elements}},
4761                        [$self->{head_element}, $el_category->{head}];
4762                } else {                } else {
4763                  !!!cp ('t113');                  !!!cp ('t113');
4764                }                }
# Line 3581  sub _tree_construction_main ($) { Line 4769  sub _tree_construction_main ($) {
4769                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4770                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4771                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4772                redo B;                next B;
4773              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4774                         $token->{tag_name} eq 'noframes') {
4775                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4776                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4777                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4778                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4779                  !!!cp ('t114');                  !!!cp ('t114');
4780                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4781                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4782                    push @{$self->{open_elements}},
4783                        [$self->{head_element}, $el_category->{head}];
4784                } else {                } else {
4785                  !!!cp ('t115');                  !!!cp ('t115');
4786                }                }
4787                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4788                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4789                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4790                redo B;                next B;
4791              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4792                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4793                  !!!cp ('t116');                  !!!cp ('t116');
4794                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4795                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4796                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4797                    !!!nack ('t116.1');
4798                  !!!next-token;                  !!!next-token;
4799                  redo B;                  next B;
4800                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4801                  !!!cp ('t117');                  !!!cp ('t117');
4802                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
4803                                    token => $token);
4804                  ## Ignore the token                  ## Ignore the token
4805                    !!!nack ('t117.1');
4806                  !!!next-token;                  !!!next-token;
4807                  redo B;                  next B;
4808                } else {                } else {
4809                  !!!cp ('t118');                  !!!cp ('t118');
4810                  #                  #
# Line 3620  sub _tree_construction_main ($) { Line 4814  sub _tree_construction_main ($) {
4814                  !!!cp ('t119');                  !!!cp ('t119');
4815                  ## As if </noscript>                  ## As if </noscript>
4816                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4817                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4818                                    token => $token);
4819                                
4820                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4821                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4822                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4823                  !!!cp ('t120');                  !!!cp ('t120');
4824                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4825                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4826                    push @{$self->{open_elements}},
4827                        [$self->{head_element}, $el_category->{head}];
4828                } else {                } else {
4829                  !!!cp ('t121');                  !!!cp ('t121');
4830                }                }
# Line 3636  sub _tree_construction_main ($) { Line 4833  sub _tree_construction_main ($) {
4833                $script_start_tag->();                $script_start_tag->();
4834                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4835                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4836                redo B;                next B;
4837              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4838                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4839                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4840                  !!!cp ('t122');                  !!!cp ('t122');
4841                  ## As if </noscript>                  ## As if </noscript>
4842                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4843                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4844                                    text => $token->{tag_name}, token => $token);
4845                                    
4846                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4847                  ## As if </head>                  ## As if </head>
# Line 3660  sub _tree_construction_main ($) { Line 4858  sub _tree_construction_main ($) {
4858                }                }
4859    
4860                ## "after head" insertion mode                ## "after head" insertion mode
4861                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4862                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4863                  !!!cp ('t126');                  !!!cp ('t126');
4864                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3670  sub _tree_construction_main ($) { Line 4868  sub _tree_construction_main ($) {
4868                } else {                } else {
4869                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4870                }                }
4871                  !!!nack ('t127.1');
4872                !!!next-token;                !!!next-token;
4873                redo B;                next B;
4874              } else {              } else {
4875                !!!cp ('t128');                !!!cp ('t128');
4876                #                #
# Line 3681  sub _tree_construction_main ($) { Line 4880  sub _tree_construction_main ($) {
4880                !!!cp ('t129');                !!!cp ('t129');
4881                ## As if </noscript>                ## As if </noscript>
4882                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4883                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4884                                  text => $token->{tag_name}, token => $token);
4885                                
4886                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4887                ## As if </head>                ## As if </head>
# Line 3700  sub _tree_construction_main ($) { Line 4900  sub _tree_construction_main ($) {
4900    
4901              ## "after head" insertion mode              ## "after head" insertion mode
4902              ## As if <body>              ## As if <body>
4903              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4904              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4905              ## reprocess              ## reprocess
4906              redo B;              !!!ack-later;
4907                next B;
4908            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4909              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4910                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4911                  !!!cp ('t132');                  !!!cp ('t132');
4912                  ## As if <head>                  ## As if <head>
4913                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4914                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4915                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4916                        [$self->{head_element}, $el_category->{head}];
4917    
4918                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4919                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4920                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4921                  !!!next-token;                  !!!next-token;
4922                  redo B;                  next B;
4923                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4924                  !!!cp ('t133');                  !!!cp ('t133');
4925                  ## As if </noscript>                  ## As if </noscript>
4926                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4927                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/',
4928                                    text => 'head', token => $token);
4929                                    
4930                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4931                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4932                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4933                  !!!next-token;                  !!!next-token;
4934                  redo B;                  next B;
4935                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4936                  !!!cp ('t134');                  !!!cp ('t134');
4937                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4938                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4939                  !!!next-token;                  !!!next-token;
4940                  redo B;                  next B;
4941                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4942                    !!!cp ('t134.1');
4943                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4944                                    token => $token);
4945                    ## Ignore the token
4946                    !!!next-token;
4947                    next B;
4948                } else {                } else {
4949                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4950                }                }
4951              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4952                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3745  sub _tree_construction_main ($) { Line 4954  sub _tree_construction_main ($) {
4954                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4955                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4956                  !!!next-token;                  !!!next-token;
4957                  redo B;                  next B;
4958                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4959                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4960                  !!!cp ('t137');                  !!!cp ('t137');
4961                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
4962                                    text => 'noscript', token => $token);
4963                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4964                  !!!next-token;                  !!!next-token;
4965                  redo B;                  next B;
4966                } else {                } else {
4967                  !!!cp ('t138');                  !!!cp ('t138');
4968                  #                  #
# Line 3759  sub _tree_construction_main ($) { Line 4970  sub _tree_construction_main ($) {
4970              } elsif ({              } elsif ({
4971                        body => 1, html => 1,                        body => 1, html => 1,
4972                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4973                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4974                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4975                  ## 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) {  
4976                  !!!cp ('t140');                  !!!cp ('t140');
4977                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4978                                    text => $token->{tag_name}, token => $token);
4979                    ## Ignore the token
4980                    !!!next-token;
4981                    next B;
4982                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4983                    !!!cp ('t140.1');
4984                    !!!parse-error (type => 'unmatched end tag',
4985                                    text => $token->{tag_name}, token => $token);
4986                  ## Ignore the token                  ## Ignore the token
4987                  !!!next-token;                  !!!next-token;
4988                  redo B;                  next B;
4989                } else {                } else {
4990                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4991                }                }
4992                              } elsif ($token->{tag_name} eq 'p') {
4993                #                !!!cp ('t142');
4994              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4995                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4996                       }->{$token->{tag_name}}) {                ## Ignore the token
4997                  !!!next-token;
4998                  next B;
4999                } elsif ($token->{tag_name} eq 'br') {
5000                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5001                  !!!cp ('t142');                  !!!cp ('t142.2');
5002                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
5003                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5004                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
5005                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
5006      
5007                    ## Reprocess in the "after head" insertion mode...
5008                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5009                    !!!cp ('t143.2');
5010                    ## As if </head>
5011                    pop @{$self->{open_elements}};
5012                    $self->{insertion_mode} = AFTER_HEAD_IM;
5013      
5014                    ## Reprocess in the "after head" insertion mode...
5015                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5016                    !!!cp ('t143.3');
5017                    ## ISSUE: Two parse errors for <head><noscript></br>
5018                    !!!parse-error (type => 'unmatched end tag',
5019                                    text => 'br', token => $token);
5020                    ## As if </noscript>
5021                    pop @{$self->{open_elements}};
5022                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
5023    
5024                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
5025                } else {                  ## As if </head>
5026                  !!!cp ('t143');                  pop @{$self->{open_elements}};
5027                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
5028    
5029                #                  ## Reprocess in the "after head" insertion mode...
5030              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5031                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
5032                  #                  #
5033                } else {                } else {
5034                  !!!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;  
5035                }                }
5036    
5037                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5038                  !!!parse-error (type => 'unmatched end tag',
5039                                  text => 'br', token => $token);
5040                  ## Ignore the token
5041                  !!!next-token;
5042                  next B;
5043                } else {
5044                  !!!cp ('t145');
5045                  !!!parse-error (type => 'unmatched end tag',
5046                                  text => $token->{tag_name}, token => $token);
5047                  ## Ignore the token
5048                  !!!next-token;
5049                  next B;
5050              }              }
5051    
5052              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5053                !!!cp ('t146');                !!!cp ('t146');
5054                ## As if </noscript>                ## As if </noscript>
5055                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5056                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
5057                                  text => $token->{tag_name}, token => $token);
5058                                
5059                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5060                ## As if </head>                ## As if </head>
# Line 3829  sub _tree_construction_main ($) { Line 5070  sub _tree_construction_main ($) {
5070              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5071  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
5072                !!!cp ('t148');                !!!cp ('t148');
5073                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5074                                  text => $token->{tag_name}, token => $token);
5075                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5076                !!!next-token;                !!!next-token;
5077                redo B;                next B;
5078              } else {              } else {
5079                !!!cp ('t149');                !!!cp ('t149');
5080              }              }
5081    
5082              ## "after head" insertion mode              ## "after head" insertion mode
5083              ## As if <body>              ## As if <body>
5084              !!!insert-element ('body');              !!!insert-element ('body',, $token);
5085              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
5086              ## reprocess              ## reprocess
5087              redo B;              next B;
5088            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5089              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5090            }            !!!cp ('t149.1');
5091    
5092              ## NOTE: As if <head>
5093              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5094              $self->{open_elements}->[-1]->[0]->append_child
5095                  ($self->{head_element});
5096              #push @{$self->{open_elements}},
5097              #    [$self->{head_element}, $el_category->{head}];
5098              #$self->{insertion_mode} = IN_HEAD_IM;
5099              ## NOTE: Reprocess.
5100    
5101              ## NOTE: As if </head>
5102              #pop @{$self->{open_elements}};
5103              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5104              ## NOTE: Reprocess.
5105              
5106              #
5107            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5108              !!!cp ('t149.2');
5109    
5110              ## NOTE: As if </head>
5111              pop @{$self->{open_elements}};
5112              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5113              ## NOTE: Reprocess.
5114    
5115              #
5116            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5117              !!!cp ('t149.3');
5118    
5119              !!!parse-error (type => 'in noscript:#eof', token => $token);
5120    
5121              ## As if </noscript>
5122              pop @{$self->{open_elements}};
5123              #$self->{insertion_mode} = IN_HEAD_IM;
5124              ## NOTE: Reprocess.
5125    
5126              ## NOTE: As if </head>
5127              pop @{$self->{open_elements}};
5128              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5129              ## NOTE: Reprocess.
5130    
5131              #
5132            } else {
5133              !!!cp ('t149.4');
5134              #
5135            }
5136    
5137            ## NOTE: As if <body>
5138            !!!insert-element ('body',, $token);
5139            $self->{insertion_mode} = IN_BODY_IM;
5140            ## NOTE: Reprocess.
5141            next B;
5142          } else {
5143            die "$0: $token->{type}: Unknown token type";
5144          }
5145    
5146            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5147      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3857  sub _tree_construction_main ($) { Line 5153  sub _tree_construction_main ($) {
5153              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5154    
5155              !!!next-token;              !!!next-token;
5156              redo B;              next B;
5157            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5158              if ({              if ({
5159                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3865  sub _tree_construction_main ($) { Line 5161  sub _tree_construction_main ($) {
5161                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5162                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5163                  ## have an element in table scope                  ## have an element in table scope
5164                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5165                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5166                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5167                      !!!cp ('t151');                      !!!cp ('t151');
5168                      $tn = $node->[1];  
5169                      last INSCOPE;                      ## Close the cell
5170                    } elsif ({                      !!!back-token; # <x>
5171                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
5172                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
5173                                  line => $token->{line},
5174                                  column => $token->{column}};
5175                        next B;
5176                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5177                      !!!cp ('t152');                      !!!cp ('t152');
5178                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
5179                    }                      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;  
5180                    }                    }
5181                                    }
5182                  !!!cp ('t154');  
5183                  ## Close the cell                  !!!cp ('t153');
5184                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
5185                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      text => $token->{tag_name}, token => $token);
5186                  redo B;                  ## Ignore the token
5187                    !!!nack ('t153.1');
5188                    !!!next-token;
5189                    next B;
5190                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5191                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5192                                    token => $token);
5193                                    
5194                  ## As if </caption>                  ## NOTE: As if </caption>.
5195                  ## have a table element in table scope                  ## have a table element in table scope
5196                  my $i;                  my $i;
5197                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5198                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5199                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5200                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
5201                      $i = $_;                        !!!cp ('t155');
5202                      last INSCOPE;                        $i = $_;
5203                    } elsif ({                        last INSCOPE;
5204                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5205                             }->{$node->[1]}) {                        !!!cp ('t156');
5206                      !!!cp ('t156');                        last;
5207                      last INSCOPE;                      }
5208                    }                    }
5209    
5210                      !!!cp ('t157');
5211                      !!!parse-error (type => 'start tag not allowed',
5212                                      text => $token->{tag_name}, token => $token);
5213                      ## Ignore the token
5214                      !!!nack ('t157.1');
5215                      !!!next-token;
5216                      next B;
5217                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5218                                    
5219                  ## generate implied end tags                  ## generate implied end tags
5220                  while ({                  while ($self->{open_elements}->[-1]->[1]
5221                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5222                    !!!cp ('t158');                    !!!cp ('t158');
5223                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5224                  }                  }
5225    
5226                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5227                    !!!cp ('t159');                    !!!cp ('t159');
5228                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5229                                      text => $self->{open_elements}->[-1]->[0]
5230                                          ->manakai_local_name,
5231                                      token => $token);
5232                  } else {                  } else {
5233                    !!!cp ('t160');                    !!!cp ('t160');
5234                  }                  }
# Line 3943  sub _tree_construction_main ($) { Line 5240  sub _tree_construction_main ($) {
5240                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5241                                    
5242                  ## reprocess                  ## reprocess
5243                  redo B;                  !!!ack-later;
5244                    next B;
5245                } else {                } else {
5246                  !!!cp ('t161');                  !!!cp ('t161');
5247                  #                  #
# Line 3959  sub _tree_construction_main ($) { Line 5257  sub _tree_construction_main ($) {
5257                  my $i;                  my $i;
5258                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5259                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5260                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5261                      !!!cp ('t163');                      !!!cp ('t163');
5262                      $i = $_;                      $i = $_;
5263                      last INSCOPE;                      last INSCOPE;
5264                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5265                      !!!cp ('t164');                      !!!cp ('t164');
5266                      last INSCOPE;                      last INSCOPE;
5267                    }                    }
5268                  } # INSCOPE                  } # INSCOPE
5269                    unless (defined $i) {                    unless (defined $i) {
5270                      !!!cp ('t165');                      !!!cp ('t165');
5271                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5272                                        text => $token->{tag_name},
5273                                        token => $token);
5274                      ## Ignore the token                      ## Ignore the token
5275                      !!!next-token;                      !!!next-token;
5276                      redo B;                      next B;
5277                    }                    }
5278                                    
5279                  ## generate implied end tags                  ## generate implied end tags
5280                  while ({                  while ($self->{open_elements}->[-1]->[1]
5281                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5282                    !!!cp ('t166');                    !!!cp ('t166');
5283                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5284                  }                  }
5285    
5286                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5287                            ne $token->{tag_name}) {
5288                    !!!cp ('t167');                    !!!cp ('t167');
5289                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5290                                      text => $self->{open_elements}->[-1]->[0]
5291                                          ->manakai_local_name,
5292                                      token => $token);
5293                  } else {                  } else {
5294                    !!!cp ('t168');                    !!!cp ('t168');
5295                  }                  }
# Line 4000  sub _tree_construction_main ($) { Line 5301  sub _tree_construction_main ($) {
5301                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5302                                    
5303                  !!!next-token;                  !!!next-token;
5304                  redo B;                  next B;
5305                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5306                  !!!cp ('t169');                  !!!cp ('t169');
5307                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5308                                    text => $token->{tag_name}, token => $token);
5309                  ## Ignore the token                  ## Ignore the token
5310                  !!!next-token;                  !!!next-token;
5311                  redo B;                  next B;
5312                } else {                } else {
5313                  !!!cp ('t170');                  !!!cp ('t170');
5314                  #                  #
# Line 4015  sub _tree_construction_main ($) { Line 5317  sub _tree_construction_main ($) {
5317                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5318                  ## have a table element in table scope                  ## have a table element in table scope
5319                  my $i;                  my $i;
5320                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5321                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5322                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5323                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
5324                      $i = $_;                        !!!cp ('t171');
5325                      last INSCOPE;                        $i = $_;
5326                    } elsif ({                        last INSCOPE;
5327                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5328                             }->{$node->[1]}) {                        !!!cp ('t172');
5329                      !!!cp ('t172');                        last;
5330                      last INSCOPE;                      }
5331                    }                    }
5332    
5333                      !!!cp ('t173');
5334                      !!!parse-error (type => 'unmatched end tag',
5335                                      text => $token->{tag_name}, token => $token);
5336                      ## Ignore the token
5337                      !!!next-token;
5338                      next B;
5339                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5340                                    
5341                  ## generate implied end tags                  ## generate implied end tags
5342                  while ({                  while ($self->{open_elements}->[-1]->[1]
5343                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5344                    !!!cp ('t174');                    !!!cp ('t174');
5345                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5346                  }                  }
5347                                    
5348                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5349                    !!!cp ('t175');                    !!!cp ('t175');
5350                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5351                                      text => $self->{open_elements}->[-1]->[0]
5352                                          ->manakai_local_name,
5353                                      token => $token);
5354                  } else {                  } else {
5355                    !!!cp ('t176');                    !!!cp ('t176');
5356                  }                  }
# Line 4058  sub _tree_construction_main ($) { Line 5362  sub _tree_construction_main ($) {
5362                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5363                                    
5364                  !!!next-token;                  !!!next-token;
5365                  redo B;                  next B;
5366                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5367                  !!!cp ('t177');                  !!!cp ('t177');
5368                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5369                                    text => $token->{tag_name}, token => $token);
5370                  ## Ignore the token                  ## Ignore the token
5371                  !!!next-token;                  !!!next-token;
5372                  redo B;                  next B;
5373                } else {                } else {
5374                  !!!cp ('t178');                  !!!cp ('t178');
5375                  #                  #
# Line 4077  sub _tree_construction_main ($) { Line 5382  sub _tree_construction_main ($) {
5382                ## have an element in table scope                ## have an element in table scope
5383                my $i;                my $i;
5384                my $tn;                my $tn;
5385                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5386                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5387                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5388                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5389                    $i = $_;                      !!!cp ('t179');
5390                    last INSCOPE;                      $i = $_;
5391                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
5392                    !!!cp ('t180');                      ## Close the cell
5393                    $tn = $node->[1];                      !!!back-token; # </x>
5394                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5395                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
5396                  } elsif ({                                column => $token->{column}};
5397                            table => 1, html => 1,                      next B;
5398                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5399                    !!!cp ('t181');                      !!!cp ('t180');
5400                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
5401                        ## NOTE: There is exactly one |td| or |th| element
5402                        ## in scope in the stack of open elements by definition.
5403                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5404                        ## ISSUE: Can this be reached?
5405                        !!!cp ('t181');
5406                        last;
5407                      }
5408                  }                  }
5409                } # INSCOPE  
               unless (defined $i) {  
5410                  !!!cp ('t182');                  !!!cp ('t182');
5411                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5412                        text => $token->{tag_name}, token => $token);
5413                  ## Ignore the token                  ## Ignore the token
5414                  !!!next-token;                  !!!next-token;
5415                  redo B;                  next B;
5416                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5417              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5418                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5419                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5420                                  token => $token);
5421    
5422                ## As if </caption>                ## As if </caption>
5423                ## have a table element in table scope                ## have a table element in table scope
5424                my $i;                my $i;
5425                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5426                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5427                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5428                    !!!cp ('t184');                    !!!cp ('t184');
5429                    $i = $_;                    $i = $_;
5430                    last INSCOPE;                    last INSCOPE;
5431                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5432                    !!!cp ('t185');                    !!!cp ('t185');
5433                    last INSCOPE;                    last INSCOPE;
5434                  }                  }
5435                } # INSCOPE                } # INSCOPE
5436                unless (defined $i) {                unless (defined $i) {
5437                  !!!cp ('t186');                  !!!cp ('t186');
5438                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag',
5439                                    text => 'caption', token => $token);
5440                  ## Ignore the token                  ## Ignore the token
5441                  !!!next-token;                  !!!next-token;
5442                  redo B;                  next B;
5443                }                }
5444                                
5445                ## generate implied end tags                ## generate implied end tags
5446                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5447                  !!!cp ('t187');                  !!!cp ('t187');
5448                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5449                }                }
5450    
5451                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5452                  !!!cp ('t188');                  !!!cp ('t188');
5453                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5454                                    text => $self->{open_elements}->[-1]->[0]
5455                                        ->manakai_local_name,
5456                                    token => $token);
5457                } else {                } else {
5458                  !!!cp ('t189');                  !!!cp ('t189');
5459                }                }
# Line 4159  sub _tree_construction_main ($) { Line 5465  sub _tree_construction_main ($) {
5465                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5466    
5467                ## reprocess                ## reprocess
5468                redo B;                next B;
5469              } elsif ({              } elsif ({
5470                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5471                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5472                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5473                  !!!cp ('t190');                  !!!cp ('t190');
5474                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5475                                    text => $token->{tag_name}, token => $token);
5476                  ## Ignore the token                  ## Ignore the token
5477                  !!!next-token;                  !!!next-token;
5478                  redo B;                  next B;
5479                } else {                } else {
5480                  !!!cp ('t191');                  !!!cp ('t191');
5481                  #                  #
# Line 4179  sub _tree_construction_main ($) { Line 5486  sub _tree_construction_main ($) {
5486                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5487                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5488                !!!cp ('t192');                !!!cp ('t192');
5489                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5490                                  text => $token->{tag_name}, token => $token);
5491                ## Ignore the token                ## Ignore the token
5492                !!!next-token;                !!!next-token;
5493                redo B;                next B;
5494              } else {              } else {
5495                !!!cp ('t193');                !!!cp ('t193');
5496                #                #
5497              }              }
5498          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5499            for my $entry (@{$self->{open_elements}}) {
5500              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5501                !!!cp ('t75');
5502                !!!parse-error (type => 'in body:#eof', token => $token);
5503                last;
5504              }
5505            }
5506    
5507            ## Stop parsing.
5508            last B;
5509        } else {        } else {
5510          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5511        }        }
# Line 4196  sub _tree_construction_main ($) { Line 5515  sub _tree_construction_main ($) {
5515      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5516        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5517          if (not $open_tables->[-1]->[1] and # tainted          if (not $open_tables->[-1]->[1] and # tainted
5518              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              $token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
5519            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5520                                
5521            unless (length $token->{data}) {            unless (length $token->{data}) {
5522              !!!cp ('t194');              !!!cp ('t194');
5523              !!!next-token;              !!!next-token;
5524              redo B;              next B;
5525            } else {            } else {
5526              !!!cp ('t195');              !!!cp ('t195');
5527            }            }
5528          }          }
5529    
5530              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5531    
5532              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5533              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4216  sub _tree_construction_main ($) { Line 5535  sub _tree_construction_main ($) {
5535              ## result in a new Text node.              ## result in a new Text node.
5536              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5537                            
5538              if ({              if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
                  table => 1, tbody => 1, tfoot => 1,  
                  thead => 1, tr => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5539                # MUST                # MUST
5540                my $foster_parent_element;                my $foster_parent_element;
5541                my $next_sibling;                my $next_sibling;
5542                my $prev_sibling;                my $prev_sibling;
5543                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5544                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5545                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5546                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5547                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4260  sub _tree_construction_main ($) { Line 5576  sub _tree_construction_main ($) {
5576          }          }
5577                            
5578          !!!next-token;          !!!next-token;
5579          redo B;          next B;
5580        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5581              if ({          if ({
5582                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5583                   th => 1, td => 1,               th => 1, td => 1,
5584                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5585                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5586                  ## Clear back to table context              ## Clear back to table context
5587                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5588                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5589                    !!!cp ('t201');                !!!cp ('t201');
5590                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5591                  }              }
5592                                
5593                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5594                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5595                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5596                }            }
5597              
5598                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5599                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5600                    !!!cp ('t202');                !!!cp ('t202');
5601                    !!!parse-error (type => 'missing start tag:tr');                !!!parse-error (type => 'missing start tag:tr', token => $token);
5602                  }              }
5603                                    
5604                  ## Clear back to table body context              ## Clear back to table body context
5605                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5606                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5607                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5608                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5609                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5610                    pop @{$self->{open_elements}};              }
                 }  
5611                                    
5612                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5613                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5614                    !!!cp ('t204');                    !!!cp ('t204');
5615                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5616                      !!!nack ('t204');
5617                    !!!next-token;                    !!!next-token;
5618                    redo B;                    next B;
5619                  } else {                  } else {
5620                    !!!cp ('t205');                    !!!cp ('t205');
5621                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5622                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5623                  }                  }
5624                } else {                } else {
# Line 4310  sub _tree_construction_main ($) { Line 5626  sub _tree_construction_main ($) {
5626                }                }
5627    
5628                ## Clear back to table row context                ## Clear back to table row context
5629                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5630                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5631                  !!!cp ('t207');                  !!!cp ('t207');
5632                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5633                }                }
5634                                
5635                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5636                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5637    
5638                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5639                                
5640                  !!!nack ('t207.1');
5641                !!!next-token;                !!!next-token;
5642                redo B;                next B;
5643              } elsif ({              } elsif ({
5644                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5645                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4335  sub _tree_construction_main ($) { Line 5651  sub _tree_construction_main ($) {
5651                  my $i;                  my $i;
5652                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5653                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5654                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5655                      !!!cp ('t208');                      !!!cp ('t208');
5656                      $i = $_;                      $i = $_;
5657                      last INSCOPE;                      last INSCOPE;
5658                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5659                      !!!cp ('t209');                      !!!cp ('t209');
5660                      last INSCOPE;                      last INSCOPE;
5661                    }                    }
5662                  } # INSCOPE                  } # INSCOPE
5663                  unless (defined $i) {                  unless (defined $i) {
5664                   !!!cp ('t210');                    !!!cp ('t210');
5665  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5666                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag',
5667                                      text => $token->{tag_name}, token => $token);
5668                    ## Ignore the token                    ## Ignore the token
5669                      !!!nack ('t210.1');
5670                    !!!next-token;                    !!!next-token;
5671                    redo B;                    next B;
5672                  }                  }
5673                                    
5674                  ## Clear back to table row context                  ## Clear back to table row context
5675                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5676                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5677                    !!!cp ('t211');                    !!!cp ('t211');
5678                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5679                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4372  sub _tree_construction_main ($) { Line 5684  sub _tree_construction_main ($) {
5684                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5685                    !!!cp ('t212');                    !!!cp ('t212');
5686                    ## reprocess                    ## reprocess
5687                    redo B;                    !!!ack-later;
5688                      next B;
5689                  } else {                  } else {
5690                    !!!cp ('t213');                    !!!cp ('t213');
5691                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4384  sub _tree_construction_main ($) { Line 5697  sub _tree_construction_main ($) {
5697                  my $i;                  my $i;
5698                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5699                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5700                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5701                      !!!cp ('t214');                      !!!cp ('t214');
5702                      $i = $_;                      $i = $_;
5703                      last INSCOPE;                      last INSCOPE;
5704                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5705                      !!!cp ('t215');                      !!!cp ('t215');
5706                      last INSCOPE;                      last INSCOPE;
5707                    }                    }
5708                  } # INSCOPE                  } # INSCOPE
5709                  unless (defined $i) {                  unless (defined $i) {
5710                    !!!cp ('t216');                    !!!cp ('t216');
5711  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5712                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5713                                      text => $token->{tag_name}, token => $token);
5714                    ## Ignore the token                    ## Ignore the token
5715                      !!!nack ('t216.1');
5716                    !!!next-token;                    !!!next-token;
5717                    redo B;                    next B;
5718                  }                  }
5719    
5720                  ## Clear back to table body context                  ## Clear back to table body context
5721                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5722                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5723                    !!!cp ('t217');                    !!!cp ('t217');
5724                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5725                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4431  sub _tree_construction_main ($) { Line 5741  sub _tree_construction_main ($) {
5741    
5742                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5743                  ## Clear back to table context                  ## Clear back to table context
5744                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5745                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5746                    !!!cp ('t219');                    !!!cp ('t219');
5747                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5748                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5749                  }                  }
5750                                    
5751                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5752                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5753                  ## reprocess                  ## reprocess
5754                  redo B;                  !!!ack-later;
5755                    next B;
5756                } elsif ({                } elsif ({
5757                          caption => 1,                          caption => 1,
5758                          colgroup => 1,                          colgroup => 1,
5759                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5760                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5761                  ## Clear back to table context                  ## Clear back to table context
5762                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5763                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5764                    !!!cp ('t220');                    !!!cp ('t220');
5765                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5766                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4458  sub _tree_construction_main ($) { Line 5769  sub _tree_construction_main ($) {
5769                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5770                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5771                                    
5772                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5773                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5774                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5775                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4467  sub _tree_construction_main ($) { Line 5778  sub _tree_construction_main ($) {
5778                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5779                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5780                  !!!next-token;                  !!!next-token;
5781                  redo B;                  !!!nack ('t220.1');
5782                    next B;
5783                } else {                } else {
5784                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5785                }                }
5786              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5787                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5788                                  text => $self->{open_elements}->[-1]->[0]
5789                                      ->manakai_local_name,
5790                                  token => $token);
5791    
5792                ## As if </table>                ## As if </table>
5793                ## have a table element in table scope                ## have a table element in table scope
5794                my $i;                my $i;
5795                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5796                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5797                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5798                    !!!cp ('t221');                    !!!cp ('t221');
5799                    $i = $_;                    $i = $_;
5800                    last INSCOPE;                    last INSCOPE;
5801                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5802                    !!!cp ('t222');                    !!!cp ('t222');
5803                    last INSCOPE;                    last INSCOPE;
5804                  }                  }
# Line 4494  sub _tree_construction_main ($) { Line 5806  sub _tree_construction_main ($) {
5806                unless (defined $i) {                unless (defined $i) {
5807                  !!!cp ('t223');                  !!!cp ('t223');
5808  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5809                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5810                                    token => $token);
5811                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5812                    !!!nack ('t223.1');
5813                  !!!next-token;                  !!!next-token;
5814                  redo B;                  next B;
5815                }                }
5816                                
5817    ## TODO: Followings are removed from the latest spec.
5818                ## generate implied end tags                ## generate implied end tags
5819                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5820                  !!!cp ('t224');                  !!!cp ('t224');
5821                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5822                }                }
5823    
5824                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5825                  !!!cp ('t225');                  !!!cp ('t225');
5826  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5827                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5828                                    text => $self->{open_elements}->[-1]->[0]
5829                                        ->manakai_local_name,
5830                                    token => $token);
5831                } else {                } else {
5832                  !!!cp ('t226');                  !!!cp ('t226');
5833                }                }
# Line 4521  sub _tree_construction_main ($) { Line 5837  sub _tree_construction_main ($) {
5837    
5838                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5839    
5840                ## reprocess            ## reprocess
5841                redo B;            !!!ack-later;
5842              next B;
5843          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5844            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5845              !!!cp ('t227.8');              !!!cp ('t227.8');
5846              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5847              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5848              redo B;              next B;
5849            } else {            } else {
5850              !!!cp ('t227.7');              !!!cp ('t227.7');
5851              #              #
# Line 4538  sub _tree_construction_main ($) { Line 5855  sub _tree_construction_main ($) {
5855              !!!cp ('t227.6');              !!!cp ('t227.6');
5856              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5857              $script_start_tag->();              $script_start_tag->();
5858              redo B;              next B;
5859            } else {            } else {
5860              !!!cp ('t227.5');              !!!cp ('t227.5');
5861              #              #
# Line 4549  sub _tree_construction_main ($) { Line 5866  sub _tree_construction_main ($) {
5866                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5867                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5868                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5869                  !!!parse-error (type => 'in table:'.$token->{tag_name});                  !!!parse-error (type => 'in table',
5870                                    text => $token->{tag_name}, token => $token);
5871    
5872                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5873    
5874                  ## TODO: form element pointer                  ## TODO: form element pointer
5875    
5876                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5877    
5878                  !!!next-token;                  !!!next-token;
5879                  redo B;                  !!!ack ('t227.2.1');
5880                    next B;
5881                } else {                } else {
5882                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5883                  #                  #
# Line 4576  sub _tree_construction_main ($) { Line 5895  sub _tree_construction_main ($) {
5895            #            #
5896          }          }
5897    
5898          !!!parse-error (type => 'in table:'.$token->{tag_name});          !!!parse-error (type => 'in table', text => $token->{tag_name},
5899                            token => $token);
5900    
5901          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5902          #          #
# Line 4587  sub _tree_construction_main ($) { Line 5907  sub _tree_construction_main ($) {
5907                my $i;                my $i;
5908                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5909                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5910                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5911                    !!!cp ('t228');                    !!!cp ('t228');
5912                    $i = $_;                    $i = $_;
5913                    last INSCOPE;                    last INSCOPE;
5914                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5915                    !!!cp ('t229');                    !!!cp ('t229');
5916                    last INSCOPE;                    last INSCOPE;
5917                  }                  }
5918                } # INSCOPE                } # INSCOPE
5919                unless (defined $i) {                unless (defined $i) {
5920                  !!!cp ('t230');                  !!!cp ('t230');
5921                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5922                                    text => $token->{tag_name}, token => $token);
5923                  ## Ignore the token                  ## Ignore the token
5924                    !!!nack ('t230.1');
5925                  !!!next-token;                  !!!next-token;
5926                  redo B;                  next B;
5927                } else {                } else {
5928                  !!!cp ('t232');                  !!!cp ('t232');
5929                }                }
5930    
5931                ## Clear back to table row context                ## Clear back to table row context
5932                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5933                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5934                  !!!cp ('t231');                  !!!cp ('t231');
5935  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5936                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4620  sub _tree_construction_main ($) { Line 5939  sub _tree_construction_main ($) {
5939                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5940                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5941                !!!next-token;                !!!next-token;
5942                redo B;                !!!nack ('t231.1');
5943                  next B;
5944              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5945                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5946                  ## As if </tr>                  ## As if </tr>
# Line 4628  sub _tree_construction_main ($) { Line 5948  sub _tree_construction_main ($) {
5948                  my $i;                  my $i;
5949                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5950                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5951                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5952                      !!!cp ('t233');                      !!!cp ('t233');
5953                      $i = $_;                      $i = $_;
5954                      last INSCOPE;                      last INSCOPE;
5955                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5956                      !!!cp ('t234');                      !!!cp ('t234');
5957                      last INSCOPE;                      last INSCOPE;
5958                    }                    }
# Line 4642  sub _tree_construction_main ($) { Line 5960  sub _tree_construction_main ($) {
5960                  unless (defined $i) {                  unless (defined $i) {
5961                    !!!cp ('t235');                    !!!cp ('t235');
5962  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5963                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag',
5964                                      text => $token->{type}, token => $token);
5965                    ## Ignore the token                    ## Ignore the token
5966                      !!!nack ('t236.1');
5967                    !!!next-token;                    !!!next-token;
5968                    redo B;                    next B;
5969                  }                  }
5970                                    
5971                  ## Clear back to table row context                  ## Clear back to table row context
5972                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5973                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5974                    !!!cp ('t236');                    !!!cp ('t236');
5975  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5976                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4667  sub _tree_construction_main ($) { Line 5986  sub _tree_construction_main ($) {
5986                  my $i;                  my $i;
5987                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5988                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5989                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5990                      !!!cp ('t237');                      !!!cp ('t237');
5991                      $i = $_;                      $i = $_;
5992                      last INSCOPE;                      last INSCOPE;
5993                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5994                      !!!cp ('t238');                      !!!cp ('t238');
5995                      last INSCOPE;                      last INSCOPE;
5996                    }                    }
5997                  } # INSCOPE                  } # INSCOPE
5998                  unless (defined $i) {                  unless (defined $i) {
5999                    !!!cp ('t239');                    !!!cp ('t239');
6000                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
6001                                      text => $token->{tag_name}, token => $token);
6002                    ## Ignore the token                    ## Ignore the token
6003                      !!!nack ('t239.1');
6004                    !!!next-token;                    !!!next-token;
6005                    redo B;                    next B;
6006                  }                  }
6007                                    
6008                  ## Clear back to table body context                  ## Clear back to table body context
6009                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6010                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6011                    !!!cp ('t240');                    !!!cp ('t240');
6012                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6013                  }                  }
# Line 4717  sub _tree_construction_main ($) { Line 6033  sub _tree_construction_main ($) {
6033                my $i;                my $i;
6034                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6035                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6036                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
6037                    !!!cp ('t241');                    !!!cp ('t241');
6038                    $i = $_;                    $i = $_;
6039                    last INSCOPE;                    last INSCOPE;
6040                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6041                    !!!cp ('t242');                    !!!cp ('t242');
6042                    last INSCOPE;                    last INSCOPE;
6043                  }                  }
6044                } # INSCOPE                } # INSCOPE
6045                unless (defined $i) {                unless (defined $i) {
6046                  !!!cp ('t243');                  !!!cp ('t243');
6047                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6048                                    text => $token->{tag_name}, token => $token);
6049                  ## Ignore the token                  ## Ignore the token
6050                    !!!nack ('t243.1');
6051                  !!!next-token;                  !!!next-token;
6052                  redo B;                  next B;
6053                }                }
6054                                    
6055                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4742  sub _tree_construction_main ($) { Line 6058  sub _tree_construction_main ($) {
6058                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6059                                
6060                !!!next-token;                !!!next-token;
6061                redo B;                next B;
6062              } elsif ({              } elsif ({
6063                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6064                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4752  sub _tree_construction_main ($) { Line 6068  sub _tree_construction_main ($) {
6068                  my $i;                  my $i;
6069                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6070                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6071                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6072                      !!!cp ('t247');                      !!!cp ('t247');
6073                      $i = $_;                      $i = $_;
6074                      last INSCOPE;                      last INSCOPE;
6075                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6076                      !!!cp ('t248');                      !!!cp ('t248');
6077                      last INSCOPE;                      last INSCOPE;
6078                    }                    }
6079                  } # INSCOPE                  } # INSCOPE
6080                    unless (defined $i) {                    unless (defined $i) {
6081                      !!!cp ('t249');                      !!!cp ('t249');
6082                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
6083                                        text => $token->{tag_name}, token => $token);
6084                      ## Ignore the token                      ## Ignore the token
6085                        !!!nack ('t249.1');
6086                      !!!next-token;                      !!!next-token;
6087                      redo B;                      next B;
6088                    }                    }
6089                                    
6090                  ## As if </tr>                  ## As if </tr>
# Line 4776  sub _tree_construction_main ($) { Line 6092  sub _tree_construction_main ($) {
6092                  my $i;                  my $i;
6093                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6094                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6095                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6096                      !!!cp ('t250');                      !!!cp ('t250');
6097                      $i = $_;                      $i = $_;
6098                      last INSCOPE;                      last INSCOPE;
6099                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6100                      !!!cp ('t251');                      !!!cp ('t251');
6101                      last INSCOPE;                      last INSCOPE;
6102                    }                    }
6103                  } # INSCOPE                  } # INSCOPE
6104                    unless (defined $i) {                    unless (defined $i) {
6105                      !!!cp ('t252');                      !!!cp ('t252');
6106                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
6107                                        text => 'tr', token => $token);
6108                      ## Ignore the token                      ## Ignore the token
6109                        !!!nack ('t252.1');
6110                      !!!next-token;                      !!!next-token;
6111                      redo B;                      next B;
6112                    }                    }
6113                                    
6114                  ## Clear back to table row context                  ## Clear back to table row context
6115                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6116                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6117                    !!!cp ('t253');                    !!!cp ('t253');
6118  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
6119                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4813  sub _tree_construction_main ($) { Line 6128  sub _tree_construction_main ($) {
6128                my $i;                my $i;
6129                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6130                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6131                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6132                    !!!cp ('t254');                    !!!cp ('t254');
6133                    $i = $_;                    $i = $_;
6134                    last INSCOPE;                    last INSCOPE;
6135                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6136                    !!!cp ('t255');                    !!!cp ('t255');
6137                    last INSCOPE;                    last INSCOPE;
6138                  }                  }
6139                } # INSCOPE                } # INSCOPE
6140                unless (defined $i) {                unless (defined $i) {
6141                  !!!cp ('t256');                  !!!cp ('t256');
6142                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6143                                    text => $token->{tag_name}, token => $token);
6144                  ## Ignore the token                  ## Ignore the token
6145                    !!!nack ('t256.1');
6146                  !!!next-token;                  !!!next-token;
6147                  redo B;                  next B;
6148                }                }
6149    
6150                ## Clear back to table body context                ## Clear back to table body context
6151                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6152                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
6153                  !!!cp ('t257');                  !!!cp ('t257');
6154  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
6155                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4843  sub _tree_construction_main ($) { Line 6157  sub _tree_construction_main ($) {
6157    
6158                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6159                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6160                  !!!nack ('t257.1');
6161                !!!next-token;                !!!next-token;
6162                redo B;                next B;
6163              } elsif ({              } elsif ({
6164                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6165                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6166                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6167                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6168                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6169                !!!cp ('t258');            !!!cp ('t258');
6170                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6171                ## Ignore the token                            text => $token->{tag_name}, token => $token);
6172                !!!next-token;            ## Ignore the token
6173                redo B;            !!!nack ('t258.1');
6174               !!!next-token;
6175              next B;
6176          } else {          } else {
6177            !!!cp ('t259');            !!!cp ('t259');
6178            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
6179                              text => $token->{tag_name}, token => $token);
6180    
6181            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6182            #            #
6183          }          }
6184          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6185            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6186                    @{$self->{open_elements}} == 1) { # redundant, maybe
6187              !!!parse-error (type => 'in body:#eof', token => $token);
6188              !!!cp ('t259.1');
6189              #
6190            } else {
6191              !!!cp ('t259.2');
6192              #
6193            }
6194    
6195            ## Stop parsing
6196            last B;
6197        } else {        } else {
6198          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6199        }        }
6200      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {      } elsif ($self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
6201            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6202              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {              if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6203                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6204                unless (length $token->{data}) {                unless (length $token->{data}) {
6205                  !!!cp ('t260');                  !!!cp ('t260');
6206                  !!!next-token;                  !!!next-token;
6207                  redo B;                  next B;
6208                }                }
6209              }              }
6210                            
# Line 4882  sub _tree_construction_main ($) { Line 6213  sub _tree_construction_main ($) {
6213            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6214              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6215                !!!cp ('t262');                !!!cp ('t262');
6216                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6217                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6218                  !!!ack ('t262.1');
6219                !!!next-token;                !!!next-token;
6220                redo B;                next B;
6221              } else {              } else {
6222                !!!cp ('t263');                !!!cp ('t263');
6223                #                #
6224              }              }
6225            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6226              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6227                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6228                  !!!cp ('t264');                  !!!cp ('t264');
6229                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
6230                                    text => 'colgroup', token => $token);
6231                  ## Ignore the token                  ## Ignore the token
6232                  !!!next-token;                  !!!next-token;
6233                  redo B;                  next B;
6234                } else {                } else {
6235                  !!!cp ('t265');                  !!!cp ('t265');
6236                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6237                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6238                  !!!next-token;                  !!!next-token;
6239                  redo B;                              next B;            
6240                }                }
6241              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6242                !!!cp ('t266');                !!!cp ('t266');
6243                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
6244                                  text => 'col', token => $token);
6245                ## Ignore the token                ## Ignore the token
6246                !!!next-token;                !!!next-token;
6247                redo B;                next B;
6248              } else {              } else {
6249                !!!cp ('t267');                !!!cp ('t267');
6250                #                #
6251              }              }
6252            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6253              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6254            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6255              !!!cp ('t270.2');
6256              ## Stop parsing.
6257              last B;
6258            } else {
6259              ## NOTE: As if </colgroup>.
6260              !!!cp ('t270.1');
6261              pop @{$self->{open_elements}}; # colgroup
6262              $self->{insertion_mode} = IN_TABLE_IM;
6263              ## Reprocess.
6264              next B;
6265            }
6266          } else {
6267            die "$0: $token->{type}: Unknown token type";
6268          }
6269    
6270            ## As if </colgroup>            ## As if </colgroup>
6271            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6272              !!!cp ('t269');              !!!cp ('t269');
6273              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
6274                !!!parse-error (type => 'unmatched end tag',
6275                                text => 'colgroup', token => $token);
6276              ## Ignore the token              ## Ignore the token
6277                !!!nack ('t269.1');
6278              !!!next-token;              !!!next-token;
6279              redo B;              next B;
6280            } else {            } else {
6281              !!!cp ('t270');              !!!cp ('t270');
6282              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6283              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6284                !!!ack-later;
6285              ## reprocess              ## reprocess
6286              redo B;              next B;
6287            }            }
6288      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6289        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6290          !!!cp ('t271');          !!!cp ('t271');
6291          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6292          !!!next-token;          !!!next-token;
6293          redo B;          next B;
6294        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6295              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6296                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6297                  !!!cp ('t272');              !!!cp ('t272');
6298                  ## As if </option>              ## As if </option>
6299                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6300                } else {            } else {
6301                  !!!cp ('t273');              !!!cp ('t273');
6302                }            }
6303    
6304                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6305                !!!next-token;            !!!nack ('t273.1');
6306                redo B;            !!!next-token;
6307              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6308                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6309                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6310                  ## As if </option>              !!!cp ('t274');
6311                  pop @{$self->{open_elements}};              ## As if </option>
6312                } else {              pop @{$self->{open_elements}};
6313                  !!!cp ('t275');            } else {
6314                }              !!!cp ('t275');
6315              }
6316    
6317                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6318                  !!!cp ('t276');              !!!cp ('t276');
6319                  ## As if </optgroup>              ## As if </optgroup>
6320                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6321                } else {            } else {
6322                  !!!cp ('t277');              !!!cp ('t277');
6323                }            }
6324    
6325                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6326                !!!next-token;            !!!nack ('t277.1');
6327                redo B;            !!!next-token;
6328          } elsif ($token->{tag_name} eq 'select' or            next B;
6329                   $token->{tag_name} eq 'input' or          } elsif ({
6330                       select => 1, input => 1, textarea => 1,
6331                     }->{$token->{tag_name}} or
6332                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6333                    {                    {
6334                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 4981  sub _tree_construction_main ($) { Line 6336  sub _tree_construction_main ($) {
6336                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6337                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6338            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6339            !!!parse-error (type => 'not closed:select');            !!!parse-error (type => 'not closed', text => 'select',
6340                              token => $token);
6341            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6342            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6343                ## have an element in table scope            ## have an element in table scope
6344                my $i;            my $i;
6345                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6346                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6347                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6348                    !!!cp ('t278');                !!!cp ('t278');
6349                    $i = $_;                $i = $_;
6350                    last INSCOPE;                last INSCOPE;
6351                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6352                            table => 1, html => 1,                !!!cp ('t279');
6353                           }->{$node->[1]}) {                last INSCOPE;
6354                    !!!cp ('t279');              }
6355                    last INSCOPE;            } # INSCOPE
6356                  }            unless (defined $i) {
6357                } # INSCOPE              !!!cp ('t280');
6358                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag',
6359                  !!!cp ('t280');                              text => 'select', token => $token);
6360                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6361                  ## Ignore the token              !!!nack ('t280.1');
6362                  !!!next-token;              !!!next-token;
6363                  redo B;              next B;
6364                }            }
6365                                
6366                !!!cp ('t281');            !!!cp ('t281');
6367                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6368    
6369                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6370    
6371            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
6372              !!!cp ('t281.2');              !!!nack ('t281.2');
6373              !!!next-token;              !!!next-token;
6374              redo B;              next B;
6375            } else {            } else {
6376              !!!cp ('t281.1');              !!!cp ('t281.1');
6377                !!!ack-later;
6378              ## Reprocess the token.              ## Reprocess the token.
6379              redo B;              next B;
6380            }            }
6381          } else {          } else {
6382            !!!cp ('t282');            !!!cp ('t282');
6383            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
6384                              text => $token->{tag_name}, token => $token);
6385            ## Ignore the token            ## Ignore the token
6386              !!!nack ('t282.1');
6387            !!!next-token;            !!!next-token;
6388            redo B;            next B;
6389          }          }
6390        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6391              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6392                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6393                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6394                  !!!cp ('t283');              !!!cp ('t283');
6395                  ## As if </option>              ## As if </option>
6396                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6397                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6398                  !!!cp ('t284');              !!!cp ('t284');
6399                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6400                } else {            } else {
6401                  !!!cp ('t285');              !!!cp ('t285');
6402                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6403                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6404                }              ## Ignore the token
6405                !!!next-token;            }
6406                redo B;            !!!nack ('t285.1');
6407              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6408                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6409                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6410                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6411                } else {              !!!cp ('t286');
6412                  !!!cp ('t287');              pop @{$self->{open_elements}};
6413                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
6414                  ## Ignore the token              !!!cp ('t287');
6415                }              !!!parse-error (type => 'unmatched end tag',
6416                !!!next-token;                              text => $token->{tag_name}, token => $token);
6417                redo B;              ## Ignore the token
6418              } elsif ($token->{tag_name} eq 'select') {            }
6419                ## have an element in table scope            !!!nack ('t287.1');
6420                my $i;            !!!next-token;
6421                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6422                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6423                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6424                    !!!cp ('t288');            my $i;
6425                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6426                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6427                  } elsif ({              if ($node->[1] & SELECT_EL) {
6428                            table => 1, html => 1,                !!!cp ('t288');
6429                           }->{$node->[1]}) {                $i = $_;
6430                    !!!cp ('t289');                last INSCOPE;
6431                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6432                  }                !!!cp ('t289');
6433                } # INSCOPE                last INSCOPE;
6434                unless (defined $i) {              }
6435                  !!!cp ('t290');            } # INSCOPE
6436                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
6437                  ## Ignore the token              !!!cp ('t290');
6438                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6439                  redo B;                              text => $token->{tag_name}, token => $token);
6440                }              ## Ignore the token
6441                !!!nack ('t290.1');
6442                !!!next-token;
6443                next B;
6444              }
6445                                
6446                !!!cp ('t291');            !!!cp ('t291');
6447                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6448    
6449                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6450    
6451                !!!next-token;            !!!nack ('t291.1');
6452                redo B;            !!!next-token;
6453              next B;
6454          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6455                   {                   {
6456                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6457                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6458                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6459  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6460                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6461                              text => $token->{tag_name}, token => $token);
6462                                
6463                ## have an element in table scope            ## have an element in table scope
6464                my $i;            my $i;
6465                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6466                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6467                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6468                    !!!cp ('t292');                !!!cp ('t292');
6469                    $i = $_;                $i = $_;
6470                    last INSCOPE;                last INSCOPE;
6471                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6472                            table => 1, html => 1,                !!!cp ('t293');
6473                           }->{$node->[1]}) {                last INSCOPE;
6474                    !!!cp ('t293');              }
6475                    last INSCOPE;            } # INSCOPE
6476                  }            unless (defined $i) {
6477                } # INSCOPE              !!!cp ('t294');
6478                unless (defined $i) {              ## Ignore the token
6479                  !!!cp ('t294');              !!!nack ('t294.1');
6480                  ## Ignore the token              !!!next-token;
6481                  !!!next-token;              next B;
6482                  redo B;            }
               }  
6483                                
6484                ## As if </select>            ## As if </select>
6485                ## have an element in table scope            ## have an element in table scope
6486                undef $i;            undef $i;
6487                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6488                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6489                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6490                    !!!cp ('t295');                !!!cp ('t295');
6491                    $i = $_;                $i = $_;
6492                    last INSCOPE;                last INSCOPE;
6493                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6494  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6495                    !!!cp ('t296');                !!!cp ('t296');
6496                    last INSCOPE;                last INSCOPE;
6497                  }              }
6498                } # INSCOPE            } # INSCOPE
6499                unless (defined $i) {            unless (defined $i) {
6500                  !!!cp ('t297');              !!!cp ('t297');
6501  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6502                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
6503                  ## Ignore the </select> token                              text => 'select', token => $token);
6504                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6505                  redo B;              !!!nack ('t297.1');
6506                }              !!!next-token; ## TODO: ok?
6507                next B;
6508              }
6509                                
6510                !!!cp ('t298');            !!!cp ('t298');
6511                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6512    
6513                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6514    
6515                ## reprocess            !!!ack-later;
6516                redo B;            ## reprocess
6517              next B;
6518          } else {          } else {
6519            !!!cp ('t299');            !!!cp ('t299');
6520            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
6521                              text => $token->{tag_name}, token => $token);
6522            ## Ignore the token            ## Ignore the token
6523              !!!nack ('t299.3');
6524            !!!next-token;            !!!next-token;
6525            redo B;            next B;
6526          }          }
6527          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6528            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6529                    @{$self->{open_elements}} == 1) { # redundant, maybe
6530              !!!cp ('t299.1');
6531              !!!parse-error (type => 'in body:#eof', token => $token);
6532            } else {
6533              !!!cp ('t299.2');
6534            }
6535    
6536            ## Stop parsing.
6537            last B;
6538        } else {        } else {
6539          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6540        }        }
6541      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {      } elsif ($self->{insertion_mode} & BODY_AFTER_IMS) {
6542        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6543          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6544            my $data = $1;            my $data = $1;
6545            ## As if in body            ## As if in body
6546            $reconstruct_active_formatting_elements->($insert_to_current);            $reconstruct_active_formatting_elements->($insert_to_current);
# Line 5172  sub _tree_construction_main ($) { Line 6550  sub _tree_construction_main ($) {
6550            unless (length $token->{data}) {            unless (length $token->{data}) {
6551              !!!cp ('t300');              !!!cp ('t300');
6552              !!!next-token;              !!!next-token;
6553              redo B;              next B;
6554            }            }
6555          }          }
6556                    
6557          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6558            !!!cp ('t301');            !!!cp ('t301');
6559            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
6560              #
           ## Reprocess in the "after body" insertion mode.  
6561          } else {          } else {
6562            !!!cp ('t302');            !!!cp ('t302');
6563              ## "after body" insertion mode
6564              !!!parse-error (type => 'after body:#text', token => $token);
6565              #
6566          }          }
           
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body:#character');  
6567    
6568          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6569          ## reprocess          ## reprocess
6570          redo B;          next B;
6571        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6572          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6573            !!!cp ('t303');            !!!cp ('t303');
6574            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6575                                        text => $token->{tag_name}, token => $token);
6576            ## Reprocess in the "after body" insertion mode.            #
6577          } else {          } else {
6578            !!!cp ('t304');            !!!cp ('t304');
6579              ## "after body" insertion mode
6580              !!!parse-error (type => 'after body',
6581                              text => $token->{tag_name}, token => $token);
6582              #
6583          }          }
6584    
         ## "after body" insertion mode  
         !!!parse-error (type => 'after body:'.$token->{tag_name});  
   
6585          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6586            !!!ack-later;
6587          ## reprocess          ## reprocess
6588          redo B;          next B;
6589        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6590          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6591            !!!cp ('t305');            !!!cp ('t305');
6592            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6593                              text => $token->{tag_name}, token => $token);
6594                        
6595            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6596            ## Reprocess in the "after body" insertion mode.            ## Reprocess.
6597              next B;
6598          } else {          } else {
6599            !!!cp ('t306');            !!!cp ('t306');
6600          }          }
# Line 5222  sub _tree_construction_main ($) { Line 6603  sub _tree_construction_main ($) {
6603          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6604            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6605              !!!cp ('t307');              !!!cp ('t307');
6606              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
6607                                text => 'html', token => $token);
6608              ## Ignore the token              ## Ignore the token
6609              !!!next-token;              !!!next-token;
6610              redo B;              next B;
6611            } else {            } else {
6612              !!!cp ('t308');              !!!cp ('t308');
6613              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6614              !!!next-token;              !!!next-token;
6615              redo B;              next B;
6616            }            }
6617          } else {          } else {
6618            !!!cp ('t309');            !!!cp ('t309');
6619            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
6620                              text => $token->{tag_name}, token => $token);
6621    
6622            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6623            ## reprocess            ## reprocess
6624            redo B;            next B;
6625          }          }
6626          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6627            !!!cp ('t309.2');
6628            ## Stop parsing
6629            last B;
6630        } else {        } else {
6631          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6632        }        }
6633      } elsif ($self->{insertion_mode} & FRAME_IMS) {      } elsif ($self->{insertion_mode} & FRAME_IMS) {
6634        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6635          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0C\x20]+)//) {
6636            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
6637                        
6638            unless (length $token->{data}) {            unless (length $token->{data}) {
6639              !!!cp ('t310');              !!!cp ('t310');
6640              !!!next-token;              !!!next-token;
6641              redo B;              next B;
6642            }            }
6643          }          }
6644                    
6645          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0C\x20]+//) {
6646            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6647              !!!cp ('t311');              !!!cp ('t311');
6648              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
6649            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6650              !!!cp ('t312');              !!!cp ('t312');
6651              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6652            } else { # "after html frameset"            } else { # "after after frameset"
6653              !!!cp ('t313');              !!!cp ('t313');
6654              !!!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');  
6655            }            }
6656                        
6657            ## Ignore the token.            ## Ignore the token.
# Line 5279  sub _tree_construction_main ($) { Line 6662  sub _tree_construction_main ($) {
6662              !!!cp ('t315');              !!!cp ('t315');
6663              !!!next-token;              !!!next-token;
6664            }            }
6665            redo B;            next B;
6666          }          }
6667                    
6668          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6669        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!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');  
         }  
   
6670          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6671              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6672            !!!cp ('t318');            !!!cp ('t318');
6673            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6674              !!!nack ('t318.1');
6675            !!!next-token;            !!!next-token;
6676            redo B;            next B;
6677          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6678                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6679            !!!cp ('t319');            !!!cp ('t319');
6680            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6681            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6682              !!!ack ('t319.1');
6683            !!!next-token;            !!!next-token;
6684            redo B;            next B;
6685          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6686            !!!cp ('t320');            !!!cp ('t320');
6687            ## NOTE: As if in body.            ## NOTE: As if in head.
6688            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6689            redo B;            next B;
6690    
6691              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6692              ## has no parse error.
6693          } else {          } else {
6694            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6695              !!!cp ('t321');              !!!cp ('t321');
6696              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
6697            } else {                              text => $token->{tag_name}, token => $token);
6698              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6699              !!!cp ('t322');              !!!cp ('t322');
6700              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
6701                                text => $token->{tag_name}, token => $token);
6702              } else { # "after after frameset"
6703                !!!cp ('t322.2');
6704                !!!parse-error (type => 'after after frameset',
6705                                text => $token->{tag_name}, token => $token);
6706            }            }
6707            ## Ignore the token            ## Ignore the token
6708              !!!nack ('t322.1');
6709            !!!next-token;            !!!next-token;
6710            redo B;            next B;
6711          }          }
6712        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!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');  
         }  
   
6713          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6714              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6715            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6716                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6717              !!!cp ('t325');              !!!cp ('t325');
6718              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6719                                text => $token->{tag_name}, token => $token);
6720              ## Ignore the token              ## Ignore the token
6721              !!!next-token;              !!!next-token;
6722            } else {            } else {
# Line 5350  sub _tree_construction_main ($) { Line 6726  sub _tree_construction_main ($) {
6726            }            }
6727    
6728            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6729                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6730              !!!cp ('t327');              !!!cp ('t327');
6731              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6732            } else {            } else {
6733              !!!cp ('t328');              !!!cp ('t328');
6734            }            }
6735            redo B;            next B;
6736          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6737                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6738            !!!cp ('t329');            !!!cp ('t329');
6739            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6740            !!!next-token;            !!!next-token;
6741            redo B;            next B;
6742          } else {          } else {
6743            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6744              !!!cp ('t330');              !!!cp ('t330');
6745              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/',
6746            } else {                              text => $token->{tag_name}, token => $token);
6747              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6748                !!!cp ('t330.1');
6749                !!!parse-error (type => 'after frameset:/',
6750                                text => $token->{tag_name}, token => $token);
6751              } else { # "after after html"
6752              !!!cp ('t331');              !!!cp ('t331');
6753              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after after frameset:/',
6754                                text => $token->{tag_name}, token => $token);
6755            }            }
6756            ## Ignore the token            ## Ignore the token
6757            !!!next-token;            !!!next-token;
6758            redo B;            next B;
6759            }
6760          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6761            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6762                    @{$self->{open_elements}} == 1) { # redundant, maybe
6763              !!!cp ('t331.1');
6764              !!!parse-error (type => 'in body:#eof', token => $token);
6765            } else {
6766              !!!cp ('t331.2');
6767          }          }
6768            
6769            ## Stop parsing
6770            last B;
6771        } else {        } else {
6772          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6773        }        }
# Line 5390  sub _tree_construction_main ($) { Line 6783  sub _tree_construction_main ($) {
6783          !!!cp ('t332');          !!!cp ('t332');
6784          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6785          $script_start_tag->();          $script_start_tag->();
6786          redo B;          next B;
6787        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6788          !!!cp ('t333');          !!!cp ('t333');
6789          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6790          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6791          redo B;          next B;
6792        } elsif ({        } elsif ({
6793                  base => 1, link => 1,                  base => 1, link => 1,
6794                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6795          !!!cp ('t334');          !!!cp ('t334');
6796          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6797          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6798          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6799            !!!ack ('t334.1');
6800          !!!next-token;          !!!next-token;
6801          redo B;          next B;
6802        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6803          ## NOTE: This is an "as if in head" code clone, only "-t" differs          ## NOTE: This is an "as if in head" code clone, only "-t" differs
6804          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6805          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.
6806    
6807          unless ($self->{confident}) {          unless ($self->{confident}) {
6808            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6809              !!!cp ('t335');              !!!cp ('t335');
6810                ## NOTE: Whether the encoding is supported or not is handled
6811                ## in the {change_encoding} callback.
6812              $self->{change_encoding}              $self->{change_encoding}
6813                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6814                            
6815              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6816                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6817                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6818                                           ->{has_reference});                                           ->{has_reference});
6819            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6820              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6821                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6822                      [\x09-\x0D\x20]*=                      [\x09\x0A\x0C\x0D\x20]*=
6823                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09\x0A\x0C\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6824                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09\x0A\x0C\x0D\x20][^\x09\x0A\x0C\x0D\x20\x3B]*))
6825                       /x) {
6826                !!!cp ('t336');                !!!cp ('t336');
6827                  ## NOTE: Whether the encoding is supported or not is handled
6828                  ## in the {change_encoding} callback.
6829                $self->{change_encoding}                $self->{change_encoding}
6830                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6831                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6832                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6833                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5453  sub _tree_construction_main ($) { Line 6851  sub _tree_construction_main ($) {
6851            }            }
6852          }          }
6853    
6854            !!!ack ('t338.1');
6855          !!!next-token;          !!!next-token;
6856          redo B;          next B;
6857        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6858          !!!cp ('t341');          !!!cp ('t341');
6859          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6860          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6861          redo B;          next B;
6862        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6863          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6864                                
6865          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6866              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6867            !!!cp ('t342');            !!!cp ('t342');
6868            ## Ignore the token            ## Ignore the token
6869          } else {          } else {
# Line 5478  sub _tree_construction_main ($) { Line 6877  sub _tree_construction_main ($) {
6877              }              }
6878            }            }
6879          }          }
6880            !!!nack ('t343.1');
6881          !!!next-token;          !!!next-token;
6882          redo B;          next B;
6883        } elsif ({        } elsif ({
6884                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6885                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6886                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6887                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6888                  pre => 1, listing => 1,                  pre => 1, listing => 1,
6889                    form => 1,
6890                    table => 1,
6891                    hr => 1,
6892                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6893            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6894              !!!cp ('t350');
6895              !!!parse-error (type => 'in form:form', token => $token);
6896              ## Ignore the token
6897              !!!nack ('t350.1');
6898              !!!next-token;
6899              next B;
6900            }
6901    
6902          ## has a p element in scope          ## has a p element in scope
6903          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6904            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6905              !!!cp ('t344');              !!!cp ('t344');
6906              !!!back-token;              !!!back-token; # <form>
6907              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6908              redo B;                        line => $token->{line}, column => $token->{column}};
6909            } elsif ({              next B;
6910                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6911              !!!cp ('t345');              !!!cp ('t345');
6912              last INSCOPE;              last INSCOPE;
6913            }            }
6914          } # INSCOPE          } # INSCOPE
6915                        
6916          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6917          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6918              !!!nack ('t346.1');
6919            !!!next-token;            !!!next-token;
6920            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6921              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5517  sub _tree_construction_main ($) { Line 6928  sub _tree_construction_main ($) {
6928            } else {            } else {
6929              !!!cp ('t348');              !!!cp ('t348');
6930            }            }
6931          } else {          } elsif ($token->{tag_name} eq 'form') {
6932            !!!cp ('t347');            !!!cp ('t347.1');
6933              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6934    
6935              !!!nack ('t347.2');
6936            !!!next-token;            !!!next-token;
6937          }          } elsif ($token->{tag_name} eq 'table') {
6938          redo B;            !!!cp ('t382');
6939        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6940          if (defined $self->{form_element}) {            
6941            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6942            !!!parse-error (type => 'in form:form');  
6943            ## Ignore the token            !!!nack ('t382.1');
6944              !!!next-token;
6945            } elsif ($token->{tag_name} eq 'hr') {
6946              !!!cp ('t386');
6947              pop @{$self->{open_elements}};
6948            
6949              !!!nack ('t386.1');
6950            !!!next-token;            !!!next-token;
           redo B;  
6951          } else {          } else {
6952            ## has a p element in scope            !!!nack ('t347.1');
           INSCOPE: for (reverse @{$self->{open_elements}}) {  
             if ($_->[1] eq 'p') {  
               !!!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];  
6953            !!!next-token;            !!!next-token;
           redo B;  
6954          }          }
6955        } elsif ($token->{tag_name} eq 'li') {          next B;
6956          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6957          ## has a p element in scope          ## has a p element in scope
6958          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6959            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6960              !!!cp ('t353');              !!!cp ('t353');
6961              !!!back-token;              !!!back-token; # <x>
6962              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6963              redo B;                        line => $token->{line}, column => $token->{column}};
6964            } elsif ({              next B;
6965                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6966              !!!cp ('t354');              !!!cp ('t354');
6967              last INSCOPE;              last INSCOPE;
6968            }            }
# Line 5571  sub _tree_construction_main ($) { Line 6971  sub _tree_construction_main ($) {
6971          ## Step 1          ## Step 1
6972          my $i = -1;          my $i = -1;
6973          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6974            my $li_or_dtdd = {li => {li => 1},
6975                              dt => {dt => 1, dd => 1},
6976                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6977          LI: {          LI: {
6978            ## Step 2            ## Step 2
6979            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6980              if ($i != -1) {              if ($i != -1) {
6981                !!!cp ('t355');                !!!cp ('t355');
6982                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6983                                $self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
6984                                      ->manakai_local_name,
6985                                  token => $token);
6986              } else {              } else {
6987                !!!cp ('t356');                !!!cp ('t356');
6988              }              }
# Line 5588  sub _tree_construction_main ($) { Line 6993  sub _tree_construction_main ($) {
6993            }            }
6994                        
6995            ## Step 3            ## Step 3
6996            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6997                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6998                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6999                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
7000                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
7001                  not ($node->[1] & DIV_EL)) {
7002              !!!cp ('t358');              !!!cp ('t358');
7003              last LI;              last LI;
7004            }            }
# Line 5604  sub _tree_construction_main ($) { Line 7010  sub _tree_construction_main ($) {
7010            redo LI;            redo LI;
7011          } # LI          } # LI
7012                        
7013          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7014          !!!next-token;          !!!nack ('t359.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!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});  
7015          !!!next-token;          !!!next-token;
7016          redo B;          next B;
7017        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
7018          ## has a p element in scope          ## has a p element in scope
7019          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
7020            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
7021              !!!cp ('t367');              !!!cp ('t367');
7022              !!!back-token;              !!!back-token; # <plaintext>
7023              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
7024              redo B;                        line => $token->{line}, column => $token->{column}};
7025            } elsif ({              next B;
7026                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
7027              !!!cp ('t368');              !!!cp ('t368');
7028              last INSCOPE;              last INSCOPE;
7029            }            }
7030          } # INSCOPE          } # INSCOPE
7031                        
7032          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7033                        
7034          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7035                        
7036            !!!nack ('t368.1');
7037          !!!next-token;          !!!next-token;
7038          redo B;          next B;
7039        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
7040          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7041            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
7042            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
7043              !!!cp ('t371');              !!!cp ('t371');
7044              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
7045                            
7046              !!!back-token;              !!!back-token; # <a>
7047              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
7048              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
7049                $formatting_end_tag->($token);
7050                            
7051              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
7052                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5720  sub _tree_construction_main ($) { Line 7071  sub _tree_construction_main ($) {
7071                        
7072          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7073    
7074          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7075          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7076    
7077            !!!nack ('t374.1');
7078          !!!next-token;          !!!next-token;
7079          redo B;          next B;
       } elsif ({  
                 b => 1, big => 1, em => 1, font => 1, i => 1,  
                 s => 1, small => 1, strile => 1,  
                 strong => 1, tt => 1, u => 1,  
                }->{$token->{tag_name}}) {  
         !!!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;  
7080        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
7081          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7082    
7083          ## has a |nobr| element in scope          ## has a |nobr| element in scope
7084          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7085            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7086            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
7087              !!!cp ('t376');              !!!cp ('t376');
7088              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
7089              !!!back-token;              !!!back-token; # <nobr>
7090              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7091              redo B;                        line => $token->{line}, column => $token->{column}};
7092            } elsif ({              next B;
7093                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7094              !!!cp ('t377');              !!!cp ('t377');
7095              last INSCOPE;              last INSCOPE;
7096            }            }
7097          } # INSCOPE          } # INSCOPE
7098                    
7099          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7100          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7101                    
7102            !!!nack ('t377.1');
7103          !!!next-token;          !!!next-token;
7104          redo B;          next B;
7105        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7106          ## has a button element in scope          ## has a button element in scope
7107          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7108            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7109            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7110              !!!cp ('t378');              !!!cp ('t378');
7111              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
7112              !!!back-token;              !!!back-token; # <button>
7113              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7114              redo B;                        line => $token->{line}, column => $token->{column}};
7115            } elsif ({              next B;
7116                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7117              !!!cp ('t379');              !!!cp ('t379');
7118              last INSCOPE;              last INSCOPE;
7119            }            }
# Line 5785  sub _tree_construction_main ($) { Line 7121  sub _tree_construction_main ($) {
7121                        
7122          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7123                        
7124          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7125    
7126          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
7127    
7128          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7129    
7130            !!!nack ('t379.1');
7131          !!!next-token;          !!!next-token;
7132          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;  
7133        } elsif ({        } elsif ({
7134                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7135                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7136                  image => 1,                  noembed => 1,
7137                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7138                    noscript => 0, ## TODO: 1 if scripting is enabled
7139                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7140          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7141            !!!cp ('t384');            !!!cp ('t381');
7142            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
7143          } else {          } else {
7144            !!!cp ('t385');            !!!cp ('t399');
7145          }          }
7146            ## NOTE: There is an "as if in body" code clone.
7147          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7148          $reconstruct_active_formatting_elements->($insert_to_current);          next B;
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         pop @{$self->{open_elements}};  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'hr') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!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;  
7149        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7150          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7151                    
7152          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7153            !!!cp ('t389');            !!!cp ('t389');
7154            ## Ignore the token            ## Ignore the token
7155              !!!nack ('t389'); ## NOTE: Not acknowledged.
7156            !!!next-token;            !!!next-token;
7157            redo B;            next B;
7158          } else {          } else {
7159              !!!ack ('t391.1');
7160    
7161            my $at = $token->{attributes};            my $at = $token->{attributes};
7162            my $form_attrs;            my $form_attrs;
7163            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5903  sub _tree_construction_main ($) { Line 7167  sub _tree_construction_main ($) {
7167            delete $at->{prompt};            delete $at->{prompt};
7168            my @tokens = (            my @tokens = (
7169                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7170                           attributes => $form_attrs},                           attributes => $form_attrs,
7171                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7172                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7173                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7174                            {type => START_TAG_TOKEN, tag_name => 'p',
7175                             line => $token->{line}, column => $token->{column}},
7176                            {type => START_TAG_TOKEN, tag_name => 'label',
7177                             line => $token->{line}, column => $token->{column}},
7178                         );                         );
7179            if ($prompt_attr) {            if ($prompt_attr) {
7180              !!!cp ('t390');              !!!cp ('t390');
7181              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7182                               #line => $token->{line}, column => $token->{column},
7183                              };
7184            } else {            } else {
7185              !!!cp ('t391');              !!!cp ('t391');
7186              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7187                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7188                               #line => $token->{line}, column => $token->{column},
7189                              }; # SHOULD
7190              ## TODO: make this configurable              ## TODO: make this configurable
7191            }            }
7192            push @tokens,            push @tokens,
7193                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7194                             line => $token->{line}, column => $token->{column}},
7195                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7196                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7197                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7198                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7199                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7200            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7201                             line => $token->{line}, column => $token->{column}},
7202                            {type => END_TAG_TOKEN, tag_name => 'form',
7203                             line => $token->{line}, column => $token->{column}};
7204            !!!back-token (@tokens);            !!!back-token (@tokens);
7205            redo B;            !!!next-token;
7206              next B;
7207          }          }
7208        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7209          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7210          my $el;          my $el;
7211          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7212                    
7213          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7214          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5940  sub _tree_construction_main ($) { Line 7217  sub _tree_construction_main ($) {
7217          $insert->($el);          $insert->($el);
7218                    
7219          my $text = '';          my $text = '';
7220            !!!nack ('t392.1');
7221          !!!next-token;          !!!next-token;
7222          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7223            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5970  sub _tree_construction_main ($) { Line 7248  sub _tree_construction_main ($) {
7248            ## Ignore the token            ## Ignore the token
7249          } else {          } else {
7250            !!!cp ('t398');            !!!cp ('t398');
7251            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7252          }          }
7253          !!!next-token;          !!!next-token;
7254            next B;
7255          } elsif ($token->{tag_name} eq 'rt' or
7256                   $token->{tag_name} eq 'rp') {
7257            ## has a |ruby| element in scope
7258            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7259              my $node = $self->{open_elements}->[$_];
7260              if ($node->[1] & RUBY_EL) {
7261                !!!cp ('t398.1');
7262                ## generate implied end tags
7263                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7264                  !!!cp ('t398.2');
7265                  pop @{$self->{open_elements}};
7266                }
7267                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7268                  !!!cp ('t398.3');
7269                  !!!parse-error (type => 'not closed',
7270                                  text => $self->{open_elements}->[-1]->[0]
7271                                      ->manakai_local_name,
7272                                  token => $token);
7273                  pop @{$self->{open_elements}}
7274                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7275                }
7276                last INSCOPE;
7277              } elsif ($node->[1] & SCOPING_EL) {
7278                !!!cp ('t398.4');
7279                last INSCOPE;
7280              }
7281            } # INSCOPE
7282    
7283            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7284    
7285            !!!nack ('t398.5');
7286            !!!next-token;
7287          redo B;          redo B;
7288        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7289                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!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');  
7290          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
7291    
7292          ## TODO: associate with $self->{form_element} if defined          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7293    
7294            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7295    
7296            ## "adjust foreign attributes" - done in insert-element-f
7297            
7298            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7299                    
7300          if ($self->{insertion_mode} & TABLE_IMS or          if ($self->{self_closing}) {
7301              $self->{insertion_mode} & BODY_TABLE_IMS or            pop @{$self->{open_elements}};
7302              $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {            !!!ack ('t398.1');
           !!!cp ('t400.1');  
           $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;  
7303          } else {          } else {
7304            !!!cp ('t400.2');            !!!cp ('t398.2');
7305            $self->{insertion_mode} = IN_SELECT_IM;            $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7306              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7307              ## mode, "in body" (not "in foreign content") secondary insertion
7308              ## mode, maybe.
7309          }          }
7310    
7311          !!!next-token;          !!!next-token;
7312          redo B;          next B;
7313        } elsif ({        } elsif ({
7314                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7315                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6010  sub _tree_construction_main ($) { Line 7317  sub _tree_construction_main ($) {
7317                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7318                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7319          !!!cp ('t401');          !!!cp ('t401');
7320          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
7321                            text => $token->{tag_name}, token => $token);
7322          ## Ignore the token          ## Ignore the token
7323            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7324          !!!next-token;          !!!next-token;
7325          redo B;          next B;
7326                    
7327          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7328        } else {        } else {
7329          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
7330              !!!cp ('t384');
7331              !!!parse-error (type => 'image', token => $token);
7332              $token->{tag_name} = 'img';
7333            } else {
7334              !!!cp ('t385');
7335            }
7336    
7337            ## NOTE: There is an "as if <br>" code clone.
7338          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7339                    
7340          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7341    
7342            if ({
7343                 applet => 1, marquee => 1, object => 1,
7344                }->{$token->{tag_name}}) {
7345              !!!cp ('t380');
7346              push @$active_formatting_elements, ['#marker', ''];
7347              !!!nack ('t380.1');
7348            } elsif ({
7349                      b => 1, big => 1, em => 1, font => 1, i => 1,
7350                      s => 1, small => 1, strile => 1,
7351                      strong => 1, tt => 1, u => 1,
7352                     }->{$token->{tag_name}}) {
7353              !!!cp ('t375');
7354              push @$active_formatting_elements, $self->{open_elements}->[-1];
7355              !!!nack ('t375.1');
7356            } elsif ($token->{tag_name} eq 'input') {
7357              !!!cp ('t388');
7358              ## TODO: associate with $self->{form_element} if defined
7359              pop @{$self->{open_elements}};
7360              !!!ack ('t388.2');
7361            } elsif ({
7362                      area => 1, basefont => 1, bgsound => 1, br => 1,
7363                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7364                      #image => 1,
7365                     }->{$token->{tag_name}}) {
7366              !!!cp ('t388.1');
7367              pop @{$self->{open_elements}};
7368              !!!ack ('t388.3');
7369            } elsif ($token->{tag_name} eq 'select') {
7370              ## TODO: associate with $self->{form_element} if defined
7371            
7372              if ($self->{insertion_mode} & TABLE_IMS or
7373                  $self->{insertion_mode} & BODY_TABLE_IMS or
7374                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7375                !!!cp ('t400.1');
7376                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7377              } else {
7378                !!!cp ('t400.2');
7379                $self->{insertion_mode} = IN_SELECT_IM;
7380              }
7381              !!!nack ('t400.3');
7382            } else {
7383              !!!nack ('t402');
7384            }
7385                    
7386          !!!next-token;          !!!next-token;
7387          redo B;          next B;
7388        }        }
7389      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7390        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7391          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7392              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7393            for (@{$self->{open_elements}}) {          INSCOPE: {
7394              unless ({            for (reverse @{$self->{open_elements}}) {
7395                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7396                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7397                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7398                      }->{$_->[1]}) {                last INSCOPE;
7399                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
7400                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
7401              } else {                last;
               !!!cp ('t404');  
7402              }              }
7403            }            }
7404    
7405            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7406                              text => $token->{tag_name}, token => $token);
7407              ## NOTE: Ignore the token.
7408            !!!next-token;            !!!next-token;
7409            redo B;            next B;
7410          } else {          } # INSCOPE
7411            !!!cp ('t405');  
7412            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          for (@{$self->{open_elements}}) {
7413            ## Ignore the token            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7414            !!!next-token;              !!!cp ('t403');
7415            redo B;              !!!parse-error (type => 'not closed',
7416                                text => $_->[0]->manakai_local_name,
7417                                token => $token);
7418                last;
7419              } else {
7420                !!!cp ('t404');
7421              }
7422          }          }
7423    
7424            $self->{insertion_mode} = AFTER_BODY_IM;
7425            !!!next-token;
7426            next B;
7427        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7428          if (@{$self->{open_elements}} > 1 and $self->{open_elements}->[1]->[1] eq 'body') {          ## TODO: Update this code.  It seems that the code below is not
7429            ## up-to-date, though it has same effect as speced.
7430            if (@{$self->{open_elements}} > 1 and
7431                $self->{open_elements}->[1]->[1] & BODY_EL) {
7432            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7433            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7434              !!!cp ('t406');              !!!cp ('t406');
7435              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
7436                                text => $self->{open_elements}->[1]->[0]
7437                                    ->manakai_local_name,
7438                                token => $token);
7439            } else {            } else {
7440              !!!cp ('t407');              !!!cp ('t407');
7441            }            }
7442            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7443            ## reprocess            ## reprocess
7444            redo B;            next B;
7445          } else {          } else {
7446            !!!cp ('t408');            !!!cp ('t408');
7447            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7448                              text => $token->{tag_name}, token => $token);
7449            ## Ignore the token            ## Ignore the token
7450            !!!next-token;            !!!next-token;
7451            redo B;            next B;
7452          }          }
7453        } elsif ({        } elsif ({
7454                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7455                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7456                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
7457                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7458                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7459                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7460          ## has an element in scope          ## has an element in scope
7461          my $i;          my $i;
7462          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7463            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7464            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7465              !!!cp ('t410');              !!!cp ('t410');
7466              $i = $_;              $i = $_;
7467              last INSCOPE;              last INSCOPE;
7468            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7469              !!!cp ('t411');              !!!cp ('t411');
7470              last INSCOPE;              last INSCOPE;
7471            }            }
# Line 6097  sub _tree_construction_main ($) { Line 7473  sub _tree_construction_main ($) {
7473    
7474          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7475            !!!cp ('t413');            !!!cp ('t413');
7476            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7477                              text => $token->{tag_name}, token => $token);
7478              ## NOTE: Ignore the token.
7479          } else {          } else {
7480            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7481            while ({            while ({
7482                      ## END_TAG_OPTIONAL_EL
7483                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7484                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7485                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7486                    p => 1,                    p => 1,
7487                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7488                      rp => 1,
7489                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7490              !!!cp ('t409');              !!!cp ('t409');
7491              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7492            }            }
7493    
7494            ## Step 2.            ## Step 2.
7495            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7496                      ne $token->{tag_name}) {
7497              !!!cp ('t412');              !!!cp ('t412');
7498              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7499                                text => $self->{open_elements}->[-1]->[0]
7500                                    ->manakai_local_name,
7501                                token => $token);
7502            } else {            } else {
7503              !!!cp ('t414');              !!!cp ('t414');
7504            }            }
# Line 6124  sub _tree_construction_main ($) { Line 7509  sub _tree_construction_main ($) {
7509            ## Step 4.            ## Step 4.
7510            $clear_up_to_marker->()            $clear_up_to_marker->()
7511                if {                if {
7512                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7513                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7514          }          }
7515          !!!next-token;          !!!next-token;
7516          redo B;          next B;
7517        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7518          undef $self->{form_element};          undef $self->{form_element};
7519    
# Line 6136  sub _tree_construction_main ($) { Line 7521  sub _tree_construction_main ($) {
7521          my $i;          my $i;
7522          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7523            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7524            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7525              !!!cp ('t418');              !!!cp ('t418');
7526              $i = $_;              $i = $_;
7527              last INSCOPE;              last INSCOPE;
7528            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7529              !!!cp ('t419');              !!!cp ('t419');
7530              last INSCOPE;              last INSCOPE;
7531            }            }
# Line 6151  sub _tree_construction_main ($) { Line 7533  sub _tree_construction_main ($) {
7533    
7534          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7535            !!!cp ('t421');            !!!cp ('t421');
7536            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7537                              text => $token->{tag_name}, token => $token);
7538              ## NOTE: Ignore the token.
7539          } else {          } else {
7540            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7541            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7542              !!!cp ('t417');              !!!cp ('t417');
7543              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7544            }            }
7545                        
7546            ## Step 2.            ## Step 2.
7547            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7548                      ne $token->{tag_name}) {
7549              !!!cp ('t417.1');              !!!cp ('t417.1');
7550              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7551                                text => $self->{open_elements}->[-1]->[0]
7552                                    ->manakai_local_name,
7553                                token => $token);
7554            } else {            } else {
7555              !!!cp ('t420');              !!!cp ('t420');
7556            }              }  
# Line 6174  sub _tree_construction_main ($) { Line 7560  sub _tree_construction_main ($) {
7560          }          }
7561    
7562          !!!next-token;          !!!next-token;
7563          redo B;          next B;
7564        } elsif ({        } elsif ({
7565                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7566                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6182  sub _tree_construction_main ($) { Line 7568  sub _tree_construction_main ($) {
7568          my $i;          my $i;
7569          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7570            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7571            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7572              !!!cp ('t423');              !!!cp ('t423');
7573              $i = $_;              $i = $_;
7574              last INSCOPE;              last INSCOPE;
7575            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7576              !!!cp ('t424');              !!!cp ('t424');
7577              last INSCOPE;              last INSCOPE;
7578            }            }
# Line 6199  sub _tree_construction_main ($) { Line 7580  sub _tree_construction_main ($) {
7580    
7581          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7582            !!!cp ('t425.1');            !!!cp ('t425.1');
7583            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7584                              text => $token->{tag_name}, token => $token);
7585              ## NOTE: Ignore the token.
7586          } else {          } else {
7587            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7588            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7589              !!!cp ('t422');              !!!cp ('t422');
7590              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7591            }            }
7592                        
7593            ## Step 2.            ## Step 2.
7594            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7595                      ne $token->{tag_name}) {
7596              !!!cp ('t425');              !!!cp ('t425');
7597              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
7598                                text => $token->{tag_name}, token => $token);
7599            } else {            } else {
7600              !!!cp ('t426');              !!!cp ('t426');
7601            }            }
# Line 6222  sub _tree_construction_main ($) { Line 7605  sub _tree_construction_main ($) {
7605          }          }
7606                    
7607          !!!next-token;          !!!next-token;
7608          redo B;          next B;
7609        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7610          ## has an element in scope          ## has an element in scope
7611          my $i;          my $i;
7612          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7613            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7614            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7615              !!!cp ('t410.1');              !!!cp ('t410.1');
7616              $i = $_;              $i = $_;
7617              last INSCOPE;              last INSCOPE;
7618            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7619              !!!cp ('t411.1');              !!!cp ('t411.1');
7620              last INSCOPE;              last INSCOPE;
7621            }            }
7622          } # INSCOPE          } # INSCOPE
7623    
7624          if (defined $i) {          if (defined $i) {
7625            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7626                      ne $token->{tag_name}) {
7627              !!!cp ('t412.1');              !!!cp ('t412.1');
7628              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7629                                text => $self->{open_elements}->[-1]->[0]
7630                                    ->manakai_local_name,
7631                                token => $token);
7632            } else {            } else {
7633              !!!cp ('t414.1');              !!!cp ('t414.1');
7634            }            }
# Line 6252  sub _tree_construction_main ($) { Line 7636  sub _tree_construction_main ($) {
7636            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7637          } else {          } else {
7638            !!!cp ('t413.1');            !!!cp ('t413.1');
7639            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7640                              text => $token->{tag_name}, token => $token);
7641    
7642            !!!cp ('t415.1');            !!!cp ('t415.1');
7643            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7644            my $el;            my $el;
7645            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
7646            $insert->($el);            $insert->($el);
7647            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7648          }          }
7649    
7650          !!!next-token;          !!!next-token;
7651          redo B;          next B;
7652        } elsif ({        } elsif ({
7653                  a => 1,                  a => 1,
7654                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6271  sub _tree_construction_main ($) { Line 7656  sub _tree_construction_main ($) {
7656                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7657                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7658          !!!cp ('t427');          !!!cp ('t427');
7659          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7660          redo B;          next B;
7661        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7662          !!!cp ('t428');          !!!cp ('t428');
7663          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
7664                            text => 'br', token => $token);
7665    
7666          ## As if <br>          ## As if <br>
7667          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7668                    
7669          my $el;          my $el;
7670          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7671          $insert->($el);          $insert->($el);
7672                    
7673          ## Ignore the token.          ## Ignore the token.
7674          !!!next-token;          !!!next-token;
7675          redo B;          next B;
7676        } elsif ({        } elsif ({
7677                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7678                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6300  sub _tree_construction_main ($) { Line 7686  sub _tree_construction_main ($) {
7686                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7687                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7688          !!!cp ('t429');          !!!cp ('t429');
7689          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
7690                            text => $token->{tag_name}, token => $token);
7691          ## Ignore the token          ## Ignore the token
7692          !!!next-token;          !!!next-token;
7693          redo B;          next B;
7694                    
7695          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7696                    
# Line 6314  sub _tree_construction_main ($) { Line 7701  sub _tree_construction_main ($) {
7701    
7702          ## Step 2          ## Step 2
7703          S2: {          S2: {
7704            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7705              ## Step 1              ## Step 1
7706              ## generate implied end tags              ## generate implied end tags
7707              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7708                !!!cp ('t430');                !!!cp ('t430');
7709                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7710                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7711                  ## which seems wrong.
7712                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7713                  $node_i++;
7714              }              }
7715                    
7716              ## Step 2              ## Step 2
7717              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7718                        ne $token->{tag_name}) {
7719                !!!cp ('t431');                !!!cp ('t431');
7720                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7721                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7722                                  text => $self->{open_elements}->[-1]->[0]
7723                                      ->manakai_local_name,
7724                                  token => $token);
7725              } else {              } else {
7726                !!!cp ('t432');                !!!cp ('t432');
7727              }              }
7728                            
7729              ## Step 3              ## Step 3
7730              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7731    
7732              !!!next-token;              !!!next-token;
7733              last S2;              last S2;
7734            } else {            } else {
7735              ## Step 3              ## Step 3
7736              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7737                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7738                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7739                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7740                !!!cp ('t433');                !!!cp ('t433');
7741                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
7742                                  text => $token->{tag_name}, token => $token);
7743                ## Ignore the token                ## Ignore the token
7744                !!!next-token;                !!!next-token;
7745                last S2;                last S2;
# Line 6362  sub _tree_construction_main ($) { Line 7755  sub _tree_construction_main ($) {
7755            ## Step 5;            ## Step 5;
7756            redo S2;            redo S2;
7757          } # S2          } # S2
7758          redo B;          next B;
7759        }        }
7760      }      }
7761      redo B;      next B;
7762      } continue { # B
7763        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7764          ## NOTE: The code below is executed in cases where it does not have
7765          ## to be, but it it is harmless even in those cases.
7766          ## has an element in scope
7767          INSCOPE: {
7768            for (reverse 0..$#{$self->{open_elements}}) {
7769              my $node = $self->{open_elements}->[$_];
7770              if ($node->[1] & FOREIGN_EL) {
7771                last INSCOPE;
7772              } elsif ($node->[1] & SCOPING_EL) {
7773                last;
7774              }
7775            }
7776            
7777            ## NOTE: No foreign element in scope.
7778            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7779          } # INSCOPE
7780        }
7781    } # B    } # B
7782    
7783    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6373  sub _tree_construction_main ($) { Line 7785  sub _tree_construction_main ($) {
7785    ## TODO: script stuffs    ## TODO: script stuffs
7786  } # _tree_construct_main  } # _tree_construct_main
7787    
7788  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7789    my $class = shift;    my $class = shift;
7790    my $node = shift;    my $node = shift;
7791    my $s = \$_[0];    #my $s = \$_[0];
7792    my $onerror = $_[1];    my $onerror = $_[1];
7793      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7794    
7795    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7796    
# Line 6396  sub set_inner_html ($$$) { Line 7809  sub set_inner_html ($$$) {
7809      }      }
7810    
7811      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7812      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7813    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7814      ## TODO: If non-html element      ## TODO: If non-html element
7815    
7816      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7817    
7818    ## TODO: Support for $get_wrapper
7819    
7820      ## Step 1 # MUST      ## Step 1 # MUST
7821      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7822      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6411  sub set_inner_html ($$$) { Line 7826  sub set_inner_html ($$$) {
7826    
7827      ## Step 8 # MUST      ## Step 8 # MUST
7828      my $i = 0;      my $i = 0;
7829      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7830      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7831      $p->{set_next_char} = sub {      require Whatpm::Charset::DecodeHandle;
7832        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7833        $input = $get_wrapper->($input);
7834        $p->{set_nc} = sub {
7835        my $self = shift;        my $self = shift;
7836    
7837        pop @{$self->{prev_char}};        my $char = '';
7838        unshift @{$self->{prev_char}}, $self->{next_char};        if (defined $self->{next_nc}) {
7839            $char = $self->{next_nc};
7840            delete $self->{next_nc};
7841            $self->{nc} = ord $char;
7842          } else {
7843            $self->{char_buffer} = '';
7844            $self->{char_buffer_pos} = 0;
7845            
7846            my $count = $input->manakai_read_until
7847                ($self->{char_buffer}, qr/[^\x00\x0A\x0D]/,
7848                 $self->{char_buffer_pos});
7849            if ($count) {
7850              $self->{line_prev} = $self->{line};
7851              $self->{column_prev} = $self->{column};
7852              $self->{column}++;
7853              $self->{nc}
7854                  = ord substr ($self->{char_buffer},
7855                                $self->{char_buffer_pos}++, 1);
7856              return;
7857            }
7858            
7859            if ($input->read ($char, 1)) {
7860              $self->{nc} = ord $char;
7861            } else {
7862              $self->{nc} = -1;
7863              return;
7864            }
7865          }
7866    
7867        $self->{next_char} = -1 and return if $i >= length $$s;        ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7868        $self->{next_char} = ord substr $$s, $i++, 1;        $p->{column}++;
7869        $column++;  
7870          if ($self->{nc} == 0x000A) { # LF
7871        if ($self->{next_char} == 0x000A) { # LF          $p->{line}++;
7872          $line++;          $p->{column} = 0;
         $column = 0;  
7873          !!!cp ('i1');          !!!cp ('i1');
7874        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{nc} == 0x000D) { # CR
7875          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7876          $self->{next_char} = 0x000A; # LF # MUST          my $next = '';
7877          $line++;          if ($input->read ($next, 1) and $next ne "\x0A") {
7878          $column = 0;            $self->{next_nc} = $next;
7879            }
7880            $self->{nc} = 0x000A; # LF # MUST
7881            $p->{line}++;
7882            $p->{column} = 0;
7883          !!!cp ('i2');          !!!cp ('i2');
7884        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{nc} == 0x0000) { # NULL
         $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
         !!!cp ('i3');  
       } elsif ($self->{next_char} == 0x0000) { # NULL  
7885          !!!cp ('i4');          !!!cp ('i4');
7886          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7887          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{nc} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7888        }        }
7889      };      };
7890      $p->{prev_char} = [-1, -1, -1];  
7891      $p->{next_char} = -1;      $p->{read_until} = sub {
7892              #my ($scalar, $specials_range, $offset) = @_;
7893          return 0 if defined $p->{next_nc};
7894    
7895          my $pattern = qr/[^$_[1]\x00\x0A\x0D]/;
7896          my $offset = $_[2] || 0;
7897          
7898          if ($p->{char_buffer_pos} < length $p->{char_buffer}) {
7899            pos ($p->{char_buffer}) = $p->{char_buffer_pos};
7900            if ($p->{char_buffer} =~ /\G(?>$pattern)+/) {
7901              substr ($_[0], $offset)
7902                  = substr ($p->{char_buffer}, $-[0], $+[0] - $-[0]);
7903              my $count = $+[0] - $-[0];
7904              if ($count) {
7905                $p->{column} += $count;
7906                $p->{char_buffer_pos} += $count;
7907                $p->{line_prev} = $p->{line};
7908                $p->{column_prev} = $p->{column} - 1;
7909                $p->{nc} = -1;
7910              }
7911              return $count;
7912            } else {
7913              return 0;
7914            }
7915          } else {
7916            my $count = $input->manakai_read_until ($_[0], $pattern, $_[2]);
7917            if ($count) {
7918              $p->{column} += $count;
7919              $p->{column_prev} += $count;
7920              $p->{nc} = -1;
7921            }
7922            return $count;
7923          }
7924        }; # $p->{read_until}
7925    
7926      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7927        my (%opt) = @_;        my (%opt) = @_;
7928        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7929          my $column = $opt{column};
7930          if (defined $opt{token} and defined $opt{token}->{line}) {
7931            $line = $opt{token}->{line};
7932            $column = $opt{token}->{column};
7933          }
7934          warn "Parse error ($opt{type}) at line $line column $column\n";
7935      };      };
7936      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7937        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7938      };      };
7939            
7940        my $char_onerror = sub {
7941          my (undef, $type, %opt) = @_;
7942          $ponerror->(layer => 'encode',
7943                      line => $p->{line}, column => $p->{column} + 1,
7944                      %opt, type => $type);
7945        }; # $char_onerror
7946        $input->onerror ($char_onerror);
7947    
7948      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7949      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7950    
# Line 6474  sub set_inner_html ($$$) { Line 7966  sub set_inner_html ($$$) {
7966          unless defined $p->{content_model};          unless defined $p->{content_model};
7967          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7968    
7969      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7970          ## TODO: Foreign element OK?
7971    
7972      ## Step 3      ## Step 3
7973      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6484  sub set_inner_html ($$$) { Line 7977  sub set_inner_html ($$$) {
7977      $doc->append_child ($root);      $doc->append_child ($root);
7978    
7979      ## Step 5 # MUST      ## Step 5 # MUST
7980      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7981    
7982      undef $p->{head_element};      undef $p->{head_element};
7983    
# Line 6530  sub set_inner_html ($$$) { Line 8023  sub set_inner_html ($$$) {
8023      ## ISSUE: mutation events?      ## ISSUE: mutation events?
8024    
8025      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
8026    
8027        delete $p->{parse_error}; # delete loop
8028    } else {    } else {
8029      die "$0: |set_inner_html| is not defined for node of type $nt";      die "$0: |set_inner_html| is not defined for node of type $nt";
8030    }    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24