/[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.86 by wakaba, Thu Mar 6 15:56:52 2008 UTC revision 1.178 by wakaba, Sun Sep 14 11:57:41 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231  };  };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296    };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 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  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
360    my $charset = shift;    my $charset_name = shift;
361    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;  
   }  
362    
363    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
364      my $self = shift;      my (%opt) = @_;
365      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
366      ## TODO: if $charset is supported    };
367      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      ## "Change the encoding" algorithm:    my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      ## Step 1        };
372      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
373        $charset = 'utf-8';    ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403      }      }
404    
405      ## Step 2      ## Step 2
406      if (defined $self->{input_encoding} and      my $byte_buffer = '';
407          $self->{input_encoding} eq $charset) {      for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419        $self->{confident} = 1;        $self->{confident} = 1;
420        return;        last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
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;
434          last SNIFFING;
435      }      }
436    
437      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
438          ':'.$charset, level => 'w');      ## TODO: <meta charset>
439    
440      ## Step 3      ## Step 5
441      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
442    
443      ## Step 4      ## Step 6
444      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559    }; # $self->{change_encoding}    }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        line => $self->{line}, column => $self->{column} + 1,
565                        %opt, type => $type);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
575    my $return;    my $return;
576    try {    try {
577      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
579      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
580      $s = \ (Encode::decode ($charset, $$bytes_s));      
581      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $return = $self->parse_char_string ($s, @args);  
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605    };    };
606    return $return;    return $return;
607  } # parse_byte_string  } # parse_byte_stream
608    
609  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610  ## 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 615  sub parse_byte_string ($$$$;$) {
615  ## 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
616  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
617    
618  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      my $s = ref $_[0] ? $_[0] : \($_[0]);
622      require Whatpm::Charset::DecodeHandle;
623      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
633    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
636    
# Line 175  sub parse_string ($$$;$) { Line 639  sub parse_string ($$$;$) {
639    $self->{confident} = 1 unless exists $self->{confident};    $self->{confident} = 1 unless exists $self->{confident};
640    $self->{document}->input_encoding ($self->{input_encoding})    $self->{document}->input_encoding ($self->{input_encoding})
641        if defined $self->{input_encoding};        if defined $self->{input_encoding};
642    ## TODO: |{input_encoding}| is needless?
643    
644    my $i = 0;    my $i = 0;
645    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
646    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
647    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
648      my $self = shift;      my $self = shift;
649    
650      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
651      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
652    
653      $self->{next_char} = -1 and return if $i >= length $$s;      my $char = '';
654      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
655      $column++;        $char = $self->{next_next_char};
656          delete $self->{next_next_char};
657          $self->{next_char} = ord $char;
658        } else {
659          if ($input->read ($char, 1)) {
660            $self->{next_char} = ord $char;
661          } else {
662            $self->{next_char} = -1;
663            return;
664          }
665        }
666    
667        ($self->{line_prev}, $self->{column_prev})
668            = ($self->{line}, $self->{column});
669        $self->{column}++;
670            
671      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
672        $line++;        !!!cp ('j1');
673        $column = 0;        $self->{line}++;
674          $self->{column} = 0;
675      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
676        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
677    ## TODO: support for abort/streaming
678          my $next = '';
679          if ($input->read ($next, 1) and $next ne "\x0A") {
680            $self->{next_next_char} = $next;
681          }
682        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
683        $line++;        $self->{line}++;
684        $column = 0;        $self->{column} = 0;
685      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
686          !!!cp ('j3');
687        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
688      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
689          !!!cp ('j4');
690        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
691        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
692        } elsif ($self->{next_char} <= 0x0008 or
693                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
694                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
695                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
696                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
697    ## ISSUE: U+FDE0-U+FDEF are not excluded
698                 {
699                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
700                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
701                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
702                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
703                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
704                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
705                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
706                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
707                  0x10FFFE => 1, 0x10FFFF => 1,
708                 }->{$self->{next_char}}) {
709          !!!cp ('j5');
710          if ($self->{next_char} < 0x10000) {
711            !!!parse-error (type => 'control char',
712                            text => (sprintf 'U+%04X', $self->{next_char}));
713          } else {
714            !!!parse-error (type => 'control char',
715                            text => (sprintf 'U-%08X', $self->{next_char}));
716          }
717      }      }
718    };    };
719    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
720    $self->{next_char} = -1;    $self->{next_char} = -1;
721    
722      $self->{read_until} = sub {
723        #my ($scalar, $specials_range, $offset) = @_;
724        my $specials_range = $_[1];
725        return 0 if defined $self->{next_next_char};
726        my $count = $input->manakai_read_until
727           ($_[0],
728            qr/(?![$specials_range\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
729            $_[2]);
730        if ($count) {
731          $self->{column} += $count;
732          $self->{column_prev} += $count;
733          $self->{prev_char} = [-1, -1, -1];
734          $self->{next_char} = -1;
735        }
736        return $count;
737      }; # $self->{read_until}
738    
739    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
740      my (%opt) = @_;      my (%opt) = @_;
741      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
742        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
743        warn "Parse error ($opt{type}) at line $line column $column\n";
744    };    };
745    $self->{parse_error} = sub {    $self->{parse_error} = sub {
746      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
747    };    };
748    
749    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 751  sub parse_string ($$$;$) {
751    $self->_construct_tree;    $self->_construct_tree;
752    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
753    
754      delete $self->{parse_error}; # remove loop
755    
756    return $self->{document};    return $self->{document};
757  } # parse_string  } # parse_char_stream
758    
759  sub new ($) {  sub new ($) {
760    my $class = shift;    my $class = shift;
761    my $self = bless {}, $class;    my $self = bless {
762        level => {must => 'm',
763                  should => 's',
764                  warn => 'w',
765                  info => 'i',
766                  uncertain => 'u'},
767      }, $class;
768    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
769      $self->{next_char} = -1;      $self->{next_char} = -1;
770    };    };
# Line 254  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 793  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
793  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
794    
795  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
796  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
797  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
798  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
799  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 265  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 804  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
804  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
805  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
806  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
807  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
808  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
809  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
810  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 826  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
826  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
827  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
828  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
829    sub SELF_CLOSING_START_TAG_STATE () { 34 }
830    sub CDATA_SECTION_STATE () { 35 }
831    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
832    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
833    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
834    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
835    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
836    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
837    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
838    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
839    ## NOTE: "Entity data state", "entity in attribute value state", and
840    ## "consume a character reference" algorithm are jointly implemented
841    ## using the following six states:
842    sub ENTITY_STATE () { 44 }
843    sub ENTITY_HASH_STATE () { 45 }
844    sub NCR_NUM_STATE () { 46 }
845    sub HEXREF_X_STATE () { 47 }
846    sub HEXREF_HEX_STATE () { 48 }
847    sub ENTITY_NAME_STATE () { 49 }
848    
849  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
850  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 861  sub TABLE_IMS ()      { 0b1000000 }
861  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
862  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
863  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
864    sub SELECT_IMS ()     { 0b10000000000 }
865    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
866        ## NOTE: "in foreign content" insertion mode is special; it is combined
867        ## with the secondary insertion mode.  In this parser, they are stored
868        ## together in the bit-or'ed form.
869    
870  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
871    
# Line 325  sub IN_TABLE_IM () { TABLE_IMS } Line 888  sub IN_TABLE_IM () { TABLE_IMS }
888  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
889  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
890  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
891  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
892    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
893  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
894    
895  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 333  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 897  sub IN_COLUMN_GROUP_IM () { 0b10 }
897  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
898    my $self = shift;    my $self = shift;
899    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
900      #$self->{state_keyword}; # initialized when used
901      #$self->{entity__value}; # initialized when used
902      #$self->{entity__match}; # initialized when used
903    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
904    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
905    undef $self->{current_attribute};    undef $self->{current_attribute};
906    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
907    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
908    $self->{char} = [];    delete $self->{self_closing};
909    # $self->{next_char}    # $self->{next_char}
910    !!!next-input-character;    !!!next-input-character;
911    $self->{token} = [];    $self->{token} = [];
# Line 358  sub _initialize_tokenizer ($) { Line 925  sub _initialize_tokenizer ($) {
925  ##        ->{value}  ##        ->{value}
926  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
927  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
928    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
929    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
930    ##     while the token is pushed back to the stack.
931    
932  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
933    
# Line 367  sub _initialize_tokenizer ($) { Line 937  sub _initialize_tokenizer ($) {
937  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
938  ## and removed from the list.  ## and removed from the list.
939    
940  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
941  ## documents and not to user agents and conformance checkers,  ## (This requirement was dropped from HTML5 spec, unfortunately.)
 ## contains some requirements that are not detected by the  
 ## parsing algorithm:  
 ## - Some requirements on character encoding declarations. ## TODO  
 ## - "Elements MUST NOT contain content that their content model disallows."  
 ##   ... Some are parse error, some are not (will be reported by c.c.).  
 ## - Polytheistic slash SHOULD NOT be used. (Applied only to atheists.) ## TODO  
 ## - Text (in elements, attributes, and comments) SHOULD NOT contain  
 ##   control characters other than space characters. ## TODO: (what is control character? C0, C1 and DEL?  Unicode control character?)  
   
 ## TODO: HTML5 poses authors two SHOULD-level requirements that cannot  
 ## be detected by the HTML5 parsing algorithm:  
 ## - Text,  
942    
943  sub _get_next_token ($) {  sub _get_next_token ($) {
944    my $self = shift;    my $self = shift;
945    
946      if ($self->{self_closing}) {
947        !!!parse-error (type => 'nestc', token => $self->{current_token});
948        ## NOTE: The |self_closing| flag is only set by start tag token.
949        ## In addition, when a start tag token is emitted, it is always set to
950        ## |current_token|.
951        delete $self->{self_closing};
952      }
953    
954    if (@{$self->{token}}) {    if (@{$self->{token}}) {
955        $self->{self_closing} = $self->{token}->[0]->{self_closing};
956      return shift @{$self->{token}};      return shift @{$self->{token}};
957    }    }
958    
# Line 394  sub _get_next_token ($) { Line 962  sub _get_next_token ($) {
962          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
963              not $self->{escape}) {              not $self->{escape}) {
964            !!!cp (1);            !!!cp (1);
965            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
966              ## "entity data state".  In this implementation, the tokenizer
967              ## is switched to the |ENTITY_STATE|, which is an implementation
968              ## of the "consume a character reference" algorithm.
969              $self->{entity_additional} = -1;
970              $self->{prev_state} = DATA_STATE;
971              $self->{state} = ENTITY_STATE;
972            !!!next-input-character;            !!!next-input-character;
973            redo A;            redo A;
974          } else {          } else {
# Line 447  sub _get_next_token ($) { Line 1021  sub _get_next_token ($) {
1021          #          #
1022        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1023          !!!cp (11);          !!!cp (11);
1024          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
1025                      line => $self->{line}, column => $self->{column}});
1026          last A; ## TODO: ok?          last A; ## TODO: ok?
1027        } else {        } else {
1028          !!!cp (12);          !!!cp (12);
1029        }        }
1030        # Anything else        # Anything else
1031        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1032                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
1033                       line => $self->{line}, column => $self->{column},
1034                      };
1035          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1036    
1037        ## Stay in the data state        ## Stay in the data state
1038        !!!next-input-character;        !!!next-input-character;
1039    
1040        !!!emit ($token);        !!!emit ($token);
1041    
1042        redo A;        redo A;
     } elsif ($self->{state} == ENTITY_DATA_STATE) {  
       ## (cannot happen in CDATA state)  
         
       my $token = $self->_tokenize_attempt_to_consume_an_entity (0, -1);  
   
       $self->{state} = DATA_STATE;  
       # next-input-character is already done  
   
       unless (defined $token) {  
         !!!cp (13);  
         !!!emit ({type => CHARACTER_TOKEN, data => '&'});  
       } else {  
         !!!cp (14);  
         !!!emit ($token);  
       }  
   
       redo A;  
1043      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1044        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1045          if ($self->{next_char} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
# Line 490  sub _get_next_token ($) { Line 1052  sub _get_next_token ($) {
1052            ## reconsume            ## reconsume
1053            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1054    
1055            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1056                        line => $self->{line_prev},
1057                        column => $self->{column_prev},
1058                       });
1059    
1060            redo A;            redo A;
1061          }          }
# Line 510  sub _get_next_token ($) { Line 1075  sub _get_next_token ($) {
1075            !!!cp (19);            !!!cp (19);
1076            $self->{current_token}            $self->{current_token}
1077              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1078                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1079                   line => $self->{line_prev},
1080                   column => $self->{column_prev}};
1081            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1082            !!!next-input-character;            !!!next-input-character;
1083            redo A;            redo A;
# Line 518  sub _get_next_token ($) { Line 1085  sub _get_next_token ($) {
1085                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1086            !!!cp (20);            !!!cp (20);
1087            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1088                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1089                                        line => $self->{line_prev},
1090                                        column => $self->{column_prev}};
1091            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1092            !!!next-input-character;            !!!next-input-character;
1093            redo A;            redo A;
1094          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1095            !!!cp (21);            !!!cp (21);
1096            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1097                              line => $self->{line_prev},
1098                              column => $self->{column_prev});
1099            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1100            !!!next-input-character;            !!!next-input-character;
1101    
1102            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1103                        line => $self->{line_prev},
1104                        column => $self->{column_prev},
1105                       });
1106    
1107            redo A;            redo A;
1108          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1109            !!!cp (22);            !!!cp (22);
1110            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1111                              line => $self->{line_prev},
1112                              column => $self->{column_prev});
1113            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1114              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1115                                        line => $self->{line_prev},
1116                                        column => $self->{column_prev},
1117                                       };
1118            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1119            redo A;            redo A;
1120          } else {          } else {
1121            !!!cp (23);            !!!cp (23);
1122            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1123                              line => $self->{line_prev},
1124                              column => $self->{column_prev});
1125            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1126            ## reconsume            ## reconsume
1127    
1128            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1129                        line => $self->{line_prev},
1130                        column => $self->{column_prev},
1131                       });
1132    
1133            redo A;            redo A;
1134          }          }
# Line 551  sub _get_next_token ($) { Line 1136  sub _get_next_token ($) {
1136          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1137        }        }
1138      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1139          ## NOTE: The "close tag open state" in the spec is implemented as
1140          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1141    
1142          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1143        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1144          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1145            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1146            my @next_char;            $self->{state_keyword} = '';
1147            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            ## Reconsume.
1148              push @next_char, $self->{next_char};            redo A;
             my $c = ord substr ($self->{last_emitted_start_tag_name}, $i, 1);  
             my $C = 0x0061 <= $c && $c <= 0x007A ? $c - 0x0020 : $c;  
             if ($self->{next_char} == $c or $self->{next_char} == $C) {  
               !!!cp (24);  
               !!!next-input-character;  
               next TAGNAME;  
             } else {  
               !!!cp (25);  
               $self->{next_char} = shift @next_char; # reconsume  
               !!!back-next-input-character (@next_char);  
               $self->{state} = DATA_STATE;  
   
               !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
     
               redo A;  
             }  
           }  
           push @next_char, $self->{next_char};  
         
           unless ($self->{next_char} == 0x0009 or # HT  
                   $self->{next_char} == 0x000A or # LF  
                   $self->{next_char} == 0x000B or # VT  
                   $self->{next_char} == 0x000C or # FF  
                   $self->{next_char} == 0x0020 or # SP  
                   $self->{next_char} == 0x003E or # >  
                   $self->{next_char} == 0x002F or # /  
                   $self->{next_char} == -1) {  
             !!!cp (26);  
             $self->{next_char} = shift @next_char; # reconsume  
             !!!back-next-input-character (@next_char);  
             $self->{state} = DATA_STATE;  
             !!!emit ({type => CHARACTER_TOKEN, data => '</'});  
             redo A;  
           } else {  
             !!!cp (27);  
             $self->{next_char} = shift @next_char;  
             !!!back-next-input-character (@next_char);  
             # and consume...  
           }  
1149          } else {          } else {
1150            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1151              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1152            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1153            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1154            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1155              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1156                        line => $l, column => $c,
1157                       });
1158            redo A;            redo A;
1159          }          }
1160        }        }
1161          
1162        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1163            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1164          !!!cp (29);          !!!cp (29);
1165          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1166                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1167                   tag_name => chr ($self->{next_char} + 0x0020),
1168                   line => $l, column => $c};
1169          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1170          !!!next-input-character;          !!!next-input-character;
1171          redo A;          redo A;
# Line 618  sub _get_next_token ($) { Line 1173  sub _get_next_token ($) {
1173                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1174          !!!cp (30);          !!!cp (30);
1175          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1176                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1177                                      line => $l, column => $c};
1178          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1179          !!!next-input-character;          !!!next-input-character;
1180          redo A;          redo A;
1181        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1182          !!!cp (31);          !!!cp (31);
1183          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1184                            line => $self->{line_prev}, ## "<" in "</>"
1185                            column => $self->{column_prev} - 1);
1186          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1187          !!!next-input-character;          !!!next-input-character;
1188          redo A;          redo A;
# Line 634  sub _get_next_token ($) { Line 1192  sub _get_next_token ($) {
1192          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1193          # reconsume          # reconsume
1194    
1195          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1196                      line => $l, column => $c,
1197                     });
1198    
1199          redo A;          redo A;
1200        } else {        } else {
1201          !!!cp (33);          !!!cp (33);
1202          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1203          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1204          ## $self->{next_char} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1205          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1206                                      column => $self->{column_prev} - 1,
1207                                     };
1208            ## NOTE: $self->{next_char} is intentionally left as is.
1209            ## Although the "anything else" case of the spec not explicitly
1210            ## states that the next input character is to be reconsumed,
1211            ## it will be included to the |data| of the comment token
1212            ## generated from the bogus end tag, as defined in the
1213            ## "bogus comment state" entry.
1214            redo A;
1215          }
1216        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1217          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1218          if (length $ch) {
1219            my $CH = $ch;
1220            $ch =~ tr/a-z/A-Z/;
1221            my $nch = chr $self->{next_char};
1222            if ($nch eq $ch or $nch eq $CH) {
1223              !!!cp (24);
1224              ## Stay in the state.
1225              $self->{state_keyword} .= $nch;
1226              !!!next-input-character;
1227              redo A;
1228            } else {
1229              !!!cp (25);
1230              $self->{state} = DATA_STATE;
1231              ## Reconsume.
1232              !!!emit ({type => CHARACTER_TOKEN,
1233                        data => '</' . $self->{state_keyword},
1234                        line => $self->{line_prev},
1235                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1236                       });
1237              redo A;
1238            }
1239          } else { # after "<{tag-name}"
1240            unless ({
1241                     0x0009 => 1, # HT
1242                     0x000A => 1, # LF
1243                     0x000B => 1, # VT
1244                     0x000C => 1, # FF
1245                     0x0020 => 1, # SP
1246                     0x003E => 1, # >
1247                     0x002F => 1, # /
1248                     -1 => 1, # EOF
1249                    }->{$self->{next_char}}) {
1250              !!!cp (26);
1251              ## Reconsume.
1252              $self->{state} = DATA_STATE;
1253              !!!emit ({type => CHARACTER_TOKEN,
1254                        data => '</' . $self->{state_keyword},
1255                        line => $self->{line_prev},
1256                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1257                       });
1258              redo A;
1259            } else {
1260              !!!cp (27);
1261              $self->{current_token}
1262                  = {type => END_TAG_TOKEN,
1263                     tag_name => $self->{last_emitted_start_tag_name},
1264                     line => $self->{line_prev},
1265                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1266              $self->{state} = TAG_NAME_STATE;
1267              ## Reconsume.
1268              redo A;
1269            }
1270        }        }
1271      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1272        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 657  sub _get_next_token ($) { Line 1281  sub _get_next_token ($) {
1281        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1282          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1283            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1284            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1285          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1286            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 690  sub _get_next_token ($) { Line 1312  sub _get_next_token ($) {
1312          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1313          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1314            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1315            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1316          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1317            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 712  sub _get_next_token ($) { Line 1332  sub _get_next_token ($) {
1332    
1333          redo A;          redo A;
1334        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1335            !!!cp (42);
1336            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1337          !!!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  
1338          redo A;          redo A;
1339        } else {        } else {
1340          !!!cp (44);          !!!cp (44);
# Line 747  sub _get_next_token ($) { Line 1357  sub _get_next_token ($) {
1357        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1358          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1359            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1360            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1361          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1362            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 770  sub _get_next_token ($) { Line 1378  sub _get_next_token ($) {
1378        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1379                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1380          !!!cp (49);          !!!cp (49);
1381          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1382                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1383                   value => '',
1384                   line => $self->{line}, column => $self->{column}};
1385          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1386          !!!next-input-character;          !!!next-input-character;
1387          redo A;          redo A;
1388        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1389            !!!cp (50);
1390            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1391          !!!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  
1392          redo A;          redo A;
1393        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1394          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1395          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1396            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1397            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1398          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1399            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 825  sub _get_next_token ($) { Line 1423  sub _get_next_token ($) {
1423          } else {          } else {
1424            !!!cp (56);            !!!cp (56);
1425          }          }
1426          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1427                                value => ''};              = {name => chr ($self->{next_char}),
1428                   value => '',
1429                   line => $self->{line}, column => $self->{column}};
1430          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1431          !!!next-input-character;          !!!next-input-character;
1432          redo A;          redo A;
# Line 836  sub _get_next_token ($) { Line 1436  sub _get_next_token ($) {
1436          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1437              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1438            !!!cp (57);            !!!cp (57);
1439            !!!parse-error (type => 'duplicate attribute:'.$self->{current_attribute}->{name});            !!!parse-error (type => 'duplicate attribute', text => $self->{current_attribute}->{name}, line => $self->{current_attribute}->{line}, column => $self->{current_attribute}->{column});
1440            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1441          } else {          } else {
1442            !!!cp (58);            !!!cp (58);
# Line 865  sub _get_next_token ($) { Line 1465  sub _get_next_token ($) {
1465          $before_leave->();          $before_leave->();
1466          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1467            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1468            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1469          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1470            !!!cp (62);            !!!cp (62);
# Line 891  sub _get_next_token ($) { Line 1489  sub _get_next_token ($) {
1489          !!!next-input-character;          !!!next-input-character;
1490          redo A;          redo A;
1491        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1492            !!!cp (64);
1493          $before_leave->();          $before_leave->();
1494            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1495          !!!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  
1496          redo A;          redo A;
1497        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1498          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1499          $before_leave->();          $before_leave->();
1500          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1501            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1502            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1503          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1504            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 963  sub _get_next_token ($) { Line 1549  sub _get_next_token ($) {
1549        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1550          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1551            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1552            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1553          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1554            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 987  sub _get_next_token ($) { Line 1571  sub _get_next_token ($) {
1571        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1572                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1573          !!!cp (76);          !!!cp (76);
1574          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1575                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1576                   value => '',
1577                   line => $self->{line}, column => $self->{column}};
1578          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1579          !!!next-input-character;          !!!next-input-character;
1580          redo A;          redo A;
1581        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1582            !!!cp (77);
1583            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1584          !!!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  
1585          redo A;          redo A;
1586        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1587          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1588          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1589            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1590            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1591          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1592            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1034  sub _get_next_token ($) { Line 1607  sub _get_next_token ($) {
1607    
1608          redo A;          redo A;
1609        } else {        } else {
1610          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1611          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1612                                value => ''};            !!!cp (78);
1613              !!!parse-error (type => 'bad attribute name');
1614            } else {
1615              !!!cp (82);
1616            }
1617            $self->{current_attribute}
1618                = {name => chr ($self->{next_char}),
1619                   value => '',
1620                   line => $self->{line}, column => $self->{column}};
1621          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1622          !!!next-input-character;          !!!next-input-character;
1623          redo A;                  redo A;        
# Line 1067  sub _get_next_token ($) { Line 1648  sub _get_next_token ($) {
1648          !!!next-input-character;          !!!next-input-character;
1649          redo A;          redo A;
1650        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1651            !!!parse-error (type => 'empty unquoted attribute value');
1652          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1653            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1654            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1655          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1656            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1094  sub _get_next_token ($) { Line 1674  sub _get_next_token ($) {
1674          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1675          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1676            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1677            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1678          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1679            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1135  sub _get_next_token ($) { Line 1713  sub _get_next_token ($) {
1713          redo A;          redo A;
1714        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1715          !!!cp (96);          !!!cp (96);
1716          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1717          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1718            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1719            ## implementation of the "consume a character reference" algorithm.
1720            $self->{prev_state} = $self->{state};
1721            $self->{entity_additional} = 0x0022; # "
1722            $self->{state} = ENTITY_STATE;
1723          !!!next-input-character;          !!!next-input-character;
1724          redo A;          redo A;
1725        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1726          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1727          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1728            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1729            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1730          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1731            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1167  sub _get_next_token ($) { Line 1748  sub _get_next_token ($) {
1748        } else {        } else {
1749          !!!cp (100);          !!!cp (100);
1750          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1751            $self->{read_until}->($self->{current_attribute}->{value},
1752                                  q["&],
1753                                  length $self->{current_attribute}->{value});
1754    
1755          ## Stay in the state          ## Stay in the state
1756          !!!next-input-character;          !!!next-input-character;
1757          redo A;          redo A;
# Line 1179  sub _get_next_token ($) { Line 1764  sub _get_next_token ($) {
1764          redo A;          redo A;
1765        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1766          !!!cp (102);          !!!cp (102);
1767          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1768          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1769            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1770            ## implementation of the "consume a character reference" algorithm.
1771            $self->{entity_additional} = 0x0027; # '
1772            $self->{prev_state} = $self->{state};
1773            $self->{state} = ENTITY_STATE;
1774          !!!next-input-character;          !!!next-input-character;
1775          redo A;          redo A;
1776        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1777          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1778          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1779            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1780            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1781          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1782            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1211  sub _get_next_token ($) { Line 1799  sub _get_next_token ($) {
1799        } else {        } else {
1800          !!!cp (106);          !!!cp (106);
1801          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1802            $self->{read_until}->($self->{current_attribute}->{value},
1803                                  q['&],
1804                                  length $self->{current_attribute}->{value});
1805    
1806          ## Stay in the state          ## Stay in the state
1807          !!!next-input-character;          !!!next-input-character;
1808          redo A;          redo A;
# Line 1227  sub _get_next_token ($) { Line 1819  sub _get_next_token ($) {
1819          redo A;          redo A;
1820        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1821          !!!cp (108);          !!!cp (108);
1822          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1823          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1824            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1825            ## implementation of the "consume a character reference" algorithm.
1826            $self->{entity_additional} = -1;
1827            $self->{prev_state} = $self->{state};
1828            $self->{state} = ENTITY_STATE;
1829          !!!next-input-character;          !!!next-input-character;
1830          redo A;          redo A;
1831        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1832          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1833            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1834            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1835          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1836            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1259  sub _get_next_token ($) { Line 1854  sub _get_next_token ($) {
1854          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1855          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1856            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1857            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1858          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1859            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1292  sub _get_next_token ($) { Line 1885  sub _get_next_token ($) {
1885            !!!cp (116);            !!!cp (116);
1886          }          }
1887          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1888            $self->{read_until}->($self->{current_attribute}->{value},
1889                                  q["'=& >],
1890                                  length $self->{current_attribute}->{value});
1891    
1892          ## Stay in the state          ## Stay in the state
1893          !!!next-input-character;          !!!next-input-character;
1894          redo A;          redo A;
1895        }        }
     } 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;  
1896      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1897        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1898            $self->{next_char} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
# Line 1331  sub _get_next_token ($) { Line 1906  sub _get_next_token ($) {
1906        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1907          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1908            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1909            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1910          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1911            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1353  sub _get_next_token ($) { Line 1926  sub _get_next_token ($) {
1926    
1927          redo A;          redo A;
1928        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1929            !!!cp (122);
1930            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1931          !!!next-input-character;          !!!next-input-character;
1932          if ($self->{next_char} == 0x003E and # >          redo A;
1933              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1934              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1935            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1936            !!!cp (122);            !!!cp (122.3);
1937            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1938            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1939              if ($self->{current_token}->{attributes}) {
1940                !!!cp (122.1);
1941                !!!parse-error (type => 'end tag attribute');
1942              } else {
1943                ## NOTE: This state should never be reached.
1944                !!!cp (122.2);
1945              }
1946          } else {          } else {
1947            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1948          }          }
1949          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1950          # next-input-character is already done          ## Reconsume.
1951            !!!emit ($self->{current_token}); # start tag or end tag
1952          redo A;          redo A;
1953        } else {        } else {
1954          !!!cp (124);          !!!cp ('124.1');
1955          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1956          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1957          ## reconsume          ## reconsume
1958          redo A;          redo A;
1959        }        }
1960      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1961        ## (only happen if PCDATA state)        if ($self->{next_char} == 0x003E) { # >
1962                  if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1963        my $token = {type => COMMENT_TOKEN, data => ''};            !!!cp ('124.2');
1964              !!!parse-error (type => 'nestc', token => $self->{current_token});
1965        BC: {            ## TODO: Different type than slash in start tag
1966          if ($self->{next_char} == 0x003E) { # >            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1967            !!!cp (124);            if ($self->{current_token}->{attributes}) {
1968            $self->{state} = DATA_STATE;              !!!cp ('124.4');
1969            !!!next-input-character;              !!!parse-error (type => 'end tag attribute');
1970              } else {
1971            !!!emit ($token);              !!!cp ('124.5');
1972              }
1973              ## TODO: Test |<title></title/>|
1974            } else {
1975              !!!cp ('124.3');
1976              $self->{self_closing} = 1;
1977            }
1978    
1979            redo A;          $self->{state} = DATA_STATE;
1980          } elsif ($self->{next_char} == -1) {          !!!next-input-character;
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
1981    
1982            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
1983    
1984            redo A;          redo A;
1985          } elsif ($self->{next_char} == -1) {
1986            !!!parse-error (type => 'unclosed tag');
1987            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1988              !!!cp (124.7);
1989              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1990            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1991              if ($self->{current_token}->{attributes}) {
1992                !!!cp (124.5);
1993                !!!parse-error (type => 'end tag attribute');
1994              } else {
1995                ## NOTE: This state should never be reached.
1996                !!!cp (124.6);
1997              }
1998          } else {          } else {
1999            !!!cp (126);            die "$0: $self->{current_token}->{type}: Unknown token type";
           $token->{data} .= chr ($self->{next_char});  
           !!!next-input-character;  
           redo BC;  
2000          }          }
2001        } # BC          $self->{state} = DATA_STATE;
2002            ## Reconsume.
2003            !!!emit ($self->{current_token}); # start tag or end tag
2004            redo A;
2005          } else {
2006            !!!cp ('124.4');
2007            !!!parse-error (type => 'nestc');
2008            ## TODO: This error type is wrong.
2009            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2010            ## Reconsume.
2011            redo A;
2012          }
2013        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2014          ## (only happen if PCDATA state)
2015    
2016          ## NOTE: Unlike spec's "bogus comment state", this implementation
2017          ## consumes characters one-by-one basis.
2018          
2019          if ($self->{next_char} == 0x003E) { # >
2020            !!!cp (124);
2021            $self->{state} = DATA_STATE;
2022            !!!next-input-character;
2023    
2024        die "$0: _get_next_token: unexpected case [BC]";          !!!emit ($self->{current_token}); # comment
2025            redo A;
2026          } elsif ($self->{next_char} == -1) {
2027            !!!cp (125);
2028            $self->{state} = DATA_STATE;
2029            ## reconsume
2030    
2031            !!!emit ($self->{current_token}); # comment
2032            redo A;
2033          } else {
2034            !!!cp (126);
2035            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2036            $self->{read_until}->($self->{current_token}->{data},
2037                                  q[>],
2038                                  length $self->{current_token}->{data});
2039    
2040            ## Stay in the state.
2041            !!!next-input-character;
2042            redo A;
2043          }
2044      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2045        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my @next_char;  
       push @next_char, $self->{next_char};  
2046                
2047        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2048            !!!cp (133);
2049            $self->{state} = MD_HYPHEN_STATE;
2050          !!!next-input-character;          !!!next-input-character;
2051          push @next_char, $self->{next_char};          redo A;
         if ($self->{next_char} == 0x002D) { # -  
           !!!cp (127);  
           $self->{current_token} = {type => COMMENT_TOKEN, data => ''};  
           $self->{state} = COMMENT_START_STATE;  
           !!!next-input-character;  
           redo A;  
         } else {  
           !!!cp (128);  
         }  
2052        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2053                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2054            ## ASCII case-insensitive.
2055            !!!cp (130);
2056            $self->{state} = MD_DOCTYPE_STATE;
2057            $self->{state_keyword} = chr $self->{next_char};
2058          !!!next-input-character;          !!!next-input-character;
2059          push @next_char, $self->{next_char};          redo A;
2060          if ($self->{next_char} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2061              $self->{next_char} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2062            !!!next-input-character;                 $self->{next_char} == 0x005B) { # [
2063            push @next_char, $self->{next_char};          !!!cp (135.4);                
2064            if ($self->{next_char} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2065                $self->{next_char} == 0x0063) { # c          $self->{state_keyword} = '[';
2066              !!!next-input-character;          !!!next-input-character;
2067              push @next_char, $self->{next_char};          redo A;
             if ($self->{next_char} == 0x0054 or # T  
                 $self->{next_char} == 0x0074) { # t  
               !!!next-input-character;  
               push @next_char, $self->{next_char};  
               if ($self->{next_char} == 0x0059 or # Y  
                   $self->{next_char} == 0x0079) { # y  
                 !!!next-input-character;  
                 push @next_char, $self->{next_char};  
                 if ($self->{next_char} == 0x0050 or # P  
                     $self->{next_char} == 0x0070) { # p  
                   !!!next-input-character;  
                   push @next_char, $self->{next_char};  
                   if ($self->{next_char} == 0x0045 or # E  
                       $self->{next_char} == 0x0065) { # e  
                     !!!cp (129);  
                     ## TODO: What a stupid code this is!  
                     $self->{state} = DOCTYPE_STATE;  
                     !!!next-input-character;  
                     redo A;  
                   } else {  
                     !!!cp (130);  
                   }  
                 } else {  
                   !!!cp (131);  
                 }  
               } else {  
                 !!!cp (132);  
               }  
             } else {  
               !!!cp (133);  
             }  
           } else {  
             !!!cp (134);  
           }  
         } else {  
           !!!cp (135);  
         }  
2068        } else {        } else {
2069          !!!cp (136);          !!!cp (136);
2070        }        }
2071    
2072        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2073        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2074        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2075          ## Reconsume.
2076        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2077          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2078                                    line => $self->{line_prev},
2079                                    column => $self->{column_prev} - 1,
2080                                   };
2081        redo A;        redo A;
2082              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2083        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2084        ## 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);
2085            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2086                                      line => $self->{line_prev},
2087                                      column => $self->{column_prev} - 2,
2088                                     };
2089            $self->{state} = COMMENT_START_STATE;
2090            !!!next-input-character;
2091            redo A;
2092          } else {
2093            !!!cp (128);
2094            !!!parse-error (type => 'bogus comment',
2095                            line => $self->{line_prev},
2096                            column => $self->{column_prev} - 2);
2097            $self->{state} = BOGUS_COMMENT_STATE;
2098            ## Reconsume.
2099            $self->{current_token} = {type => COMMENT_TOKEN,
2100                                      data => '-',
2101                                      line => $self->{line_prev},
2102                                      column => $self->{column_prev} - 2,
2103                                     };
2104            redo A;
2105          }
2106        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2107          ## ASCII case-insensitive.
2108          if ($self->{next_char} == [
2109                undef,
2110                0x004F, # O
2111                0x0043, # C
2112                0x0054, # T
2113                0x0059, # Y
2114                0x0050, # P
2115              ]->[length $self->{state_keyword}] or
2116              $self->{next_char} == [
2117                undef,
2118                0x006F, # o
2119                0x0063, # c
2120                0x0074, # t
2121                0x0079, # y
2122                0x0070, # p
2123              ]->[length $self->{state_keyword}]) {
2124            !!!cp (131);
2125            ## Stay in the state.
2126            $self->{state_keyword} .= chr $self->{next_char};
2127            !!!next-input-character;
2128            redo A;
2129          } elsif ((length $self->{state_keyword}) == 6 and
2130                   ($self->{next_char} == 0x0045 or # E
2131                    $self->{next_char} == 0x0065)) { # e
2132            !!!cp (129);
2133            $self->{state} = DOCTYPE_STATE;
2134            $self->{current_token} = {type => DOCTYPE_TOKEN,
2135                                      quirks => 1,
2136                                      line => $self->{line_prev},
2137                                      column => $self->{column_prev} - 7,
2138                                     };
2139            !!!next-input-character;
2140            redo A;
2141          } else {
2142            !!!cp (132);        
2143            !!!parse-error (type => 'bogus comment',
2144                            line => $self->{line_prev},
2145                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2146            $self->{state} = BOGUS_COMMENT_STATE;
2147            ## Reconsume.
2148            $self->{current_token} = {type => COMMENT_TOKEN,
2149                                      data => $self->{state_keyword},
2150                                      line => $self->{line_prev},
2151                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2152                                     };
2153            redo A;
2154          }
2155        } elsif ($self->{state} == MD_CDATA_STATE) {
2156          if ($self->{next_char} == {
2157                '[' => 0x0043, # C
2158                '[C' => 0x0044, # D
2159                '[CD' => 0x0041, # A
2160                '[CDA' => 0x0054, # T
2161                '[CDAT' => 0x0041, # A
2162              }->{$self->{state_keyword}}) {
2163            !!!cp (135.1);
2164            ## Stay in the state.
2165            $self->{state_keyword} .= chr $self->{next_char};
2166            !!!next-input-character;
2167            redo A;
2168          } elsif ($self->{state_keyword} eq '[CDATA' and
2169                   $self->{next_char} == 0x005B) { # [
2170            !!!cp (135.2);
2171            $self->{current_token} = {type => CHARACTER_TOKEN,
2172                                      data => '',
2173                                      line => $self->{line_prev},
2174                                      column => $self->{column_prev} - 7};
2175            $self->{state} = CDATA_SECTION_STATE;
2176            !!!next-input-character;
2177            redo A;
2178          } else {
2179            !!!cp (135.3);
2180            !!!parse-error (type => 'bogus comment',
2181                            line => $self->{line_prev},
2182                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2183            $self->{state} = BOGUS_COMMENT_STATE;
2184            ## Reconsume.
2185            $self->{current_token} = {type => COMMENT_TOKEN,
2186                                      data => $self->{state_keyword},
2187                                      line => $self->{line_prev},
2188                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2189                                     };
2190            redo A;
2191          }
2192      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2193        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2194          !!!cp (137);          !!!cp (137);
# Line 1566  sub _get_next_token ($) { Line 2271  sub _get_next_token ($) {
2271        } else {        } else {
2272          !!!cp (147);          !!!cp (147);
2273          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2274            $self->{read_until}->($self->{current_token}->{data},
2275                                  q[-],
2276                                  length $self->{current_token}->{data});
2277    
2278          ## Stay in the state          ## Stay in the state
2279          !!!next-input-character;          !!!next-input-character;
2280          redo A;          redo A;
# Line 1603  sub _get_next_token ($) { Line 2312  sub _get_next_token ($) {
2312          redo A;          redo A;
2313        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2314          !!!cp (152);          !!!cp (152);
2315          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2316                            line => $self->{line_prev},
2317                            column => $self->{column_prev});
2318          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2319          ## Stay in the state          ## Stay in the state
2320          !!!next-input-character;          !!!next-input-character;
# Line 1619  sub _get_next_token ($) { Line 2330  sub _get_next_token ($) {
2330          redo A;          redo A;
2331        } else {        } else {
2332          !!!cp (154);          !!!cp (154);
2333          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2334                            line => $self->{line_prev},
2335                            column => $self->{column_prev});
2336          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2337          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2338          !!!next-input-character;          !!!next-input-character;
# Line 1658  sub _get_next_token ($) { Line 2371  sub _get_next_token ($) {
2371          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2372          !!!next-input-character;          !!!next-input-character;
2373    
2374          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2375    
2376          redo A;          redo A;
2377        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1667  sub _get_next_token ($) { Line 2380  sub _get_next_token ($) {
2380          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2381          ## reconsume          ## reconsume
2382    
2383          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2384    
2385          redo A;          redo A;
2386        } else {        } else {
2387          !!!cp (160);          !!!cp (160);
2388          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2389              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2390  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2391          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2392          !!!next-input-character;          !!!next-input-character;
# Line 1749  sub _get_next_token ($) { Line 2459  sub _get_next_token ($) {
2459          redo A;          redo A;
2460        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2461                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2462            $self->{state} = PUBLIC_STATE;
2463            $self->{state_keyword} = chr $self->{next_char};
2464          !!!next-input-character;          !!!next-input-character;
2465          if ($self->{next_char} == 0x0055 or # U          redo A;
             $self->{next_char} == 0x0075) { # u  
           !!!next-input-character;  
           if ($self->{next_char} == 0x0042 or # B  
               $self->{next_char} == 0x0062) { # b  
             !!!next-input-character;  
             if ($self->{next_char} == 0x004C or # L  
                 $self->{next_char} == 0x006C) { # l  
               !!!next-input-character;  
               if ($self->{next_char} == 0x0049 or # I  
                   $self->{next_char} == 0x0069) { # i  
                 !!!next-input-character;  
                 if ($self->{next_char} == 0x0043 or # C  
                     $self->{next_char} == 0x0063) { # c  
                   !!!cp (168);  
                   $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;  
                   !!!next-input-character;  
                   redo A;  
                 } else {  
                   !!!cp (169);  
                 }  
               } else {  
                 !!!cp (170);  
               }  
             } else {  
               !!!cp (171);  
             }  
           } else {  
             !!!cp (172);  
           }  
         } else {  
           !!!cp (173);  
         }  
   
         #  
2466        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2467                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2468            $self->{state} = SYSTEM_STATE;
2469            $self->{state_keyword} = chr $self->{next_char};
2470          !!!next-input-character;          !!!next-input-character;
2471          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);  
         }  
   
         #  
2472        } else {        } else {
2473          !!!cp (180);          !!!cp (180);
2474            !!!parse-error (type => 'string after DOCTYPE name');
2475            $self->{current_token}->{quirks} = 1;
2476    
2477            $self->{state} = BOGUS_DOCTYPE_STATE;
2478          !!!next-input-character;          !!!next-input-character;
2479          #          redo A;
2480        }        }
2481        } elsif ($self->{state} == PUBLIC_STATE) {
2482          ## ASCII case-insensitive
2483          if ($self->{next_char} == [
2484                undef,
2485                0x0055, # U
2486                0x0042, # B
2487                0x004C, # L
2488                0x0049, # I
2489              ]->[length $self->{state_keyword}] or
2490              $self->{next_char} == [
2491                undef,
2492                0x0075, # u
2493                0x0062, # b
2494                0x006C, # l
2495                0x0069, # i
2496              ]->[length $self->{state_keyword}]) {
2497            !!!cp (175);
2498            ## Stay in the state.
2499            $self->{state_keyword} .= chr $self->{next_char};
2500            !!!next-input-character;
2501            redo A;
2502          } elsif ((length $self->{state_keyword}) == 5 and
2503                   ($self->{next_char} == 0x0043 or # C
2504                    $self->{next_char} == 0x0063)) { # c
2505            !!!cp (168);
2506            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2507            !!!next-input-character;
2508            redo A;
2509          } else {
2510            !!!cp (169);
2511            !!!parse-error (type => 'string after DOCTYPE name',
2512                            line => $self->{line_prev},
2513                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2514            $self->{current_token}->{quirks} = 1;
2515    
2516        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2517        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2518            redo A;
2519          }
2520        } elsif ($self->{state} == SYSTEM_STATE) {
2521          ## ASCII case-insensitive
2522          if ($self->{next_char} == [
2523                undef,
2524                0x0059, # Y
2525                0x0053, # S
2526                0x0054, # T
2527                0x0045, # E
2528              ]->[length $self->{state_keyword}] or
2529              $self->{next_char} == [
2530                undef,
2531                0x0079, # y
2532                0x0073, # s
2533                0x0074, # t
2534                0x0065, # e
2535              ]->[length $self->{state_keyword}]) {
2536            !!!cp (170);
2537            ## Stay in the state.
2538            $self->{state_keyword} .= chr $self->{next_char};
2539            !!!next-input-character;
2540            redo A;
2541          } elsif ((length $self->{state_keyword}) == 5 and
2542                   ($self->{next_char} == 0x004D or # M
2543                    $self->{next_char} == 0x006D)) { # m
2544            !!!cp (171);
2545            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2546            !!!next-input-character;
2547            redo A;
2548          } else {
2549            !!!cp (172);
2550            !!!parse-error (type => 'string after DOCTYPE name',
2551                            line => $self->{line_prev},
2552                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2553            $self->{current_token}->{quirks} = 1;
2554    
2555        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2556        # next-input-character is already done          ## Reconsume.
2557        redo A;          redo A;
2558          }
2559      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2560        if ({        if ({
2561              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 1919  sub _get_next_token ($) { Line 2640  sub _get_next_token ($) {
2640          !!!cp (190);          !!!cp (190);
2641          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2642              .= chr $self->{next_char};              .= chr $self->{next_char};
2643            $self->{read_until}->($self->{current_token}->{public_identifier},
2644                                  q[">],
2645                                  length $self->{current_token}->{public_identifier});
2646    
2647          ## Stay in the state          ## Stay in the state
2648          !!!next-input-character;          !!!next-input-character;
2649          redo A;          redo A;
# Line 1955  sub _get_next_token ($) { Line 2680  sub _get_next_token ($) {
2680          !!!cp (194);          !!!cp (194);
2681          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2682              .= chr $self->{next_char};              .= chr $self->{next_char};
2683            $self->{read_until}->($self->{current_token}->{public_identifier},
2684                                  q['>],
2685                                  length $self->{current_token}->{public_identifier});
2686    
2687          ## Stay in the state          ## Stay in the state
2688          !!!next-input-character;          !!!next-input-character;
2689          redo A;          redo A;
# Line 2067  sub _get_next_token ($) { Line 2796  sub _get_next_token ($) {
2796          redo A;          redo A;
2797        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2798          !!!cp (208);          !!!cp (208);
2799          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2800    
2801          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2802          !!!next-input-character;          !!!next-input-character;
# Line 2091  sub _get_next_token ($) { Line 2820  sub _get_next_token ($) {
2820          !!!cp (210);          !!!cp (210);
2821          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2822              .= chr $self->{next_char};              .= chr $self->{next_char};
2823            $self->{read_until}->($self->{current_token}->{system_identifier},
2824                                  q[">],
2825                                  length $self->{current_token}->{system_identifier});
2826    
2827          ## Stay in the state          ## Stay in the state
2828          !!!next-input-character;          !!!next-input-character;
2829          redo A;          redo A;
# Line 2103  sub _get_next_token ($) { Line 2836  sub _get_next_token ($) {
2836          redo A;          redo A;
2837        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2838          !!!cp (212);          !!!cp (212);
2839          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2840    
2841          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2842          !!!next-input-character;          !!!next-input-character;
# Line 2127  sub _get_next_token ($) { Line 2860  sub _get_next_token ($) {
2860          !!!cp (214);          !!!cp (214);
2861          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2862              .= chr $self->{next_char};              .= chr $self->{next_char};
2863            $self->{read_until}->($self->{current_token}->{system_identifier},
2864                                  q['>],
2865                                  length $self->{current_token}->{system_identifier});
2866    
2867          ## Stay in the state          ## Stay in the state
2868          !!!next-input-character;          !!!next-input-character;
2869          redo A;          redo A;
# Line 2151  sub _get_next_token ($) { Line 2888  sub _get_next_token ($) {
2888        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2889          !!!cp (217);          !!!cp (217);
2890          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2891          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2892          ## reconsume          ## reconsume
2893    
# Line 2188  sub _get_next_token ($) { Line 2924  sub _get_next_token ($) {
2924          redo A;          redo A;
2925        } else {        } else {
2926          !!!cp (221);          !!!cp (221);
2927            my $s = '';
2928            $self->{read_until}->($s, q[>], 0);
2929    
2930          ## Stay in the state          ## Stay in the state
2931          !!!next-input-character;          !!!next-input-character;
2932          redo A;          redo A;
2933        }        }
2934      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2935        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2936      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2937    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2938          
2939    die "$0: _get_next_token: unexpected case";        if ($self->{next_char} == 0x005D) { # ]
2940  } # _get_next_token          !!!cp (221.1);
2941            $self->{state} = CDATA_SECTION_MSE1_STATE;
2942            !!!next-input-character;
2943            redo A;
2944          } elsif ($self->{next_char} == -1) {
2945            $self->{state} = DATA_STATE;
2946            !!!next-input-character;
2947            if (length $self->{current_token}->{data}) { # character
2948              !!!cp (221.2);
2949              !!!emit ($self->{current_token}); # character
2950            } else {
2951              !!!cp (221.3);
2952              ## No token to emit. $self->{current_token} is discarded.
2953            }        
2954            redo A;
2955          } else {
2956            !!!cp (221.4);
2957            $self->{current_token}->{data} .= chr $self->{next_char};
2958            $self->{read_until}->($self->{current_token}->{data},
2959                                  q<]>,
2960                                  length $self->{current_token}->{data});
2961    
2962  sub _tokenize_attempt_to_consume_an_entity ($$$) {          ## Stay in the state.
2963    my ($self, $in_attr, $additional) = @_;          !!!next-input-character;
2964            redo A;
2965          }
2966    
2967    if ({        ## ISSUE: "text tokens" in spec.
2968         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2969         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{next_char} == 0x005D) { # ]
2970         $additional => 1,          !!!cp (221.5);
2971        }->{$self->{next_char}}) {          $self->{state} = CDATA_SECTION_MSE2_STATE;
2972      !!!cp (1001);          !!!next-input-character;
2973      ## Don't consume          redo A;
2974      ## No error        } else {
2975      return undef;          !!!cp (221.6);
2976    } elsif ($self->{next_char} == 0x0023) { # #          $self->{current_token}->{data} .= ']';
2977      !!!next-input-character;          $self->{state} = CDATA_SECTION_STATE;
2978      if ($self->{next_char} == 0x0078 or # x          ## Reconsume.
2979          $self->{next_char} == 0x0058) { # X          redo A;
2980        my $code;        }
2981        X: {      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2982          my $x_char = $self->{next_char};        if ($self->{next_char} == 0x003E) { # >
2983          !!!next-input-character;          $self->{state} = DATA_STATE;
2984          if (0x0030 <= $self->{next_char} and          !!!next-input-character;
2985              $self->{next_char} <= 0x0039) { # 0..9          if (length $self->{current_token}->{data}) { # character
2986            !!!cp (1002);            !!!cp (221.7);
2987            $code ||= 0;            !!!emit ($self->{current_token}); # character
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0030;  
           redo X;  
         } elsif (0x0061 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0066) { # a..f  
           !!!cp (1003);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0060 + 9;  
           redo X;  
         } elsif (0x0041 <= $self->{next_char} and  
                  $self->{next_char} <= 0x0046) { # A..F  
           !!!cp (1004);  
           $code ||= 0;  
           $code *= 0x10;  
           $code += $self->{next_char} - 0x0040 + 9;  
           redo X;  
         } elsif (not defined $code) { # no hexadecimal digit  
           !!!cp (1005);  
           !!!parse-error (type => 'bare hcro');  
           !!!back-next-input-character ($x_char, $self->{next_char});  
           $self->{next_char} = 0x0023; # #  
           return undef;  
         } elsif ($self->{next_char} == 0x003B) { # ;  
           !!!cp (1006);  
           !!!next-input-character;  
2988          } else {          } else {
2989            !!!cp (1007);            !!!cp (221.8);
2990            !!!parse-error (type => 'no refc');            ## No token to emit. $self->{current_token} is discarded.
2991          }          }
2992            redo A;
2993          } elsif ($self->{next_char} == 0x005D) { # ]
2994            !!!cp (221.9); # character
2995            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2996            ## Stay in the state.
2997            !!!next-input-character;
2998            redo A;
2999          } else {
3000            !!!cp (221.11);
3001            $self->{current_token}->{data} .= ']]'; # character
3002            $self->{state} = CDATA_SECTION_STATE;
3003            ## Reconsume.
3004            redo A;
3005          }
3006        } elsif ($self->{state} == ENTITY_STATE) {
3007          if ({
3008            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3009            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3010            $self->{entity_additional} => 1,
3011          }->{$self->{next_char}}) {
3012            !!!cp (1001);
3013            ## Don't consume
3014            ## No error
3015            ## Return nothing.
3016            #
3017          } elsif ($self->{next_char} == 0x0023) { # #
3018            !!!cp (999);
3019            $self->{state} = ENTITY_HASH_STATE;
3020            $self->{state_keyword} = '#';
3021            !!!next-input-character;
3022            redo A;
3023          } elsif ((0x0041 <= $self->{next_char} and
3024                    $self->{next_char} <= 0x005A) or # A..Z
3025                   (0x0061 <= $self->{next_char} and
3026                    $self->{next_char} <= 0x007A)) { # a..z
3027            !!!cp (998);
3028            require Whatpm::_NamedEntityList;
3029            $self->{state} = ENTITY_NAME_STATE;
3030            $self->{state_keyword} = chr $self->{next_char};
3031            $self->{entity__value} = $self->{state_keyword};
3032            $self->{entity__match} = 0;
3033            !!!next-input-character;
3034            redo A;
3035          } else {
3036            !!!cp (1027);
3037            !!!parse-error (type => 'bare ero');
3038            ## Return nothing.
3039            #
3040          }
3041    
3042          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3043            !!!cp (1008);        ## reference" algorithm.  In other word, there is an "&" character
3044            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## that does not introduce a character reference, which would be
3045            $code = 0xFFFD;        ## appended to the parent element or the attribute value in later
3046          } elsif ($code > 0x10FFFF) {        ## process of the tokenizer.
3047            !!!cp (1009);  
3048            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        if ($self->{prev_state} == DATA_STATE) {
3049            $code = 0xFFFD;          !!!cp (997);
3050          } elsif ($code == 0x000D) {          $self->{state} = $self->{prev_state};
3051            !!!cp (1010);          ## Reconsume.
3052            !!!parse-error (type => 'CR character reference');          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3053            $code = 0x000A;                    line => $self->{line_prev},
3054          } elsif (0x80 <= $code and $code <= 0x9F) {                    column => $self->{column_prev},
3055            !!!cp (1011);                   });
3056            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          redo A;
3057            $code = $c1_entity_char->{$code};        } else {
3058          }          !!!cp (996);
3059            $self->{current_attribute}->{value} .= '&';
3060          return {type => CHARACTER_TOKEN, data => chr $code,          $self->{state} = $self->{prev_state};
3061                  has_reference => 1};          ## Reconsume.
3062        } # X          redo A;
3063      } elsif (0x0030 <= $self->{next_char} and        }
3064               $self->{next_char} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3065        my $code = $self->{next_char} - 0x0030;        if ($self->{next_char} == 0x0078 or # x
3066        !!!next-input-character;            $self->{next_char} == 0x0058) { # X
3067                  !!!cp (995);
3068        while (0x0030 <= $self->{next_char} and          $self->{state} = HEXREF_X_STATE;
3069                  $self->{next_char} <= 0x0039) { # 0..9          $self->{state_keyword} .= chr $self->{next_char};
3070            !!!next-input-character;
3071            redo A;
3072          } elsif (0x0030 <= $self->{next_char} and
3073                   $self->{next_char} <= 0x0039) { # 0..9
3074            !!!cp (994);
3075            $self->{state} = NCR_NUM_STATE;
3076            $self->{state_keyword} = $self->{next_char} - 0x0030;
3077            !!!next-input-character;
3078            redo A;
3079          } else {
3080            !!!parse-error (type => 'bare nero',
3081                            line => $self->{line_prev},
3082                            column => $self->{column_prev} - 1);
3083    
3084            ## NOTE: According to the spec algorithm, nothing is returned,
3085            ## and then "&#" is appended to the parent element or the attribute
3086            ## value in the later processing.
3087    
3088            if ($self->{prev_state} == DATA_STATE) {
3089              !!!cp (1019);
3090              $self->{state} = $self->{prev_state};
3091              ## Reconsume.
3092              !!!emit ({type => CHARACTER_TOKEN,
3093                        data => '&#',
3094                        line => $self->{line_prev},
3095                        column => $self->{column_prev} - 1,
3096                       });
3097              redo A;
3098            } else {
3099              !!!cp (993);
3100              $self->{current_attribute}->{value} .= '&#';
3101              $self->{state} = $self->{prev_state};
3102              ## Reconsume.
3103              redo A;
3104            }
3105          }
3106        } elsif ($self->{state} == NCR_NUM_STATE) {
3107          if (0x0030 <= $self->{next_char} and
3108              $self->{next_char} <= 0x0039) { # 0..9
3109          !!!cp (1012);          !!!cp (1012);
3110          $code *= 10;          $self->{state_keyword} *= 10;
3111          $code += $self->{next_char} - 0x0030;          $self->{state_keyword} += $self->{next_char} - 0x0030;
3112                    
3113            ## Stay in the state.
3114          !!!next-input-character;          !!!next-input-character;
3115        }          redo A;
3116          } elsif ($self->{next_char} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3117          !!!cp (1013);          !!!cp (1013);
3118          !!!next-input-character;          !!!next-input-character;
3119            #
3120        } else {        } else {
3121          !!!cp (1014);          !!!cp (1014);
3122          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3123            ## Reconsume.
3124            #
3125        }        }
3126    
3127          my $code = $self->{state_keyword};
3128          my $l = $self->{line_prev};
3129          my $c = $self->{column_prev};
3130        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3131          !!!cp (1015);          !!!cp (1015);
3132          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
3133                            text => (sprintf 'U+%04X', $code),
3134                            line => $l, column => $c);
3135          $code = 0xFFFD;          $code = 0xFFFD;
3136        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3137          !!!cp (1016);          !!!cp (1016);
3138          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
3139                            text => (sprintf 'U-%08X', $code),
3140                            line => $l, column => $c);
3141          $code = 0xFFFD;          $code = 0xFFFD;
3142        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3143          !!!cp (1017);          !!!cp (1017);
3144          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
3145                            line => $l, column => $c);
3146          $code = 0x000A;          $code = 0x000A;
3147        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3148          !!!cp (1018);          !!!cp (1018);
3149          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3150                            text => (sprintf 'U+%04X', $code),
3151                            line => $l, column => $c);
3152          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3153        }        }
3154          
3155        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        if ($self->{prev_state} == DATA_STATE) {
3156      } else {          !!!cp (992);
3157        !!!cp (1019);          $self->{state} = $self->{prev_state};
3158        !!!parse-error (type => 'bare nero');          ## Reconsume.
3159        !!!back-next-input-character ($self->{next_char});          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3160        $self->{next_char} = 0x0023; # #                    line => $l, column => $c,
3161        return undef;                   });
3162      }          redo A;
3163    } elsif ((0x0041 <= $self->{next_char} and        } else {
3164              $self->{next_char} <= 0x005A) or          !!!cp (991);
3165             (0x0061 <= $self->{next_char} and          $self->{current_attribute}->{value} .= chr $code;
3166              $self->{next_char} <= 0x007A)) {          $self->{current_attribute}->{has_reference} = 1;
3167      my $entity_name = chr $self->{next_char};          $self->{state} = $self->{prev_state};
3168      !!!next-input-character;          ## Reconsume.
3169            redo A;
3170      my $value = $entity_name;        }
3171      my $match = 0;      } elsif ($self->{state} == HEXREF_X_STATE) {
3172      require Whatpm::_NamedEntityList;        if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3173      our $EntityChar;            (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3174              (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3175      while (length $entity_name < 10 and          # 0..9, A..F, a..f
3176             ## NOTE: Some number greater than the maximum length of entity name          !!!cp (990);
3177             ((0x0041 <= $self->{next_char} and # a          $self->{state} = HEXREF_HEX_STATE;
3178               $self->{next_char} <= 0x005A) or # x          $self->{state_keyword} = 0;
3179              (0x0061 <= $self->{next_char} and # a          ## Reconsume.
3180               $self->{next_char} <= 0x007A) or # z          redo A;
3181              (0x0030 <= $self->{next_char} and # 0        } else {
3182               $self->{next_char} <= 0x0039) or # 9          !!!parse-error (type => 'bare hcro',
3183              $self->{next_char} == 0x003B)) { # ;                          line => $self->{line_prev},
3184        $entity_name .= chr $self->{next_char};                          column => $self->{column_prev} - 2);
3185        if (defined $EntityChar->{$entity_name}) {  
3186          if ($self->{next_char} == 0x003B) { # ;          ## NOTE: According to the spec algorithm, nothing is returned,
3187            !!!cp (1020);          ## and then "&#" followed by "X" or "x" is appended to the parent
3188            $value = $EntityChar->{$entity_name};          ## element or the attribute value in the later processing.
3189            $match = 1;  
3190            !!!next-input-character;          if ($self->{prev_state} == DATA_STATE) {
3191            last;            !!!cp (1005);
3192              $self->{state} = $self->{prev_state};
3193              ## Reconsume.
3194              !!!emit ({type => CHARACTER_TOKEN,
3195                        data => '&' . $self->{state_keyword},
3196                        line => $self->{line_prev},
3197                        column => $self->{column_prev} - length $self->{state_keyword},
3198                       });
3199              redo A;
3200          } else {          } else {
3201            !!!cp (1021);            !!!cp (989);
3202            $value = $EntityChar->{$entity_name};            $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3203            $match = -1;            $self->{state} = $self->{prev_state};
3204            !!!next-input-character;            ## Reconsume.
3205              redo A;
3206          }          }
3207        } else {        }
3208          !!!cp (1022);      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3209          $value .= chr $self->{next_char};        if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3210          $match *= 2;          # 0..9
3211            !!!cp (1002);
3212            $self->{state_keyword} *= 0x10;
3213            $self->{state_keyword} += $self->{next_char} - 0x0030;
3214            ## Stay in the state.
3215            !!!next-input-character;
3216            redo A;
3217          } elsif (0x0061 <= $self->{next_char} and
3218                   $self->{next_char} <= 0x0066) { # a..f
3219            !!!cp (1003);
3220            $self->{state_keyword} *= 0x10;
3221            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3222            ## Stay in the state.
3223          !!!next-input-character;          !!!next-input-character;
3224            redo A;
3225          } elsif (0x0041 <= $self->{next_char} and
3226                   $self->{next_char} <= 0x0046) { # A..F
3227            !!!cp (1004);
3228            $self->{state_keyword} *= 0x10;
3229            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3230            ## Stay in the state.
3231            !!!next-input-character;
3232            redo A;
3233          } elsif ($self->{next_char} == 0x003B) { # ;
3234            !!!cp (1006);
3235            !!!next-input-character;
3236            #
3237          } else {
3238            !!!cp (1007);
3239            !!!parse-error (type => 'no refc',
3240                            line => $self->{line},
3241                            column => $self->{column});
3242            ## Reconsume.
3243            #
3244        }        }
3245      }  
3246              my $code = $self->{state_keyword};
3247      if ($match > 0) {        my $l = $self->{line_prev};
3248        !!!cp (1023);        my $c = $self->{column_prev};
3249        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3250      } elsif ($match < 0) {          !!!cp (1008);
3251        !!!parse-error (type => 'no refc');          !!!parse-error (type => 'invalid character reference',
3252        if ($in_attr and $match < -1) {                          text => (sprintf 'U+%04X', $code),
3253          !!!cp (1024);                          line => $l, column => $c);
3254          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          $code = 0xFFFD;
3255          } elsif ($code > 0x10FFFF) {
3256            !!!cp (1009);
3257            !!!parse-error (type => 'invalid character reference',
3258                            text => (sprintf 'U-%08X', $code),
3259                            line => $l, column => $c);
3260            $code = 0xFFFD;
3261          } elsif ($code == 0x000D) {
3262            !!!cp (1010);
3263            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3264            $code = 0x000A;
3265          } elsif (0x80 <= $code and $code <= 0x9F) {
3266            !!!cp (1011);
3267            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3268            $code = $c1_entity_char->{$code};
3269          }
3270    
3271          if ($self->{prev_state} == DATA_STATE) {
3272            !!!cp (988);
3273            $self->{state} = $self->{prev_state};
3274            ## Reconsume.
3275            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3276                      line => $l, column => $c,
3277                     });
3278            redo A;
3279          } else {
3280            !!!cp (987);
3281            $self->{current_attribute}->{value} .= chr $code;
3282            $self->{current_attribute}->{has_reference} = 1;
3283            $self->{state} = $self->{prev_state};
3284            ## Reconsume.
3285            redo A;
3286          }
3287        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3288          if (length $self->{state_keyword} < 30 and
3289              ## NOTE: Some number greater than the maximum length of entity name
3290              ((0x0041 <= $self->{next_char} and # a
3291                $self->{next_char} <= 0x005A) or # x
3292               (0x0061 <= $self->{next_char} and # a
3293                $self->{next_char} <= 0x007A) or # z
3294               (0x0030 <= $self->{next_char} and # 0
3295                $self->{next_char} <= 0x0039) or # 9
3296               $self->{next_char} == 0x003B)) { # ;
3297            our $EntityChar;
3298            $self->{state_keyword} .= chr $self->{next_char};
3299            if (defined $EntityChar->{$self->{state_keyword}}) {
3300              if ($self->{next_char} == 0x003B) { # ;
3301                !!!cp (1020);
3302                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3303                $self->{entity__match} = 1;
3304                !!!next-input-character;
3305                #
3306              } else {
3307                !!!cp (1021);
3308                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3309                $self->{entity__match} = -1;
3310                ## Stay in the state.
3311                !!!next-input-character;
3312                redo A;
3313              }
3314            } else {
3315              !!!cp (1022);
3316              $self->{entity__value} .= chr $self->{next_char};
3317              $self->{entity__match} *= 2;
3318              ## Stay in the state.
3319              !!!next-input-character;
3320              redo A;
3321            }
3322          }
3323    
3324          my $data;
3325          my $has_ref;
3326          if ($self->{entity__match} > 0) {
3327            !!!cp (1023);
3328            $data = $self->{entity__value};
3329            $has_ref = 1;
3330            #
3331          } elsif ($self->{entity__match} < 0) {
3332            !!!parse-error (type => 'no refc');
3333            if ($self->{prev_state} != DATA_STATE and # in attribute
3334                $self->{entity__match} < -1) {
3335              !!!cp (1024);
3336              $data = '&' . $self->{state_keyword};
3337              #
3338            } else {
3339              !!!cp (1025);
3340              $data = $self->{entity__value};
3341              $has_ref = 1;
3342              #
3343            }
3344        } else {        } else {
3345          !!!cp (1025);          !!!cp (1026);
3346          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          !!!parse-error (type => 'bare ero',
3347                            line => $self->{line_prev},
3348                            column => $self->{column_prev} - length $self->{state_keyword});
3349            $data = '&' . $self->{state_keyword};
3350            #
3351          }
3352      
3353          ## NOTE: In these cases, when a character reference is found,
3354          ## it is consumed and a character token is returned, or, otherwise,
3355          ## nothing is consumed and returned, according to the spec algorithm.
3356          ## In this implementation, anything that has been examined by the
3357          ## tokenizer is appended to the parent element or the attribute value
3358          ## as string, either literal string when no character reference or
3359          ## entity-replaced string otherwise, in this stage, since any characters
3360          ## that would not be consumed are appended in the data state or in an
3361          ## appropriate attribute value state anyway.
3362    
3363          if ($self->{prev_state} == DATA_STATE) {
3364            !!!cp (986);
3365            $self->{state} = $self->{prev_state};
3366            ## Reconsume.
3367            !!!emit ({type => CHARACTER_TOKEN,
3368                      data => $data,
3369                      line => $self->{line_prev},
3370                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3371                     });
3372            redo A;
3373          } else {
3374            !!!cp (985);
3375            $self->{current_attribute}->{value} .= $data;
3376            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3377            $self->{state} = $self->{prev_state};
3378            ## Reconsume.
3379            redo A;
3380        }        }
3381      } else {      } else {
3382        !!!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};  
3383      }      }
3384    } else {    } # A  
3385      !!!cp (1027);  
3386      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3387      !!!parse-error (type => 'bare ero');  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3388    
3389  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3390    my $self = shift;    my $self = shift;
# Line 2400  sub _initialize_tree_constructor ($) { Line 3393  sub _initialize_tree_constructor ($) {
3393    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3394    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3395    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3396      $self->{document}->set_user_data (manakai_source_line => 1);
3397      $self->{document}->set_user_data (manakai_source_column => 1);
3398  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3399    
3400  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2454  sub _tree_construction_initial ($) { Line 3449  sub _tree_construction_initial ($) {
3449        ## language.        ## language.
3450        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3451        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3452        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3453        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3454            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3455          !!!cp ('t1');          !!!cp ('t1');
3456          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3457        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3458          !!!cp ('t2');          !!!cp ('t2');
3459          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!parse-error (type => 'not HTML5', token => $token);
3460          !!!parse-error (type => 'not HTML5');        } elsif (defined $token->{public_identifier}) {
3461            if ($token->{public_identifier} eq 'XSLT-compat') {
3462              !!!cp ('t1.2');
3463              !!!parse-error (type => 'XSLT-compat', token => $token,
3464                              level => $self->{level}->{should});
3465            } else {
3466              !!!parse-error (type => 'not HTML5', token => $token);
3467            }
3468        } else {        } else {
3469          !!!cp ('t3');          !!!cp ('t3');
3470            #
3471        }        }
3472                
3473        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3474          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3475          ## NOTE: Default value for both |public_id| and |system_id| attributes
3476          ## are empty strings, so that we don't set any value in missing cases.
3477        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3478            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3479        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2484  sub _tree_construction_initial ($) { Line 3488  sub _tree_construction_initial ($) {
3488        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3489          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3490          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3491          if ({          my $prefix = [
3492            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3493            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3494            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3495            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3496            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3497            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3498            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3499            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3500            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3501            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3502            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3503            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3504            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3505            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3506            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3507            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3508            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3509            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3510            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3511            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3512            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3513            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3514            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3515            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3516            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3517            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3518            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3519            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3520            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3521            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3522            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3523            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3524            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3525            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3526            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3527            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3528            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3529            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3530            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3531            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3532            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3533            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3534            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3535            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3536            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3537            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3538            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3539            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3540            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3541            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3542            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3543            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3544            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3545            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3546            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3547            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3548            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3549            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3550            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3551            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3552            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3553            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3554            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3555            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3556            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3557            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3558            "-//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}) {  
3559            !!!cp ('t5');            !!!cp ('t5');
3560            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3561          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3562                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3563            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3564              !!!cp ('t6');              !!!cp ('t6');
3565              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2569  sub _tree_construction_initial ($) { Line 3567  sub _tree_construction_initial ($) {
3567              !!!cp ('t7');              !!!cp ('t7');
3568              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3569            }            }
3570          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3571                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3572            !!!cp ('t8');            !!!cp ('t8');
3573            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3574          } else {          } else {
# Line 2583  sub _tree_construction_initial ($) { Line 3581  sub _tree_construction_initial ($) {
3581          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3582          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3583          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") {
3584            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3585              ## marked as quirks.
3586            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3587            !!!cp ('t11');            !!!cp ('t11');
3588          } else {          } else {
# Line 2602  sub _tree_construction_initial ($) { Line 3601  sub _tree_construction_initial ($) {
3601                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3602               }->{$token->{type}}) {               }->{$token->{type}}) {
3603        !!!cp ('t14');        !!!cp ('t14');
3604        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3605        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3606        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3607        ## reprocess        ## reprocess
3608          !!!ack-later;
3609        return;        return;
3610      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3611        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2623  sub _tree_construction_initial ($) { Line 3623  sub _tree_construction_initial ($) {
3623          !!!cp ('t17');          !!!cp ('t17');
3624        }        }
3625    
3626        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3627        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3628        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3629        ## reprocess        ## reprocess
# Line 2652  sub _tree_construction_root_element ($) Line 3652  sub _tree_construction_root_element ($)
3652    B: {    B: {
3653        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3654          !!!cp ('t19');          !!!cp ('t19');
3655          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3656          ## Ignore the token          ## Ignore the token
3657          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3658          !!!next-token;          !!!next-token;
# Line 2686  sub _tree_construction_root_element ($) Line 3686  sub _tree_construction_root_element ($)
3686        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3687          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3688            my $root_element;            my $root_element;
3689            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3690            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3691            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3692                  [$root_element, $el_category->{html}];
3693    
3694            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3695              !!!cp ('t24');              !!!cp ('t24');
3696              $self->{application_cache_selection}              $self->{application_cache_selection}
3697                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3698              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3699                ## According to Hixie (#whatwg 2008-03-19), it should be
3700                ## resolved against the base URI of the document in HTML
3701                ## or xml:base of the element in XHTML.
3702            } else {            } else {
3703              !!!cp ('t25');              !!!cp ('t25');
3704              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3705            }            }
3706    
3707              !!!nack ('t25c');
3708    
3709            !!!next-token;            !!!next-token;
3710            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3711          } else {          } else {
# Line 2716  sub _tree_construction_root_element ($) Line 3722  sub _tree_construction_root_element ($)
3722          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3723        }        }
3724    
3725      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3726        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3727      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3728      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3729    
3730      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3731    
3732      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3733        !!!ack-later;
3734      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3735    
3736      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2743  sub _reset_insertion_mode ($) { Line 3751  sub _reset_insertion_mode ($) {
3751            
3752      ## Step 3      ## Step 3
3753      S3: {      S3: {
       ## ISSUE: Oops! "If node is the first node in the stack of open  
       ## elements, then set last to true. If the context element of the  
       ## HTML fragment parsing algorithm is neither a td element nor a  
       ## th element, then set node to the context element. (fragment case)":  
       ## The second "if" is in the scope of the first "if"!?  
3754        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3755          $last = 1;          $last = 1;
3756          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3757            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3758                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3759              !!!cp ('t27');          } else {
3760              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3761          }          }
3762        }        }
3763              
3764        ## Step 4..13        ## Step 4..14
3765        my $new_mode = {        my $new_mode;
3766          if ($node->[1] & FOREIGN_EL) {
3767            !!!cp ('t28.1');
3768            ## NOTE: Strictly spaking, the line below only applies to MathML and
3769            ## SVG elements.  Currently the HTML syntax supports only MathML and
3770            ## SVG elements as foreigners.
3771            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3772          } elsif ($node->[1] & TABLE_CELL_EL) {
3773            if ($last) {
3774              !!!cp ('t28.2');
3775              #
3776            } else {
3777              !!!cp ('t28.3');
3778              $new_mode = IN_CELL_IM;
3779            }
3780          } else {
3781            !!!cp ('t28.4');
3782            $new_mode = {
3783                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3784                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3785                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3786                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3787                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3788                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2779  sub _reset_insertion_mode ($) { Line 3793  sub _reset_insertion_mode ($) {
3793                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3794                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3795                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3796                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3797          }
3798        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3799                
3800        ## Step 14        ## Step 15
3801        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3802          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3803            !!!cp ('t29');            !!!cp ('t29');
3804            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2797  sub _reset_insertion_mode ($) { Line 3812  sub _reset_insertion_mode ($) {
3812          !!!cp ('t31');          !!!cp ('t31');
3813        }        }
3814                
3815        ## Step 15        ## Step 16
3816        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3817                
3818        ## Step 16        ## Step 17
3819        $i--;        $i--;
3820        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3821                
3822        ## Step 17        ## Step 18
3823        redo S3;        redo S3;
3824      } # S3      } # S3
3825    
# Line 2908  sub _tree_construction_main ($) { Line 3923  sub _tree_construction_main ($) {
3923      !!!cp ('t39');      !!!cp ('t39');
3924    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3925    
3926    my $parse_rcdata = sub ($$) {    my $insert;
3927      my ($content_model_flag, $insert) = @_;  
3928      my $parse_rcdata = sub ($) {
3929        my ($content_model_flag) = @_;
3930    
3931      ## Step 1      ## Step 1
3932      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3933      my $el;      my $el;
3934      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3935    
3936      ## Step 2      ## Step 2
3937      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3938    
3939      ## Step 3      ## Step 3
3940      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2925  sub _tree_construction_main ($) { Line 3942  sub _tree_construction_main ($) {
3942    
3943      ## Step 4      ## Step 4
3944      my $text = '';      my $text = '';
3945        !!!nack ('t40.1');
3946      !!!next-token;      !!!next-token;
3947      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3948        !!!cp ('t40');        !!!cp ('t40');
# Line 2947  sub _tree_construction_main ($) { Line 3965  sub _tree_construction_main ($) {
3965          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3966        !!!cp ('t42');        !!!cp ('t42');
3967        ## Ignore the token        ## Ignore the token
     } elsif ($content_model_flag == CDATA_CONTENT_MODEL) {  
       !!!cp ('t43');  
       !!!parse-error (type => 'in CDATA:#'.$token->{type});  
     } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {  
       !!!cp ('t44');  
       !!!parse-error (type => 'in RCDATA:#'.$token->{type});  
3968      } else {      } else {
3969        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3970          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3971            !!!cp ('t43');
3972            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3973          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3974            !!!cp ('t44');
3975            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3976          } else {
3977            die "$0: $content_model_flag in parse_rcdata";
3978          }
3979      }      }
3980      !!!next-token;      !!!next-token;
3981    }; # $parse_rcdata    }; # $parse_rcdata
3982    
3983    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3984      my $script_el;      my $script_el;
3985      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3986      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3987    
3988      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3989      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3990            
3991      my $text = '';      my $text = '';
3992        !!!nack ('t45.1');
3993      !!!next-token;      !!!next-token;
3994      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3995        !!!cp ('t45');        !!!cp ('t45');
# Line 2988  sub _tree_construction_main ($) { Line 4009  sub _tree_construction_main ($) {
4009        ## Ignore the token        ## Ignore the token
4010      } else {      } else {
4011        !!!cp ('t48');        !!!cp ('t48');
4012        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#eof', token => $token);
4013        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4014        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4015      }      }
# Line 3011  sub _tree_construction_main ($) { Line 4032  sub _tree_construction_main ($) {
4032      !!!next-token;      !!!next-token;
4033    }; # $script_start_tag    }; # $script_start_tag
4034    
4035      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
4036      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
4037      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4038    
4039    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4040      my $tag_name = shift;      my $end_tag_token = shift;
4041        my $tag_name = $end_tag_token->{tag_name};
4042    
4043        ## NOTE: The adoption agency algorithm (AAA).
4044    
4045      FET: {      FET: {
4046        ## Step 1        ## Step 1
4047        my $formatting_element;        my $formatting_element;
4048        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4049        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4050          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4051              !!!cp ('t52');
4052              last AFE;
4053            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4054                         eq $tag_name) {
4055            !!!cp ('t51');            !!!cp ('t51');
4056            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4057            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4058            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
4059          }          }
4060        } # AFE        } # AFE
4061        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4062          !!!cp ('t53');          !!!cp ('t53');
4063          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4064          ## Ignore the token          ## Ignore the token
4065          !!!next-token;          !!!next-token;
4066          return;          return;
# Line 3048  sub _tree_construction_main ($) { Line 4077  sub _tree_construction_main ($) {
4077              last INSCOPE;              last INSCOPE;
4078            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4079              !!!cp ('t55');              !!!cp ('t55');
4080              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
4081                                text => $token->{tag_name},
4082                                token => $end_tag_token);
4083              ## Ignore the token              ## Ignore the token
4084              !!!next-token;              !!!next-token;
4085              return;              return;
4086            }            }
4087          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4088            !!!cp ('t56');            !!!cp ('t56');
4089            $in_scope = 0;            $in_scope = 0;
4090          }          }
4091        } # INSCOPE        } # INSCOPE
4092        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4093          !!!cp ('t57');          !!!cp ('t57');
4094          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
4095                            text => $token->{tag_name},
4096                            token => $end_tag_token);
4097          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4098          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4099          return;          return;
4100        }        }
4101        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4102          !!!cp ('t58');          !!!cp ('t58');
4103          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
4104                            text => $self->{open_elements}->[-1]->[0]
4105                                ->manakai_local_name,
4106                            token => $end_tag_token);
4107        }        }
4108                
4109        ## Step 2        ## Step 2
# Line 3078  sub _tree_construction_main ($) { Line 4111  sub _tree_construction_main ($) {
4111        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4112        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4113          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4114          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4115              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4116              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4117               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4118            !!!cp ('t59');            !!!cp ('t59');
4119            $furthest_block = $node;            $furthest_block = $node;
4120            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3167  sub _tree_construction_main ($) { Line 4200  sub _tree_construction_main ($) {
4200        } # S7          } # S7  
4201                
4202        ## Step 8        ## Step 8
4203        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4204            my $foster_parent_element;
4205            my $next_sibling;
4206            OE: for (reverse 0..$#{$self->{open_elements}}) {
4207              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4208                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4209                                 if (defined $parent and $parent->node_type == 1) {
4210                                   !!!cp ('t65.1');
4211                                   $foster_parent_element = $parent;
4212                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4213                                 } else {
4214                                   !!!cp ('t65.2');
4215                                   $foster_parent_element
4216                                     = $self->{open_elements}->[$_ - 1]->[0];
4217                                 }
4218                                 last OE;
4219                               }
4220                             } # OE
4221                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4222                               unless defined $foster_parent_element;
4223            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4224            $open_tables->[-1]->[1] = 1; # tainted
4225          } else {
4226            !!!cp ('t65.3');
4227            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4228          }
4229                
4230        ## Step 9        ## Step 9
4231        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3213  sub _tree_construction_main ($) { Line 4271  sub _tree_construction_main ($) {
4271      } # FET      } # FET
4272    }; # $formatting_end_tag    }; # $formatting_end_tag
4273    
4274    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4275      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4276    }; # $insert_to_current    }; # $insert_to_current
4277    
4278    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4279                         my $child = shift;      my $child = shift;
4280                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4281                              table => 1, tbody => 1, tfoot => 1,        # MUST
4282                              thead => 1, tr => 1,        my $foster_parent_element;
4283                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4284                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4285                           my $foster_parent_element;          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
                          my $next_sibling;  
                          OE: for (reverse 0..$#{$self->{open_elements}}) {  
                            if ($self->{open_elements}->[$_]->[1] eq 'table') {  
4286                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4287                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4288                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3245  sub _tree_construction_main ($) { Line 4300  sub _tree_construction_main ($) {
4300                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4301                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4302                             ($child, $next_sibling);                             ($child, $next_sibling);
4303                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4304                           !!!cp ('t72');      } else {
4305                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
4306                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
4307        }
4308    }; # $insert_to_foster    }; # $insert_to_foster
4309    
4310    my $insert;    B: while (1) {
   
   B: {  
4311      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4312        !!!cp ('t73');        !!!cp ('t73');
4313        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4314        ## Ignore the token        ## Ignore the token
4315        ## Stay in the phase        ## Stay in the phase
4316        !!!next-token;        !!!next-token;
4317        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;  
4318      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4319               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4320        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4321          !!!cp ('t79');          !!!cp ('t79');
4322          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4323          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4324        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4325          !!!cp ('t80');          !!!cp ('t80');
4326          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4327          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4328        } else {        } else {
4329          !!!cp ('t81');          !!!cp ('t81');
4330        }        }
4331    
4332        !!!cp ('t82');        !!!cp ('t82');
4333        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
4334        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4335        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4336          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3318  sub _tree_construction_main ($) { Line 4340  sub _tree_construction_main ($) {
4340               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4341          }          }
4342        }        }
4343          !!!nack ('t84.1');
4344        !!!next-token;        !!!next-token;
4345        redo B;        next B;
4346      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4347        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4348        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3333  sub _tree_construction_main ($) { Line 4356  sub _tree_construction_main ($) {
4356          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4357        }        }
4358        !!!next-token;        !!!next-token;
4359        redo B;        next B;
4360      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4361          if ($token->{type} == CHARACTER_TOKEN) {
4362            !!!cp ('t87.1');
4363            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4364            !!!next-token;
4365            next B;
4366          } elsif ($token->{type} == START_TAG_TOKEN) {
4367            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4368                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4369                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4370                ($token->{tag_name} eq 'svg' and
4371                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4372              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4373              !!!cp ('t87.2');
4374              #
4375            } elsif ({
4376                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4377                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4378                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4379                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4380                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4381                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4382                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4383                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4384                     }->{$token->{tag_name}}) {
4385              !!!cp ('t87.2');
4386              !!!parse-error (type => 'not closed',
4387                              text => $self->{open_elements}->[-1]->[0]
4388                                  ->manakai_local_name,
4389                              token => $token);
4390    
4391              pop @{$self->{open_elements}}
4392                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4393    
4394              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4395              ## Reprocess.
4396              next B;
4397            } else {
4398              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4399              my $tag_name = $token->{tag_name};
4400              if ($nsuri eq $SVG_NS) {
4401                $tag_name = {
4402                   altglyph => 'altGlyph',
4403                   altglyphdef => 'altGlyphDef',
4404                   altglyphitem => 'altGlyphItem',
4405                   animatecolor => 'animateColor',
4406                   animatemotion => 'animateMotion',
4407                   animatetransform => 'animateTransform',
4408                   clippath => 'clipPath',
4409                   feblend => 'feBlend',
4410                   fecolormatrix => 'feColorMatrix',
4411                   fecomponenttransfer => 'feComponentTransfer',
4412                   fecomposite => 'feComposite',
4413                   feconvolvematrix => 'feConvolveMatrix',
4414                   fediffuselighting => 'feDiffuseLighting',
4415                   fedisplacementmap => 'feDisplacementMap',
4416                   fedistantlight => 'feDistantLight',
4417                   feflood => 'feFlood',
4418                   fefunca => 'feFuncA',
4419                   fefuncb => 'feFuncB',
4420                   fefuncg => 'feFuncG',
4421                   fefuncr => 'feFuncR',
4422                   fegaussianblur => 'feGaussianBlur',
4423                   feimage => 'feImage',
4424                   femerge => 'feMerge',
4425                   femergenode => 'feMergeNode',
4426                   femorphology => 'feMorphology',
4427                   feoffset => 'feOffset',
4428                   fepointlight => 'fePointLight',
4429                   fespecularlighting => 'feSpecularLighting',
4430                   fespotlight => 'feSpotLight',
4431                   fetile => 'feTile',
4432                   feturbulence => 'feTurbulence',
4433                   foreignobject => 'foreignObject',
4434                   glyphref => 'glyphRef',
4435                   lineargradient => 'linearGradient',
4436                   radialgradient => 'radialGradient',
4437                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4438                   textpath => 'textPath',  
4439                }->{$tag_name} || $tag_name;
4440              }
4441    
4442              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4443    
4444              ## "adjust foreign attributes" - done in insert-element-f
4445    
4446              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4447    
4448              if ($self->{self_closing}) {
4449                pop @{$self->{open_elements}};
4450                !!!ack ('t87.3');
4451              } else {
4452                !!!cp ('t87.4');
4453              }
4454    
4455              !!!next-token;
4456              next B;
4457            }
4458          } elsif ($token->{type} == END_TAG_TOKEN) {
4459            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4460            !!!cp ('t87.5');
4461            #
4462          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4463            !!!cp ('t87.6');
4464            !!!parse-error (type => 'not closed',
4465                            text => $self->{open_elements}->[-1]->[0]
4466                                ->manakai_local_name,
4467                            token => $token);
4468    
4469            pop @{$self->{open_elements}}
4470                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4471    
4472            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4473            ## Reprocess.
4474            next B;
4475          } else {
4476            die "$0: $token->{type}: Unknown token type";        
4477          }
4478        }
4479    
4480        if ($self->{insertion_mode} & HEAD_IMS) {
4481        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4482          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4483            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4484                !!!cp ('t88.2');
4485                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4486                #
4487              } else {
4488                !!!cp ('t88.1');
4489                ## Ignore the token.
4490                #
4491              }
4492            unless (length $token->{data}) {            unless (length $token->{data}) {
4493              !!!cp ('t88');              !!!cp ('t88');
4494              !!!next-token;              !!!next-token;
4495              redo B;              next B;
4496            }            }
4497    ## TODO: set $token->{column} appropriately
4498          }          }
4499    
4500          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4501            !!!cp ('t89');            !!!cp ('t89');
4502            ## As if <head>            ## As if <head>
4503            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4504            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4505            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4506                  [$self->{head_element}, $el_category->{head}];
4507    
4508            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4509            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3360  sub _tree_construction_main ($) { Line 4513  sub _tree_construction_main ($) {
4513            !!!cp ('t90');            !!!cp ('t90');
4514            ## As if </noscript>            ## As if </noscript>
4515            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4516            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4517                        
4518            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4519            ## As if </head>            ## As if </head>
# Line 3376  sub _tree_construction_main ($) { Line 4529  sub _tree_construction_main ($) {
4529            !!!cp ('t92');            !!!cp ('t92');
4530          }          }
4531    
4532              ## "after head" insertion mode          ## "after head" insertion mode
4533              ## As if <body>          ## As if <body>
4534              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4535              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4536              ## reprocess          ## reprocess
4537              redo B;          next B;
4538            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4539              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4540                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4541                  !!!cp ('t93');              !!!cp ('t93');
4542                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4543                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4544                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4545                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4546                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4547                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4548                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4549                  !!!cp ('t94');              !!!next-token;
4550                  #              next B;
4551                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4552                  !!!cp ('t95');              !!!cp ('t93.2');
4553                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4554                  ## Ignore the token                              token => $token);
4555                  !!!next-token;              ## Ignore the token
4556                  redo B;              !!!nack ('t93.3');
4557                }              !!!next-token;
4558              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4559                !!!cp ('t96');            } else {
4560                ## As if <head>              !!!cp ('t95');
4561                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4562                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4563                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4564                !!!nack ('t95.1');
4565                !!!next-token;
4566                next B;
4567              }
4568            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4569              !!!cp ('t96');
4570              ## As if <head>
4571              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4572              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4573              push @{$self->{open_elements}},
4574                  [$self->{head_element}, $el_category->{head}];
4575    
4576                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4577                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4578              } else {          } else {
4579                !!!cp ('t97');            !!!cp ('t97');
4580              }          }
4581    
4582              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4583                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4584                  !!!cp ('t98');                  !!!cp ('t98');
4585                  ## As if </noscript>                  ## As if </noscript>
4586                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4587                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4588                                    token => $token);
4589                                
4590                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4591                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3431  sub _tree_construction_main ($) { Line 4596  sub _tree_construction_main ($) {
4596                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4597                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4598                  !!!cp ('t100');                  !!!cp ('t100');
4599                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4600                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4601                    push @{$self->{open_elements}},
4602                        [$self->{head_element}, $el_category->{head}];
4603                } else {                } else {
4604                  !!!cp ('t101');                  !!!cp ('t101');
4605                }                }
4606                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4607                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4608                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4609                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4610                  !!!nack ('t101.1');
4611                !!!next-token;                !!!next-token;
4612                redo B;                next B;
4613              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4614                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4615                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4616                  !!!cp ('t102');                  !!!cp ('t102');
4617                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4618                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4619                    push @{$self->{open_elements}},
4620                        [$self->{head_element}, $el_category->{head}];
4621                } else {                } else {
4622                  !!!cp ('t103');                  !!!cp ('t103');
4623                }                }
4624                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4625                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4626                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4627                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4628                  !!!ack ('t103.1');
4629                !!!next-token;                !!!next-token;
4630                redo B;                next B;
4631              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4632                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4633                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4634                  !!!cp ('t104');                  !!!cp ('t104');
4635                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4636                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4637                    push @{$self->{open_elements}},
4638                        [$self->{head_element}, $el_category->{head}];
4639                } else {                } else {
4640                  !!!cp ('t105');                  !!!cp ('t105');
4641                }                }
4642                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4643                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.
4644    
4645                unless ($self->{confident}) {                unless ($self->{confident}) {
4646                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4647                    !!!cp ('t106');                    !!!cp ('t106');
4648                      ## NOTE: Whether the encoding is supported or not is handled
4649                      ## in the {change_encoding} callback.
4650                    $self->{change_encoding}                    $self->{change_encoding}
4651                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4652                             $token);
4653                                        
4654                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4655                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4656                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4657                                                 ->{has_reference});                                                 ->{has_reference});
4658                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4659                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4660                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4661                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4662                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4663                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4664                      !!!cp ('t107');                      !!!cp ('t107');
4665                        ## NOTE: Whether the encoding is supported or not is handled
4666                        ## in the {change_encoding} callback.
4667                      $self->{change_encoding}                      $self->{change_encoding}
4668                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4669                               $token);
4670                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4671                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4672                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3514  sub _tree_construction_main ($) { Line 4692  sub _tree_construction_main ($) {
4692                  }                  }
4693                }                }
4694    
4695                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4696                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4697                  !!!ack ('t110.1');
4698                !!!next-token;                !!!next-token;
4699                redo B;                next B;
4700              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4701                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4702                  !!!cp ('t111');                  !!!cp ('t111');
4703                  ## As if </noscript>                  ## As if </noscript>
4704                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4705                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4706                                    token => $token);
4707                                
4708                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4709                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4710                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4711                  !!!cp ('t112');                  !!!cp ('t112');
4712                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4713                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4714                    push @{$self->{open_elements}},
4715                        [$self->{head_element}, $el_category->{head}];
4716                } else {                } else {
4717                  !!!cp ('t113');                  !!!cp ('t113');
4718                }                }
# Line 3538  sub _tree_construction_main ($) { Line 4720  sub _tree_construction_main ($) {
4720                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4721                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4722                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4723                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4724                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4725                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4726                redo B;                next B;
4727              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4728                         $token->{tag_name} eq 'noframes') {
4729                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4730                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4731                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4732                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4733                  !!!cp ('t114');                  !!!cp ('t114');
4734                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4735                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4736                    push @{$self->{open_elements}},
4737                        [$self->{head_element}, $el_category->{head}];
4738                } else {                } else {
4739                  !!!cp ('t115');                  !!!cp ('t115');
4740                }                }
4741                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4742                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4743                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4744                redo B;                next B;
4745              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4746                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4747                  !!!cp ('t116');                  !!!cp ('t116');
4748                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4749                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4750                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4751                    !!!nack ('t116.1');
4752                  !!!next-token;                  !!!next-token;
4753                  redo B;                  next B;
4754                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4755                  !!!cp ('t117');                  !!!cp ('t117');
4756                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
4757                                    token => $token);
4758                  ## Ignore the token                  ## Ignore the token
4759                    !!!nack ('t117.1');
4760                  !!!next-token;                  !!!next-token;
4761                  redo B;                  next B;
4762                } else {                } else {
4763                  !!!cp ('t118');                  !!!cp ('t118');
4764                  #                  #
# Line 3581  sub _tree_construction_main ($) { Line 4768  sub _tree_construction_main ($) {
4768                  !!!cp ('t119');                  !!!cp ('t119');
4769                  ## As if </noscript>                  ## As if </noscript>
4770                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4771                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4772                                    token => $token);
4773                                
4774                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4775                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4776                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4777                  !!!cp ('t120');                  !!!cp ('t120');
4778                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4779                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4780                    push @{$self->{open_elements}},
4781                        [$self->{head_element}, $el_category->{head}];
4782                } else {                } else {
4783                  !!!cp ('t121');                  !!!cp ('t121');
4784                }                }
4785    
4786                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4787                $script_start_tag->($insert_to_current);                $script_start_tag->();
4788                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4789                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4790                redo B;                next B;
4791              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4792                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4793                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4794                  !!!cp ('t122');                  !!!cp ('t122');
4795                  ## As if </noscript>                  ## As if </noscript>
4796                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4797                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4798                                    text => $token->{tag_name}, token => $token);
4799                                    
4800                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4801                  ## As if </head>                  ## As if </head>
# Line 3621  sub _tree_construction_main ($) { Line 4812  sub _tree_construction_main ($) {
4812                }                }
4813    
4814                ## "after head" insertion mode                ## "after head" insertion mode
4815                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4816                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4817                  !!!cp ('t126');                  !!!cp ('t126');
4818                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3631  sub _tree_construction_main ($) { Line 4822  sub _tree_construction_main ($) {
4822                } else {                } else {
4823                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4824                }                }
4825                  !!!nack ('t127.1');
4826                !!!next-token;                !!!next-token;
4827                redo B;                next B;
4828              } else {              } else {
4829                !!!cp ('t128');                !!!cp ('t128');
4830                #                #
# Line 3642  sub _tree_construction_main ($) { Line 4834  sub _tree_construction_main ($) {
4834                !!!cp ('t129');                !!!cp ('t129');
4835                ## As if </noscript>                ## As if </noscript>
4836                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4837                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4838                                  text => $token->{tag_name}, token => $token);
4839                                
4840                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4841                ## As if </head>                ## As if </head>
# Line 3661  sub _tree_construction_main ($) { Line 4854  sub _tree_construction_main ($) {
4854    
4855              ## "after head" insertion mode              ## "after head" insertion mode
4856              ## As if <body>              ## As if <body>
4857              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4858              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4859              ## reprocess              ## reprocess
4860              redo B;              !!!ack-later;
4861                next B;
4862            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4863              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4864                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4865                  !!!cp ('t132');                  !!!cp ('t132');
4866                  ## As if <head>                  ## As if <head>
4867                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4868                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4869                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4870                        [$self->{head_element}, $el_category->{head}];
4871    
4872                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4873                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4874                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4875                  !!!next-token;                  !!!next-token;
4876                  redo B;                  next B;
4877                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4878                  !!!cp ('t133');                  !!!cp ('t133');
4879                  ## As if </noscript>                  ## As if </noscript>
4880                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4881                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/',
4882                                    text => 'head', token => $token);
4883                                    
4884                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4885                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4886                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4887                  !!!next-token;                  !!!next-token;
4888                  redo B;                  next B;
4889                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4890                  !!!cp ('t134');                  !!!cp ('t134');
4891                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4892                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4893                  !!!next-token;                  !!!next-token;
4894                  redo B;                  next B;
4895                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4896                    !!!cp ('t134.1');
4897                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4898                                    token => $token);
4899                    ## Ignore the token
4900                    !!!next-token;
4901                    next B;
4902                } else {                } else {
4903                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4904                }                }
4905              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4906                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3706  sub _tree_construction_main ($) { Line 4908  sub _tree_construction_main ($) {
4908                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4909                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4910                  !!!next-token;                  !!!next-token;
4911                  redo B;                  next B;
4912                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4913                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4914                  !!!cp ('t137');                  !!!cp ('t137');
4915                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
4916                                    text => 'noscript', token => $token);
4917                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4918                  !!!next-token;                  !!!next-token;
4919                  redo B;                  next B;
4920                } else {                } else {
4921                  !!!cp ('t138');                  !!!cp ('t138');
4922                  #                  #
# Line 3720  sub _tree_construction_main ($) { Line 4924  sub _tree_construction_main ($) {
4924              } elsif ({              } elsif ({
4925                        body => 1, html => 1,                        body => 1, html => 1,
4926                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4927                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4928                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4929                  ## 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) {  
4930                  !!!cp ('t140');                  !!!cp ('t140');
4931                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4932                                    text => $token->{tag_name}, token => $token);
4933                  ## Ignore the token                  ## Ignore the token
4934                  !!!next-token;                  !!!next-token;
4935                  redo B;                  next B;
4936                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4937                    !!!cp ('t140.1');
4938                    !!!parse-error (type => 'unmatched end tag',
4939                                    text => $token->{tag_name}, token => $token);
4940                    ## Ignore the token
4941                    !!!next-token;
4942                    next B;
4943                } else {                } else {
4944                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4945                }                }
4946                              } elsif ($token->{tag_name} eq 'p') {
4947                #                !!!cp ('t142');
4948              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4949                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4950                       }->{$token->{tag_name}}) {                ## Ignore the token
4951                  !!!next-token;
4952                  next B;
4953                } elsif ($token->{tag_name} eq 'br') {
4954                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4955                  !!!cp ('t142');                  !!!cp ('t142.2');
4956                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4957                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4958                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4959                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4960      
4961                    ## Reprocess in the "after head" insertion mode...
4962                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4963                    !!!cp ('t143.2');
4964                    ## As if </head>
4965                    pop @{$self->{open_elements}};
4966                    $self->{insertion_mode} = AFTER_HEAD_IM;
4967      
4968                    ## Reprocess in the "after head" insertion mode...
4969                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4970                    !!!cp ('t143.3');
4971                    ## ISSUE: Two parse errors for <head><noscript></br>
4972                    !!!parse-error (type => 'unmatched end tag',
4973                                    text => 'br', token => $token);
4974                    ## As if </noscript>
4975                    pop @{$self->{open_elements}};
4976                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4977    
4978                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4979                } else {                  ## As if </head>
4980                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4981                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4982    
4983                #                  ## Reprocess in the "after head" insertion mode...
4984              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4985                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4986                  #                  #
4987                } else {                } else {
4988                  !!!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;  
4989                }                }
4990    
4991                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4992                  !!!parse-error (type => 'unmatched end tag',
4993                                  text => 'br', token => $token);
4994                  ## Ignore the token
4995                  !!!next-token;
4996                  next B;
4997                } else {
4998                  !!!cp ('t145');
4999                  !!!parse-error (type => 'unmatched end tag',
5000                                  text => $token->{tag_name}, token => $token);
5001                  ## Ignore the token
5002                  !!!next-token;
5003                  next B;
5004              }              }
5005    
5006              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5007                !!!cp ('t146');                !!!cp ('t146');
5008                ## As if </noscript>                ## As if </noscript>
5009                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5010                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
5011                                  text => $token->{tag_name}, token => $token);
5012                                
5013                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5014                ## As if </head>                ## As if </head>
# Line 3790  sub _tree_construction_main ($) { Line 5024  sub _tree_construction_main ($) {
5024              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5025  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
5026                !!!cp ('t148');                !!!cp ('t148');
5027                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5028                                  text => $token->{tag_name}, token => $token);
5029                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5030                !!!next-token;                !!!next-token;
5031                redo B;                next B;
5032              } else {              } else {
5033                !!!cp ('t149');                !!!cp ('t149');
5034              }              }
5035    
5036              ## "after head" insertion mode              ## "after head" insertion mode
5037              ## As if <body>              ## As if <body>
5038              !!!insert-element ('body');              !!!insert-element ('body',, $token);
5039              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
5040              ## reprocess              ## reprocess
5041              redo B;              next B;
5042            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5043              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5044            }            !!!cp ('t149.1');
5045    
5046              ## NOTE: As if <head>
5047              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5048              $self->{open_elements}->[-1]->[0]->append_child
5049                  ($self->{head_element});
5050              #push @{$self->{open_elements}},
5051              #    [$self->{head_element}, $el_category->{head}];
5052              #$self->{insertion_mode} = IN_HEAD_IM;
5053              ## NOTE: Reprocess.
5054    
5055              ## NOTE: As if </head>
5056              #pop @{$self->{open_elements}};
5057              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5058              ## NOTE: Reprocess.
5059              
5060              #
5061            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5062              !!!cp ('t149.2');
5063    
5064              ## NOTE: As if </head>
5065              pop @{$self->{open_elements}};
5066              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5067              ## NOTE: Reprocess.
5068    
5069              #
5070            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5071              !!!cp ('t149.3');
5072    
5073              !!!parse-error (type => 'in noscript:#eof', token => $token);
5074    
5075              ## As if </noscript>
5076              pop @{$self->{open_elements}};
5077              #$self->{insertion_mode} = IN_HEAD_IM;
5078              ## NOTE: Reprocess.
5079    
5080              ## NOTE: As if </head>
5081              pop @{$self->{open_elements}};
5082              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5083              ## NOTE: Reprocess.
5084    
5085              #
5086            } else {
5087              !!!cp ('t149.4');
5088              #
5089            }
5090    
5091            ## NOTE: As if <body>
5092            !!!insert-element ('body',, $token);
5093            $self->{insertion_mode} = IN_BODY_IM;
5094            ## NOTE: Reprocess.
5095            next B;
5096          } else {
5097            die "$0: $token->{type}: Unknown token type";
5098          }
5099    
5100            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5101      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3818  sub _tree_construction_main ($) { Line 5107  sub _tree_construction_main ($) {
5107              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5108    
5109              !!!next-token;              !!!next-token;
5110              redo B;              next B;
5111            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5112              if ({              if ({
5113                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3826  sub _tree_construction_main ($) { Line 5115  sub _tree_construction_main ($) {
5115                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5116                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5117                  ## have an element in table scope                  ## have an element in table scope
5118                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5119                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5120                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5121                      !!!cp ('t151');                      !!!cp ('t151');
5122                      $tn = $node->[1];  
5123                      last INSCOPE;                      ## Close the cell
5124                    } elsif ({                      !!!back-token; # <x>
5125                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
5126                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
5127                                  line => $token->{line},
5128                                  column => $token->{column}};
5129                        next B;
5130                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5131                      !!!cp ('t152');                      !!!cp ('t152');
5132                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
5133                    }                      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;  
5134                    }                    }
5135                                    }
5136                  !!!cp ('t154');  
5137                  ## Close the cell                  !!!cp ('t153');
5138                  !!!back-token; # <?>                  !!!parse-error (type => 'start tag not allowed',
5139                  $token = {type => END_TAG_TOKEN, tag_name => $tn};                      text => $token->{tag_name}, token => $token);
5140                  redo B;                  ## Ignore the token
5141                    !!!nack ('t153.1');
5142                    !!!next-token;
5143                    next B;
5144                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5145                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5146                                    token => $token);
5147                                    
5148                  ## As if </caption>                  ## NOTE: As if </caption>.
5149                  ## have a table element in table scope                  ## have a table element in table scope
5150                  my $i;                  my $i;
5151                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5152                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5153                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5154                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
5155                      $i = $_;                        !!!cp ('t155');
5156                      last INSCOPE;                        $i = $_;
5157                    } elsif ({                        last INSCOPE;
5158                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5159                             }->{$node->[1]}) {                        !!!cp ('t156');
5160                      !!!cp ('t156');                        last;
5161                      last INSCOPE;                      }
5162                    }                    }
5163    
5164                      !!!cp ('t157');
5165                      !!!parse-error (type => 'start tag not allowed',
5166                                      text => $token->{tag_name}, token => $token);
5167                      ## Ignore the token
5168                      !!!nack ('t157.1');
5169                      !!!next-token;
5170                      next B;
5171                  } # 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;  
                   }  
5172                                    
5173                  ## generate implied end tags                  ## generate implied end tags
5174                  while ({                  while ($self->{open_elements}->[-1]->[1]
5175                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5176                    !!!cp ('t158');                    !!!cp ('t158');
5177                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5178                  }                  }
5179    
5180                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5181                    !!!cp ('t159');                    !!!cp ('t159');
5182                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5183                                      text => $self->{open_elements}->[-1]->[0]
5184                                          ->manakai_local_name,
5185                                      token => $token);
5186                  } else {                  } else {
5187                    !!!cp ('t160');                    !!!cp ('t160');
5188                  }                  }
# Line 3904  sub _tree_construction_main ($) { Line 5194  sub _tree_construction_main ($) {
5194                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5195                                    
5196                  ## reprocess                  ## reprocess
5197                  redo B;                  !!!ack-later;
5198                    next B;
5199                } else {                } else {
5200                  !!!cp ('t161');                  !!!cp ('t161');
5201                  #                  #
# Line 3920  sub _tree_construction_main ($) { Line 5211  sub _tree_construction_main ($) {
5211                  my $i;                  my $i;
5212                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5213                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5214                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5215                      !!!cp ('t163');                      !!!cp ('t163');
5216                      $i = $_;                      $i = $_;
5217                      last INSCOPE;                      last INSCOPE;
5218                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5219                      !!!cp ('t164');                      !!!cp ('t164');
5220                      last INSCOPE;                      last INSCOPE;
5221                    }                    }
5222                  } # INSCOPE                  } # INSCOPE
5223                    unless (defined $i) {                    unless (defined $i) {
5224                      !!!cp ('t165');                      !!!cp ('t165');
5225                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5226                                        text => $token->{tag_name},
5227                                        token => $token);
5228                      ## Ignore the token                      ## Ignore the token
5229                      !!!next-token;                      !!!next-token;
5230                      redo B;                      next B;
5231                    }                    }
5232                                    
5233                  ## generate implied end tags                  ## generate implied end tags
5234                  while ({                  while ($self->{open_elements}->[-1]->[1]
5235                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5236                    !!!cp ('t166');                    !!!cp ('t166');
5237                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5238                  }                  }
5239    
5240                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5241                            ne $token->{tag_name}) {
5242                    !!!cp ('t167');                    !!!cp ('t167');
5243                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5244                                      text => $self->{open_elements}->[-1]->[0]
5245                                          ->manakai_local_name,
5246                                      token => $token);
5247                  } else {                  } else {
5248                    !!!cp ('t168');                    !!!cp ('t168');
5249                  }                  }
# Line 3961  sub _tree_construction_main ($) { Line 5255  sub _tree_construction_main ($) {
5255                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5256                                    
5257                  !!!next-token;                  !!!next-token;
5258                  redo B;                  next B;
5259                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5260                  !!!cp ('t169');                  !!!cp ('t169');
5261                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5262                                    text => $token->{tag_name}, token => $token);
5263                  ## Ignore the token                  ## Ignore the token
5264                  !!!next-token;                  !!!next-token;
5265                  redo B;                  next B;
5266                } else {                } else {
5267                  !!!cp ('t170');                  !!!cp ('t170');
5268                  #                  #
# Line 3976  sub _tree_construction_main ($) { Line 5271  sub _tree_construction_main ($) {
5271                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5272                  ## have a table element in table scope                  ## have a table element in table scope
5273                  my $i;                  my $i;
5274                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5275                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5276                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5277                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
5278                      $i = $_;                        !!!cp ('t171');
5279                      last INSCOPE;                        $i = $_;
5280                    } elsif ({                        last INSCOPE;
5281                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5282                             }->{$node->[1]}) {                        !!!cp ('t172');
5283                      !!!cp ('t172');                        last;
5284                      last INSCOPE;                      }
5285                    }                    }
5286    
5287                      !!!cp ('t173');
5288                      !!!parse-error (type => 'unmatched end tag',
5289                                      text => $token->{tag_name}, token => $token);
5290                      ## Ignore the token
5291                      !!!next-token;
5292                      next B;
5293                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5294                                    
5295                  ## generate implied end tags                  ## generate implied end tags
5296                  while ({                  while ($self->{open_elements}->[-1]->[1]
5297                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5298                    !!!cp ('t174');                    !!!cp ('t174');
5299                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5300                  }                  }
5301                                    
5302                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5303                    !!!cp ('t175');                    !!!cp ('t175');
5304                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5305                                      text => $self->{open_elements}->[-1]->[0]
5306                                          ->manakai_local_name,
5307                                      token => $token);
5308                  } else {                  } else {
5309                    !!!cp ('t176');                    !!!cp ('t176');
5310                  }                  }
# Line 4019  sub _tree_construction_main ($) { Line 5316  sub _tree_construction_main ($) {
5316                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5317                                    
5318                  !!!next-token;                  !!!next-token;
5319                  redo B;                  next B;
5320                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5321                  !!!cp ('t177');                  !!!cp ('t177');
5322                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5323                                    text => $token->{tag_name}, token => $token);
5324                  ## Ignore the token                  ## Ignore the token
5325                  !!!next-token;                  !!!next-token;
5326                  redo B;                  next B;
5327                } else {                } else {
5328                  !!!cp ('t178');                  !!!cp ('t178');
5329                  #                  #
# Line 4038  sub _tree_construction_main ($) { Line 5336  sub _tree_construction_main ($) {
5336                ## have an element in table scope                ## have an element in table scope
5337                my $i;                my $i;
5338                my $tn;                my $tn;
5339                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5340                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5341                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5342                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5343                    $i = $_;                      !!!cp ('t179');
5344                    last INSCOPE;                      $i = $_;
5345                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
5346                    !!!cp ('t180');                      ## Close the cell
5347                    $tn = $node->[1];                      !!!back-token; # </x>
5348                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5349                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
5350                  } elsif ({                                column => $token->{column}};
5351                            table => 1, html => 1,                      next B;
5352                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5353                    !!!cp ('t181');                      !!!cp ('t180');
5354                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
5355                        ## NOTE: There is exactly one |td| or |th| element
5356                        ## in scope in the stack of open elements by definition.
5357                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5358                        ## ISSUE: Can this be reached?
5359                        !!!cp ('t181');
5360                        last;
5361                      }
5362                  }                  }
5363                } # INSCOPE  
               unless (defined $i) {  
5364                  !!!cp ('t182');                  !!!cp ('t182');
5365                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5366                        text => $token->{tag_name}, token => $token);
5367                  ## Ignore the token                  ## Ignore the token
5368                  !!!next-token;                  !!!next-token;
5369                  redo B;                  next B;
5370                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5371              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5372                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5373                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5374                                  token => $token);
5375    
5376                ## As if </caption>                ## As if </caption>
5377                ## have a table element in table scope                ## have a table element in table scope
5378                my $i;                my $i;
5379                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5380                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5381                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5382                    !!!cp ('t184');                    !!!cp ('t184');
5383                    $i = $_;                    $i = $_;
5384                    last INSCOPE;                    last INSCOPE;
5385                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5386                    !!!cp ('t185');                    !!!cp ('t185');
5387                    last INSCOPE;                    last INSCOPE;
5388                  }                  }
5389                } # INSCOPE                } # INSCOPE
5390                unless (defined $i) {                unless (defined $i) {
5391                  !!!cp ('t186');                  !!!cp ('t186');
5392                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag',
5393                                    text => 'caption', token => $token);
5394                  ## Ignore the token                  ## Ignore the token
5395                  !!!next-token;                  !!!next-token;
5396                  redo B;                  next B;
5397                }                }
5398                                
5399                ## generate implied end tags                ## generate implied end tags
5400                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5401                  !!!cp ('t187');                  !!!cp ('t187');
5402                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5403                }                }
5404    
5405                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5406                  !!!cp ('t188');                  !!!cp ('t188');
5407                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5408                                    text => $self->{open_elements}->[-1]->[0]
5409                                        ->manakai_local_name,
5410                                    token => $token);
5411                } else {                } else {
5412                  !!!cp ('t189');                  !!!cp ('t189');
5413                }                }
# Line 4120  sub _tree_construction_main ($) { Line 5419  sub _tree_construction_main ($) {
5419                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5420    
5421                ## reprocess                ## reprocess
5422                redo B;                next B;
5423              } elsif ({              } elsif ({
5424                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5425                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5426                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5427                  !!!cp ('t190');                  !!!cp ('t190');
5428                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5429                                    text => $token->{tag_name}, token => $token);
5430                  ## Ignore the token                  ## Ignore the token
5431                  !!!next-token;                  !!!next-token;
5432                  redo B;                  next B;
5433                } else {                } else {
5434                  !!!cp ('t191');                  !!!cp ('t191');
5435                  #                  #
# Line 4140  sub _tree_construction_main ($) { Line 5440  sub _tree_construction_main ($) {
5440                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5441                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5442                !!!cp ('t192');                !!!cp ('t192');
5443                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5444                                  text => $token->{tag_name}, token => $token);
5445                ## Ignore the token                ## Ignore the token
5446                !!!next-token;                !!!next-token;
5447                redo B;                next B;
5448              } else {              } else {
5449                !!!cp ('t193');                !!!cp ('t193');
5450                #                #
5451              }              }
5452          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5453            for my $entry (@{$self->{open_elements}}) {
5454              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5455                !!!cp ('t75');
5456                !!!parse-error (type => 'in body:#eof', token => $token);
5457                last;
5458              }
5459            }
5460    
5461            ## Stop parsing.
5462            last B;
5463        } else {        } else {
5464          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5465        }        }
# Line 4156  sub _tree_construction_main ($) { Line 5468  sub _tree_construction_main ($) {
5468        #        #
5469      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5470        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5471              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5472                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5473              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5474                                
5475                unless (length $token->{data}) {            unless (length $token->{data}) {
5476                  !!!cp ('t194');              !!!cp ('t194');
5477                  !!!next-token;              !!!next-token;
5478                  redo B;              next B;
5479                } else {            } else {
5480                  !!!cp ('t195');              !!!cp ('t195');
5481                }            }
5482              }          }
5483    
5484              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5485    
5486              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5487              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4176  sub _tree_construction_main ($) { Line 5489  sub _tree_construction_main ($) {
5489              ## result in a new Text node.              ## result in a new Text node.
5490              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5491                            
5492              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]}) {  
5493                # MUST                # MUST
5494                my $foster_parent_element;                my $foster_parent_element;
5495                my $next_sibling;                my $next_sibling;
5496                my $prev_sibling;                my $prev_sibling;
5497                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5498                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5499                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5500                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5501                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4213  sub _tree_construction_main ($) { Line 5523  sub _tree_construction_main ($) {
5523                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5524                     $next_sibling);                     $next_sibling);
5525                }                }
5526              } else {            $open_tables->[-1]->[1] = 1; # tainted
5527                !!!cp ('t200');          } else {
5528                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5529              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5530            }
5531                            
5532              !!!next-token;          !!!next-token;
5533              redo B;          next B;
5534        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5535              if ({          if ({
5536                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5537                   th => 1, td => 1,               th => 1, td => 1,
5538                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5539                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5540                  ## Clear back to table context              ## Clear back to table context
5541                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5542                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5543                    !!!cp ('t201');                !!!cp ('t201');
5544                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                pop @{$self->{open_elements}};
5545                    pop @{$self->{open_elements}};              }
5546                  }              
5547                                !!!insert-element ('tbody',, $token);
5548                  !!!insert-element ('tbody');              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5549                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              ## reprocess in the "in table body" insertion mode...
5550                  ## reprocess in the "in table body" insertion mode...            }
5551                }            
5552              if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5553                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {              unless ($token->{tag_name} eq 'tr') {
5554                  unless ($token->{tag_name} eq 'tr') {                !!!cp ('t202');
5555                    !!!cp ('t202');                !!!parse-error (type => 'missing start tag:tr', token => $token);
5556                    !!!parse-error (type => 'missing start tag:tr');              }
                 }  
5557                                    
5558                  ## Clear back to table body context              ## Clear back to table body context
5559                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5560                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5561                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5562                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5563                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5564                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              }
                   pop @{$self->{open_elements}};  
                 }  
5565                                    
5566                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5567                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5568                    !!!cp ('t204');                    !!!cp ('t204');
5569                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5570                      !!!nack ('t204');
5571                    !!!next-token;                    !!!next-token;
5572                    redo B;                    next B;
5573                  } else {                  } else {
5574                    !!!cp ('t205');                    !!!cp ('t205');
5575                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5576                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5577                  }                  }
5578                } else {                } else {
# Line 4271  sub _tree_construction_main ($) { Line 5580  sub _tree_construction_main ($) {
5580                }                }
5581    
5582                ## Clear back to table row context                ## Clear back to table row context
5583                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5584                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5585                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5586                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5587                }                }
5588                                
5589                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5590                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5591    
5592                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5593                                
5594                  !!!nack ('t207.1');
5595                !!!next-token;                !!!next-token;
5596                redo B;                next B;
5597              } elsif ({              } elsif ({
5598                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5599                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4297  sub _tree_construction_main ($) { Line 5605  sub _tree_construction_main ($) {
5605                  my $i;                  my $i;
5606                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5607                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5608                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5609                      !!!cp ('t208');                      !!!cp ('t208');
5610                      $i = $_;                      $i = $_;
5611                      last INSCOPE;                      last INSCOPE;
5612                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5613                      !!!cp ('t209');                      !!!cp ('t209');
5614                      last INSCOPE;                      last INSCOPE;
5615                    }                    }
5616                  } # INSCOPE                  } # INSCOPE
5617                  unless (defined $i) {                  unless (defined $i) {
5618                   !!!cp ('t210');                    !!!cp ('t210');
5619  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5620                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag',
5621                                      text => $token->{tag_name}, token => $token);
5622                    ## Ignore the token                    ## Ignore the token
5623                      !!!nack ('t210.1');
5624                    !!!next-token;                    !!!next-token;
5625                    redo B;                    next B;
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 ('t211');                    !!!cp ('t211');
5632                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5633                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5634                  }                  }
5635                                    
# Line 4335  sub _tree_construction_main ($) { Line 5638  sub _tree_construction_main ($) {
5638                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5639                    !!!cp ('t212');                    !!!cp ('t212');
5640                    ## reprocess                    ## reprocess
5641                    redo B;                    !!!ack-later;
5642                      next B;
5643                  } else {                  } else {
5644                    !!!cp ('t213');                    !!!cp ('t213');
5645                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4347  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 ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5655                      !!!cp ('t214');                      !!!cp ('t214');
5656                      $i = $_;                      $i = $_;
5657                      last INSCOPE;                      last INSCOPE;
5658                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5659                      !!!cp ('t215');                      !!!cp ('t215');
5660                      last INSCOPE;                      last INSCOPE;
5661                    }                    }
5662                  } # INSCOPE                  } # INSCOPE
5663                  unless (defined $i) {                  unless (defined $i) {
5664                    !!!cp ('t216');                    !!!cp ('t216');
5665  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5666                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5667                                      text => $token->{tag_name}, token => $token);
5668                    ## Ignore the token                    ## Ignore the token
5669                      !!!nack ('t216.1');
5670                    !!!next-token;                    !!!next-token;
5671                    redo B;                    next B;
5672                  }                  }
5673    
5674                  ## Clear back to table body context                  ## Clear back to table body context
5675                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5676                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5677                    !!!cp ('t217');                    !!!cp ('t217');
5678                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5679                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5680                  }                  }
5681                                    
# Line 4395  sub _tree_construction_main ($) { Line 5695  sub _tree_construction_main ($) {
5695    
5696                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5697                  ## Clear back to table context                  ## Clear back to table context
5698                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5699                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5700                    !!!cp ('t219');                    !!!cp ('t219');
5701                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5702                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5703                  }                  }
5704                                    
5705                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5706                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5707                  ## reprocess                  ## reprocess
5708                  redo B;                  !!!ack-later;
5709                    next B;
5710                } elsif ({                } elsif ({
5711                          caption => 1,                          caption => 1,
5712                          colgroup => 1,                          colgroup => 1,
5713                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5714                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5715                  ## Clear back to table context                  ## Clear back to table context
5716                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5717                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5718                    !!!cp ('t220');                    !!!cp ('t220');
5719                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5720                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5721                  }                  }
5722                                    
5723                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5724                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5725                                    
5726                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5727                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5728                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5729                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4433  sub _tree_construction_main ($) { Line 5732  sub _tree_construction_main ($) {
5732                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5733                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5734                  !!!next-token;                  !!!next-token;
5735                  redo B;                  !!!nack ('t220.1');
5736                    next B;
5737                } else {                } else {
5738                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5739                }                }
5740              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5741                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5742                                  text => $self->{open_elements}->[-1]->[0]
5743                                      ->manakai_local_name,
5744                                  token => $token);
5745    
5746                ## As if </table>                ## As if </table>
5747                ## have a table element in table scope                ## have a table element in table scope
5748                my $i;                my $i;
5749                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5750                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5751                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5752                    !!!cp ('t221');                    !!!cp ('t221');
5753                    $i = $_;                    $i = $_;
5754                    last INSCOPE;                    last INSCOPE;
5755                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5756                    !!!cp ('t222');                    !!!cp ('t222');
5757                    last INSCOPE;                    last INSCOPE;
5758                  }                  }
# Line 4460  sub _tree_construction_main ($) { Line 5760  sub _tree_construction_main ($) {
5760                unless (defined $i) {                unless (defined $i) {
5761                  !!!cp ('t223');                  !!!cp ('t223');
5762  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5763                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5764                                    token => $token);
5765                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5766                    !!!nack ('t223.1');
5767                  !!!next-token;                  !!!next-token;
5768                  redo B;                  next B;
5769                }                }
5770                                
5771    ## TODO: Followings are removed from the latest spec.
5772                ## generate implied end tags                ## generate implied end tags
5773                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5774                  !!!cp ('t224');                  !!!cp ('t224');
5775                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5776                }                }
5777    
5778                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5779                  !!!cp ('t225');                  !!!cp ('t225');
5780  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5781                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5782                                    text => $self->{open_elements}->[-1]->[0]
5783                                        ->manakai_local_name,
5784                                    token => $token);
5785                } else {                } else {
5786                  !!!cp ('t226');                  !!!cp ('t226');
5787                }                }
5788    
5789                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5790                  pop @{$open_tables};
5791    
5792                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5793    
5794                ## reprocess            ## reprocess
5795                redo B;            !!!ack-later;
5796              next B;
5797            } elsif ($token->{tag_name} eq 'style') {
5798              if (not $open_tables->[-1]->[1]) { # tainted
5799                !!!cp ('t227.8');
5800                ## NOTE: This is a "as if in head" code clone.
5801                $parse_rcdata->(CDATA_CONTENT_MODEL);
5802                next B;
5803              } else {
5804                !!!cp ('t227.7');
5805                #
5806              }
5807            } elsif ($token->{tag_name} eq 'script') {
5808              if (not $open_tables->[-1]->[1]) { # tainted
5809                !!!cp ('t227.6');
5810                ## NOTE: This is a "as if in head" code clone.
5811                $script_start_tag->();
5812                next B;
5813              } else {
5814                !!!cp ('t227.5');
5815                #
5816              }
5817            } elsif ($token->{tag_name} eq 'input') {
5818              if (not $open_tables->[-1]->[1]) { # tainted
5819                if ($token->{attributes}->{type}) { ## TODO: case
5820                  my $type = lc $token->{attributes}->{type}->{value};
5821                  if ($type eq 'hidden') {
5822                    !!!cp ('t227.3');
5823                    !!!parse-error (type => 'in table',
5824                                    text => $token->{tag_name}, token => $token);
5825    
5826                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5827    
5828                    ## TODO: form element pointer
5829    
5830                    pop @{$self->{open_elements}};
5831    
5832                    !!!next-token;
5833                    !!!ack ('t227.2.1');
5834                    next B;
5835                  } else {
5836                    !!!cp ('t227.2');
5837                    #
5838                  }
5839                } else {
5840                  !!!cp ('t227.1');
5841                  #
5842                }
5843              } else {
5844                !!!cp ('t227.4');
5845                #
5846              }
5847          } else {          } else {
5848            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5849            #            #
5850          }          }
5851    
5852            !!!parse-error (type => 'in table', text => $token->{tag_name},
5853                            token => $token);
5854    
5855            $insert = $insert_to_foster;
5856            #
5857        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5858              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5859                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4502  sub _tree_construction_main ($) { Line 5861  sub _tree_construction_main ($) {
5861                my $i;                my $i;
5862                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5863                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5864                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5865                    !!!cp ('t228');                    !!!cp ('t228');
5866                    $i = $_;                    $i = $_;
5867                    last INSCOPE;                    last INSCOPE;
5868                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5869                    !!!cp ('t229');                    !!!cp ('t229');
5870                    last INSCOPE;                    last INSCOPE;
5871                  }                  }
5872                } # INSCOPE                } # INSCOPE
5873                unless (defined $i) {                unless (defined $i) {
5874                  !!!cp ('t230');                  !!!cp ('t230');
5875                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5876                                    text => $token->{tag_name}, token => $token);
5877                  ## Ignore the token                  ## Ignore the token
5878                    !!!nack ('t230.1');
5879                  !!!next-token;                  !!!next-token;
5880                  redo B;                  next B;
5881                } else {                } else {
5882                  !!!cp ('t232');                  !!!cp ('t232');
5883                }                }
5884    
5885                ## Clear back to table row context                ## Clear back to table row context
5886                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5887                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5888                  !!!cp ('t231');                  !!!cp ('t231');
5889  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5890                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5891                }                }
5892    
5893                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5894                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5895                !!!next-token;                !!!next-token;
5896                redo B;                !!!nack ('t231.1');
5897                  next B;
5898              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5899                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5900                  ## As if </tr>                  ## As if </tr>
# Line 4544  sub _tree_construction_main ($) { Line 5902  sub _tree_construction_main ($) {
5902                  my $i;                  my $i;
5903                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5904                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5905                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5906                      !!!cp ('t233');                      !!!cp ('t233');
5907                      $i = $_;                      $i = $_;
5908                      last INSCOPE;                      last INSCOPE;
5909                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5910                      !!!cp ('t234');                      !!!cp ('t234');
5911                      last INSCOPE;                      last INSCOPE;
5912                    }                    }
# Line 4558  sub _tree_construction_main ($) { Line 5914  sub _tree_construction_main ($) {
5914                  unless (defined $i) {                  unless (defined $i) {
5915                    !!!cp ('t235');                    !!!cp ('t235');
5916  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5917                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag',
5918                                      text => $token->{type}, token => $token);
5919                    ## Ignore the token                    ## Ignore the token
5920                      !!!nack ('t236.1');
5921                    !!!next-token;                    !!!next-token;
5922                    redo B;                    next B;
5923                  }                  }
5924                                    
5925                  ## Clear back to table row context                  ## Clear back to table row context
5926                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5927                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5928                    !!!cp ('t236');                    !!!cp ('t236');
5929  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5930                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5931                  }                  }
5932                                    
# Line 4584  sub _tree_construction_main ($) { Line 5940  sub _tree_construction_main ($) {
5940                  my $i;                  my $i;
5941                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5942                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5943                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5944                      !!!cp ('t237');                      !!!cp ('t237');
5945                      $i = $_;                      $i = $_;
5946                      last INSCOPE;                      last INSCOPE;
5947                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5948                      !!!cp ('t238');                      !!!cp ('t238');
5949                      last INSCOPE;                      last INSCOPE;
5950                    }                    }
5951                  } # INSCOPE                  } # INSCOPE
5952                  unless (defined $i) {                  unless (defined $i) {
5953                    !!!cp ('t239');                    !!!cp ('t239');
5954                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5955                                      text => $token->{tag_name}, token => $token);
5956                    ## Ignore the token                    ## Ignore the token
5957                      !!!nack ('t239.1');
5958                    !!!next-token;                    !!!next-token;
5959                    redo B;                    next B;
5960                  }                  }
5961                                    
5962                  ## Clear back to table body context                  ## Clear back to table body context
5963                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5964                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5965                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5966                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5967                  }                  }
5968                                    
# Line 4626  sub _tree_construction_main ($) { Line 5978  sub _tree_construction_main ($) {
5978                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5979                }                }
5980    
5981                  ## NOTE: </table> in the "in table" insertion mode.
5982                  ## When you edit the code fragment below, please ensure that
5983                  ## the code for <table> in the "in table" insertion mode
5984                  ## is synced with it.
5985    
5986                ## have a table element in table scope                ## have a table element in table scope
5987                my $i;                my $i;
5988                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5989                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5990                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5991                    !!!cp ('t241');                    !!!cp ('t241');
5992                    $i = $_;                    $i = $_;
5993                    last INSCOPE;                    last INSCOPE;
5994                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5995                    !!!cp ('t242');                    !!!cp ('t242');
5996                    last INSCOPE;                    last INSCOPE;
5997                  }                  }
5998                } # INSCOPE                } # INSCOPE
5999                unless (defined $i) {                unless (defined $i) {
6000                  !!!cp ('t243');                  !!!cp ('t243');
6001                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6002                                    text => $token->{tag_name}, token => $token);
6003                  ## Ignore the token                  ## Ignore the token
6004                    !!!nack ('t243.1');
6005                  !!!next-token;                  !!!next-token;
6006                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               while ({  
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
 ## ISSUE: Can this case be reached?  
                 pop @{$self->{open_elements}};  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
6007                }                }
6008                                    
6009                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
6010                  pop @{$open_tables};
6011                                
6012                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6013                                
6014                !!!next-token;                !!!next-token;
6015                redo B;                next B;
6016              } elsif ({              } elsif ({
6017                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6018                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4680  sub _tree_construction_main ($) { Line 6022  sub _tree_construction_main ($) {
6022                  my $i;                  my $i;
6023                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6024                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6025                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6026                      !!!cp ('t247');                      !!!cp ('t247');
6027                      $i = $_;                      $i = $_;
6028                      last INSCOPE;                      last INSCOPE;
6029                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6030                      !!!cp ('t248');                      !!!cp ('t248');
6031                      last INSCOPE;                      last INSCOPE;
6032                    }                    }
6033                  } # INSCOPE                  } # INSCOPE
6034                    unless (defined $i) {                    unless (defined $i) {
6035                      !!!cp ('t249');                      !!!cp ('t249');
6036                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
6037                                        text => $token->{tag_name}, token => $token);
6038                      ## Ignore the token                      ## Ignore the token
6039                        !!!nack ('t249.1');
6040                      !!!next-token;                      !!!next-token;
6041                      redo B;                      next B;
6042                    }                    }
6043                                    
6044                  ## As if </tr>                  ## As if </tr>
# Line 4704  sub _tree_construction_main ($) { Line 6046  sub _tree_construction_main ($) {
6046                  my $i;                  my $i;
6047                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6048                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6049                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6050                      !!!cp ('t250');                      !!!cp ('t250');
6051                      $i = $_;                      $i = $_;
6052                      last INSCOPE;                      last INSCOPE;
6053                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6054                      !!!cp ('t251');                      !!!cp ('t251');
6055                      last INSCOPE;                      last INSCOPE;
6056                    }                    }
6057                  } # INSCOPE                  } # INSCOPE
6058                    unless (defined $i) {                    unless (defined $i) {
6059                      !!!cp ('t252');                      !!!cp ('t252');
6060                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
6061                                        text => 'tr', token => $token);
6062                      ## Ignore the token                      ## Ignore the token
6063                        !!!nack ('t252.1');
6064                      !!!next-token;                      !!!next-token;
6065                      redo B;                      next B;
6066                    }                    }
6067                                    
6068                  ## Clear back to table row context                  ## Clear back to table row context
6069                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6070                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6071                    !!!cp ('t253');                    !!!cp ('t253');
6072  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6073                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6074                  }                  }
6075                                    
# Line 4742  sub _tree_construction_main ($) { Line 6082  sub _tree_construction_main ($) {
6082                my $i;                my $i;
6083                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6084                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6085                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6086                    !!!cp ('t254');                    !!!cp ('t254');
6087                    $i = $_;                    $i = $_;
6088                    last INSCOPE;                    last INSCOPE;
6089                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6090                    !!!cp ('t255');                    !!!cp ('t255');
6091                    last INSCOPE;                    last INSCOPE;
6092                  }                  }
6093                } # INSCOPE                } # INSCOPE
6094                unless (defined $i) {                unless (defined $i) {
6095                  !!!cp ('t256');                  !!!cp ('t256');
6096                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6097                                    text => $token->{tag_name}, token => $token);
6098                  ## Ignore the token                  ## Ignore the token
6099                    !!!nack ('t256.1');
6100                  !!!next-token;                  !!!next-token;
6101                  redo B;                  next B;
6102                }                }
6103    
6104                ## Clear back to table body context                ## Clear back to table body context
6105                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6106                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
6107                  !!!cp ('t257');                  !!!cp ('t257');
6108  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
6109                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6110                }                }
6111    
6112                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6113                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6114                  !!!nack ('t257.1');
6115                !!!next-token;                !!!next-token;
6116                redo B;                next B;
6117              } elsif ({              } elsif ({
6118                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6119                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6120                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6121                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6122                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6123                !!!cp ('t258');            !!!cp ('t258');
6124                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6125                ## Ignore the token                            text => $token->{tag_name}, token => $token);
6126                !!!next-token;            ## Ignore the token
6127                redo B;            !!!nack ('t258.1');
6128               !!!next-token;
6129              next B;
6130          } else {          } else {
6131            !!!cp ('t259');            !!!cp ('t259');
6132            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
6133                              text => $token->{tag_name}, token => $token);
6134    
6135            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6136            #            #
6137          }          }
6138          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6139            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6140                    @{$self->{open_elements}} == 1) { # redundant, maybe
6141              !!!parse-error (type => 'in body:#eof', token => $token);
6142              !!!cp ('t259.1');
6143              #
6144            } else {
6145              !!!cp ('t259.2');
6146              #
6147            }
6148    
6149            ## Stop parsing
6150            last B;
6151        } else {        } else {
6152          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6153        }        }
# Line 4803  sub _tree_construction_main ($) { Line 6158  sub _tree_construction_main ($) {
6158                unless (length $token->{data}) {                unless (length $token->{data}) {
6159                  !!!cp ('t260');                  !!!cp ('t260');
6160                  !!!next-token;                  !!!next-token;
6161                  redo B;                  next B;
6162                }                }
6163              }              }
6164                            
# Line 4812  sub _tree_construction_main ($) { Line 6167  sub _tree_construction_main ($) {
6167            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6168              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6169                !!!cp ('t262');                !!!cp ('t262');
6170                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6171                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6172                  !!!ack ('t262.1');
6173                !!!next-token;                !!!next-token;
6174                redo B;                next B;
6175              } else {              } else {
6176                !!!cp ('t263');                !!!cp ('t263');
6177                #                #
6178              }              }
6179            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6180              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6181                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6182                  !!!cp ('t264');                  !!!cp ('t264');
6183                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
6184                                    text => 'colgroup', token => $token);
6185                  ## Ignore the token                  ## Ignore the token
6186                  !!!next-token;                  !!!next-token;
6187                  redo B;                  next B;
6188                } else {                } else {
6189                  !!!cp ('t265');                  !!!cp ('t265');
6190                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6191                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6192                  !!!next-token;                  !!!next-token;
6193                  redo B;                              next B;            
6194                }                }
6195              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6196                !!!cp ('t266');                !!!cp ('t266');
6197                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
6198                                  text => 'col', token => $token);
6199                ## Ignore the token                ## Ignore the token
6200                !!!next-token;                !!!next-token;
6201                redo B;                next B;
6202              } else {              } else {
6203                !!!cp ('t267');                !!!cp ('t267');
6204                #                #
6205              }              }
6206            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6207              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6208            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6209              !!!cp ('t270.2');
6210              ## Stop parsing.
6211              last B;
6212            } else {
6213              ## NOTE: As if </colgroup>.
6214              !!!cp ('t270.1');
6215              pop @{$self->{open_elements}}; # colgroup
6216              $self->{insertion_mode} = IN_TABLE_IM;
6217              ## Reprocess.
6218              next B;
6219            }
6220          } else {
6221            die "$0: $token->{type}: Unknown token type";
6222          }
6223    
6224            ## As if </colgroup>            ## As if </colgroup>
6225            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6226              !!!cp ('t269');              !!!cp ('t269');
6227              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
6228                !!!parse-error (type => 'unmatched end tag',
6229                                text => 'colgroup', token => $token);
6230              ## Ignore the token              ## Ignore the token
6231                !!!nack ('t269.1');
6232              !!!next-token;              !!!next-token;
6233              redo B;              next B;
6234            } else {            } else {
6235              !!!cp ('t270');              !!!cp ('t270');
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                !!!ack-later;
6239              ## reprocess              ## reprocess
6240              redo B;              next B;
6241            }            }
6242      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6243        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6244          !!!cp ('t271');          !!!cp ('t271');
6245          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6246          !!!next-token;          !!!next-token;
6247          redo B;          next B;
6248        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6249              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6250                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6251                  !!!cp ('t272');              !!!cp ('t272');
6252                  ## As if </option>              ## As if </option>
6253                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6254                } else {            } else {
6255                  !!!cp ('t273');              !!!cp ('t273');
6256                }            }
6257    
6258                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6259                !!!next-token;            !!!nack ('t273.1');
6260                redo B;            !!!next-token;
6261              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6262                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6263                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6264                  ## As if </option>              !!!cp ('t274');
6265                  pop @{$self->{open_elements}};              ## As if </option>
6266                } else {              pop @{$self->{open_elements}};
6267                  !!!cp ('t275');            } else {
6268                }              !!!cp ('t275');
6269              }
6270    
6271                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6272                  !!!cp ('t276');              !!!cp ('t276');
6273                  ## As if </optgroup>              ## As if </optgroup>
6274                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6275                } else {            } else {
6276                  !!!cp ('t277');              !!!cp ('t277');
6277                }            }
6278    
6279                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6280                !!!next-token;            !!!nack ('t277.1');
6281                redo B;            !!!next-token;
6282              } elsif ($token->{tag_name} eq 'select') {            next B;
6283  ## TODO: The type below is not good - <select> is replaced by </select>          } elsif ({
6284                !!!parse-error (type => 'not closed:select');                     select => 1, input => 1, textarea => 1,
6285                ## As if </select> instead                   }->{$token->{tag_name}} or
6286                ## have an element in table scope                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6287                my $i;                    {
6288                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                     caption => 1, table => 1,
6289                  my $node = $self->{open_elements}->[$_];                     tbody => 1, tfoot => 1, thead => 1,
6290                  if ($node->[1] eq $token->{tag_name}) {                     tr => 1, td => 1, th => 1,
6291                    !!!cp ('t278');                    }->{$token->{tag_name}})) {
6292                    $i = $_;            ## TODO: The type below is not good - <select> is replaced by </select>
6293                    last INSCOPE;            !!!parse-error (type => 'not closed', text => 'select',
6294                  } elsif ({                            token => $token);
6295                            table => 1, html => 1,            ## NOTE: As if the token were </select> (<select> case) or
6296                           }->{$node->[1]}) {            ## as if there were </select> (otherwise).
6297                    !!!cp ('t279');            ## have an element in table scope
6298                    last INSCOPE;            my $i;
6299                  }            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6300                } # INSCOPE              my $node = $self->{open_elements}->[$_];
6301                unless (defined $i) {              if ($node->[1] & SELECT_EL) {
6302                  !!!cp ('t280');                !!!cp ('t278');
6303                  !!!parse-error (type => 'unmatched end tag:select');                $i = $_;
6304                  ## Ignore the token                last INSCOPE;
6305                  !!!next-token;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6306                  redo B;                !!!cp ('t279');
6307                }                last INSCOPE;
6308                }
6309              } # INSCOPE
6310              unless (defined $i) {
6311                !!!cp ('t280');
6312                !!!parse-error (type => 'unmatched end tag',
6313                                text => 'select', token => $token);
6314                ## Ignore the token
6315                !!!nack ('t280.1');
6316                !!!next-token;
6317                next B;
6318              }
6319                                
6320                !!!cp ('t281');            !!!cp ('t281');
6321                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6322    
6323                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6324    
6325                !!!next-token;            if ($token->{tag_name} eq 'select') {
6326                redo B;              !!!nack ('t281.2');
6327                !!!next-token;
6328                next B;
6329              } else {
6330                !!!cp ('t281.1');
6331                !!!ack-later;
6332                ## Reprocess the token.
6333                next B;
6334              }
6335          } else {          } else {
6336            !!!cp ('t282');            !!!cp ('t282');
6337            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
6338                              text => $token->{tag_name}, token => $token);
6339            ## Ignore the token            ## Ignore the token
6340              !!!nack ('t282.1');
6341            !!!next-token;            !!!next-token;
6342            redo B;            next B;
6343          }          }
6344        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6345              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6346                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6347                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6348                  !!!cp ('t283');              !!!cp ('t283');
6349                  ## As if </option>              ## As if </option>
6350                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6351                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6352                  !!!cp ('t284');              !!!cp ('t284');
6353                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6354                } else {            } else {
6355                  !!!cp ('t285');              !!!cp ('t285');
6356                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6357                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6358                }              ## Ignore the token
6359                !!!next-token;            }
6360                redo B;            !!!nack ('t285.1');
6361              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6362                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6363                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6364                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6365                } else {              !!!cp ('t286');
6366                  !!!cp ('t287');              pop @{$self->{open_elements}};
6367                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
6368                  ## Ignore the token              !!!cp ('t287');
6369                }              !!!parse-error (type => 'unmatched end tag',
6370                !!!next-token;                              text => $token->{tag_name}, token => $token);
6371                redo B;              ## Ignore the token
6372              } elsif ($token->{tag_name} eq 'select') {            }
6373                ## have an element in table scope            !!!nack ('t287.1');
6374                my $i;            !!!next-token;
6375                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6376                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6377                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6378                    !!!cp ('t288');            my $i;
6379                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6380                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6381                  } elsif ({              if ($node->[1] & SELECT_EL) {
6382                            table => 1, html => 1,                !!!cp ('t288');
6383                           }->{$node->[1]}) {                $i = $_;
6384                    !!!cp ('t289');                last INSCOPE;
6385                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6386                  }                !!!cp ('t289');
6387                } # INSCOPE                last INSCOPE;
6388                unless (defined $i) {              }
6389                  !!!cp ('t290');            } # INSCOPE
6390                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
6391                  ## Ignore the token              !!!cp ('t290');
6392                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6393                  redo B;                              text => $token->{tag_name}, token => $token);
6394                }              ## Ignore the token
6395                !!!nack ('t290.1');
6396                !!!next-token;
6397                next B;
6398              }
6399                                
6400                !!!cp ('t291');            !!!cp ('t291');
6401                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6402    
6403                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6404    
6405                !!!next-token;            !!!nack ('t291.1');
6406                redo B;            !!!next-token;
6407              } elsif ({            next B;
6408                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6409                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6410                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6411                      tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6412                     }->{$token->{tag_name}}) {
6413  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6414                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6415                              text => $token->{tag_name}, token => $token);
6416                                
6417                ## have an element in table scope            ## have an element in table scope
6418                my $i;            my $i;
6419                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6420                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6421                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6422                    !!!cp ('t292');                !!!cp ('t292');
6423                    $i = $_;                $i = $_;
6424                    last INSCOPE;                last INSCOPE;
6425                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6426                            table => 1, html => 1,                !!!cp ('t293');
6427                           }->{$node->[1]}) {                last INSCOPE;
6428                    !!!cp ('t293');              }
6429                    last INSCOPE;            } # INSCOPE
6430                  }            unless (defined $i) {
6431                } # INSCOPE              !!!cp ('t294');
6432                unless (defined $i) {              ## Ignore the token
6433                  !!!cp ('t294');              !!!nack ('t294.1');
6434                  ## Ignore the token              !!!next-token;
6435                  !!!next-token;              next B;
6436                  redo B;            }
               }  
6437                                
6438                ## As if </select>            ## As if </select>
6439                ## have an element in table scope            ## have an element in table scope
6440                undef $i;            undef $i;
6441                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6442                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6443                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6444                    !!!cp ('t295');                !!!cp ('t295');
6445                    $i = $_;                $i = $_;
6446                    last INSCOPE;                last INSCOPE;
6447                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6448  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6449                    !!!cp ('t296');                !!!cp ('t296');
6450                    last INSCOPE;                last INSCOPE;
6451                  }              }
6452                } # INSCOPE            } # INSCOPE
6453                unless (defined $i) {            unless (defined $i) {
6454                  !!!cp ('t297');              !!!cp ('t297');
6455  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6456                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
6457                  ## Ignore the </select> token                              text => 'select', token => $token);
6458                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6459                  redo B;              !!!nack ('t297.1');
6460                }              !!!next-token; ## TODO: ok?
6461                next B;
6462              }
6463                                
6464                !!!cp ('t298');            !!!cp ('t298');
6465                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6466    
6467                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6468    
6469                ## reprocess            !!!ack-later;
6470                redo B;            ## reprocess
6471              next B;
6472          } else {          } else {
6473            !!!cp ('t299');            !!!cp ('t299');
6474            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
6475                              text => $token->{tag_name}, token => $token);
6476            ## Ignore the token            ## Ignore the token
6477              !!!nack ('t299.3');
6478            !!!next-token;            !!!next-token;
6479            redo B;            next B;
6480            }
6481          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6482            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6483                    @{$self->{open_elements}} == 1) { # redundant, maybe
6484              !!!cp ('t299.1');
6485              !!!parse-error (type => 'in body:#eof', token => $token);
6486            } else {
6487              !!!cp ('t299.2');
6488          }          }
6489    
6490            ## Stop parsing.
6491            last B;
6492        } else {        } else {
6493          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6494        }        }
# Line 5086  sub _tree_construction_main ($) { Line 6504  sub _tree_construction_main ($) {
6504            unless (length $token->{data}) {            unless (length $token->{data}) {
6505              !!!cp ('t300');              !!!cp ('t300');
6506              !!!next-token;              !!!next-token;
6507              redo B;              next B;
6508            }            }
6509          }          }
6510                    
6511          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6512            !!!cp ('t301');            !!!cp ('t301');
6513            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
6514    
6515            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6516          } else {          } else {
# Line 5100  sub _tree_construction_main ($) { Line 6518  sub _tree_construction_main ($) {
6518          }          }
6519                    
6520          ## "after body" insertion mode          ## "after body" insertion mode
6521          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6522    
6523          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6524          ## reprocess          ## reprocess
6525          redo B;          next B;
6526        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6527          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6528            !!!cp ('t303');            !!!cp ('t303');
6529            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6530                              text => $token->{tag_name}, token => $token);
6531                        
6532            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6533          } else {          } else {
# Line 5116  sub _tree_construction_main ($) { Line 6535  sub _tree_construction_main ($) {
6535          }          }
6536    
6537          ## "after body" insertion mode          ## "after body" insertion mode
6538          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6539                            text => $token->{tag_name}, token => $token);
6540    
6541          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6542            !!!ack-later;
6543          ## reprocess          ## reprocess
6544          redo B;          next B;
6545        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6546          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6547            !!!cp ('t305');            !!!cp ('t305');
6548            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6549                              text => $token->{tag_name}, token => $token);
6550                        
6551            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6552            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5136  sub _tree_construction_main ($) { Line 6558  sub _tree_construction_main ($) {
6558          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6559            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6560              !!!cp ('t307');              !!!cp ('t307');
6561              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
6562                                text => 'html', token => $token);
6563              ## Ignore the token              ## Ignore the token
6564              !!!next-token;              !!!next-token;
6565              redo B;              next B;
6566            } else {            } else {
6567              !!!cp ('t308');              !!!cp ('t308');
6568              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6569              !!!next-token;              !!!next-token;
6570              redo B;              next B;
6571            }            }
6572          } else {          } else {
6573            !!!cp ('t309');            !!!cp ('t309');
6574            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
6575                              text => $token->{tag_name}, token => $token);
6576    
6577            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6578            ## reprocess            ## reprocess
6579            redo B;            next B;
6580          }          }
6581          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6582            !!!cp ('t309.2');
6583            ## Stop parsing
6584            last B;
6585        } else {        } else {
6586          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6587        }        }
# Line 5165  sub _tree_construction_main ($) { Line 6593  sub _tree_construction_main ($) {
6593            unless (length $token->{data}) {            unless (length $token->{data}) {
6594              !!!cp ('t310');              !!!cp ('t310');
6595              !!!next-token;              !!!next-token;
6596              redo B;              next B;
6597            }            }
6598          }          }
6599                    
6600          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6601            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6602              !!!cp ('t311');              !!!cp ('t311');
6603              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
6604            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6605              !!!cp ('t312');              !!!cp ('t312');
6606              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6607            } else { # "after html frameset"            } else { # "after after frameset"
6608              !!!cp ('t313');              !!!cp ('t313');
6609              !!!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');  
6610            }            }
6611                        
6612            ## Ignore the token.            ## Ignore the token.
# Line 5193  sub _tree_construction_main ($) { Line 6617  sub _tree_construction_main ($) {
6617              !!!cp ('t315');              !!!cp ('t315');
6618              !!!next-token;              !!!next-token;
6619            }            }
6620            redo B;            next B;
6621          }          }
6622                    
6623          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6624        } 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');  
         }  
   
6625          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6626              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6627            !!!cp ('t318');            !!!cp ('t318');
6628            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6629              !!!nack ('t318.1');
6630            !!!next-token;            !!!next-token;
6631            redo B;            next B;
6632          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6633                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6634            !!!cp ('t319');            !!!cp ('t319');
6635            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6636            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6637              !!!ack ('t319.1');
6638            !!!next-token;            !!!next-token;
6639            redo B;            next B;
6640          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6641            !!!cp ('t320');            !!!cp ('t320');
6642            ## NOTE: As if in body.            ## NOTE: As if in head.
6643            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6644            redo B;            next B;
6645    
6646              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6647              ## has no parse error.
6648          } else {          } else {
6649            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6650              !!!cp ('t321');              !!!cp ('t321');
6651              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
6652            } else {                              text => $token->{tag_name}, token => $token);
6653              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6654              !!!cp ('t322');              !!!cp ('t322');
6655              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
6656                                text => $token->{tag_name}, token => $token);
6657              } else { # "after after frameset"
6658                !!!cp ('t322.2');
6659                !!!parse-error (type => 'after after frameset',
6660                                text => $token->{tag_name}, token => $token);
6661            }            }
6662            ## Ignore the token            ## Ignore the token
6663              !!!nack ('t322.1');
6664            !!!next-token;            !!!next-token;
6665            redo B;            next B;
6666          }          }
6667        } 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');  
         }  
   
6668          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6669              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6670            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6671                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6672              !!!cp ('t325');              !!!cp ('t325');
6673              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6674                                text => $token->{tag_name}, token => $token);
6675              ## Ignore the token              ## Ignore the token
6676              !!!next-token;              !!!next-token;
6677            } else {            } else {
# Line 5264  sub _tree_construction_main ($) { Line 6681  sub _tree_construction_main ($) {
6681            }            }
6682    
6683            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6684                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6685              !!!cp ('t327');              !!!cp ('t327');
6686              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6687            } else {            } else {
6688              !!!cp ('t328');              !!!cp ('t328');
6689            }            }
6690            redo B;            next B;
6691          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6692                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6693            !!!cp ('t329');            !!!cp ('t329');
6694            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6695            !!!next-token;            !!!next-token;
6696            redo B;            next B;
6697          } else {          } else {
6698            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6699              !!!cp ('t330');              !!!cp ('t330');
6700              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/',
6701            } else {                              text => $token->{tag_name}, token => $token);
6702              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6703                !!!cp ('t330.1');
6704                !!!parse-error (type => 'after frameset:/',
6705                                text => $token->{tag_name}, token => $token);
6706              } else { # "after after html"
6707              !!!cp ('t331');              !!!cp ('t331');
6708              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after after frameset:/',
6709                                text => $token->{tag_name}, token => $token);
6710            }            }
6711            ## Ignore the token            ## Ignore the token
6712            !!!next-token;            !!!next-token;
6713            redo B;            next B;
6714            }
6715          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6716            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6717                    @{$self->{open_elements}} == 1) { # redundant, maybe
6718              !!!cp ('t331.1');
6719              !!!parse-error (type => 'in body:#eof', token => $token);
6720            } else {
6721              !!!cp ('t331.2');
6722          }          }
6723            
6724            ## Stop parsing
6725            last B;
6726        } else {        } else {
6727          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6728        }        }
# Line 5303  sub _tree_construction_main ($) { Line 6737  sub _tree_construction_main ($) {
6737        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6738          !!!cp ('t332');          !!!cp ('t332');
6739          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6740          $script_start_tag->($insert);          $script_start_tag->();
6741          redo B;          next B;
6742        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6743          !!!cp ('t333');          !!!cp ('t333');
6744          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6745          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6746          redo B;          next B;
6747        } elsif ({        } elsif ({
6748                  base => 1, link => 1,                  base => 1, link => 1,
6749                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6750          !!!cp ('t334');          !!!cp ('t334');
6751          ## 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
6752          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6753          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6754            !!!ack ('t334.1');
6755          !!!next-token;          !!!next-token;
6756          redo B;          next B;
6757        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6758          ## 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
6759          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6760          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.
6761    
6762          unless ($self->{confident}) {          unless ($self->{confident}) {
6763            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6764              !!!cp ('t335');              !!!cp ('t335');
6765                ## NOTE: Whether the encoding is supported or not is handled
6766                ## in the {change_encoding} callback.
6767              $self->{change_encoding}              $self->{change_encoding}
6768                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6769                            
6770              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6771                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6772                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6773                                           ->{has_reference});                                           ->{has_reference});
6774            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6775              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6776                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6777                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6778                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6779                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6780                !!!cp ('t336');                !!!cp ('t336');
6781                  ## NOTE: Whether the encoding is supported or not is handled
6782                  ## in the {change_encoding} callback.
6783                $self->{change_encoding}                $self->{change_encoding}
6784                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6785                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6786                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6787                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5367  sub _tree_construction_main ($) { Line 6805  sub _tree_construction_main ($) {
6805            }            }
6806          }          }
6807    
6808            !!!ack ('t338.1');
6809          !!!next-token;          !!!next-token;
6810          redo B;          next B;
6811        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6812          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6813          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6814          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6815            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6816        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6817          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6818                                
6819          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6820              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6821            !!!cp ('t342');            !!!cp ('t342');
6822            ## Ignore the token            ## Ignore the token
6823          } else {          } else {
# Line 5401  sub _tree_construction_main ($) { Line 6831  sub _tree_construction_main ($) {
6831              }              }
6832            }            }
6833          }          }
6834            !!!nack ('t343.1');
6835          !!!next-token;          !!!next-token;
6836          redo B;          next B;
6837        } elsif ({        } elsif ({
6838                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6839                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6840                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6841                  listing => 1, menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6842                  pre => 1,                  pre => 1, listing => 1,
6843                    form => 1,
6844                    table => 1,
6845                    hr => 1,
6846                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6847            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6848              !!!cp ('t350');
6849              !!!parse-error (type => 'in form:form', token => $token);
6850              ## Ignore the token
6851              !!!nack ('t350.1');
6852              !!!next-token;
6853              next B;
6854            }
6855    
6856          ## has a p element in scope          ## has a p element in scope
6857          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6858            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6859              !!!cp ('t344');              !!!cp ('t344');
6860              !!!back-token;              !!!back-token; # <form>
6861              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6862              redo B;                        line => $token->{line}, column => $token->{column}};
6863            } elsif ({              next B;
6864                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6865              !!!cp ('t345');              !!!cp ('t345');
6866              last INSCOPE;              last INSCOPE;
6867            }            }
6868          } # INSCOPE          } # INSCOPE
6869                        
6870          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6871          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6872              !!!nack ('t346.1');
6873            !!!next-token;            !!!next-token;
6874            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6875              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5440  sub _tree_construction_main ($) { Line 6882  sub _tree_construction_main ($) {
6882            } else {            } else {
6883              !!!cp ('t348');              !!!cp ('t348');
6884            }            }
6885          } else {          } elsif ($token->{tag_name} eq 'form') {
6886            !!!cp ('t347');            !!!cp ('t347.1');
6887              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6888    
6889              !!!nack ('t347.2');
6890            !!!next-token;            !!!next-token;
6891          }          } elsif ($token->{tag_name} eq 'table') {
6892          redo B;            !!!cp ('t382');
6893        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6894          if (defined $self->{form_element}) {            
6895            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6896            !!!parse-error (type => 'in form:form');  
6897            ## Ignore the token            !!!nack ('t382.1');
6898              !!!next-token;
6899            } elsif ($token->{tag_name} eq 'hr') {
6900              !!!cp ('t386');
6901              pop @{$self->{open_elements}};
6902            
6903              !!!nack ('t386.1');
6904            !!!next-token;            !!!next-token;
           redo B;  
6905          } else {          } else {
6906            ## 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];  
6907            !!!next-token;            !!!next-token;
           redo B;  
6908          }          }
6909        } elsif ($token->{tag_name} eq 'li') {          next B;
6910          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6911          ## has a p element in scope          ## has a p element in scope
6912          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6913            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6914              !!!cp ('t353');              !!!cp ('t353');
6915              !!!back-token;              !!!back-token; # <x>
6916              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6917              redo B;                        line => $token->{line}, column => $token->{column}};
6918            } elsif ({              next B;
6919                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6920              !!!cp ('t354');              !!!cp ('t354');
6921              last INSCOPE;              last INSCOPE;
6922            }            }
# Line 5494  sub _tree_construction_main ($) { Line 6925  sub _tree_construction_main ($) {
6925          ## Step 1          ## Step 1
6926          my $i = -1;          my $i = -1;
6927          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6928            my $li_or_dtdd = {li => {li => 1},
6929                              dt => {dt => 1, dd => 1},
6930                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6931          LI: {          LI: {
6932            ## Step 2            ## Step 2
6933            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6934              if ($i != -1) {              if ($i != -1) {
6935                !!!cp ('t355');                !!!cp ('t355');
6936                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6937                                $self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
6938                                      ->manakai_local_name,
6939                                  token => $token);
6940              } else {              } else {
6941                !!!cp ('t356');                !!!cp ('t356');
6942              }              }
# Line 5511  sub _tree_construction_main ($) { Line 6947  sub _tree_construction_main ($) {
6947            }            }
6948                        
6949            ## Step 3            ## Step 3
6950            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6951                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6952                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6953                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6954                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6955                  not ($node->[1] & DIV_EL)) {
6956              !!!cp ('t358');              !!!cp ('t358');
6957              last LI;              last LI;
6958            }            }
# Line 5527  sub _tree_construction_main ($) { Line 6964  sub _tree_construction_main ($) {
6964            redo LI;            redo LI;
6965          } # LI          } # LI
6966                        
6967          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6968            !!!nack ('t359.1');
6969          !!!next-token;          !!!next-token;
6970          redo B;          next B;
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
6971        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6972          ## has a p element in scope          ## has a p element in scope
6973          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6974            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6975              !!!cp ('t367');              !!!cp ('t367');
6976              !!!back-token;              !!!back-token; # <plaintext>
6977              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6978              redo B;                        line => $token->{line}, column => $token->{column}};
6979            } elsif ({              next B;
6980                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6981              !!!cp ('t368');              !!!cp ('t368');
6982              last INSCOPE;              last INSCOPE;
6983            }            }
6984          } # INSCOPE          } # INSCOPE
6985                        
6986          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6987                        
6988          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6989                        
6990            !!!nack ('t368.1');
6991          !!!next-token;          !!!next-token;
6992          redo B;          next B;
6993        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6994          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6995            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6996            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6997              !!!cp ('t371');              !!!cp ('t371');
6998              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6999                            
7000              !!!back-token;              !!!back-token; # <a>
7001              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
7002              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
7003                $formatting_end_tag->($token);
7004                            
7005              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
7006                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5643  sub _tree_construction_main ($) { Line 7025  sub _tree_construction_main ($) {
7025                        
7026          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7027    
7028          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7029          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7030    
7031            !!!nack ('t374.1');
7032          !!!next-token;          !!!next-token;
7033          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;  
7034        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
7035          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7036    
7037          ## has a |nobr| element in scope          ## has a |nobr| element in scope
7038          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7039            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7040            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
7041              !!!cp ('t376');              !!!cp ('t376');
7042              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
7043              !!!back-token;              !!!back-token; # <nobr>
7044              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7045              redo B;                        line => $token->{line}, column => $token->{column}};
7046            } elsif ({              next B;
7047                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7048              !!!cp ('t377');              !!!cp ('t377');
7049              last INSCOPE;              last INSCOPE;
7050            }            }
7051          } # INSCOPE          } # INSCOPE
7052                    
7053          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7054          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7055                    
7056            !!!nack ('t377.1');
7057          !!!next-token;          !!!next-token;
7058          redo B;          next B;
7059        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7060          ## has a button element in scope          ## has a button element in scope
7061          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7062            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7063            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7064              !!!cp ('t378');              !!!cp ('t378');
7065              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
7066              !!!back-token;              !!!back-token; # <button>
7067              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7068              redo B;                        line => $token->{line}, column => $token->{column}};
7069            } elsif ({              next B;
7070                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7071              !!!cp ('t379');              !!!cp ('t379');
7072              last INSCOPE;              last INSCOPE;
7073            }            }
# Line 5708  sub _tree_construction_main ($) { Line 7075  sub _tree_construction_main ($) {
7075                        
7076          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7077                        
7078          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7079    
7080          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
7081    
7082          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7083    
7084            !!!nack ('t379.1');
7085          !!!next-token;          !!!next-token;
7086          redo B;          next B;
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
7087        } elsif ({        } elsif ({
7088                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7089                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7090                  image => 1,                  noembed => 1,
7091                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7092                    noscript => 0, ## TODO: 1 if scripting is enabled
7093                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7094          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7095            !!!cp ('t384');            !!!cp ('t381');
7096            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
7097          } else {          } else {
7098            !!!cp ('t385');            !!!cp ('t399');
7099          }          }
7100            ## NOTE: There is an "as if in body" code clone.
7101          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7102          $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;  
7103        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7104          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7105                    
7106          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7107            !!!cp ('t389');            !!!cp ('t389');
7108            ## Ignore the token            ## Ignore the token
7109              !!!nack ('t389'); ## NOTE: Not acknowledged.
7110            !!!next-token;            !!!next-token;
7111            redo B;            next B;
7112          } else {          } else {
7113              !!!ack ('t391.1');
7114    
7115            my $at = $token->{attributes};            my $at = $token->{attributes};
7116            my $form_attrs;            my $form_attrs;
7117            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5825  sub _tree_construction_main ($) { Line 7121  sub _tree_construction_main ($) {
7121            delete $at->{prompt};            delete $at->{prompt};
7122            my @tokens = (            my @tokens = (
7123                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7124                           attributes => $form_attrs},                           attributes => $form_attrs,
7125                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7126                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7127                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7128                            {type => START_TAG_TOKEN, tag_name => 'p',
7129                             line => $token->{line}, column => $token->{column}},
7130                            {type => START_TAG_TOKEN, tag_name => 'label',
7131                             line => $token->{line}, column => $token->{column}},
7132                         );                         );
7133            if ($prompt_attr) {            if ($prompt_attr) {
7134              !!!cp ('t390');              !!!cp ('t390');
7135              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7136                               #line => $token->{line}, column => $token->{column},
7137                              };
7138            } else {            } else {
7139              !!!cp ('t391');              !!!cp ('t391');
7140              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7141                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7142                               #line => $token->{line}, column => $token->{column},
7143                              }; # SHOULD
7144              ## TODO: make this configurable              ## TODO: make this configurable
7145            }            }
7146            push @tokens,            push @tokens,
7147                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7148                             line => $token->{line}, column => $token->{column}},
7149                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7150                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7151                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7152                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7153                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7154            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7155                             line => $token->{line}, column => $token->{column}},
7156                            {type => END_TAG_TOKEN, tag_name => 'form',
7157                             line => $token->{line}, column => $token->{column}};
7158            !!!back-token (@tokens);            !!!back-token (@tokens);
7159            redo B;            !!!next-token;
7160              next B;
7161          }          }
7162        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7163          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7164          my $el;          my $el;
7165          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7166                    
7167          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7168          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5862  sub _tree_construction_main ($) { Line 7171  sub _tree_construction_main ($) {
7171          $insert->($el);          $insert->($el);
7172                    
7173          my $text = '';          my $text = '';
7174            !!!nack ('t392.1');
7175          !!!next-token;          !!!next-token;
7176          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7177            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5892  sub _tree_construction_main ($) { Line 7202  sub _tree_construction_main ($) {
7202            ## Ignore the token            ## Ignore the token
7203          } else {          } else {
7204            !!!cp ('t398');            !!!cp ('t398');
7205            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7206          }          }
7207          !!!next-token;          !!!next-token;
7208            next B;
7209          } elsif ($token->{tag_name} eq 'rt' or
7210                   $token->{tag_name} eq 'rp') {
7211            ## has a |ruby| element in scope
7212            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7213              my $node = $self->{open_elements}->[$_];
7214              if ($node->[1] & RUBY_EL) {
7215                !!!cp ('t398.1');
7216                ## generate implied end tags
7217                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7218                  !!!cp ('t398.2');
7219                  pop @{$self->{open_elements}};
7220                }
7221                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7222                  !!!cp ('t398.3');
7223                  !!!parse-error (type => 'not closed',
7224                                  text => $self->{open_elements}->[-1]->[0]
7225                                      ->manakai_local_name,
7226                                  token => $token);
7227                  pop @{$self->{open_elements}}
7228                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7229                }
7230                last INSCOPE;
7231              } elsif ($node->[1] & SCOPING_EL) {
7232                !!!cp ('t398.4');
7233                last INSCOPE;
7234              }
7235            } # INSCOPE
7236    
7237            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7238    
7239            !!!nack ('t398.5');
7240            !!!next-token;
7241          redo B;          redo B;
7242        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7243                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
7244          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
7245    
7246          ## TODO: associate with $self->{form_element} if defined          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7247    
7248            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7249    
7250            ## "adjust foreign attributes" - done in insert-element-f
7251            
7252            !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7253                    
7254          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
7255              pop @{$self->{open_elements}};
7256              !!!ack ('t398.1');
7257            } else {
7258              !!!cp ('t398.2');
7259              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7260              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7261              ## mode, "in body" (not "in foreign content") secondary insertion
7262              ## mode, maybe.
7263            }
7264    
7265          !!!next-token;          !!!next-token;
7266          redo B;          next B;
7267        } elsif ({        } elsif ({
7268                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7269                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5924  sub _tree_construction_main ($) { Line 7271  sub _tree_construction_main ($) {
7271                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7272                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7273          !!!cp ('t401');          !!!cp ('t401');
7274          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
7275                            text => $token->{tag_name}, token => $token);
7276          ## Ignore the token          ## Ignore the token
7277            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7278          !!!next-token;          !!!next-token;
7279          redo B;          next B;
7280                    
7281          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7282        } else {        } else {
7283          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
7284              !!!cp ('t384');
7285              !!!parse-error (type => 'image', token => $token);
7286              $token->{tag_name} = 'img';
7287            } else {
7288              !!!cp ('t385');
7289            }
7290    
7291            ## NOTE: There is an "as if <br>" code clone.
7292          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7293                    
7294          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7295    
7296            if ({
7297                 applet => 1, marquee => 1, object => 1,
7298                }->{$token->{tag_name}}) {
7299              !!!cp ('t380');
7300              push @$active_formatting_elements, ['#marker', ''];
7301              !!!nack ('t380.1');
7302            } elsif ({
7303                      b => 1, big => 1, em => 1, font => 1, i => 1,
7304                      s => 1, small => 1, strile => 1,
7305                      strong => 1, tt => 1, u => 1,
7306                     }->{$token->{tag_name}}) {
7307              !!!cp ('t375');
7308              push @$active_formatting_elements, $self->{open_elements}->[-1];
7309              !!!nack ('t375.1');
7310            } elsif ($token->{tag_name} eq 'input') {
7311              !!!cp ('t388');
7312              ## TODO: associate with $self->{form_element} if defined
7313              pop @{$self->{open_elements}};
7314              !!!ack ('t388.2');
7315            } elsif ({
7316                      area => 1, basefont => 1, bgsound => 1, br => 1,
7317                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7318                      #image => 1,
7319                     }->{$token->{tag_name}}) {
7320              !!!cp ('t388.1');
7321              pop @{$self->{open_elements}};
7322              !!!ack ('t388.3');
7323            } elsif ($token->{tag_name} eq 'select') {
7324              ## TODO: associate with $self->{form_element} if defined
7325            
7326              if ($self->{insertion_mode} & TABLE_IMS or
7327                  $self->{insertion_mode} & BODY_TABLE_IMS or
7328                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7329                !!!cp ('t400.1');
7330                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7331              } else {
7332                !!!cp ('t400.2');
7333                $self->{insertion_mode} = IN_SELECT_IM;
7334              }
7335              !!!nack ('t400.3');
7336            } else {
7337              !!!nack ('t402');
7338            }
7339                    
7340          !!!next-token;          !!!next-token;
7341          redo B;          next B;
7342        }        }
7343      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7344        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7345          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7346              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7347            for (@{$self->{open_elements}}) {          INSCOPE: {
7348              unless ({            for (reverse @{$self->{open_elements}}) {
7349                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7350                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7351                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7352                      }->{$_->[1]}) {                last INSCOPE;
7353                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
7354                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
7355              } else {                last;
               !!!cp ('t404');  
7356              }              }
7357            }            }
7358    
7359            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7360                              text => $token->{tag_name}, token => $token);
7361              ## NOTE: Ignore the token.
7362            !!!next-token;            !!!next-token;
7363            redo B;            next B;
7364          } else {          } # INSCOPE
7365            !!!cp ('t405');  
7366            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          for (@{$self->{open_elements}}) {
7367            ## Ignore the token            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7368            !!!next-token;              !!!cp ('t403');
7369            redo B;              !!!parse-error (type => 'not closed',
7370                                text => $_->[0]->manakai_local_name,
7371                                token => $token);
7372                last;
7373              } else {
7374                !!!cp ('t404');
7375              }
7376          }          }
7377    
7378            $self->{insertion_mode} = AFTER_BODY_IM;
7379            !!!next-token;
7380            next B;
7381        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7382          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
7383            ## up-to-date, though it has same effect as speced.
7384            if (@{$self->{open_elements}} > 1 and
7385                $self->{open_elements}->[1]->[1] & BODY_EL) {
7386            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7387            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7388              !!!cp ('t406');              !!!cp ('t406');
7389              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
7390                                text => $self->{open_elements}->[1]->[0]
7391                                    ->manakai_local_name,
7392                                token => $token);
7393            } else {            } else {
7394              !!!cp ('t407');              !!!cp ('t407');
7395            }            }
7396            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7397            ## reprocess            ## reprocess
7398            redo B;            next B;
7399          } else {          } else {
7400            !!!cp ('t408');            !!!cp ('t408');
7401            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7402                              text => $token->{tag_name}, token => $token);
7403            ## Ignore the token            ## Ignore the token
7404            !!!next-token;            !!!next-token;
7405            redo B;            next B;
7406          }          }
7407        } elsif ({        } elsif ({
7408                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7409                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7410                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7411                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7412                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7413                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7414          ## has an element in scope          ## has an element in scope
7415          my $i;          my $i;
7416          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7417            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7418            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             while ({  
                     dd => ($token->{tag_name} ne 'dd'),  
                     dt => ($token->{tag_name} ne 'dt'),  
                     li => ($token->{tag_name} ne 'li'),  
                     p => ($token->{tag_name} ne 'p'),  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               pop @{$self->{open_elements}};  
             }  
               
7419              !!!cp ('t410');              !!!cp ('t410');
7420              $i = $_;              $i = $_;
7421              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7422            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7423              !!!cp ('t411');              !!!cp ('t411');
7424              last INSCOPE;              last INSCOPE;
7425            }            }
7426          } # INSCOPE          } # INSCOPE
7427            
7428          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7429            if (defined $i) {            !!!cp ('t413');
7430              !!!parse-error (type => 'unmatched end tag',
7431                              text => $token->{tag_name}, token => $token);
7432              ## NOTE: Ignore the token.
7433            } else {
7434              ## Step 1. generate implied end tags
7435              while ({
7436                      ## END_TAG_OPTIONAL_EL
7437                      dd => ($token->{tag_name} ne 'dd'),
7438                      dt => ($token->{tag_name} ne 'dt'),
7439                      li => ($token->{tag_name} ne 'li'),
7440                      p => 1,
7441                      rt => 1,
7442                      rp => 1,
7443                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7444                !!!cp ('t409');
7445                pop @{$self->{open_elements}};
7446              }
7447    
7448              ## Step 2.
7449              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7450                      ne $token->{tag_name}) {
7451              !!!cp ('t412');              !!!cp ('t412');
7452              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7453                                text => $self->{open_elements}->[-1]->[0]
7454                                    ->manakai_local_name,
7455                                token => $token);
7456            } else {            } else {
7457              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
7458            }            }
7459          }  
7460                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
7461            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7462          } elsif ($token->{tag_name} eq 'p') {  
7463            !!!cp ('t415');            ## Step 4.
7464            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
7465            my $el;                if {
7466            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
7467            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
7468          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7469          !!!next-token;          !!!next-token;
7470          redo B;          next B;
7471        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7472            undef $self->{form_element};
7473    
7474          ## has an element in scope          ## has an element in scope
7475            my $i;
7476          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7477            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7478            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               pop @{$self->{open_elements}};  
             }  
   
7479              !!!cp ('t418');              !!!cp ('t418');
7480                $i = $_;
7481              last INSCOPE;              last INSCOPE;
7482            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7483              !!!cp ('t419');              !!!cp ('t419');
7484              last INSCOPE;              last INSCOPE;
7485            }            }
7486          } # INSCOPE          } # INSCOPE
7487            
7488          if ($self->{open_elements}->[-1]->[1] eq $token->{tag_name}) {          unless (defined $i) { # has an element in scope
           !!!cp ('t420');  
           pop @{$self->{open_elements}};  
         } else {  
7489            !!!cp ('t421');            !!!cp ('t421');
7490            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7491                              text => $token->{tag_name}, token => $token);
7492              ## NOTE: Ignore the token.
7493            } else {
7494              ## Step 1. generate implied end tags
7495              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7496                !!!cp ('t417');
7497                pop @{$self->{open_elements}};
7498              }
7499              
7500              ## Step 2.
7501              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7502                      ne $token->{tag_name}) {
7503                !!!cp ('t417.1');
7504                !!!parse-error (type => 'not closed',
7505                                text => $self->{open_elements}->[-1]->[0]
7506                                    ->manakai_local_name,
7507                                token => $token);
7508              } else {
7509                !!!cp ('t420');
7510              }  
7511              
7512              ## Step 3.
7513              splice @{$self->{open_elements}}, $i;
7514          }          }
7515    
         undef $self->{form_element};  
7516          !!!next-token;          !!!next-token;
7517          redo B;          next B;
7518        } elsif ({        } elsif ({
7519                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7520                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6091  sub _tree_construction_main ($) { Line 7522  sub _tree_construction_main ($) {
7522          my $i;          my $i;
7523          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7524            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7525            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             while ({  
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               pop @{$self->{open_elements}};  
             }  
   
7526              !!!cp ('t423');              !!!cp ('t423');
7527              $i = $_;              $i = $_;
7528              last INSCOPE;              last INSCOPE;
7529            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7530              !!!cp ('t424');              !!!cp ('t424');
7531              last INSCOPE;              last INSCOPE;
7532            }            }
7533          } # INSCOPE          } # INSCOPE
7534    
7535            unless (defined $i) { # has an element in scope
7536              !!!cp ('t425.1');
7537              !!!parse-error (type => 'unmatched end tag',
7538                              text => $token->{tag_name}, token => $token);
7539              ## NOTE: Ignore the token.
7540            } else {
7541              ## Step 1. generate implied end tags
7542              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7543                !!!cp ('t422');
7544                pop @{$self->{open_elements}};
7545              }
7546              
7547              ## Step 2.
7548              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7549                      ne $token->{tag_name}) {
7550                !!!cp ('t425');
7551                !!!parse-error (type => 'unmatched end tag',
7552                                text => $token->{tag_name}, token => $token);
7553              } else {
7554                !!!cp ('t426');
7555              }
7556    
7557              ## Step 3.
7558              splice @{$self->{open_elements}}, $i;
7559            }
7560                    
7561          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
7562            !!!cp ('t425');          next B;
7563            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
7564            ## has an element in scope
7565            my $i;
7566            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7567              my $node = $self->{open_elements}->[$_];
7568              if ($node->[1] & P_EL) {
7569                !!!cp ('t410.1');
7570                $i = $_;
7571                last INSCOPE;
7572              } elsif ($node->[1] & SCOPING_EL) {
7573                !!!cp ('t411.1');
7574                last INSCOPE;
7575              }
7576            } # INSCOPE
7577    
7578            if (defined $i) {
7579              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7580                      ne $token->{tag_name}) {
7581                !!!cp ('t412.1');
7582                !!!parse-error (type => 'not closed',
7583                                text => $self->{open_elements}->[-1]->[0]
7584                                    ->manakai_local_name,
7585                                token => $token);
7586              } else {
7587                !!!cp ('t414.1');
7588              }
7589    
7590              splice @{$self->{open_elements}}, $i;
7591          } else {          } else {
7592            !!!cp ('t426');            !!!cp ('t413.1');
7593              !!!parse-error (type => 'unmatched end tag',
7594                              text => $token->{tag_name}, token => $token);
7595    
7596              !!!cp ('t415.1');
7597              ## As if <p>, then reprocess the current token
7598              my $el;
7599              !!!create-element ($el, $HTML_NS, 'p',, $token);
7600              $insert->($el);
7601              ## NOTE: Not inserted into |$self->{open_elements}|.
7602          }          }
7603            
         splice @{$self->{open_elements}}, $i if defined $i;  
7604          !!!next-token;          !!!next-token;
7605          redo B;          next B;
7606        } elsif ({        } elsif ({
7607                  a => 1,                  a => 1,
7608                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6131  sub _tree_construction_main ($) { Line 7610  sub _tree_construction_main ($) {
7610                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7611                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7612          !!!cp ('t427');          !!!cp ('t427');
7613          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7614          redo B;          next B;
7615        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7616          !!!cp ('t428');          !!!cp ('t428');
7617          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
7618                            text => 'br', token => $token);
7619    
7620          ## As if <br>          ## As if <br>
7621          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7622                    
7623          my $el;          my $el;
7624          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7625          $insert->($el);          $insert->($el);
7626                    
7627          ## Ignore the token.          ## Ignore the token.
7628          !!!next-token;          !!!next-token;
7629          redo B;          next B;
7630        } elsif ({        } elsif ({
7631                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7632                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6160  sub _tree_construction_main ($) { Line 7640  sub _tree_construction_main ($) {
7640                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7641                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7642          !!!cp ('t429');          !!!cp ('t429');
7643          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
7644                            text => $token->{tag_name}, token => $token);
7645          ## Ignore the token          ## Ignore the token
7646          !!!next-token;          !!!next-token;
7647          redo B;          next B;
7648                    
7649          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7650                    
# Line 6174  sub _tree_construction_main ($) { Line 7655  sub _tree_construction_main ($) {
7655    
7656          ## Step 2          ## Step 2
7657          S2: {          S2: {
7658            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7659              ## Step 1              ## Step 1
7660              ## generate implied end tags              ## generate implied end tags
7661              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7662                !!!cp ('t430');                !!!cp ('t430');
7663                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7664                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7665                  ## which seems wrong.
7666                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7667                  $node_i++;
7668              }              }
7669                    
7670              ## Step 2              ## Step 2
7671              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7672                        ne $token->{tag_name}) {
7673                !!!cp ('t431');                !!!cp ('t431');
7674                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7675                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7676                                  text => $self->{open_elements}->[-1]->[0]
7677                                      ->manakai_local_name,
7678                                  token => $token);
7679              } else {              } else {
7680                !!!cp ('t432');                !!!cp ('t432');
7681              }              }
7682                            
7683              ## Step 3              ## Step 3
7684              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7685    
7686              !!!next-token;              !!!next-token;
7687              last S2;              last S2;
7688            } else {            } else {
7689              ## Step 3              ## Step 3
7690              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7691                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7692                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7693                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7694                !!!cp ('t433');                !!!cp ('t433');
7695                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
7696                                  text => $token->{tag_name}, token => $token);
7697                ## Ignore the token                ## Ignore the token
7698                !!!next-token;                !!!next-token;
7699                last S2;                last S2;
# Line 6222  sub _tree_construction_main ($) { Line 7709  sub _tree_construction_main ($) {
7709            ## Step 5;            ## Step 5;
7710            redo S2;            redo S2;
7711          } # S2          } # S2
7712          redo B;          next B;
7713        }        }
7714      }      }
7715      redo B;      next B;
7716      } continue { # B
7717        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7718          ## NOTE: The code below is executed in cases where it does not have
7719          ## to be, but it it is harmless even in those cases.
7720          ## has an element in scope
7721          INSCOPE: {
7722            for (reverse 0..$#{$self->{open_elements}}) {
7723              my $node = $self->{open_elements}->[$_];
7724              if ($node->[1] & FOREIGN_EL) {
7725                last INSCOPE;
7726              } elsif ($node->[1] & SCOPING_EL) {
7727                last;
7728              }
7729            }
7730            
7731            ## NOTE: No foreign element in scope.
7732            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7733          } # INSCOPE
7734        }
7735    } # B    } # B
7736    
7737    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6233  sub _tree_construction_main ($) { Line 7739  sub _tree_construction_main ($) {
7739    ## TODO: script stuffs    ## TODO: script stuffs
7740  } # _tree_construct_main  } # _tree_construct_main
7741    
7742  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7743    my $class = shift;    my $class = shift;
7744    my $node = shift;    my $node = shift;
7745    my $s = \$_[0];    #my $s = \$_[0];
7746    my $onerror = $_[1];    my $onerror = $_[1];
7747      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7748    
7749    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7750    
# Line 6256  sub set_inner_html ($$$) { Line 7763  sub set_inner_html ($$$) {
7763      }      }
7764    
7765      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7766      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7767    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7768      ## TODO: If non-html element      ## TODO: If non-html element
7769    
7770      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7771    
7772    ## TODO: Support for $get_wrapper
7773    
7774      ## Step 1 # MUST      ## Step 1 # MUST
7775      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7776      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6271  sub set_inner_html ($$$) { Line 7780  sub set_inner_html ($$$) {
7780    
7781      ## Step 8 # MUST      ## Step 8 # MUST
7782      my $i = 0;      my $i = 0;
7783      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7784      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7785        require Whatpm::Charset::DecodeHandle;
7786        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7787        $input = $get_wrapper->($input);
7788      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7789        my $self = shift;        my $self = shift;
7790    
7791        pop @{$self->{prev_char}};        pop @{$self->{prev_char}};
7792        unshift @{$self->{prev_char}}, $self->{next_char};        unshift @{$self->{prev_char}}, $self->{next_char};
7793    
7794        $self->{next_char} = -1 and return if $i >= length $$s;        my $char = '';
7795        $self->{next_char} = ord substr $$s, $i++, 1;        if (defined $self->{next_next_char}) {
7796        $column++;          $char = $self->{next_next_char};
7797            delete $self->{next_next_char};
7798            $self->{next_char} = ord $char;
7799          } else {
7800            if ($input->read ($char, 1)) {
7801              $self->{next_char} = ord $char;
7802            } else {
7803              $self->{next_char} = -1;
7804              return;
7805            }
7806          }
7807    
7808          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7809          $p->{column}++;
7810    
7811        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7812          $line++;          $p->{line}++;
7813          $column = 0;          $p->{column} = 0;
7814          !!!cp ('i1');          !!!cp ('i1');
7815        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7816          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7817            my $next = '';
7818            if ($input->read ($next, 1) and $next ne "\x0A") {
7819              $self->{next_next_char} = $next;
7820            }
7821          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7822          $line++;          $p->{line}++;
7823          $column = 0;          $p->{column} = 0;
7824          !!!cp ('i2');          !!!cp ('i2');
7825        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7826          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6300  sub set_inner_html ($$$) { Line 7829  sub set_inner_html ($$$) {
7829          !!!cp ('i4');          !!!cp ('i4');
7830          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7831          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7832          } elsif ($self->{next_char} <= 0x0008 or
7833                   (0x000E <= $self->{next_char} and
7834                    $self->{next_char} <= 0x001F) or
7835                   (0x007F <= $self->{next_char} and
7836                    $self->{next_char} <= 0x009F) or
7837                   (0xD800 <= $self->{next_char} and
7838                    $self->{next_char} <= 0xDFFF) or
7839                   (0xFDD0 <= $self->{next_char} and
7840                    $self->{next_char} <= 0xFDDF) or
7841                   {
7842                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7843                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7844                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7845                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7846                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7847                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7848                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7849                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7850                    0x10FFFE => 1, 0x10FFFF => 1,
7851                   }->{$self->{next_char}}) {
7852            !!!cp ('i4.1');
7853            if ($self->{next_char} < 0x10000) {
7854              !!!parse-error (type => 'control char',
7855                              text => (sprintf 'U+%04X', $self->{next_char}));
7856            } else {
7857              !!!parse-error (type => 'control char',
7858                              text => (sprintf 'U-%08X', $self->{next_char}));
7859            }
7860        }        }
7861      };      };
7862      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7863      $p->{next_char} = -1;      $p->{next_char} = -1;
7864        
7865        $p->{read_until} = sub {
7866          #my ($scalar, $specials_range, $offset) = @_;
7867          my $specials_range = $_[1];
7868          return 0 if defined $p->{next_next_char};
7869          my $count = $input->manakai_read_until
7870            ($_[0],
7871             qr/(?![$specials_range\x{FDD0}-\x{FDDF}\x{FFFE}\x{FFFF}\x{1FFFE}\x{1FFFF}\x{2FFFE}\x{2FFFF}\x{3FFFE}\x{3FFFF}\x{4FFFE}\x{4FFFF}\x{5FFFE}\x{5FFFF}\x{6FFFE}\x{6FFFF}\x{7FFFE}\x{7FFFF}\x{8FFFE}\x{8FFFF}\x{9FFFE}\x{9FFFF}\x{AFFFE}\x{AFFFF}\x{BFFFE}\x{BFFFF}\x{CFFFE}\x{CFFFF}\x{DFFFE}\x{DFFFF}\x{EFFFE}\x{EFFFF}\x{FFFFE}\x{FFFFF}])[\x20-\x7E\xA0-\x{D7FF}\x{E000}-\x{10FFFD}]/,
7872             $_[2]);
7873          if ($count) {
7874            $p->{column} += $count;
7875            $p->{column_prev} += $count;
7876            $p->{prev_char} = [-1, -1, -1];
7877            $p->{next_char} = -1;
7878          }
7879          return $count;
7880        }; # $p->{read_until}
7881    
7882      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7883        my (%opt) = @_;        my (%opt) = @_;
7884        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7885          my $column = $opt{column};
7886          if (defined $opt{token} and defined $opt{token}->{line}) {
7887            $line = $opt{token}->{line};
7888            $column = $opt{token}->{column};
7889          }
7890          warn "Parse error ($opt{type}) at line $line column $column\n";
7891      };      };
7892      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7893        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7894      };      };
7895            
7896        my $char_onerror = sub {
7897          my (undef, $type, %opt) = @_;
7898          $ponerror->(layer => 'encode',
7899                      line => $p->{line}, column => $p->{column} + 1,
7900                      %opt, type => $type);
7901        }; # $char_onerror
7902        $input->onerror ($char_onerror);
7903    
7904      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7905      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7906    
# Line 6334  sub set_inner_html ($$$) { Line 7922  sub set_inner_html ($$$) {
7922          unless defined $p->{content_model};          unless defined $p->{content_model};
7923          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7924    
7925      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7926          ## TODO: Foreign element OK?
7927    
7928      ## Step 3      ## Step 3
7929      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6344  sub set_inner_html ($$$) { Line 7933  sub set_inner_html ($$$) {
7933      $doc->append_child ($root);      $doc->append_child ($root);
7934    
7935      ## Step 5 # MUST      ## Step 5 # MUST
7936      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7937    
7938      undef $p->{head_element};      undef $p->{head_element};
7939    
# Line 6390  sub set_inner_html ($$$) { Line 7979  sub set_inner_html ($$$) {
7979      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7980    
7981      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7982    
7983        delete $p->{parse_error}; # delete loop
7984    } else {    } else {
7985      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";
7986    }    }

Legend:
Removed from v.1.86  
changed lines
  Added in v.1.178

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24