/[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.81 by wakaba, Tue Mar 4 14:51:54 2008 UTC revision 1.172 by wakaba, Sun Sep 14 03:07:58 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                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
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 177  sub parse_string ($$$;$) { Line 641  sub parse_string ($$$;$) {
641        if defined $self->{input_encoding};        if defined $self->{input_encoding};
642    
643    my $i = 0;    my $i = 0;
644    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
645    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
646    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
647      my $self = shift;      my $self = shift;
648    
649      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
653      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
654      $column++;        $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662        ($self->{line_prev}, $self->{column_prev})
663            = ($self->{line}, $self->{column});
664        $self->{column}++;
665            
666      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667        $line++;        !!!cp ('j1');
668        $column = 0;        $self->{line}++;
669          $self->{column} = 0;
670      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
671        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
672    ## TODO: support for abort/streaming
673          my $next = $input->getc;
674          if (defined $next and $next ne "\x0A") {
675            $self->{next_next_char} = $next;
676          }
677        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
678        $line++;        $self->{line}++;
679        $column = 0;        $self->{column} = 0;
680      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
681          !!!cp ('j3');
682        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
683      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
684          !!!cp ('j4');
685        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
686        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
687        } elsif ($self->{next_char} <= 0x0008 or
688                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
689                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
690                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
691                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
692    ## ISSUE: U+FDE0-U+FDEF are not excluded
693                 {
694                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
695                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
696                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
697                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
698                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
699                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
700                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
701                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
702                  0x10FFFE => 1, 0x10FFFF => 1,
703                 }->{$self->{next_char}}) {
704          !!!cp ('j5');
705          if ($self->{next_char} < 0x10000) {
706            !!!parse-error (type => 'control char',
707                            text => (sprintf 'U+%04X', $self->{next_char}));
708          } else {
709            !!!parse-error (type => 'control char',
710                            text => (sprintf 'U-%08X', $self->{next_char}));
711          }
712      }      }
713    };    };
714    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
715    $self->{next_char} = -1;    $self->{next_char} = -1;
716    
717      $self->{read_until} = sub {
718        #my ($scalar, $specials_range, $offset) = @_;
719        my $specials_range = $_[1];
720        return 0 if defined $self->{next_next_char};
721        my $count = $input->manakai_read_until
722           ($_[0],
723            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}]/,
724            $_[2]);
725        if ($count) {
726          $self->{column} += $count;
727          $self->{column_prev} += $count;
728          $self->{prev_char} = [-1, -1, -1];
729          $self->{next_char} = -1;
730        }
731        return $count;
732      }; # $self->{read_until}
733    
734    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
735      my (%opt) = @_;      my (%opt) = @_;
736      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
737        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
738        warn "Parse error ($opt{type}) at line $line column $column\n";
739    };    };
740    $self->{parse_error} = sub {    $self->{parse_error} = sub {
741      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
742    };    };
743    
744    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 746  sub parse_string ($$$;$) {
746    $self->_construct_tree;    $self->_construct_tree;
747    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
748    
749      delete $self->{parse_error}; # remove loop
750    
751    return $self->{document};    return $self->{document};
752  } # parse_string  } # parse_char_stream
753    
754  sub new ($) {  sub new ($) {
755    my $class = shift;    my $class = shift;
756    my $self = bless {}, $class;    my $self = bless {
757        level => {must => 'm',
758                  should => 's',
759                  warn => 'w',
760                  info => 'i',
761                  uncertain => 'u'},
762      }, $class;
763    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
764      $self->{next_char} = -1;      $self->{next_char} = -1;
765    };    };
# Line 254  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 788  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
788  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
789    
790  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
791  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
792  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
793  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
794  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 265  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 799  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
799  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
800  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
801  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
802  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
803  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
804  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
805  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 821  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
821  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
822  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
823  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
824    sub SELF_CLOSING_START_TAG_STATE () { 34 }
825    sub CDATA_SECTION_STATE () { 35 }
826    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
827    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
828    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
829    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
830    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
831    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
832    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
833    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
834    ## NOTE: "Entity data state", "entity in attribute value state", and
835    ## "consume a character reference" algorithm are jointly implemented
836    ## using the following six states:
837    sub ENTITY_STATE () { 44 }
838    sub ENTITY_HASH_STATE () { 45 }
839    sub NCR_NUM_STATE () { 46 }
840    sub HEXREF_X_STATE () { 47 }
841    sub HEXREF_HEX_STATE () { 48 }
842    sub ENTITY_NAME_STATE () { 49 }
843    
844  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
845  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 303  sub TABLE_IMS ()      { 0b1000000 } Line 856  sub TABLE_IMS ()      { 0b1000000 }
856  sub ROW_IMS ()        { 0b10000000 }  sub ROW_IMS ()        { 0b10000000 }
857  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
858  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
859    sub SELECT_IMS ()     { 0b10000000000 }
860    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
861        ## NOTE: "in foreign content" insertion mode is special; it is combined
862        ## with the secondary insertion mode.  In this parser, they are stored
863        ## together in the bit-or'ed form.
864    
865    ## NOTE: "initial" and "before html" insertion modes have no constants.
866    
867    ## NOTE: "after after body" insertion mode.
868  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }  sub AFTER_HTML_BODY_IM () { AFTER_HTML_IMS | BODY_AFTER_IMS }
869    
870    ## NOTE: "after after frameset" insertion mode.
871  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }  sub AFTER_HTML_FRAMESET_IM () { AFTER_HTML_IMS | FRAME_IMS }
872    
873  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }  sub IN_HEAD_IM () { HEAD_IMS | 0b00 }
874  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }  sub IN_HEAD_NOSCRIPT_IM () { HEAD_IMS | 0b01 }
875  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }  sub AFTER_HEAD_IM () { HEAD_IMS | 0b10 }
# Line 319  sub IN_TABLE_IM () { TABLE_IMS } Line 883  sub IN_TABLE_IM () { TABLE_IMS }
883  sub AFTER_BODY_IM () { BODY_AFTER_IMS }  sub AFTER_BODY_IM () { BODY_AFTER_IMS }
884  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }  sub IN_FRAMESET_IM () { FRAME_IMS | 0b01 }
885  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }  sub AFTER_FRAMESET_IM () { FRAME_IMS | 0b10 }
886  sub IN_SELECT_IM () { 0b01 }  sub IN_SELECT_IM () { SELECT_IMS | 0b01 }
887    sub IN_SELECT_IN_TABLE_IM () { SELECT_IMS | 0b10 }
888  sub IN_COLUMN_GROUP_IM () { 0b10 }  sub IN_COLUMN_GROUP_IM () { 0b10 }
889    
890  ## Implementations MUST act as if state machine in the spec  ## Implementations MUST act as if state machine in the spec
# Line 327  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 892  sub IN_COLUMN_GROUP_IM () { 0b10 }
892  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
893    my $self = shift;    my $self = shift;
894    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
895      #$self->{state_keyword}; # initialized when used
896      #$self->{entity__value}; # initialized when used
897      #$self->{entity__match}; # initialized when used
898    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
899    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
900    undef $self->{current_attribute};    undef $self->{current_attribute};
901    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
902    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
903    $self->{char} = [];    delete $self->{self_closing};
904    # $self->{next_char}    # $self->{next_char}
905    !!!next-input-character;    !!!next-input-character;
906    $self->{token} = [];    $self->{token} = [];
# Line 352  sub _initialize_tokenizer ($) { Line 920  sub _initialize_tokenizer ($) {
920  ##        ->{value}  ##        ->{value}
921  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
922  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
923    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
924    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
925    ##     while the token is pushed back to the stack.
926    
927  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
928    
# Line 361  sub _initialize_tokenizer ($) { Line 932  sub _initialize_tokenizer ($) {
932  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
933  ## and removed from the list.  ## and removed from the list.
934    
935  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
936  ## 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,  
937    
938  sub _get_next_token ($) {  sub _get_next_token ($) {
939    my $self = shift;    my $self = shift;
940    
941      if ($self->{self_closing}) {
942        !!!parse-error (type => 'nestc', token => $self->{current_token});
943        ## NOTE: The |self_closing| flag is only set by start tag token.
944        ## In addition, when a start tag token is emitted, it is always set to
945        ## |current_token|.
946        delete $self->{self_closing};
947      }
948    
949    if (@{$self->{token}}) {    if (@{$self->{token}}) {
950        $self->{self_closing} = $self->{token}->[0]->{self_closing};
951      return shift @{$self->{token}};      return shift @{$self->{token}};
952    }    }
953    
# Line 388  sub _get_next_token ($) { Line 957  sub _get_next_token ($) {
957          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
958              not $self->{escape}) {              not $self->{escape}) {
959            !!!cp (1);            !!!cp (1);
960            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
961              ## "entity data state".  In this implementation, the tokenizer
962              ## is switched to the |ENTITY_STATE|, which is an implementation
963              ## of the "consume a character reference" algorithm.
964              $self->{entity_additional} = -1;
965              $self->{prev_state} = DATA_STATE;
966              $self->{state} = ENTITY_STATE;
967            !!!next-input-character;            !!!next-input-character;
968            redo A;            redo A;
969          } else {          } else {
# Line 441  sub _get_next_token ($) { Line 1016  sub _get_next_token ($) {
1016          #          #
1017        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1018          !!!cp (11);          !!!cp (11);
1019          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
1020                      line => $self->{line}, column => $self->{column}});
1021          last A; ## TODO: ok?          last A; ## TODO: ok?
1022        } else {        } else {
1023          !!!cp (12);          !!!cp (12);
1024        }        }
1025        # Anything else        # Anything else
1026        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1027                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
1028                       line => $self->{line}, column => $self->{column},
1029                      };
1030          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1031    
1032        ## Stay in the data state        ## Stay in the data state
1033        !!!next-input-character;        !!!next-input-character;
1034    
1035        !!!emit ($token);        !!!emit ($token);
1036    
1037        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;  
1038      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1039        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1040          if ($self->{next_char} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
# Line 484  sub _get_next_token ($) { Line 1047  sub _get_next_token ($) {
1047            ## reconsume            ## reconsume
1048            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1049    
1050            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1051                        line => $self->{line_prev},
1052                        column => $self->{column_prev},
1053                       });
1054    
1055            redo A;            redo A;
1056          }          }
# Line 504  sub _get_next_token ($) { Line 1070  sub _get_next_token ($) {
1070            !!!cp (19);            !!!cp (19);
1071            $self->{current_token}            $self->{current_token}
1072              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1073                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1074                   line => $self->{line_prev},
1075                   column => $self->{column_prev}};
1076            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1077            !!!next-input-character;            !!!next-input-character;
1078            redo A;            redo A;
# Line 512  sub _get_next_token ($) { Line 1080  sub _get_next_token ($) {
1080                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1081            !!!cp (20);            !!!cp (20);
1082            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1083                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1084                                        line => $self->{line_prev},
1085                                        column => $self->{column_prev}};
1086            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1087            !!!next-input-character;            !!!next-input-character;
1088            redo A;            redo A;
1089          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1090            !!!cp (21);            !!!cp (21);
1091            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1092                              line => $self->{line_prev},
1093                              column => $self->{column_prev});
1094            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1095            !!!next-input-character;            !!!next-input-character;
1096    
1097            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1098                        line => $self->{line_prev},
1099                        column => $self->{column_prev},
1100                       });
1101    
1102            redo A;            redo A;
1103          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1104            !!!cp (22);            !!!cp (22);
1105            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1106                              line => $self->{line_prev},
1107                              column => $self->{column_prev});
1108            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1109              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1110                                        line => $self->{line_prev},
1111                                        column => $self->{column_prev},
1112                                       };
1113            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1114            redo A;            redo A;
1115          } else {          } else {
1116            !!!cp (23);            !!!cp (23);
1117            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1118                              line => $self->{line_prev},
1119                              column => $self->{column_prev});
1120            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1121            ## reconsume            ## reconsume
1122    
1123            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1124                        line => $self->{line_prev},
1125                        column => $self->{column_prev},
1126                       });
1127    
1128            redo A;            redo A;
1129          }          }
# Line 545  sub _get_next_token ($) { Line 1131  sub _get_next_token ($) {
1131          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1132        }        }
1133      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1134          ## NOTE: The "close tag open state" in the spec is implemented as
1135          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1136    
1137          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1138        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1139          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1140            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1141            my @next_char;            $self->{state_keyword} = '';
1142            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            ## Reconsume.
1143              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...  
           }  
1144          } else {          } else {
1145            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1146              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1147            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1148            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1149            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1150              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1151                        line => $l, column => $c,
1152                       });
1153            redo A;            redo A;
1154          }          }
1155        }        }
1156          
1157        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1158            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1159          !!!cp (29);          !!!cp (29);
1160          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1161                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1162                   tag_name => chr ($self->{next_char} + 0x0020),
1163                   line => $l, column => $c};
1164          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1165          !!!next-input-character;          !!!next-input-character;
1166          redo A;          redo A;
# Line 612  sub _get_next_token ($) { Line 1168  sub _get_next_token ($) {
1168                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1169          !!!cp (30);          !!!cp (30);
1170          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1171                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1172                                      line => $l, column => $c};
1173          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1174          !!!next-input-character;          !!!next-input-character;
1175          redo A;          redo A;
1176        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1177          !!!cp (31);          !!!cp (31);
1178          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1179                            line => $self->{line_prev}, ## "<" in "</>"
1180                            column => $self->{column_prev} - 1);
1181          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1182          !!!next-input-character;          !!!next-input-character;
1183          redo A;          redo A;
# Line 628  sub _get_next_token ($) { Line 1187  sub _get_next_token ($) {
1187          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1188          # reconsume          # reconsume
1189    
1190          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1191                      line => $l, column => $c,
1192                     });
1193    
1194          redo A;          redo A;
1195        } else {        } else {
1196          !!!cp (33);          !!!cp (33);
1197          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1198          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1199          ## $self->{next_char} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1200          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1201                                      column => $self->{column_prev} - 1,
1202                                     };
1203            ## NOTE: $self->{next_char} is intentionally left as is.
1204            ## Although the "anything else" case of the spec not explicitly
1205            ## states that the next input character is to be reconsumed,
1206            ## it will be included to the |data| of the comment token
1207            ## generated from the bogus end tag, as defined in the
1208            ## "bogus comment state" entry.
1209            redo A;
1210          }
1211        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1212          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1213          if (length $ch) {
1214            my $CH = $ch;
1215            $ch =~ tr/a-z/A-Z/;
1216            my $nch = chr $self->{next_char};
1217            if ($nch eq $ch or $nch eq $CH) {
1218              !!!cp (24);
1219              ## Stay in the state.
1220              $self->{state_keyword} .= $nch;
1221              !!!next-input-character;
1222              redo A;
1223            } else {
1224              !!!cp (25);
1225              $self->{state} = DATA_STATE;
1226              ## Reconsume.
1227              !!!emit ({type => CHARACTER_TOKEN,
1228                        data => '</' . $self->{state_keyword},
1229                        line => $self->{line_prev},
1230                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1231                       });
1232              redo A;
1233            }
1234          } else { # after "<{tag-name}"
1235            unless ({
1236                     0x0009 => 1, # HT
1237                     0x000A => 1, # LF
1238                     0x000B => 1, # VT
1239                     0x000C => 1, # FF
1240                     0x0020 => 1, # SP
1241                     0x003E => 1, # >
1242                     0x002F => 1, # /
1243                     -1 => 1, # EOF
1244                    }->{$self->{next_char}}) {
1245              !!!cp (26);
1246              ## Reconsume.
1247              $self->{state} = DATA_STATE;
1248              !!!emit ({type => CHARACTER_TOKEN,
1249                        data => '</' . $self->{state_keyword},
1250                        line => $self->{line_prev},
1251                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1252                       });
1253              redo A;
1254            } else {
1255              !!!cp (27);
1256              $self->{current_token}
1257                  = {type => END_TAG_TOKEN,
1258                     tag_name => $self->{last_emitted_start_tag_name},
1259                     line => $self->{line_prev},
1260                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1261              $self->{state} = TAG_NAME_STATE;
1262              ## Reconsume.
1263              redo A;
1264            }
1265        }        }
1266      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1267        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 651  sub _get_next_token ($) { Line 1276  sub _get_next_token ($) {
1276        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1277          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1278            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1279            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1280          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1281            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 684  sub _get_next_token ($) { Line 1307  sub _get_next_token ($) {
1307          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1308          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1309            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1310            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1311          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1312            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 706  sub _get_next_token ($) { Line 1327  sub _get_next_token ($) {
1327    
1328          redo A;          redo A;
1329        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1330            !!!cp (42);
1331            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1332          !!!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  
1333          redo A;          redo A;
1334        } else {        } else {
1335          !!!cp (44);          !!!cp (44);
# Line 741  sub _get_next_token ($) { Line 1352  sub _get_next_token ($) {
1352        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1353          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1354            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1355            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1356          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1357            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 764  sub _get_next_token ($) { Line 1373  sub _get_next_token ($) {
1373        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1374                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1375          !!!cp (49);          !!!cp (49);
1376          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1377                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1378                   value => '',
1379                   line => $self->{line}, column => $self->{column}};
1380          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1381          !!!next-input-character;          !!!next-input-character;
1382          redo A;          redo A;
1383        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1384            !!!cp (50);
1385            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1386          !!!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  
1387          redo A;          redo A;
1388        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1389          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1390          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1391            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1392            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1393          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1394            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 819  sub _get_next_token ($) { Line 1418  sub _get_next_token ($) {
1418          } else {          } else {
1419            !!!cp (56);            !!!cp (56);
1420          }          }
1421          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1422                                value => ''};              = {name => chr ($self->{next_char}),
1423                   value => '',
1424                   line => $self->{line}, column => $self->{column}};
1425          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1426          !!!next-input-character;          !!!next-input-character;
1427          redo A;          redo A;
# Line 830  sub _get_next_token ($) { Line 1431  sub _get_next_token ($) {
1431          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1432              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1433            !!!cp (57);            !!!cp (57);
1434            !!!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});
1435            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1436          } else {          } else {
1437            !!!cp (58);            !!!cp (58);
# Line 859  sub _get_next_token ($) { Line 1460  sub _get_next_token ($) {
1460          $before_leave->();          $before_leave->();
1461          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1462            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1463            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1464          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1465            !!!cp (62);            !!!cp (62);
# Line 885  sub _get_next_token ($) { Line 1484  sub _get_next_token ($) {
1484          !!!next-input-character;          !!!next-input-character;
1485          redo A;          redo A;
1486        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1487            !!!cp (64);
1488          $before_leave->();          $before_leave->();
1489            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1490          !!!next-input-character;          !!!next-input-character;
         if ($self->{next_char} == 0x003E and # >  
             $self->{current_token}->{type} == START_TAG_TOKEN and  
             $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {  
           # permitted slash  
           !!!cp (64);  
           #  
         } else {  
           !!!cp (65);  
           !!!parse-error (type => 'nestc');  
         }  
         $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;  
         # next-input-character is already done  
1491          redo A;          redo A;
1492        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1493          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1494          $before_leave->();          $before_leave->();
1495          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1496            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1497            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1498          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1499            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 957  sub _get_next_token ($) { Line 1544  sub _get_next_token ($) {
1544        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1545          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1546            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1547            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1548          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1549            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 981  sub _get_next_token ($) { Line 1566  sub _get_next_token ($) {
1566        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1567                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1568          !!!cp (76);          !!!cp (76);
1569          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1570                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1571                   value => '',
1572                   line => $self->{line}, column => $self->{column}};
1573          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1574          !!!next-input-character;          !!!next-input-character;
1575          redo A;          redo A;
1576        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1577            !!!cp (77);
1578            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1579          !!!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  
1580          redo A;          redo A;
1581        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1582          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1583          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1584            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1585            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1586          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1587            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1028  sub _get_next_token ($) { Line 1602  sub _get_next_token ($) {
1602    
1603          redo A;          redo A;
1604        } else {        } else {
1605          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1606          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1607                                value => ''};            !!!cp (78);
1608              !!!parse-error (type => 'bad attribute name');
1609            } else {
1610              !!!cp (82);
1611            }
1612            $self->{current_attribute}
1613                = {name => chr ($self->{next_char}),
1614                   value => '',
1615                   line => $self->{line}, column => $self->{column}};
1616          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1617          !!!next-input-character;          !!!next-input-character;
1618          redo A;                  redo A;        
# Line 1061  sub _get_next_token ($) { Line 1643  sub _get_next_token ($) {
1643          !!!next-input-character;          !!!next-input-character;
1644          redo A;          redo A;
1645        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1646            !!!parse-error (type => 'empty unquoted attribute value');
1647          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1648            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1649            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1650          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1651            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1088  sub _get_next_token ($) { Line 1669  sub _get_next_token ($) {
1669          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1670          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1671            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1672            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1673          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1674            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1129  sub _get_next_token ($) { Line 1708  sub _get_next_token ($) {
1708          redo A;          redo A;
1709        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1710          !!!cp (96);          !!!cp (96);
1711          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1712          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1713            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1714            ## implementation of the "consume a character reference" algorithm.
1715            $self->{prev_state} = $self->{state};
1716            $self->{entity_additional} = 0x0022; # "
1717            $self->{state} = ENTITY_STATE;
1718          !!!next-input-character;          !!!next-input-character;
1719          redo A;          redo A;
1720        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1721          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1722          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1723            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1724            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1725          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1726            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1173  sub _get_next_token ($) { Line 1755  sub _get_next_token ($) {
1755          redo A;          redo A;
1756        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1757          !!!cp (102);          !!!cp (102);
1758          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1759          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1760            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1761            ## implementation of the "consume a character reference" algorithm.
1762            $self->{entity_additional} = 0x0027; # '
1763            $self->{prev_state} = $self->{state};
1764            $self->{state} = ENTITY_STATE;
1765          !!!next-input-character;          !!!next-input-character;
1766          redo A;          redo A;
1767        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1768          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1769          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1770            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1771            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1772          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1773            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1221  sub _get_next_token ($) { Line 1806  sub _get_next_token ($) {
1806          redo A;          redo A;
1807        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1808          !!!cp (108);          !!!cp (108);
1809          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1810          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1811            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1812            ## implementation of the "consume a character reference" algorithm.
1813            $self->{entity_additional} = -1;
1814            $self->{prev_state} = $self->{state};
1815            $self->{state} = ENTITY_STATE;
1816          !!!next-input-character;          !!!next-input-character;
1817          redo A;          redo A;
1818        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1819          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1820            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1821            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1822          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1823            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1253  sub _get_next_token ($) { Line 1841  sub _get_next_token ($) {
1841          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1842          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1843            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1844            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1845          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1846            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1290  sub _get_next_token ($) { Line 1876  sub _get_next_token ($) {
1876          !!!next-input-character;          !!!next-input-character;
1877          redo A;          redo A;
1878        }        }
     } 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;  
1879      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1880        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1881            $self->{next_char} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
# Line 1325  sub _get_next_token ($) { Line 1889  sub _get_next_token ($) {
1889        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1890          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1891            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1892            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1893          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1894            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1347  sub _get_next_token ($) { Line 1909  sub _get_next_token ($) {
1909    
1910          redo A;          redo A;
1911        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1912            !!!cp (122);
1913            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1914          !!!next-input-character;          !!!next-input-character;
1915          if ($self->{next_char} == 0x003E and # >          redo A;
1916              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1917              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1918            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1919            !!!cp (122);            !!!cp (122.3);
1920            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1921            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1922              if ($self->{current_token}->{attributes}) {
1923                !!!cp (122.1);
1924                !!!parse-error (type => 'end tag attribute');
1925              } else {
1926                ## NOTE: This state should never be reached.
1927                !!!cp (122.2);
1928              }
1929          } else {          } else {
1930            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1931          }          }
1932          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1933          # next-input-character is already done          ## Reconsume.
1934            !!!emit ($self->{current_token}); # start tag or end tag
1935          redo A;          redo A;
1936        } else {        } else {
1937          !!!cp (124);          !!!cp ('124.1');
1938          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1939          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1940          ## reconsume          ## reconsume
1941          redo A;          redo A;
1942        }        }
1943      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1944        ## (only happen if PCDATA state)        if ($self->{next_char} == 0x003E) { # >
1945                  if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1946        my $token = {type => COMMENT_TOKEN, data => ''};            !!!cp ('124.2');
1947              !!!parse-error (type => 'nestc', token => $self->{current_token});
1948        BC: {            ## TODO: Different type than slash in start tag
1949          if ($self->{next_char} == 0x003E) { # >            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1950            !!!cp (124);            if ($self->{current_token}->{attributes}) {
1951            $self->{state} = DATA_STATE;              !!!cp ('124.4');
1952            !!!next-input-character;              !!!parse-error (type => 'end tag attribute');
1953              } else {
1954            !!!emit ($token);              !!!cp ('124.5');
1955              }
1956              ## TODO: Test |<title></title/>|
1957            } else {
1958              !!!cp ('124.3');
1959              $self->{self_closing} = 1;
1960            }
1961    
1962            redo A;          $self->{state} = DATA_STATE;
1963          } elsif ($self->{next_char} == -1) {          !!!next-input-character;
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
1964    
1965            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
1966    
1967            redo A;          redo A;
1968          } elsif ($self->{next_char} == -1) {
1969            !!!parse-error (type => 'unclosed tag');
1970            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1971              !!!cp (124.7);
1972              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1973            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1974              if ($self->{current_token}->{attributes}) {
1975                !!!cp (124.5);
1976                !!!parse-error (type => 'end tag attribute');
1977              } else {
1978                ## NOTE: This state should never be reached.
1979                !!!cp (124.6);
1980              }
1981          } else {          } else {
1982            !!!cp (126);            die "$0: $self->{current_token}->{type}: Unknown token type";
           $token->{data} .= chr ($self->{next_char});  
           !!!next-input-character;  
           redo BC;  
1983          }          }
1984        } # BC          $self->{state} = DATA_STATE;
1985            ## Reconsume.
1986            !!!emit ($self->{current_token}); # start tag or end tag
1987            redo A;
1988          } else {
1989            !!!cp ('124.4');
1990            !!!parse-error (type => 'nestc');
1991            ## TODO: This error type is wrong.
1992            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1993            ## Reconsume.
1994            redo A;
1995          }
1996        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1997          ## (only happen if PCDATA state)
1998    
1999          ## NOTE: Unlike spec's "bogus comment state", this implementation
2000          ## consumes characters one-by-one basis.
2001          
2002          if ($self->{next_char} == 0x003E) { # >
2003            !!!cp (124);
2004            $self->{state} = DATA_STATE;
2005            !!!next-input-character;
2006    
2007        die "$0: _get_next_token: unexpected case [BC]";          !!!emit ($self->{current_token}); # comment
2008            redo A;
2009          } elsif ($self->{next_char} == -1) {
2010            !!!cp (125);
2011            $self->{state} = DATA_STATE;
2012            ## reconsume
2013    
2014            !!!emit ($self->{current_token}); # comment
2015            redo A;
2016          } else {
2017            !!!cp (126);
2018            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2019            ## Stay in the state.
2020            !!!next-input-character;
2021            redo A;
2022          }
2023      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2024        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my @next_char;  
       push @next_char, $self->{next_char};  
2025                
2026        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2027            !!!cp (133);
2028            $self->{state} = MD_HYPHEN_STATE;
2029          !!!next-input-character;          !!!next-input-character;
2030          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);  
         }  
2031        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2032                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2033            ## ASCII case-insensitive.
2034            !!!cp (130);
2035            $self->{state} = MD_DOCTYPE_STATE;
2036            $self->{state_keyword} = chr $self->{next_char};
2037          !!!next-input-character;          !!!next-input-character;
2038          push @next_char, $self->{next_char};          redo A;
2039          if ($self->{next_char} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2040              $self->{next_char} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2041            !!!next-input-character;                 $self->{next_char} == 0x005B) { # [
2042            push @next_char, $self->{next_char};          !!!cp (135.4);                
2043            if ($self->{next_char} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2044                $self->{next_char} == 0x0063) { # c          $self->{state_keyword} = '[';
2045              !!!next-input-character;          !!!next-input-character;
2046              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);  
         }  
2047        } else {        } else {
2048          !!!cp (136);          !!!cp (136);
2049        }        }
2050    
2051        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2052        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2053        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2054          ## Reconsume.
2055        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2056          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2057                                    line => $self->{line_prev},
2058                                    column => $self->{column_prev} - 1,
2059                                   };
2060        redo A;        redo A;
2061              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2062        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2063        ## 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);
2064            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2065                                      line => $self->{line_prev},
2066                                      column => $self->{column_prev} - 2,
2067                                     };
2068            $self->{state} = COMMENT_START_STATE;
2069            !!!next-input-character;
2070            redo A;
2071          } else {
2072            !!!cp (128);
2073            !!!parse-error (type => 'bogus comment',
2074                            line => $self->{line_prev},
2075                            column => $self->{column_prev} - 2);
2076            $self->{state} = BOGUS_COMMENT_STATE;
2077            ## Reconsume.
2078            $self->{current_token} = {type => COMMENT_TOKEN,
2079                                      data => '-',
2080                                      line => $self->{line_prev},
2081                                      column => $self->{column_prev} - 2,
2082                                     };
2083            redo A;
2084          }
2085        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2086          ## ASCII case-insensitive.
2087          if ($self->{next_char} == [
2088                undef,
2089                0x004F, # O
2090                0x0043, # C
2091                0x0054, # T
2092                0x0059, # Y
2093                0x0050, # P
2094              ]->[length $self->{state_keyword}] or
2095              $self->{next_char} == [
2096                undef,
2097                0x006F, # o
2098                0x0063, # c
2099                0x0074, # t
2100                0x0079, # y
2101                0x0070, # p
2102              ]->[length $self->{state_keyword}]) {
2103            !!!cp (131);
2104            ## Stay in the state.
2105            $self->{state_keyword} .= chr $self->{next_char};
2106            !!!next-input-character;
2107            redo A;
2108          } elsif ((length $self->{state_keyword}) == 6 and
2109                   ($self->{next_char} == 0x0045 or # E
2110                    $self->{next_char} == 0x0065)) { # e
2111            !!!cp (129);
2112            $self->{state} = DOCTYPE_STATE;
2113            $self->{current_token} = {type => DOCTYPE_TOKEN,
2114                                      quirks => 1,
2115                                      line => $self->{line_prev},
2116                                      column => $self->{column_prev} - 7,
2117                                     };
2118            !!!next-input-character;
2119            redo A;
2120          } else {
2121            !!!cp (132);        
2122            !!!parse-error (type => 'bogus comment',
2123                            line => $self->{line_prev},
2124                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2125            $self->{state} = BOGUS_COMMENT_STATE;
2126            ## Reconsume.
2127            $self->{current_token} = {type => COMMENT_TOKEN,
2128                                      data => $self->{state_keyword},
2129                                      line => $self->{line_prev},
2130                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2131                                     };
2132            redo A;
2133          }
2134        } elsif ($self->{state} == MD_CDATA_STATE) {
2135          if ($self->{next_char} == {
2136                '[' => 0x0043, # C
2137                '[C' => 0x0044, # D
2138                '[CD' => 0x0041, # A
2139                '[CDA' => 0x0054, # T
2140                '[CDAT' => 0x0041, # A
2141              }->{$self->{state_keyword}}) {
2142            !!!cp (135.1);
2143            ## Stay in the state.
2144            $self->{state_keyword} .= chr $self->{next_char};
2145            !!!next-input-character;
2146            redo A;
2147          } elsif ($self->{state_keyword} eq '[CDATA' and
2148                   $self->{next_char} == 0x005B) { # [
2149            !!!cp (135.2);
2150            $self->{current_token} = {type => CHARACTER_TOKEN,
2151                                      data => '',
2152                                      line => $self->{line_prev},
2153                                      column => $self->{column_prev} - 7};
2154            $self->{state} = CDATA_SECTION_STATE;
2155            !!!next-input-character;
2156            redo A;
2157          } else {
2158            !!!cp (135.3);
2159            !!!parse-error (type => 'bogus comment',
2160                            line => $self->{line_prev},
2161                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2162            $self->{state} = BOGUS_COMMENT_STATE;
2163            ## Reconsume.
2164            $self->{current_token} = {type => COMMENT_TOKEN,
2165                                      data => $self->{state_keyword},
2166                                      line => $self->{line_prev},
2167                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2168                                     };
2169            redo A;
2170          }
2171      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2172        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2173          !!!cp (137);          !!!cp (137);
# Line 1597  sub _get_next_token ($) { Line 2287  sub _get_next_token ($) {
2287          redo A;          redo A;
2288        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2289          !!!cp (152);          !!!cp (152);
2290          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2291                            line => $self->{line_prev},
2292                            column => $self->{column_prev});
2293          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2294          ## Stay in the state          ## Stay in the state
2295          !!!next-input-character;          !!!next-input-character;
# Line 1613  sub _get_next_token ($) { Line 2305  sub _get_next_token ($) {
2305          redo A;          redo A;
2306        } else {        } else {
2307          !!!cp (154);          !!!cp (154);
2308          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2309                            line => $self->{line_prev},
2310                            column => $self->{column_prev});
2311          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2312          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2313          !!!next-input-character;          !!!next-input-character;
# Line 1652  sub _get_next_token ($) { Line 2346  sub _get_next_token ($) {
2346          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2347          !!!next-input-character;          !!!next-input-character;
2348    
2349          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2350    
2351          redo A;          redo A;
2352        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1661  sub _get_next_token ($) { Line 2355  sub _get_next_token ($) {
2355          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2356          ## reconsume          ## reconsume
2357    
2358          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2359    
2360          redo A;          redo A;
2361        } else {        } else {
2362          !!!cp (160);          !!!cp (160);
2363          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2364              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2365  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2366          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2367          !!!next-input-character;          !!!next-input-character;
# Line 1743  sub _get_next_token ($) { Line 2434  sub _get_next_token ($) {
2434          redo A;          redo A;
2435        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2436                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2437            $self->{state} = PUBLIC_STATE;
2438            $self->{state_keyword} = chr $self->{next_char};
2439          !!!next-input-character;          !!!next-input-character;
2440          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);  
         }  
   
         #  
2441        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2442                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2443            $self->{state} = SYSTEM_STATE;
2444            $self->{state_keyword} = chr $self->{next_char};
2445          !!!next-input-character;          !!!next-input-character;
2446          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);  
         }  
   
         #  
2447        } else {        } else {
2448          !!!cp (180);          !!!cp (180);
2449            !!!parse-error (type => 'string after DOCTYPE name');
2450            $self->{current_token}->{quirks} = 1;
2451    
2452            $self->{state} = BOGUS_DOCTYPE_STATE;
2453          !!!next-input-character;          !!!next-input-character;
2454          #          redo A;
2455        }        }
2456        } elsif ($self->{state} == PUBLIC_STATE) {
2457          ## ASCII case-insensitive
2458          if ($self->{next_char} == [
2459                undef,
2460                0x0055, # U
2461                0x0042, # B
2462                0x004C, # L
2463                0x0049, # I
2464              ]->[length $self->{state_keyword}] or
2465              $self->{next_char} == [
2466                undef,
2467                0x0075, # u
2468                0x0062, # b
2469                0x006C, # l
2470                0x0069, # i
2471              ]->[length $self->{state_keyword}]) {
2472            !!!cp (175);
2473            ## Stay in the state.
2474            $self->{state_keyword} .= chr $self->{next_char};
2475            !!!next-input-character;
2476            redo A;
2477          } elsif ((length $self->{state_keyword}) == 5 and
2478                   ($self->{next_char} == 0x0043 or # C
2479                    $self->{next_char} == 0x0063)) { # c
2480            !!!cp (168);
2481            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2482            !!!next-input-character;
2483            redo A;
2484          } else {
2485            !!!cp (169);
2486            !!!parse-error (type => 'string after DOCTYPE name',
2487                            line => $self->{line_prev},
2488                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2489            $self->{current_token}->{quirks} = 1;
2490    
2491        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2492        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2493            redo A;
2494          }
2495        } elsif ($self->{state} == SYSTEM_STATE) {
2496          ## ASCII case-insensitive
2497          if ($self->{next_char} == [
2498                undef,
2499                0x0059, # Y
2500                0x0053, # S
2501                0x0054, # T
2502                0x0045, # E
2503              ]->[length $self->{state_keyword}] or
2504              $self->{next_char} == [
2505                undef,
2506                0x0079, # y
2507                0x0073, # s
2508                0x0074, # t
2509                0x0065, # e
2510              ]->[length $self->{state_keyword}]) {
2511            !!!cp (170);
2512            ## Stay in the state.
2513            $self->{state_keyword} .= chr $self->{next_char};
2514            !!!next-input-character;
2515            redo A;
2516          } elsif ((length $self->{state_keyword}) == 5 and
2517                   ($self->{next_char} == 0x004D or # M
2518                    $self->{next_char} == 0x006D)) { # m
2519            !!!cp (171);
2520            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2521            !!!next-input-character;
2522            redo A;
2523          } else {
2524            !!!cp (172);
2525            !!!parse-error (type => 'string after DOCTYPE name',
2526                            line => $self->{line_prev},
2527                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2528            $self->{current_token}->{quirks} = 1;
2529    
2530        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2531        # next-input-character is already done          ## Reconsume.
2532        redo A;          redo A;
2533          }
2534      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2535        if ({        if ({
2536              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 2061  sub _get_next_token ($) { Line 2763  sub _get_next_token ($) {
2763          redo A;          redo A;
2764        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2765          !!!cp (208);          !!!cp (208);
2766          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2767    
2768          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2769          !!!next-input-character;          !!!next-input-character;
# Line 2097  sub _get_next_token ($) { Line 2799  sub _get_next_token ($) {
2799          redo A;          redo A;
2800        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2801          !!!cp (212);          !!!cp (212);
2802          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2803    
2804          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2805          !!!next-input-character;          !!!next-input-character;
# Line 2145  sub _get_next_token ($) { Line 2847  sub _get_next_token ($) {
2847        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2848          !!!cp (217);          !!!cp (217);
2849          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2850          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2851          ## reconsume          ## reconsume
2852    
# Line 2186  sub _get_next_token ($) { Line 2887  sub _get_next_token ($) {
2887          !!!next-input-character;          !!!next-input-character;
2888          redo A;          redo A;
2889        }        }
2890      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2891        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2892      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2893    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2894          
2895    die "$0: _get_next_token: unexpected case";        if ($self->{next_char} == 0x005D) { # ]
2896  } # _get_next_token          !!!cp (221.1);
2897            $self->{state} = CDATA_SECTION_MSE1_STATE;
2898  sub _tokenize_attempt_to_consume_an_entity ($$$) {          !!!next-input-character;
2899    my ($self, $in_attr, $additional) = @_;          redo A;
2900          } elsif ($self->{next_char} == -1) {
2901            $self->{state} = DATA_STATE;
2902            !!!next-input-character;
2903            if (length $self->{current_token}->{data}) { # character
2904              !!!cp (221.2);
2905              !!!emit ($self->{current_token}); # character
2906            } else {
2907              !!!cp (221.3);
2908              ## No token to emit. $self->{current_token} is discarded.
2909            }        
2910            redo A;
2911          } else {
2912            !!!cp (221.4);
2913            $self->{current_token}->{data} .= chr $self->{next_char};
2914            ## Stay in the state.
2915            !!!next-input-character;
2916            redo A;
2917          }
2918    
2919    if ({        ## ISSUE: "text tokens" in spec.
2920         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2921         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{next_char} == 0x005D) { # ]
2922         $additional => 1,          !!!cp (221.5);
2923        }->{$self->{next_char}}) {          $self->{state} = CDATA_SECTION_MSE2_STATE;
2924      !!!cp (1001);          !!!next-input-character;
2925      ## Don't consume          redo A;
2926      ## No error        } else {
2927      return undef;          !!!cp (221.6);
2928    } elsif ($self->{next_char} == 0x0023) { # #          $self->{current_token}->{data} .= ']';
2929      !!!next-input-character;          $self->{state} = CDATA_SECTION_STATE;
2930      if ($self->{next_char} == 0x0078 or # x          ## Reconsume.
2931          $self->{next_char} == 0x0058) { # X          redo A;
2932        my $code;        }
2933        X: {      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2934          my $x_char = $self->{next_char};        if ($self->{next_char} == 0x003E) { # >
2935          !!!next-input-character;          $self->{state} = DATA_STATE;
2936          if (0x0030 <= $self->{next_char} and          !!!next-input-character;
2937              $self->{next_char} <= 0x0039) { # 0..9          if (length $self->{current_token}->{data}) { # character
2938            !!!cp (1002);            !!!cp (221.7);
2939            $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;  
2940          } else {          } else {
2941            !!!cp (1007);            !!!cp (221.8);
2942            !!!parse-error (type => 'no refc');            ## No token to emit. $self->{current_token} is discarded.
2943          }          }
2944            redo A;
2945          } elsif ($self->{next_char} == 0x005D) { # ]
2946            !!!cp (221.9); # character
2947            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2948            ## Stay in the state.
2949            !!!next-input-character;
2950            redo A;
2951          } else {
2952            !!!cp (221.11);
2953            $self->{current_token}->{data} .= ']]'; # character
2954            $self->{state} = CDATA_SECTION_STATE;
2955            ## Reconsume.
2956            redo A;
2957          }
2958        } elsif ($self->{state} == ENTITY_STATE) {
2959          if ({
2960            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2961            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
2962            $self->{entity_additional} => 1,
2963          }->{$self->{next_char}}) {
2964            !!!cp (1001);
2965            ## Don't consume
2966            ## No error
2967            ## Return nothing.
2968            #
2969          } elsif ($self->{next_char} == 0x0023) { # #
2970            !!!cp (999);
2971            $self->{state} = ENTITY_HASH_STATE;
2972            $self->{state_keyword} = '#';
2973            !!!next-input-character;
2974            redo A;
2975          } elsif ((0x0041 <= $self->{next_char} and
2976                    $self->{next_char} <= 0x005A) or # A..Z
2977                   (0x0061 <= $self->{next_char} and
2978                    $self->{next_char} <= 0x007A)) { # a..z
2979            !!!cp (998);
2980            require Whatpm::_NamedEntityList;
2981            $self->{state} = ENTITY_NAME_STATE;
2982            $self->{state_keyword} = chr $self->{next_char};
2983            $self->{entity__value} = $self->{state_keyword};
2984            $self->{entity__match} = 0;
2985            !!!next-input-character;
2986            redo A;
2987          } else {
2988            !!!cp (1027);
2989            !!!parse-error (type => 'bare ero');
2990            ## Return nothing.
2991            #
2992          }
2993    
2994          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
2995            !!!cp (1008);        ## reference" algorithm.  In other word, there is an "&" character
2996            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## that does not introduce a character reference, which would be
2997            $code = 0xFFFD;        ## appended to the parent element or the attribute value in later
2998          } elsif ($code > 0x10FFFF) {        ## process of the tokenizer.
2999            !!!cp (1009);  
3000            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        if ($self->{prev_state} == DATA_STATE) {
3001            $code = 0xFFFD;          !!!cp (997);
3002          } elsif ($code == 0x000D) {          $self->{state} = $self->{prev_state};
3003            !!!cp (1010);          ## Reconsume.
3004            !!!parse-error (type => 'CR character reference');          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3005            $code = 0x000A;                    line => $self->{line_prev},
3006          } elsif (0x80 <= $code and $code <= 0x9F) {                    column => $self->{column_prev},
3007            !!!cp (1011);                   });
3008            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          redo A;
3009            $code = $c1_entity_char->{$code};        } else {
3010          }          !!!cp (996);
3011            $self->{current_attribute}->{value} .= '&';
3012          return {type => CHARACTER_TOKEN, data => chr $code,          $self->{state} = $self->{prev_state};
3013                  has_reference => 1};          ## Reconsume.
3014        } # X          redo A;
3015      } elsif (0x0030 <= $self->{next_char} and        }
3016               $self->{next_char} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3017        my $code = $self->{next_char} - 0x0030;        if ($self->{next_char} == 0x0078 or # x
3018        !!!next-input-character;            $self->{next_char} == 0x0058) { # X
3019                  !!!cp (995);
3020        while (0x0030 <= $self->{next_char} and          $self->{state} = HEXREF_X_STATE;
3021                  $self->{next_char} <= 0x0039) { # 0..9          $self->{state_keyword} .= chr $self->{next_char};
3022            !!!next-input-character;
3023            redo A;
3024          } elsif (0x0030 <= $self->{next_char} and
3025                   $self->{next_char} <= 0x0039) { # 0..9
3026            !!!cp (994);
3027            $self->{state} = NCR_NUM_STATE;
3028            $self->{state_keyword} = $self->{next_char} - 0x0030;
3029            !!!next-input-character;
3030            redo A;
3031          } else {
3032            !!!parse-error (type => 'bare nero',
3033                            line => $self->{line_prev},
3034                            column => $self->{column_prev} - 1);
3035    
3036            ## NOTE: According to the spec algorithm, nothing is returned,
3037            ## and then "&#" is appended to the parent element or the attribute
3038            ## value in the later processing.
3039    
3040            if ($self->{prev_state} == DATA_STATE) {
3041              !!!cp (1019);
3042              $self->{state} = $self->{prev_state};
3043              ## Reconsume.
3044              !!!emit ({type => CHARACTER_TOKEN,
3045                        data => '&#',
3046                        line => $self->{line_prev},
3047                        column => $self->{column_prev} - 1,
3048                       });
3049              redo A;
3050            } else {
3051              !!!cp (993);
3052              $self->{current_attribute}->{value} .= '&#';
3053              $self->{state} = $self->{prev_state};
3054              ## Reconsume.
3055              redo A;
3056            }
3057          }
3058        } elsif ($self->{state} == NCR_NUM_STATE) {
3059          if (0x0030 <= $self->{next_char} and
3060              $self->{next_char} <= 0x0039) { # 0..9
3061          !!!cp (1012);          !!!cp (1012);
3062          $code *= 10;          $self->{state_keyword} *= 10;
3063          $code += $self->{next_char} - 0x0030;          $self->{state_keyword} += $self->{next_char} - 0x0030;
3064                    
3065            ## Stay in the state.
3066          !!!next-input-character;          !!!next-input-character;
3067        }          redo A;
3068          } elsif ($self->{next_char} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3069          !!!cp (1013);          !!!cp (1013);
3070          !!!next-input-character;          !!!next-input-character;
3071            #
3072        } else {        } else {
3073          !!!cp (1014);          !!!cp (1014);
3074          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3075            ## Reconsume.
3076            #
3077        }        }
3078    
3079          my $code = $self->{state_keyword};
3080          my $l = $self->{line_prev};
3081          my $c = $self->{column_prev};
3082        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3083          !!!cp (1015);          !!!cp (1015);
3084          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
3085                            text => (sprintf 'U+%04X', $code),
3086                            line => $l, column => $c);
3087          $code = 0xFFFD;          $code = 0xFFFD;
3088        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3089          !!!cp (1016);          !!!cp (1016);
3090          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
3091                            text => (sprintf 'U-%08X', $code),
3092                            line => $l, column => $c);
3093          $code = 0xFFFD;          $code = 0xFFFD;
3094        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3095          !!!cp (1017);          !!!cp (1017);
3096          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
3097                            line => $l, column => $c);
3098          $code = 0x000A;          $code = 0x000A;
3099        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3100          !!!cp (1018);          !!!cp (1018);
3101          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3102                            text => (sprintf 'U+%04X', $code),
3103                            line => $l, column => $c);
3104          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3105        }        }
3106          
3107        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        if ($self->{prev_state} == DATA_STATE) {
3108      } else {          !!!cp (992);
3109        !!!cp (1019);          $self->{state} = $self->{prev_state};
3110        !!!parse-error (type => 'bare nero');          ## Reconsume.
3111        !!!back-next-input-character ($self->{next_char});          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3112        $self->{next_char} = 0x0023; # #                    line => $l, column => $c,
3113        return undef;                   });
3114      }          redo A;
3115    } elsif ((0x0041 <= $self->{next_char} and        } else {
3116              $self->{next_char} <= 0x005A) or          !!!cp (991);
3117             (0x0061 <= $self->{next_char} and          $self->{current_attribute}->{value} .= chr $code;
3118              $self->{next_char} <= 0x007A)) {          $self->{current_attribute}->{has_reference} = 1;
3119      my $entity_name = chr $self->{next_char};          $self->{state} = $self->{prev_state};
3120      !!!next-input-character;          ## Reconsume.
3121            redo A;
3122      my $value = $entity_name;        }
3123      my $match = 0;      } elsif ($self->{state} == HEXREF_X_STATE) {
3124      require Whatpm::_NamedEntityList;        if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3125      our $EntityChar;            (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3126              (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3127      while (length $entity_name < 10 and          # 0..9, A..F, a..f
3128             ## NOTE: Some number greater than the maximum length of entity name          !!!cp (990);
3129             ((0x0041 <= $self->{next_char} and # a          $self->{state} = HEXREF_HEX_STATE;
3130               $self->{next_char} <= 0x005A) or # x          $self->{state_keyword} = 0;
3131              (0x0061 <= $self->{next_char} and # a          ## Reconsume.
3132               $self->{next_char} <= 0x007A) or # z          redo A;
3133              (0x0030 <= $self->{next_char} and # 0        } else {
3134               $self->{next_char} <= 0x0039) or # 9          !!!parse-error (type => 'bare hcro',
3135              $self->{next_char} == 0x003B)) { # ;                          line => $self->{line_prev},
3136        $entity_name .= chr $self->{next_char};                          column => $self->{column_prev} - 2);
3137        if (defined $EntityChar->{$entity_name}) {  
3138          if ($self->{next_char} == 0x003B) { # ;          ## NOTE: According to the spec algorithm, nothing is returned,
3139            !!!cp (1020);          ## and then "&#" followed by "X" or "x" is appended to the parent
3140            $value = $EntityChar->{$entity_name};          ## element or the attribute value in the later processing.
3141            $match = 1;  
3142            !!!next-input-character;          if ($self->{prev_state} == DATA_STATE) {
3143            last;            !!!cp (1005);
3144              $self->{state} = $self->{prev_state};
3145              ## Reconsume.
3146              !!!emit ({type => CHARACTER_TOKEN,
3147                        data => '&' . $self->{state_keyword},
3148                        line => $self->{line_prev},
3149                        column => $self->{column_prev} - length $self->{state_keyword},
3150                       });
3151              redo A;
3152          } else {          } else {
3153            !!!cp (1021);            !!!cp (989);
3154            $value = $EntityChar->{$entity_name};            $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3155            $match = -1;            $self->{state} = $self->{prev_state};
3156            !!!next-input-character;            ## Reconsume.
3157              redo A;
3158          }          }
3159        } else {        }
3160          !!!cp (1022);      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3161          $value .= chr $self->{next_char};        if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3162          $match *= 2;          # 0..9
3163            !!!cp (1002);
3164            $self->{state_keyword} *= 0x10;
3165            $self->{state_keyword} += $self->{next_char} - 0x0030;
3166            ## Stay in the state.
3167          !!!next-input-character;          !!!next-input-character;
3168            redo A;
3169          } elsif (0x0061 <= $self->{next_char} and
3170                   $self->{next_char} <= 0x0066) { # a..f
3171            !!!cp (1003);
3172            $self->{state_keyword} *= 0x10;
3173            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3174            ## Stay in the state.
3175            !!!next-input-character;
3176            redo A;
3177          } elsif (0x0041 <= $self->{next_char} and
3178                   $self->{next_char} <= 0x0046) { # A..F
3179            !!!cp (1004);
3180            $self->{state_keyword} *= 0x10;
3181            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3182            ## Stay in the state.
3183            !!!next-input-character;
3184            redo A;
3185          } elsif ($self->{next_char} == 0x003B) { # ;
3186            !!!cp (1006);
3187            !!!next-input-character;
3188            #
3189          } else {
3190            !!!cp (1007);
3191            !!!parse-error (type => 'no refc',
3192                            line => $self->{line},
3193                            column => $self->{column});
3194            ## Reconsume.
3195            #
3196        }        }
3197      }  
3198              my $code = $self->{state_keyword};
3199      if ($match > 0) {        my $l = $self->{line_prev};
3200        !!!cp (1023);        my $c = $self->{column_prev};
3201        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3202      } elsif ($match < 0) {          !!!cp (1008);
3203        !!!parse-error (type => 'no refc');          !!!parse-error (type => 'invalid character reference',
3204        if ($in_attr and $match < -1) {                          text => (sprintf 'U+%04X', $code),
3205          !!!cp (1024);                          line => $l, column => $c);
3206          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          $code = 0xFFFD;
3207          } elsif ($code > 0x10FFFF) {
3208            !!!cp (1009);
3209            !!!parse-error (type => 'invalid character reference',
3210                            text => (sprintf 'U-%08X', $code),
3211                            line => $l, column => $c);
3212            $code = 0xFFFD;
3213          } elsif ($code == 0x000D) {
3214            !!!cp (1010);
3215            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3216            $code = 0x000A;
3217          } elsif (0x80 <= $code and $code <= 0x9F) {
3218            !!!cp (1011);
3219            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3220            $code = $c1_entity_char->{$code};
3221          }
3222    
3223          if ($self->{prev_state} == DATA_STATE) {
3224            !!!cp (988);
3225            $self->{state} = $self->{prev_state};
3226            ## Reconsume.
3227            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3228                      line => $l, column => $c,
3229                     });
3230            redo A;
3231          } else {
3232            !!!cp (987);
3233            $self->{current_attribute}->{value} .= chr $code;
3234            $self->{current_attribute}->{has_reference} = 1;
3235            $self->{state} = $self->{prev_state};
3236            ## Reconsume.
3237            redo A;
3238          }
3239        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3240          if (length $self->{state_keyword} < 30 and
3241              ## NOTE: Some number greater than the maximum length of entity name
3242              ((0x0041 <= $self->{next_char} and # a
3243                $self->{next_char} <= 0x005A) or # x
3244               (0x0061 <= $self->{next_char} and # a
3245                $self->{next_char} <= 0x007A) or # z
3246               (0x0030 <= $self->{next_char} and # 0
3247                $self->{next_char} <= 0x0039) or # 9
3248               $self->{next_char} == 0x003B)) { # ;
3249            our $EntityChar;
3250            $self->{state_keyword} .= chr $self->{next_char};
3251            if (defined $EntityChar->{$self->{state_keyword}}) {
3252              if ($self->{next_char} == 0x003B) { # ;
3253                !!!cp (1020);
3254                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3255                $self->{entity__match} = 1;
3256                !!!next-input-character;
3257                #
3258              } else {
3259                !!!cp (1021);
3260                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3261                $self->{entity__match} = -1;
3262                ## Stay in the state.
3263                !!!next-input-character;
3264                redo A;
3265              }
3266            } else {
3267              !!!cp (1022);
3268              $self->{entity__value} .= chr $self->{next_char};
3269              $self->{entity__match} *= 2;
3270              ## Stay in the state.
3271              !!!next-input-character;
3272              redo A;
3273            }
3274          }
3275    
3276          my $data;
3277          my $has_ref;
3278          if ($self->{entity__match} > 0) {
3279            !!!cp (1023);
3280            $data = $self->{entity__value};
3281            $has_ref = 1;
3282            #
3283          } elsif ($self->{entity__match} < 0) {
3284            !!!parse-error (type => 'no refc');
3285            if ($self->{prev_state} != DATA_STATE and # in attribute
3286                $self->{entity__match} < -1) {
3287              !!!cp (1024);
3288              $data = '&' . $self->{state_keyword};
3289              #
3290            } else {
3291              !!!cp (1025);
3292              $data = $self->{entity__value};
3293              $has_ref = 1;
3294              #
3295            }
3296        } else {        } else {
3297          !!!cp (1025);          !!!cp (1026);
3298          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          !!!parse-error (type => 'bare ero',
3299                            line => $self->{line_prev},
3300                            column => $self->{column_prev});
3301            $data = '&' . $self->{state_keyword};
3302            #
3303          }
3304      
3305          ## NOTE: In these cases, when a character reference is found,
3306          ## it is consumed and a character token is returned, or, otherwise,
3307          ## nothing is consumed and returned, according to the spec algorithm.
3308          ## In this implementation, anything that has been examined by the
3309          ## tokenizer is appended to the parent element or the attribute value
3310          ## as string, either literal string when no character reference or
3311          ## entity-replaced string otherwise, in this stage, since any characters
3312          ## that would not be consumed are appended in the data state or in an
3313          ## appropriate attribute value state anyway.
3314    
3315          if ($self->{prev_state} == DATA_STATE) {
3316            !!!cp (986);
3317            $self->{state} = $self->{prev_state};
3318            ## Reconsume.
3319            !!!emit ({type => CHARACTER_TOKEN,
3320                      data => $data,
3321                      line => $self->{line_prev},
3322                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3323                     });
3324            redo A;
3325          } else {
3326            !!!cp (985);
3327            $self->{current_attribute}->{value} .= $data;
3328            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3329            $self->{state} = $self->{prev_state};
3330            ## Reconsume.
3331            redo A;
3332        }        }
3333      } else {      } else {
3334        !!!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};  
3335      }      }
3336    } else {    } # A  
3337      !!!cp (1027);  
3338      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3339      !!!parse-error (type => 'bare ero');  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3340    
3341  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3342    my $self = shift;    my $self = shift;
# Line 2394  sub _initialize_tree_constructor ($) { Line 3345  sub _initialize_tree_constructor ($) {
3345    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3346    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3347    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3348      $self->{document}->set_user_data (manakai_source_line => 1);
3349      $self->{document}->set_user_data (manakai_source_column => 1);
3350  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3351    
3352  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2420  sub _construct_tree ($) { Line 3373  sub _construct_tree ($) {
3373        
3374    !!!next-token;    !!!next-token;
3375    
   $self->{insertion_mode} = BEFORE_HEAD_IM;  
3376    undef $self->{form_element};    undef $self->{form_element};
3377    undef $self->{head_element};    undef $self->{head_element};
3378    $self->{open_elements} = [];    $self->{open_elements} = [];
3379    undef $self->{inner_html_node};    undef $self->{inner_html_node};
3380    
3381      ## NOTE: The "initial" insertion mode.
3382    $self->_tree_construction_initial; # MUST    $self->_tree_construction_initial; # MUST
3383    
3384      ## NOTE: The "before html" insertion mode.
3385    $self->_tree_construction_root_element;    $self->_tree_construction_root_element;
3386      $self->{insertion_mode} = BEFORE_HEAD_IM;
3387    
3388      ## NOTE: The "before head" insertion mode and so on.
3389    $self->_tree_construction_main;    $self->_tree_construction_main;
3390  } # _construct_tree  } # _construct_tree
3391    
3392  sub _tree_construction_initial ($) {  sub _tree_construction_initial ($) {
3393    my $self = shift;    my $self = shift;
3394    
3395      ## NOTE: "initial" insertion mode
3396    
3397    INITIAL: {    INITIAL: {
3398      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
3399        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"        ## NOTE: Conformance checkers MAY, instead of reporting "not HTML5"
# Line 2440  sub _tree_construction_initial ($) { Line 3401  sub _tree_construction_initial ($) {
3401        ## language.        ## language.
3402        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3403        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3404        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3405        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3406            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3407          !!!cp ('t1');          !!!cp ('t1');
3408          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3409        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3410          !!!cp ('t2');          !!!cp ('t2');
3411          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!parse-error (type => 'not HTML5', token => $token);
3412          !!!parse-error (type => 'not HTML5');        } elsif (defined $token->{public_identifier}) {
3413            if ($token->{public_identifier} eq 'XSLT-compat') {
3414              !!!cp ('t1.2');
3415              !!!parse-error (type => 'XSLT-compat', token => $token,
3416                              level => $self->{level}->{should});
3417            } else {
3418              !!!parse-error (type => 'not HTML5', token => $token);
3419            }
3420        } else {        } else {
3421          !!!cp ('t3');          !!!cp ('t3');
3422            #
3423        }        }
3424                
3425        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3426          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3427          ## NOTE: Default value for both |public_id| and |system_id| attributes
3428          ## are empty strings, so that we don't set any value in missing cases.
3429        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3430            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3431        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2470  sub _tree_construction_initial ($) { Line 3440  sub _tree_construction_initial ($) {
3440        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3441          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3442          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3443          if ({          my $prefix = [
3444            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3445            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3446            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3447            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3448            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3449            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3450            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3451            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3452            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3453            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3454            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3455            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3456            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3457            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3458            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3459            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3460            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3461            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3462            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3463            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3464            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3465            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3466            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3467            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3468            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3469            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3470            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3471            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3472            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3473            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3474            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3475            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3476            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3477            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3478            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3479            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3480            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3481            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3482            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3483            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3484            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3485            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3486            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3487            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3488            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3489            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3490            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3491            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3492            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3493            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3494            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3495            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3496            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3497            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3498            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3499            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3500            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3501            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3502            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3503            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3504            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3505            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3506            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3507            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3508            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3509            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3510            "-//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}) {  
3511            !!!cp ('t5');            !!!cp ('t5');
3512            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3513          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3514                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3515            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3516              !!!cp ('t6');              !!!cp ('t6');
3517              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2555  sub _tree_construction_initial ($) { Line 3519  sub _tree_construction_initial ($) {
3519              !!!cp ('t7');              !!!cp ('t7');
3520              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3521            }            }
3522          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3523                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3524            !!!cp ('t8');            !!!cp ('t8');
3525            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3526          } else {          } else {
# Line 2569  sub _tree_construction_initial ($) { Line 3533  sub _tree_construction_initial ($) {
3533          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3534          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3535          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") {
3536            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3537              ## marked as quirks.
3538            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3539            !!!cp ('t11');            !!!cp ('t11');
3540          } else {          } else {
# Line 2579  sub _tree_construction_initial ($) { Line 3544  sub _tree_construction_initial ($) {
3544          !!!cp ('t13');          !!!cp ('t13');
3545        }        }
3546                
3547        ## Go to the root element phase.        ## Go to the "before html" insertion mode.
3548        !!!next-token;        !!!next-token;
3549        return;        return;
3550      } elsif ({      } elsif ({
# Line 2588  sub _tree_construction_initial ($) { Line 3553  sub _tree_construction_initial ($) {
3553                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3554               }->{$token->{type}}) {               }->{$token->{type}}) {
3555        !!!cp ('t14');        !!!cp ('t14');
3556        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3557        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3558        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3559        ## reprocess        ## reprocess
3560          !!!ack-later;
3561        return;        return;
3562      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3563        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2599  sub _tree_construction_initial ($) { Line 3565  sub _tree_construction_initial ($) {
3565    
3566          unless (length $token->{data}) {          unless (length $token->{data}) {
3567            !!!cp ('t15');            !!!cp ('t15');
3568            ## Stay in the phase            ## Stay in the insertion mode.
3569            !!!next-token;            !!!next-token;
3570            redo INITIAL;            redo INITIAL;
3571          } else {          } else {
# Line 2609  sub _tree_construction_initial ($) { Line 3575  sub _tree_construction_initial ($) {
3575          !!!cp ('t17');          !!!cp ('t17');
3576        }        }
3577    
3578        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3579        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3580        ## Go to the root element phase        ## Go to the "before html" insertion mode.
3581        ## reprocess        ## reprocess
3582        return;        return;
3583      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
# Line 2619  sub _tree_construction_initial ($) { Line 3585  sub _tree_construction_initial ($) {
3585        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
3586        $self->{document}->append_child ($comment);        $self->{document}->append_child ($comment);
3587                
3588        ## Stay in the phase.        ## Stay in the insertion mode.
3589        !!!next-token;        !!!next-token;
3590        redo INITIAL;        redo INITIAL;
3591      } else {      } else {
# Line 2632  sub _tree_construction_initial ($) { Line 3598  sub _tree_construction_initial ($) {
3598    
3599  sub _tree_construction_root_element ($) {  sub _tree_construction_root_element ($) {
3600    my $self = shift;    my $self = shift;
3601    
3602      ## NOTE: "before html" insertion mode.
3603        
3604    B: {    B: {
3605        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3606          !!!cp ('t19');          !!!cp ('t19');
3607          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3608          ## Ignore the token          ## Ignore the token
3609          ## Stay in the phase          ## Stay in the insertion mode.
3610          !!!next-token;          !!!next-token;
3611          redo B;          redo B;
3612        } elsif ($token->{type} == COMMENT_TOKEN) {        } elsif ($token->{type} == COMMENT_TOKEN) {
3613          !!!cp ('t20');          !!!cp ('t20');
3614          my $comment = $self->{document}->create_comment ($token->{data});          my $comment = $self->{document}->create_comment ($token->{data});
3615          $self->{document}->append_child ($comment);          $self->{document}->append_child ($comment);
3616          ## Stay in the phase          ## Stay in the insertion mode.
3617          !!!next-token;          !!!next-token;
3618          redo B;          redo B;
3619        } elsif ($token->{type} == CHARACTER_TOKEN) {        } elsif ($token->{type} == CHARACTER_TOKEN) {
# Line 2654  sub _tree_construction_root_element ($) Line 3622  sub _tree_construction_root_element ($)
3622    
3623            unless (length $token->{data}) {            unless (length $token->{data}) {
3624              !!!cp ('t21');              !!!cp ('t21');
3625              ## Stay in the phase              ## Stay in the insertion mode.
3626              !!!next-token;              !!!next-token;
3627              redo B;              redo B;
3628            } else {            } else {
# Line 2668  sub _tree_construction_root_element ($) Line 3636  sub _tree_construction_root_element ($)
3636    
3637          #          #
3638        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3639          if ($token->{tag_name} eq 'html' and          if ($token->{tag_name} eq 'html') {
3640              $token->{attributes}->{manifest}) {            my $root_element;
3641            !!!cp ('t24');            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3642            $self->{application_cache_selection}            $self->{document}->append_child ($root_element);
3643                 ->($token->{attributes}->{manifest}->{value});            push @{$self->{open_elements}},
3644            ## ISSUE: No relative reference resolution?                [$root_element, $el_category->{html}];
3645    
3646              if ($token->{attributes}->{manifest}) {
3647                !!!cp ('t24');
3648                $self->{application_cache_selection}
3649                    ->($token->{attributes}->{manifest}->{value});
3650                ## ISSUE: Spec is unclear on relative references.
3651                ## According to Hixie (#whatwg 2008-03-19), it should be
3652                ## resolved against the base URI of the document in HTML
3653                ## or xml:base of the element in XHTML.
3654              } else {
3655                !!!cp ('t25');
3656                $self->{application_cache_selection}->(undef);
3657              }
3658    
3659              !!!nack ('t25c');
3660    
3661              !!!next-token;
3662              return; ## Go to the "before head" insertion mode.
3663          } else {          } else {
3664            !!!cp ('t25');            !!!cp ('t25.1');
3665            $self->{application_cache_selection}->(undef);            #
3666          }          }
   
         ## ISSUE: There is an issue in the spec  
         #  
3667        } elsif ({        } elsif ({
3668                  END_TAG_TOKEN, 1,                  END_TAG_TOKEN, 1,
3669                  END_OF_FILE_TOKEN, 1,                  END_OF_FILE_TOKEN, 1,
3670                 }->{$token->{type}}) {                 }->{$token->{type}}) {
3671          !!!cp ('t26');          !!!cp ('t26');
         $self->{application_cache_selection}->(undef);  
   
         ## ISSUE: There is an issue in the spec  
3672          #          #
3673        } else {        } else {
3674          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3675        }        }
3676    
3677        my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3678        $self->{document}->append_child ($root_element);      !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3679        push @{$self->{open_elements}}, [$root_element, 'html'];      $self->{document}->append_child ($root_element);
3680        ## reprocess      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3681        #redo B;  
3682        return; ## Go to the main phase.      $self->{application_cache_selection}->(undef);
3683    
3684        ## NOTE: Reprocess the token.
3685        !!!ack-later;
3686        return; ## Go to the "before head" insertion mode.
3687    
3688        ## ISSUE: There is an issue in the spec
3689    } # B    } # B
3690    
3691    die "$0: _tree_construction_root_element: This should never be reached";    die "$0: _tree_construction_root_element: This should never be reached";
# Line 2717  sub _reset_insertion_mode ($) { Line 3703  sub _reset_insertion_mode ($) {
3703            
3704      ## Step 3      ## Step 3
3705      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"!?  
3706        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3707          $last = 1;          $last = 1;
3708          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3709            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3710                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3711              !!!cp ('t27');          } else {
3712              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3713          }          }
3714        }        }
3715              
3716        ## Step 4..13        ## Step 4..14
3717        my $new_mode = {        my $new_mode;
3718          if ($node->[1] & FOREIGN_EL) {
3719            !!!cp ('t28.1');
3720            ## NOTE: Strictly spaking, the line below only applies to MathML and
3721            ## SVG elements.  Currently the HTML syntax supports only MathML and
3722            ## SVG elements as foreigners.
3723            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3724          } elsif ($node->[1] & TABLE_CELL_EL) {
3725            if ($last) {
3726              !!!cp ('t28.2');
3727              #
3728            } else {
3729              !!!cp ('t28.3');
3730              $new_mode = IN_CELL_IM;
3731            }
3732          } else {
3733            !!!cp ('t28.4');
3734            $new_mode = {
3735                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3736                        td => IN_CELL_IM,                        ## NOTE: |option| and |optgroup| do not set
3737                        th => IN_CELL_IM,                        ## insertion mode to "in select" by themselves.
3738                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3739                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3740                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2751  sub _reset_insertion_mode ($) { Line 3745  sub _reset_insertion_mode ($) {
3745                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3746                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3747                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3748                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3749          }
3750        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3751                
3752        ## Step 14        ## Step 15
3753        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3754          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3755            !!!cp ('t29');            !!!cp ('t29');
3756            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2769  sub _reset_insertion_mode ($) { Line 3764  sub _reset_insertion_mode ($) {
3764          !!!cp ('t31');          !!!cp ('t31');
3765        }        }
3766                
3767        ## Step 15        ## Step 16
3768        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3769                
3770        ## Step 16        ## Step 17
3771        $i--;        $i--;
3772        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3773                
3774        ## Step 17        ## Step 18
3775        redo S3;        redo S3;
3776      } # S3      } # S3
3777    
# Line 2880  sub _tree_construction_main ($) { Line 3875  sub _tree_construction_main ($) {
3875      !!!cp ('t39');      !!!cp ('t39');
3876    }; # $clear_up_to_marker    }; # $clear_up_to_marker
3877    
3878    my $parse_rcdata = sub ($$) {    my $insert;
3879      my ($content_model_flag, $insert) = @_;  
3880      my $parse_rcdata = sub ($) {
3881        my ($content_model_flag) = @_;
3882    
3883      ## Step 1      ## Step 1
3884      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3885      my $el;      my $el;
3886      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3887    
3888      ## Step 2      ## Step 2
3889      $insert->($el); # /context node/->append_child ($el)      $insert->($el);
3890    
3891      ## Step 3      ## Step 3
3892      $self->{content_model} = $content_model_flag; # CDATA or RCDATA      $self->{content_model} = $content_model_flag; # CDATA or RCDATA
# Line 2897  sub _tree_construction_main ($) { Line 3894  sub _tree_construction_main ($) {
3894    
3895      ## Step 4      ## Step 4
3896      my $text = '';      my $text = '';
3897        !!!nack ('t40.1');
3898      !!!next-token;      !!!next-token;
3899      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3900        !!!cp ('t40');        !!!cp ('t40');
# Line 2919  sub _tree_construction_main ($) { Line 3917  sub _tree_construction_main ($) {
3917          $token->{tag_name} eq $start_tag_name) {          $token->{tag_name} eq $start_tag_name) {
3918        !!!cp ('t42');        !!!cp ('t42');
3919        ## 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});  
3920      } else {      } else {
3921        die "$0: $content_model_flag in parse_rcdata";        ## NOTE: An end-of-file token.
3922          if ($content_model_flag == CDATA_CONTENT_MODEL) {
3923            !!!cp ('t43');
3924            !!!parse-error (type => 'in CDATA:#eof', token => $token);
3925          } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3926            !!!cp ('t44');
3927            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3928          } else {
3929            die "$0: $content_model_flag in parse_rcdata";
3930          }
3931      }      }
3932      !!!next-token;      !!!next-token;
3933    }; # $parse_rcdata    }; # $parse_rcdata
3934    
3935    my $script_start_tag = sub ($) {    my $script_start_tag = sub () {
     my $insert = $_[0];  
3936      my $script_el;      my $script_el;
3937      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3938      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3939    
3940      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3941      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3942            
3943      my $text = '';      my $text = '';
3944        !!!nack ('t45.1');
3945      !!!next-token;      !!!next-token;
3946      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3947        !!!cp ('t45');        !!!cp ('t45');
# Line 2960  sub _tree_construction_main ($) { Line 3961  sub _tree_construction_main ($) {
3961        ## Ignore the token        ## Ignore the token
3962      } else {      } else {
3963        !!!cp ('t48');        !!!cp ('t48');
3964        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3965        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3966        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3967      }      }
# Line 2983  sub _tree_construction_main ($) { Line 3984  sub _tree_construction_main ($) {
3984      !!!next-token;      !!!next-token;
3985    }; # $script_start_tag    }; # $script_start_tag
3986    
3987      ## NOTE: $open_tables->[-1]->[0] is the "current table" element node.
3988      ## NOTE: $open_tables->[-1]->[1] is the "tainted" flag.
3989      my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3990    
3991    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3992      my $tag_name = shift;      my $end_tag_token = shift;
3993        my $tag_name = $end_tag_token->{tag_name};
3994    
3995        ## NOTE: The adoption agency algorithm (AAA).
3996    
3997      FET: {      FET: {
3998        ## Step 1        ## Step 1
3999        my $formatting_element;        my $formatting_element;
4000        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4001        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4002          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4003              !!!cp ('t52');
4004              last AFE;
4005            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4006                         eq $tag_name) {
4007            !!!cp ('t51');            !!!cp ('t51');
4008            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4009            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4010            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
4011          }          }
4012        } # AFE        } # AFE
4013        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4014          !!!cp ('t53');          !!!cp ('t53');
4015          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4016          ## Ignore the token          ## Ignore the token
4017          !!!next-token;          !!!next-token;
4018          return;          return;
# Line 3020  sub _tree_construction_main ($) { Line 4029  sub _tree_construction_main ($) {
4029              last INSCOPE;              last INSCOPE;
4030            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4031              !!!cp ('t55');              !!!cp ('t55');
4032              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
4033                                text => $token->{tag_name},
4034                                token => $end_tag_token);
4035              ## Ignore the token              ## Ignore the token
4036              !!!next-token;              !!!next-token;
4037              return;              return;
4038            }            }
4039          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4040            !!!cp ('t56');            !!!cp ('t56');
4041            $in_scope = 0;            $in_scope = 0;
4042          }          }
4043        } # INSCOPE        } # INSCOPE
4044        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4045          !!!cp ('t57');          !!!cp ('t57');
4046          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
4047                            text => $token->{tag_name},
4048                            token => $end_tag_token);
4049          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4050          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4051          return;          return;
4052        }        }
4053        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4054          !!!cp ('t58');          !!!cp ('t58');
4055          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
4056                            text => $self->{open_elements}->[-1]->[0]
4057                                ->manakai_local_name,
4058                            token => $end_tag_token);
4059        }        }
4060                
4061        ## Step 2        ## Step 2
# Line 3050  sub _tree_construction_main ($) { Line 4063  sub _tree_construction_main ($) {
4063        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4064        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4065          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4066          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4067              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4068              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4069               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4070            !!!cp ('t59');            !!!cp ('t59');
4071            $furthest_block = $node;            $furthest_block = $node;
4072            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3139  sub _tree_construction_main ($) { Line 4152  sub _tree_construction_main ($) {
4152        } # S7          } # S7  
4153                
4154        ## Step 8        ## Step 8
4155        $common_ancestor_node->[0]->append_child ($last_node->[0]);        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
4156            my $foster_parent_element;
4157            my $next_sibling;
4158            OE: for (reverse 0..$#{$self->{open_elements}}) {
4159              if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4160                                 my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4161                                 if (defined $parent and $parent->node_type == 1) {
4162                                   !!!cp ('t65.1');
4163                                   $foster_parent_element = $parent;
4164                                   $next_sibling = $self->{open_elements}->[$_]->[0];
4165                                 } else {
4166                                   !!!cp ('t65.2');
4167                                   $foster_parent_element
4168                                     = $self->{open_elements}->[$_ - 1]->[0];
4169                                 }
4170                                 last OE;
4171                               }
4172                             } # OE
4173                             $foster_parent_element = $self->{open_elements}->[0]->[0]
4174                               unless defined $foster_parent_element;
4175            $foster_parent_element->insert_before ($last_node->[0], $next_sibling);
4176            $open_tables->[-1]->[1] = 1; # tainted
4177          } else {
4178            !!!cp ('t65.3');
4179            $common_ancestor_node->[0]->append_child ($last_node->[0]);
4180          }
4181                
4182        ## Step 9        ## Step 9
4183        my $clone = [$formatting_element->[0]->clone_node (0),        my $clone = [$formatting_element->[0]->clone_node (0),
# Line 3185  sub _tree_construction_main ($) { Line 4223  sub _tree_construction_main ($) {
4223      } # FET      } # FET
4224    }; # $formatting_end_tag    }; # $formatting_end_tag
4225    
4226    my $insert_to_current = sub {    $insert = my $insert_to_current = sub {
4227      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);      $self->{open_elements}->[-1]->[0]->append_child ($_[0]);
4228    }; # $insert_to_current    }; # $insert_to_current
4229    
4230    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4231                         my $child = shift;      my $child = shift;
4232                         if ({      if ($self->{open_elements}->[-1]->[1] & TABLE_ROWS_EL) {
4233                              table => 1, tbody => 1, tfoot => 1,        # MUST
4234                              thead => 1, tr => 1,        my $foster_parent_element;
4235                             }->{$self->{open_elements}->[-1]->[1]}) {        my $next_sibling;
4236                           # MUST        OE: for (reverse 0..$#{$self->{open_elements}}) {
4237                           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') {  
4238                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4239                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4240                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3217  sub _tree_construction_main ($) { Line 4252  sub _tree_construction_main ($) {
4252                             unless defined $foster_parent_element;                             unless defined $foster_parent_element;
4253                           $foster_parent_element->insert_before                           $foster_parent_element->insert_before
4254                             ($child, $next_sibling);                             ($child, $next_sibling);
4255                         } else {        $open_tables->[-1]->[1] = 1; # tainted
4256                           !!!cp ('t72');      } else {
4257                           $self->{open_elements}->[-1]->[0]->append_child ($child);        !!!cp ('t72');
4258                         }        $self->{open_elements}->[-1]->[0]->append_child ($child);
4259        }
4260    }; # $insert_to_foster    }; # $insert_to_foster
4261    
4262    my $insert;    B: while (1) {
   
   B: {  
4263      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4264        !!!cp ('t73');        !!!cp ('t73');
4265        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4266        ## Ignore the token        ## Ignore the token
4267        ## Stay in the phase        ## Stay in the phase
4268        !!!next-token;        !!!next-token;
4269        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         if ({  
              dd => 1, dt => 1, li => 1, p => 1, td => 1, th => 1, tr => 1,  
              tbody => 1, tfoot=> 1, thead => 1,  
             }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           !!!back-token;  
           $token = {type => END_TAG_TOKEN, tag_name => $self->{open_elements}->[-1]->[1]};  
           redo B;  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
4270      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4271               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4272        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4273          !!!cp ('t79');          !!!cp ('t79');
4274          ## Turn into the main phase          !!!parse-error (type => 'after html', text => 'html', token => $token);
         !!!parse-error (type => 'after html:html');  
4275          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4276        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4277          !!!cp ('t80');          !!!cp ('t80');
4278          ## Turn into the main phase          !!!parse-error (type => 'after html', text => 'html', token => $token);
         !!!parse-error (type => 'after html:html');  
4279          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4280        } else {        } else {
4281          !!!cp ('t81');          !!!cp ('t81');
4282        }        }
4283    
4284  ## ISSUE: "aa<html>" is not a parse error.        !!!cp ('t82');
4285  ## ISSUE: "<html>" in fragment is not a parse error.        !!!parse-error (type => 'not first start tag', token => $token);
       unless ($token->{first_start_tag}) {  
         !!!cp ('t82');  
         !!!parse-error (type => 'not first start tag');  
       } else {  
         !!!cp ('t83');  
       }  
4286        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4287        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4288          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3301  sub _tree_construction_main ($) { Line 4292  sub _tree_construction_main ($) {
4292               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4293          }          }
4294        }        }
4295          !!!nack ('t84.1');
4296        !!!next-token;        !!!next-token;
4297        redo B;        next B;
4298      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4299        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4300        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3316  sub _tree_construction_main ($) { Line 4308  sub _tree_construction_main ($) {
4308          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4309        }        }
4310        !!!next-token;        !!!next-token;
4311        redo B;        next B;
4312      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4313          if ($token->{type} == CHARACTER_TOKEN) {
4314            !!!cp ('t87.1');
4315            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4316            !!!next-token;
4317            next B;
4318          } elsif ($token->{type} == START_TAG_TOKEN) {
4319            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4320                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4321                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4322                ($token->{tag_name} eq 'svg' and
4323                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4324              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4325              !!!cp ('t87.2');
4326              #
4327            } elsif ({
4328                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4329                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4330                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4331                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4332                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4333                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4334                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4335                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4336                     }->{$token->{tag_name}}) {
4337              !!!cp ('t87.2');
4338              !!!parse-error (type => 'not closed',
4339                              text => $self->{open_elements}->[-1]->[0]
4340                                  ->manakai_local_name,
4341                              token => $token);
4342    
4343              pop @{$self->{open_elements}}
4344                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4345    
4346              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4347              ## Reprocess.
4348              next B;
4349            } else {
4350              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4351              my $tag_name = $token->{tag_name};
4352              if ($nsuri eq $SVG_NS) {
4353                $tag_name = {
4354                   altglyph => 'altGlyph',
4355                   altglyphdef => 'altGlyphDef',
4356                   altglyphitem => 'altGlyphItem',
4357                   animatecolor => 'animateColor',
4358                   animatemotion => 'animateMotion',
4359                   animatetransform => 'animateTransform',
4360                   clippath => 'clipPath',
4361                   feblend => 'feBlend',
4362                   fecolormatrix => 'feColorMatrix',
4363                   fecomponenttransfer => 'feComponentTransfer',
4364                   fecomposite => 'feComposite',
4365                   feconvolvematrix => 'feConvolveMatrix',
4366                   fediffuselighting => 'feDiffuseLighting',
4367                   fedisplacementmap => 'feDisplacementMap',
4368                   fedistantlight => 'feDistantLight',
4369                   feflood => 'feFlood',
4370                   fefunca => 'feFuncA',
4371                   fefuncb => 'feFuncB',
4372                   fefuncg => 'feFuncG',
4373                   fefuncr => 'feFuncR',
4374                   fegaussianblur => 'feGaussianBlur',
4375                   feimage => 'feImage',
4376                   femerge => 'feMerge',
4377                   femergenode => 'feMergeNode',
4378                   femorphology => 'feMorphology',
4379                   feoffset => 'feOffset',
4380                   fepointlight => 'fePointLight',
4381                   fespecularlighting => 'feSpecularLighting',
4382                   fespotlight => 'feSpotLight',
4383                   fetile => 'feTile',
4384                   feturbulence => 'feTurbulence',
4385                   foreignobject => 'foreignObject',
4386                   glyphref => 'glyphRef',
4387                   lineargradient => 'linearGradient',
4388                   radialgradient => 'radialGradient',
4389                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4390                   textpath => 'textPath',  
4391                }->{$tag_name} || $tag_name;
4392              }
4393    
4394              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4395    
4396              ## "adjust foreign attributes" - done in insert-element-f
4397    
4398              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4399    
4400              if ($self->{self_closing}) {
4401                pop @{$self->{open_elements}};
4402                !!!ack ('t87.3');
4403              } else {
4404                !!!cp ('t87.4');
4405              }
4406    
4407              !!!next-token;
4408              next B;
4409            }
4410          } elsif ($token->{type} == END_TAG_TOKEN) {
4411            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4412            !!!cp ('t87.5');
4413            #
4414          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4415            !!!cp ('t87.6');
4416            !!!parse-error (type => 'not closed',
4417                            text => $self->{open_elements}->[-1]->[0]
4418                                ->manakai_local_name,
4419                            token => $token);
4420    
4421            pop @{$self->{open_elements}}
4422                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4423    
4424            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4425            ## Reprocess.
4426            next B;
4427          } else {
4428            die "$0: $token->{type}: Unknown token type";        
4429          }
4430        }
4431    
4432        if ($self->{insertion_mode} & HEAD_IMS) {
4433        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4434          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4435            $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4436                !!!cp ('t88.2');
4437                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4438              } else {
4439                !!!cp ('t88.1');
4440                ## Ignore the token.
4441                !!!next-token;
4442                next B;
4443              }
4444            unless (length $token->{data}) {            unless (length $token->{data}) {
4445              !!!cp ('t88');              !!!cp ('t88');
4446              !!!next-token;              !!!next-token;
4447              redo B;              next B;
4448            }            }
4449          }          }
4450    
4451          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4452            !!!cp ('t89');            !!!cp ('t89');
4453            ## As if <head>            ## As if <head>
4454            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4455            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4456            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4457                  [$self->{head_element}, $el_category->{head}];
4458    
4459            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4460            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3343  sub _tree_construction_main ($) { Line 4464  sub _tree_construction_main ($) {
4464            !!!cp ('t90');            !!!cp ('t90');
4465            ## As if </noscript>            ## As if </noscript>
4466            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4467            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4468                        
4469            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4470            ## As if </head>            ## As if </head>
# Line 3359  sub _tree_construction_main ($) { Line 4480  sub _tree_construction_main ($) {
4480            !!!cp ('t92');            !!!cp ('t92');
4481          }          }
4482    
4483              ## "after head" insertion mode          ## "after head" insertion mode
4484              ## As if <body>          ## As if <body>
4485              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4486              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4487              ## reprocess          ## reprocess
4488              redo B;          next B;
4489            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4490              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4491                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4492                  !!!cp ('t93');              !!!cp ('t93');
4493                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4494                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4495                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4496                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4497                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4498                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4499                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4500                  !!!cp ('t94');              !!!next-token;
4501                  #              next B;
4502                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4503                  !!!cp ('t95');              !!!cp ('t93.2');
4504                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4505                  ## Ignore the token                              token => $token);
4506                  !!!next-token;              ## Ignore the token
4507                  redo B;              !!!nack ('t93.3');
4508                }              !!!next-token;
4509              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4510                !!!cp ('t96');            } else {
4511                ## As if <head>              !!!cp ('t95');
4512                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4513                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4514                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4515                !!!nack ('t95.1');
4516                !!!next-token;
4517                next B;
4518              }
4519            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4520              !!!cp ('t96');
4521              ## As if <head>
4522              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4523              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4524              push @{$self->{open_elements}},
4525                  [$self->{head_element}, $el_category->{head}];
4526    
4527                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4528                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4529              } else {          } else {
4530                !!!cp ('t97');            !!!cp ('t97');
4531              }          }
4532    
4533              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4534                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4535                  !!!cp ('t98');                  !!!cp ('t98');
4536                  ## As if </noscript>                  ## As if </noscript>
4537                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4538                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4539                                    token => $token);
4540                                
4541                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4542                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3414  sub _tree_construction_main ($) { Line 4547  sub _tree_construction_main ($) {
4547                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4548                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4549                  !!!cp ('t100');                  !!!cp ('t100');
4550                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4551                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4552                    push @{$self->{open_elements}},
4553                        [$self->{head_element}, $el_category->{head}];
4554                } else {                } else {
4555                  !!!cp ('t101');                  !!!cp ('t101');
4556                }                }
4557                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4558                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4559                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4560                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4561                  !!!nack ('t101.1');
4562                !!!next-token;                !!!next-token;
4563                redo B;                next B;
4564              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4565                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4566                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4567                  !!!cp ('t102');                  !!!cp ('t102');
4568                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4569                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4570                    push @{$self->{open_elements}},
4571                        [$self->{head_element}, $el_category->{head}];
4572                } else {                } else {
4573                  !!!cp ('t103');                  !!!cp ('t103');
4574                }                }
4575                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4576                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4577                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4578                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4579                  !!!ack ('t103.1');
4580                !!!next-token;                !!!next-token;
4581                redo B;                next B;
4582              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4583                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4584                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4585                  !!!cp ('t104');                  !!!cp ('t104');
4586                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4587                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4588                    push @{$self->{open_elements}},
4589                        [$self->{head_element}, $el_category->{head}];
4590                } else {                } else {
4591                  !!!cp ('t105');                  !!!cp ('t105');
4592                }                }
4593                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4594                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.
4595    
4596                unless ($self->{confident}) {                unless ($self->{confident}) {
4597                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4598                    !!!cp ('t106');                    !!!cp ('t106');
4599                      ## NOTE: Whether the encoding is supported or not is handled
4600                      ## in the {change_encoding} callback.
4601                    $self->{change_encoding}                    $self->{change_encoding}
4602                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4603                             $token);
4604                                        
4605                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4606                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4607                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4608                                                 ->{has_reference});                                                 ->{has_reference});
4609                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4610                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4611                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4612                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4613                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4614                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4615                      !!!cp ('t107');                      !!!cp ('t107');
4616                        ## NOTE: Whether the encoding is supported or not is handled
4617                        ## in the {change_encoding} callback.
4618                      $self->{change_encoding}                      $self->{change_encoding}
4619                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4620                               $token);
4621                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4622                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4623                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3497  sub _tree_construction_main ($) { Line 4643  sub _tree_construction_main ($) {
4643                  }                  }
4644                }                }
4645    
4646                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4647                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4648                  !!!ack ('t110.1');
4649                !!!next-token;                !!!next-token;
4650                redo B;                next B;
4651              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4652                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4653                  !!!cp ('t111');                  !!!cp ('t111');
4654                  ## As if </noscript>                  ## As if </noscript>
4655                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4656                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4657                                    token => $token);
4658                                
4659                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4660                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4661                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4662                  !!!cp ('t112');                  !!!cp ('t112');
4663                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4664                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4665                    push @{$self->{open_elements}},
4666                        [$self->{head_element}, $el_category->{head}];
4667                } else {                } else {
4668                  !!!cp ('t113');                  !!!cp ('t113');
4669                }                }
# Line 3521  sub _tree_construction_main ($) { Line 4671  sub _tree_construction_main ($) {
4671                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4672                my $parent = defined $self->{head_element} ? $self->{head_element}                my $parent = defined $self->{head_element} ? $self->{head_element}
4673                    : $self->{open_elements}->[-1]->[0];                    : $self->{open_elements}->[-1]->[0];
4674                $parse_rcdata->(RCDATA_CONTENT_MODEL,                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4675                                sub { $parent->append_child ($_[0]) });                pop @{$self->{open_elements}} # <head>
               pop @{$self->{open_elements}}  
4676                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4677                redo B;                next B;
4678              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4679                         $token->{tag_name} eq 'noframes') {
4680                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4681                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4682                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4683                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4684                  !!!cp ('t114');                  !!!cp ('t114');
4685                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4686                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4687                    push @{$self->{open_elements}},
4688                        [$self->{head_element}, $el_category->{head}];
4689                } else {                } else {
4690                  !!!cp ('t115');                  !!!cp ('t115');
4691                }                }
4692                $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4693                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4694                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4695                redo B;                next B;
4696              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4697                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4698                  !!!cp ('t116');                  !!!cp ('t116');
4699                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4700                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4701                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4702                    !!!nack ('t116.1');
4703                  !!!next-token;                  !!!next-token;
4704                  redo B;                  next B;
4705                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4706                  !!!cp ('t117');                  !!!cp ('t117');
4707                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
4708                                    token => $token);
4709                  ## Ignore the token                  ## Ignore the token
4710                    !!!nack ('t117.1');
4711                  !!!next-token;                  !!!next-token;
4712                  redo B;                  next B;
4713                } else {                } else {
4714                  !!!cp ('t118');                  !!!cp ('t118');
4715                  #                  #
# Line 3564  sub _tree_construction_main ($) { Line 4719  sub _tree_construction_main ($) {
4719                  !!!cp ('t119');                  !!!cp ('t119');
4720                  ## As if </noscript>                  ## As if </noscript>
4721                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4722                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4723                                    token => $token);
4724                                
4725                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4726                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4727                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4728                  !!!cp ('t120');                  !!!cp ('t120');
4729                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4730                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4731                    push @{$self->{open_elements}},
4732                        [$self->{head_element}, $el_category->{head}];
4733                } else {                } else {
4734                  !!!cp ('t121');                  !!!cp ('t121');
4735                }                }
4736    
4737                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4738                $script_start_tag->($insert_to_current);                $script_start_tag->();
4739                pop @{$self->{open_elements}}                pop @{$self->{open_elements}} # <head>
4740                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4741                redo B;                next B;
4742              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4743                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4744                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4745                  !!!cp ('t122');                  !!!cp ('t122');
4746                  ## As if </noscript>                  ## As if </noscript>
4747                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4748                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4749                                    text => $token->{tag_name}, token => $token);
4750                                    
4751                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4752                  ## As if </head>                  ## As if </head>
# Line 3604  sub _tree_construction_main ($) { Line 4763  sub _tree_construction_main ($) {
4763                }                }
4764    
4765                ## "after head" insertion mode                ## "after head" insertion mode
4766                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4767                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4768                  !!!cp ('t126');                  !!!cp ('t126');
4769                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3614  sub _tree_construction_main ($) { Line 4773  sub _tree_construction_main ($) {
4773                } else {                } else {
4774                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4775                }                }
4776                  !!!nack ('t127.1');
4777                !!!next-token;                !!!next-token;
4778                redo B;                next B;
4779              } else {              } else {
4780                !!!cp ('t128');                !!!cp ('t128');
4781                #                #
# Line 3625  sub _tree_construction_main ($) { Line 4785  sub _tree_construction_main ($) {
4785                !!!cp ('t129');                !!!cp ('t129');
4786                ## As if </noscript>                ## As if </noscript>
4787                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4788                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4789                                  text => $token->{tag_name}, token => $token);
4790                                
4791                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4792                ## As if </head>                ## As if </head>
# Line 3644  sub _tree_construction_main ($) { Line 4805  sub _tree_construction_main ($) {
4805    
4806              ## "after head" insertion mode              ## "after head" insertion mode
4807              ## As if <body>              ## As if <body>
4808              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4809              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4810              ## reprocess              ## reprocess
4811              redo B;              !!!ack-later;
4812                next B;
4813            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4814              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4815                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4816                  !!!cp ('t132');                  !!!cp ('t132');
4817                  ## As if <head>                  ## As if <head>
4818                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4819                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4820                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4821                        [$self->{head_element}, $el_category->{head}];
4822    
4823                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4824                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4825                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4826                  !!!next-token;                  !!!next-token;
4827                  redo B;                  next B;
4828                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4829                  !!!cp ('t133');                  !!!cp ('t133');
4830                  ## As if </noscript>                  ## As if </noscript>
4831                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4832                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript:/',
4833                                    text => 'head', token => $token);
4834                                    
4835                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4836                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4837                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4838                  !!!next-token;                  !!!next-token;
4839                  redo B;                  next B;
4840                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4841                  !!!cp ('t134');                  !!!cp ('t134');
4842                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4843                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4844                  !!!next-token;                  !!!next-token;
4845                  redo B;                  next B;
4846                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4847                    !!!cp ('t134.1');
4848                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4849                                    token => $token);
4850                    ## Ignore the token
4851                    !!!next-token;
4852                    next B;
4853                } else {                } else {
4854                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4855                }                }
4856              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4857                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3689  sub _tree_construction_main ($) { Line 4859  sub _tree_construction_main ($) {
4859                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4860                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4861                  !!!next-token;                  !!!next-token;
4862                  redo B;                  next B;
4863                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4864                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4865                  !!!cp ('t137');                  !!!cp ('t137');
4866                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
4867                                    text => 'noscript', token => $token);
4868                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4869                  !!!next-token;                  !!!next-token;
4870                  redo B;                  next B;
4871                } else {                } else {
4872                  !!!cp ('t138');                  !!!cp ('t138');
4873                  #                  #
# Line 3703  sub _tree_construction_main ($) { Line 4875  sub _tree_construction_main ($) {
4875              } elsif ({              } elsif ({
4876                        body => 1, html => 1,                        body => 1, html => 1,
4877                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4878                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4879                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4880                  ## 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) {  
4881                  !!!cp ('t140');                  !!!cp ('t140');
4882                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4883                                    text => $token->{tag_name}, token => $token);
4884                  ## Ignore the token                  ## Ignore the token
4885                  !!!next-token;                  !!!next-token;
4886                  redo B;                  next B;
4887                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4888                    !!!cp ('t140.1');
4889                    !!!parse-error (type => 'unmatched end tag',
4890                                    text => $token->{tag_name}, token => $token);
4891                    ## Ignore the token
4892                    !!!next-token;
4893                    next B;
4894                } else {                } else {
4895                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4896                }                }
4897                              } elsif ($token->{tag_name} eq 'p') {
4898                #                !!!cp ('t142');
4899              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4900                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4901                       }->{$token->{tag_name}}) {                ## Ignore the token
4902                  !!!next-token;
4903                  next B;
4904                } elsif ($token->{tag_name} eq 'br') {
4905                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4906                  !!!cp ('t142');                  !!!cp ('t142.2');
4907                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4908                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4909                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4910                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4911      
4912                    ## Reprocess in the "after head" insertion mode...
4913                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4914                    !!!cp ('t143.2');
4915                    ## As if </head>
4916                    pop @{$self->{open_elements}};
4917                    $self->{insertion_mode} = AFTER_HEAD_IM;
4918      
4919                    ## Reprocess in the "after head" insertion mode...
4920                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4921                    !!!cp ('t143.3');
4922                    ## ISSUE: Two parse errors for <head><noscript></br>
4923                    !!!parse-error (type => 'unmatched end tag',
4924                                    text => 'br', token => $token);
4925                    ## As if </noscript>
4926                    pop @{$self->{open_elements}};
4927                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4928    
4929                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4930                } else {                  ## As if </head>
4931                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4932                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4933    
4934                #                  ## Reprocess in the "after head" insertion mode...
4935              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4936                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4937                  #                  #
4938                } else {                } else {
4939                  !!!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;  
4940                }                }
4941    
4942                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4943                  !!!parse-error (type => 'unmatched end tag',
4944                                  text => 'br', token => $token);
4945                  ## Ignore the token
4946                  !!!next-token;
4947                  next B;
4948                } else {
4949                  !!!cp ('t145');
4950                  !!!parse-error (type => 'unmatched end tag',
4951                                  text => $token->{tag_name}, token => $token);
4952                  ## Ignore the token
4953                  !!!next-token;
4954                  next B;
4955              }              }
4956    
4957              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4958                !!!cp ('t146');                !!!cp ('t146');
4959                ## As if </noscript>                ## As if </noscript>
4960                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4961                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4962                                  text => $token->{tag_name}, token => $token);
4963                                
4964                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4965                ## As if </head>                ## As if </head>
# Line 3771  sub _tree_construction_main ($) { Line 4973  sub _tree_construction_main ($) {
4973    
4974                ## Reprocess in the "after head" insertion mode...                ## Reprocess in the "after head" insertion mode...
4975              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4976    ## ISSUE: This case cannot be reached?
4977                !!!cp ('t148');                !!!cp ('t148');
4978                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
4979                                  text => $token->{tag_name}, token => $token);
4980                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4981                !!!next-token;                !!!next-token;
4982                redo B;                next B;
4983              } else {              } else {
4984                !!!cp ('t149');                !!!cp ('t149');
4985              }              }
4986    
4987              ## "after head" insertion mode              ## "after head" insertion mode
4988              ## As if <body>              ## As if <body>
4989              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4990              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4991              ## reprocess              ## reprocess
4992              redo B;              next B;
4993            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4994              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4995            }            !!!cp ('t149.1');
4996    
4997              ## NOTE: As if <head>
4998              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4999              $self->{open_elements}->[-1]->[0]->append_child
5000                  ($self->{head_element});
5001              #push @{$self->{open_elements}},
5002              #    [$self->{head_element}, $el_category->{head}];
5003              #$self->{insertion_mode} = IN_HEAD_IM;
5004              ## NOTE: Reprocess.
5005    
5006              ## NOTE: As if </head>
5007              #pop @{$self->{open_elements}};
5008              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5009              ## NOTE: Reprocess.
5010              
5011              #
5012            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5013              !!!cp ('t149.2');
5014    
5015              ## NOTE: As if </head>
5016              pop @{$self->{open_elements}};
5017              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5018              ## NOTE: Reprocess.
5019    
5020              #
5021            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5022              !!!cp ('t149.3');
5023    
5024              !!!parse-error (type => 'in noscript:#eof', token => $token);
5025    
5026              ## As if </noscript>
5027              pop @{$self->{open_elements}};
5028              #$self->{insertion_mode} = IN_HEAD_IM;
5029              ## NOTE: Reprocess.
5030    
5031              ## NOTE: As if </head>
5032              pop @{$self->{open_elements}};
5033              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5034              ## NOTE: Reprocess.
5035    
5036              #
5037            } else {
5038              !!!cp ('t149.4');
5039              #
5040            }
5041    
5042            ## NOTE: As if <body>
5043            !!!insert-element ('body',, $token);
5044            $self->{insertion_mode} = IN_BODY_IM;
5045            ## NOTE: Reprocess.
5046            next B;
5047          } else {
5048            die "$0: $token->{type}: Unknown token type";
5049          }
5050    
5051            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5052      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3800  sub _tree_construction_main ($) { Line 5058  sub _tree_construction_main ($) {
5058              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5059    
5060              !!!next-token;              !!!next-token;
5061              redo B;              next B;
5062            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5063              if ({              if ({
5064                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3808  sub _tree_construction_main ($) { Line 5066  sub _tree_construction_main ($) {
5066                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5067                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5068                  ## have an element in table scope                  ## have an element in table scope
5069                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5070                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5071                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5072                      !!!cp ('t151');                      !!!cp ('t151');
5073                      $tn = $node->[1];  
5074                      last INSCOPE;                      ## Close the cell
5075                    } elsif ({                      !!!back-token; # <x>
5076                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
5077                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
5078                                  line => $token->{line},
5079                                  column => $token->{column}};
5080                        next B;
5081                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5082                      !!!cp ('t152');                      !!!cp ('t152');
5083                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
5084                        last;
5085                    }                    }
5086                  } # INSCOPE                  }
5087                    unless (defined $tn) {  
5088                      !!!cp ('t153');                  !!!cp ('t153');
5089                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'start tag not allowed',
5090                      ## Ignore the token                      text => $token->{tag_name}, token => $token);
5091                      !!!next-token;                  ## Ignore the token
5092                      redo B;                  !!!nack ('t153.1');
5093                    }                  !!!next-token;
5094                                    next B;
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
5095                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5096                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5097                                    token => $token);
5098                                    
5099                  ## As if </caption>                  ## NOTE: As if </caption>.
5100                  ## have a table element in table scope                  ## have a table element in table scope
5101                  my $i;                  my $i;
5102                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5103                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5104                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5105                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
5106                      $i = $_;                        !!!cp ('t155');
5107                      last INSCOPE;                        $i = $_;
5108                    } elsif ({                        last INSCOPE;
5109                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5110                             }->{$node->[1]}) {                        !!!cp ('t156');
5111                      !!!cp ('t156');                        last;
5112                      last INSCOPE;                      }
5113                    }                    }
5114    
5115                      !!!cp ('t157');
5116                      !!!parse-error (type => 'start tag not allowed',
5117                                      text => $token->{tag_name}, token => $token);
5118                      ## Ignore the token
5119                      !!!nack ('t157.1');
5120                      !!!next-token;
5121                      next B;
5122                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5123                                    
5124                  ## generate implied end tags                  ## generate implied end tags
5125                  if ({                  while ($self->{open_elements}->[-1]->[1]
5126                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
5127                    !!!cp ('t158');                    !!!cp ('t158');
5128                    !!!back-token; # <?>                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                   !!!back-token;  
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5129                  }                  }
5130    
5131                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5132                    !!!cp ('t159');                    !!!cp ('t159');
5133                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5134                                      text => $self->{open_elements}->[-1]->[0]
5135                                          ->manakai_local_name,
5136                                      token => $token);
5137                  } else {                  } else {
5138                    !!!cp ('t160');                    !!!cp ('t160');
5139                  }                  }
# Line 3891  sub _tree_construction_main ($) { Line 5145  sub _tree_construction_main ($) {
5145                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5146                                    
5147                  ## reprocess                  ## reprocess
5148                  redo B;                  !!!ack-later;
5149                    next B;
5150                } else {                } else {
5151                  !!!cp ('t161');                  !!!cp ('t161');
5152                  #                  #
# Line 3907  sub _tree_construction_main ($) { Line 5162  sub _tree_construction_main ($) {
5162                  my $i;                  my $i;
5163                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5164                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5165                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5166                      !!!cp ('t163');                      !!!cp ('t163');
5167                      $i = $_;                      $i = $_;
5168                      last INSCOPE;                      last INSCOPE;
5169                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5170                      !!!cp ('t164');                      !!!cp ('t164');
5171                      last INSCOPE;                      last INSCOPE;
5172                    }                    }
5173                  } # INSCOPE                  } # INSCOPE
5174                    unless (defined $i) {                    unless (defined $i) {
5175                      !!!cp ('t165');                      !!!cp ('t165');
5176                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5177                                        text => $token->{tag_name},
5178                                        token => $token);
5179                      ## Ignore the token                      ## Ignore the token
5180                      !!!next-token;                      !!!next-token;
5181                      redo B;                      next B;
5182                    }                    }
5183                                    
5184                  ## generate implied end tags                  ## generate implied end tags
5185                  if ({                  while ($self->{open_elements}->[-1]->[1]
5186                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => ($token->{tag_name} eq 'th'),  
                      th => ($token->{tag_name} eq 'td'),  
                      tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
5187                    !!!cp ('t166');                    !!!cp ('t166');
5188                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5189                  }                  }
5190                    
5191                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5192                            ne $token->{tag_name}) {
5193                    !!!cp ('t167');                    !!!cp ('t167');
5194                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5195                                      text => $self->{open_elements}->[-1]->[0]
5196                                          ->manakai_local_name,
5197                                      token => $token);
5198                  } else {                  } else {
5199                    !!!cp ('t168');                    !!!cp ('t168');
5200                  }                  }
# Line 3955  sub _tree_construction_main ($) { Line 5206  sub _tree_construction_main ($) {
5206                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5207                                    
5208                  !!!next-token;                  !!!next-token;
5209                  redo B;                  next B;
5210                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5211                  !!!cp ('t169');                  !!!cp ('t169');
5212                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5213                                    text => $token->{tag_name}, token => $token);
5214                  ## Ignore the token                  ## Ignore the token
5215                  !!!next-token;                  !!!next-token;
5216                  redo B;                  next B;
5217                } else {                } else {
5218                  !!!cp ('t170');                  !!!cp ('t170');
5219                  #                  #
# Line 3970  sub _tree_construction_main ($) { Line 5222  sub _tree_construction_main ($) {
5222                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5223                  ## have a table element in table scope                  ## have a table element in table scope
5224                  my $i;                  my $i;
5225                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5226                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5227                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5228                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
5229                      $i = $_;                        !!!cp ('t171');
5230                      last INSCOPE;                        $i = $_;
5231                    } elsif ({                        last INSCOPE;
5232                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5233                             }->{$node->[1]}) {                        !!!cp ('t172');
5234                      !!!cp ('t172');                        last;
5235                      last INSCOPE;                      }
5236                    }                    }
5237    
5238                      !!!cp ('t173');
5239                      !!!parse-error (type => 'unmatched end tag',
5240                                      text => $token->{tag_name}, token => $token);
5241                      ## Ignore the token
5242                      !!!next-token;
5243                      next B;
5244                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5245                                    
5246                  ## generate implied end tags                  ## generate implied end tags
5247                  if ({                  while ($self->{open_elements}->[-1]->[1]
5248                       dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                      td => 1, th => 1, tr => 1,  
                      tbody => 1, tfoot=> 1, thead => 1,  
                     }->{$self->{open_elements}->[-1]->[1]}) {  
5249                    !!!cp ('t174');                    !!!cp ('t174');
5250                    !!!back-token;                    pop @{$self->{open_elements}};
                   $token = {type => END_TAG_TOKEN,  
                             tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                   redo B;  
5251                  }                  }
5252                                    
5253                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5254                    !!!cp ('t175');                    !!!cp ('t175');
5255                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5256                                      text => $self->{open_elements}->[-1]->[0]
5257                                          ->manakai_local_name,
5258                                      token => $token);
5259                  } else {                  } else {
5260                    !!!cp ('t176');                    !!!cp ('t176');
5261                  }                  }
# Line 4018  sub _tree_construction_main ($) { Line 5267  sub _tree_construction_main ($) {
5267                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5268                                    
5269                  !!!next-token;                  !!!next-token;
5270                  redo B;                  next B;
5271                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5272                  !!!cp ('t177');                  !!!cp ('t177');
5273                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5274                                    text => $token->{tag_name}, token => $token);
5275                  ## Ignore the token                  ## Ignore the token
5276                  !!!next-token;                  !!!next-token;
5277                  redo B;                  next B;
5278                } else {                } else {
5279                  !!!cp ('t178');                  !!!cp ('t178');
5280                  #                  #
# Line 4037  sub _tree_construction_main ($) { Line 5287  sub _tree_construction_main ($) {
5287                ## have an element in table scope                ## have an element in table scope
5288                my $i;                my $i;
5289                my $tn;                my $tn;
5290                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5291                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5292                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5293                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5294                    $i = $_;                      !!!cp ('t179');
5295                    last INSCOPE;                      $i = $_;
5296                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
5297                    !!!cp ('t180');                      ## Close the cell
5298                    $tn = $node->[1];                      !!!back-token; # </x>
5299                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5300                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
5301                  } elsif ({                                column => $token->{column}};
5302                            table => 1, html => 1,                      next B;
5303                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5304                    !!!cp ('t181');                      !!!cp ('t180');
5305                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
5306                        ## NOTE: There is exactly one |td| or |th| element
5307                        ## in scope in the stack of open elements by definition.
5308                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5309                        ## ISSUE: Can this be reached?
5310                        !!!cp ('t181');
5311                        last;
5312                      }
5313                  }                  }
5314                } # INSCOPE  
               unless (defined $i) {  
5315                  !!!cp ('t182');                  !!!cp ('t182');
5316                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5317                        text => $token->{tag_name}, token => $token);
5318                  ## Ignore the token                  ## Ignore the token
5319                  !!!next-token;                  !!!next-token;
5320                  redo B;                  next B;
5321                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5322              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5323                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5324                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5325                                  token => $token);
5326    
5327                ## As if </caption>                ## As if </caption>
5328                ## have a table element in table scope                ## have a table element in table scope
5329                my $i;                my $i;
5330                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5331                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5332                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5333                    !!!cp ('t184');                    !!!cp ('t184');
5334                    $i = $_;                    $i = $_;
5335                    last INSCOPE;                    last INSCOPE;
5336                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5337                    !!!cp ('t185');                    !!!cp ('t185');
5338                    last INSCOPE;                    last INSCOPE;
5339                  }                  }
5340                } # INSCOPE                } # INSCOPE
5341                unless (defined $i) {                unless (defined $i) {
5342                  !!!cp ('t186');                  !!!cp ('t186');
5343                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag',
5344                                    text => 'caption', token => $token);
5345                  ## Ignore the token                  ## Ignore the token
5346                  !!!next-token;                  !!!next-token;
5347                  redo B;                  next B;
5348                }                }
5349                                
5350                ## generate implied end tags                ## generate implied end tags
5351                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
5352                  !!!cp ('t187');                  !!!cp ('t187');
5353                  !!!back-token; # </table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'caption'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5354                }                }
5355    
5356                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5357                  !!!cp ('t188');                  !!!cp ('t188');
5358                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5359                                    text => $self->{open_elements}->[-1]->[0]
5360                                        ->manakai_local_name,
5361                                    token => $token);
5362                } else {                } else {
5363                  !!!cp ('t189');                  !!!cp ('t189');
5364                }                }
# Line 4126  sub _tree_construction_main ($) { Line 5370  sub _tree_construction_main ($) {
5370                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5371    
5372                ## reprocess                ## reprocess
5373                redo B;                next B;
5374              } elsif ({              } elsif ({
5375                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5376                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5377                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5378                  !!!cp ('t190');                  !!!cp ('t190');
5379                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5380                                    text => $token->{tag_name}, token => $token);
5381                  ## Ignore the token                  ## Ignore the token
5382                  !!!next-token;                  !!!next-token;
5383                  redo B;                  next B;
5384                } else {                } else {
5385                  !!!cp ('t191');                  !!!cp ('t191');
5386                  #                  #
# Line 4146  sub _tree_construction_main ($) { Line 5391  sub _tree_construction_main ($) {
5391                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5392                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5393                !!!cp ('t192');                !!!cp ('t192');
5394                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5395                                  text => $token->{tag_name}, token => $token);
5396                ## Ignore the token                ## Ignore the token
5397                !!!next-token;                !!!next-token;
5398                redo B;                next B;
5399              } else {              } else {
5400                !!!cp ('t193');                !!!cp ('t193');
5401                #                #
5402              }              }
5403          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5404            for my $entry (@{$self->{open_elements}}) {
5405              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5406                !!!cp ('t75');
5407                !!!parse-error (type => 'in body:#eof', token => $token);
5408                last;
5409              }
5410            }
5411    
5412            ## Stop parsing.
5413            last B;
5414        } else {        } else {
5415          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5416        }        }
# Line 4162  sub _tree_construction_main ($) { Line 5419  sub _tree_construction_main ($) {
5419        #        #
5420      } elsif ($self->{insertion_mode} & TABLE_IMS) {      } elsif ($self->{insertion_mode} & TABLE_IMS) {
5421        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
5422              if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if (not $open_tables->[-1]->[1] and # tainted
5423                $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
5424              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
5425                                
5426                unless (length $token->{data}) {            unless (length $token->{data}) {
5427                  !!!cp ('t194');              !!!cp ('t194');
5428                  !!!next-token;              !!!next-token;
5429                  redo B;              next B;
5430                } else {            } else {
5431                  !!!cp ('t195');              !!!cp ('t195');
5432                }            }
5433              }          }
5434    
5435              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5436    
5437              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5438              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4182  sub _tree_construction_main ($) { Line 5440  sub _tree_construction_main ($) {
5440              ## result in a new Text node.              ## result in a new Text node.
5441              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5442                            
5443              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]}) {  
5444                # MUST                # MUST
5445                my $foster_parent_element;                my $foster_parent_element;
5446                my $next_sibling;                my $next_sibling;
5447                my $prev_sibling;                my $prev_sibling;
5448                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5449                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5450                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5451                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5452                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4219  sub _tree_construction_main ($) { Line 5474  sub _tree_construction_main ($) {
5474                    ($self->{document}->create_text_node ($token->{data}),                    ($self->{document}->create_text_node ($token->{data}),
5475                     $next_sibling);                     $next_sibling);
5476                }                }
5477              } else {            $open_tables->[-1]->[1] = 1; # tainted
5478                !!!cp ('t200');          } else {
5479                $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});            !!!cp ('t200');
5480              }            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5481            }
5482                            
5483              !!!next-token;          !!!next-token;
5484              redo B;          next B;
5485        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5486              if ({          if ({
5487                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5488                   th => 1, td => 1,               th => 1, td => 1,
5489                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5490                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5491                  ## Clear back to table context              ## Clear back to table context
5492                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5493                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5494                    !!!cp ('t201');                !!!cp ('t201');
5495                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                pop @{$self->{open_elements}};
5496                    pop @{$self->{open_elements}};              }
5497                  }              
5498                                !!!insert-element ('tbody',, $token);
5499                  !!!insert-element ('tbody');              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5500                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              ## reprocess in the "in table body" insertion mode...
5501                  ## reprocess in the "in table body" insertion mode...            }
5502                }            
5503              if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5504                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {              unless ($token->{tag_name} eq 'tr') {
5505                  unless ($token->{tag_name} eq 'tr') {                !!!cp ('t202');
5506                    !!!cp ('t202');                !!!parse-error (type => 'missing start tag:tr', token => $token);
5507                    !!!parse-error (type => 'missing start tag:tr');              }
                 }  
5508                                    
5509                  ## Clear back to table body context              ## Clear back to table body context
5510                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5511                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5512                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5513                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5514                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                pop @{$self->{open_elements}};
5515                    pop @{$self->{open_elements}};              }
                 }  
5516                                    
5517                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5518                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5519                    !!!cp ('t204');                    !!!cp ('t204');
5520                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5521                      !!!nack ('t204');
5522                    !!!next-token;                    !!!next-token;
5523                    redo B;                    next B;
5524                  } else {                  } else {
5525                    !!!cp ('t205');                    !!!cp ('t205');
5526                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5527                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5528                  }                  }
5529                } else {                } else {
# Line 4276  sub _tree_construction_main ($) { Line 5531  sub _tree_construction_main ($) {
5531                }                }
5532    
5533                ## Clear back to table row context                ## Clear back to table row context
5534                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5535                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5536                  !!!cp ('t207');                  !!!cp ('t207');
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5537                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5538                }                }
5539                                
5540                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5541                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5542    
5543                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5544                                
5545                  !!!nack ('t207.1');
5546                !!!next-token;                !!!next-token;
5547                redo B;                next B;
5548              } elsif ({              } elsif ({
5549                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5550                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4302  sub _tree_construction_main ($) { Line 5556  sub _tree_construction_main ($) {
5556                  my $i;                  my $i;
5557                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5558                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5559                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5560                      !!!cp ('t208');                      !!!cp ('t208');
5561                      $i = $_;                      $i = $_;
5562                      last INSCOPE;                      last INSCOPE;
5563                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5564                      !!!cp ('t209');                      !!!cp ('t209');
5565                      last INSCOPE;                      last INSCOPE;
5566                    }                    }
5567                  } # INSCOPE                  } # INSCOPE
5568                  unless (defined $i) {                  unless (defined $i) {
5569                   !!!cp ('t210');                    !!!cp ('t210');
5570                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});  ## TODO: This type is wrong.
5571                      !!!parse-error (type => 'unmacthed end tag',
5572                                      text => $token->{tag_name}, token => $token);
5573                    ## Ignore the token                    ## Ignore the token
5574                      !!!nack ('t210.1');
5575                    !!!next-token;                    !!!next-token;
5576                    redo B;                    next B;
5577                  }                  }
5578                                    
5579                  ## Clear back to table row context                  ## Clear back to table row context
5580                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5581                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5582                    !!!cp ('t211');                    !!!cp ('t211');
5583                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this case be reached?
5584                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5585                  }                  }
5586                                    
# Line 4335  sub _tree_construction_main ($) { Line 5589  sub _tree_construction_main ($) {
5589                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5590                    !!!cp ('t212');                    !!!cp ('t212');
5591                    ## reprocess                    ## reprocess
5592                    redo B;                    !!!ack-later;
5593                      next B;
5594                  } else {                  } else {
5595                    !!!cp ('t213');                    !!!cp ('t213');
5596                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4347  sub _tree_construction_main ($) { Line 5602  sub _tree_construction_main ($) {
5602                  my $i;                  my $i;
5603                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5604                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5605                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5606                      !!!cp ('t214');                      !!!cp ('t214');
5607                      $i = $_;                      $i = $_;
5608                      last INSCOPE;                      last INSCOPE;
5609                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5610                      !!!cp ('t215');                      !!!cp ('t215');
5611                      last INSCOPE;                      last INSCOPE;
5612                    }                    }
5613                  } # INSCOPE                  } # INSCOPE
5614                  unless (defined $i) {                  unless (defined $i) {
5615                    !!!cp ('t216');                    !!!cp ('t216');
5616                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  ## TODO: This erorr type is wrong.
5617                      !!!parse-error (type => 'unmatched end tag',
5618                                      text => $token->{tag_name}, token => $token);
5619                    ## Ignore the token                    ## Ignore the token
5620                      !!!nack ('t216.1');
5621                    !!!next-token;                    !!!next-token;
5622                    redo B;                    next B;
5623                  }                  }
5624    
5625                  ## Clear back to table body context                  ## Clear back to table body context
5626                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5627                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5628                    !!!cp ('t217');                    !!!cp ('t217');
5629                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5630                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5631                  }                  }
5632                                    
# Line 4393  sub _tree_construction_main ($) { Line 5646  sub _tree_construction_main ($) {
5646    
5647                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5648                  ## Clear back to table context                  ## Clear back to table context
5649                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5650                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5651                    !!!cp ('t219');                    !!!cp ('t219');
5652                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5653                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5654                  }                  }
5655                                    
5656                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5657                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5658                  ## reprocess                  ## reprocess
5659                  redo B;                  !!!ack-later;
5660                    next B;
5661                } elsif ({                } elsif ({
5662                          caption => 1,                          caption => 1,
5663                          colgroup => 1,                          colgroup => 1,
5664                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5665                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5666                  ## Clear back to table context                  ## Clear back to table context
5667                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5668                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5669                    !!!cp ('t220');                    !!!cp ('t220');
5670                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    ## ISSUE: Can this state be reached?
5671                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5672                  }                  }
5673                                    
5674                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5675                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5676                                    
5677                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5678                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5679                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5680                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4429  sub _tree_construction_main ($) { Line 5683  sub _tree_construction_main ($) {
5683                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5684                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5685                  !!!next-token;                  !!!next-token;
5686                  redo B;                  !!!nack ('t220.1');
5687                    next B;
5688                } else {                } else {
5689                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5690                }                }
5691              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5692                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5693                                  text => $self->{open_elements}->[-1]->[0]
5694                                      ->manakai_local_name,
5695                                  token => $token);
5696    
5697                ## As if </table>                ## As if </table>
5698                ## have a table element in table scope                ## have a table element in table scope
5699                my $i;                my $i;
5700                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5701                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5702                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5703                    !!!cp ('t221');                    !!!cp ('t221');
5704                    $i = $_;                    $i = $_;
5705                    last INSCOPE;                    last INSCOPE;
5706                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5707                    !!!cp ('t222');                    !!!cp ('t222');
5708                    last INSCOPE;                    last INSCOPE;
5709                  }                  }
5710                } # INSCOPE                } # INSCOPE
5711                unless (defined $i) {                unless (defined $i) {
5712                  !!!cp ('t223');                  !!!cp ('t223');
5713                  !!!parse-error (type => 'unmatched end tag:table');  ## TODO: The following is wrong, maybe.
5714                    !!!parse-error (type => 'unmatched end tag', text => 'table',
5715                                    token => $token);
5716                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5717                    !!!nack ('t223.1');
5718                  !!!next-token;                  !!!next-token;
5719                  redo B;                  next B;
5720                }                }
5721                                
5722    ## TODO: Followings are removed from the latest spec.
5723                ## generate implied end tags                ## generate implied end tags
5724                if ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
5725                  !!!cp ('t224');                  !!!cp ('t224');
5726                  !!!back-token; # <table>                  pop @{$self->{open_elements}};
                 $token = {type => END_TAG_TOKEN, tag_name => 'table'};  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
5727                }                }
5728    
5729                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5730                  !!!cp ('t225');                  !!!cp ('t225');
5731                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  ## NOTE: |<table><tr><table>|
5732                    !!!parse-error (type => 'not closed',
5733                                    text => $self->{open_elements}->[-1]->[0]
5734                                        ->manakai_local_name,
5735                                    token => $token);
5736                } else {                } else {
5737                  !!!cp ('t226');                  !!!cp ('t226');
5738                }                }
5739    
5740                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5741                  pop @{$open_tables};
5742    
5743                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5744    
5745                ## reprocess            ## reprocess
5746                redo B;            !!!ack-later;
5747              next B;
5748            } elsif ($token->{tag_name} eq 'style') {
5749              if (not $open_tables->[-1]->[1]) { # tainted
5750                !!!cp ('t227.8');
5751                ## NOTE: This is a "as if in head" code clone.
5752                $parse_rcdata->(CDATA_CONTENT_MODEL);
5753                next B;
5754              } else {
5755                !!!cp ('t227.7');
5756                #
5757              }
5758            } elsif ($token->{tag_name} eq 'script') {
5759              if (not $open_tables->[-1]->[1]) { # tainted
5760                !!!cp ('t227.6');
5761                ## NOTE: This is a "as if in head" code clone.
5762                $script_start_tag->();
5763                next B;
5764              } else {
5765                !!!cp ('t227.5');
5766                #
5767              }
5768            } elsif ($token->{tag_name} eq 'input') {
5769              if (not $open_tables->[-1]->[1]) { # tainted
5770                if ($token->{attributes}->{type}) { ## TODO: case
5771                  my $type = lc $token->{attributes}->{type}->{value};
5772                  if ($type eq 'hidden') {
5773                    !!!cp ('t227.3');
5774                    !!!parse-error (type => 'in table',
5775                                    text => $token->{tag_name}, token => $token);
5776    
5777                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5778    
5779                    ## TODO: form element pointer
5780    
5781                    pop @{$self->{open_elements}};
5782    
5783                    !!!next-token;
5784                    !!!ack ('t227.2.1');
5785                    next B;
5786                  } else {
5787                    !!!cp ('t227.2');
5788                    #
5789                  }
5790                } else {
5791                  !!!cp ('t227.1');
5792                  #
5793                }
5794              } else {
5795                !!!cp ('t227.4');
5796                #
5797              }
5798          } else {          } else {
5799            !!!cp ('t227');            !!!cp ('t227');
           !!!parse-error (type => 'in table:'.$token->{tag_name});  
   
           $insert = $insert_to_foster;  
5800            #            #
5801          }          }
5802    
5803            !!!parse-error (type => 'in table', text => $token->{tag_name},
5804                            token => $token);
5805    
5806            $insert = $insert_to_foster;
5807            #
5808        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
5809              if ($token->{tag_name} eq 'tr' and              if ($token->{tag_name} eq 'tr' and
5810                  $self->{insertion_mode} == IN_ROW_IM) {                  $self->{insertion_mode} == IN_ROW_IM) {
# Line 4502  sub _tree_construction_main ($) { Line 5812  sub _tree_construction_main ($) {
5812                my $i;                my $i;
5813                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5814                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5815                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5816                    !!!cp ('t228');                    !!!cp ('t228');
5817                    $i = $_;                    $i = $_;
5818                    last INSCOPE;                    last INSCOPE;
5819                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5820                    !!!cp ('t229');                    !!!cp ('t229');
5821                    last INSCOPE;                    last INSCOPE;
5822                  }                  }
5823                } # INSCOPE                } # INSCOPE
5824                unless (defined $i) {                unless (defined $i) {
5825                  !!!cp ('t230');                  !!!cp ('t230');
5826                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5827                                    text => $token->{tag_name}, token => $token);
5828                  ## Ignore the token                  ## Ignore the token
5829                    !!!nack ('t230.1');
5830                  !!!next-token;                  !!!next-token;
5831                  redo B;                  next B;
5832                } else {                } else {
5833                  !!!cp ('t232');                  !!!cp ('t232');
5834                }                }
5835    
5836                ## Clear back to table row context                ## Clear back to table row context
5837                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5838                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5839                  !!!cp ('t231');                  !!!cp ('t231');
5840                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5841                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5842                }                }
5843    
5844                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5845                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5846                !!!next-token;                !!!next-token;
5847                redo B;                !!!nack ('t231.1');
5848                  next B;
5849              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5850                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5851                  ## As if </tr>                  ## As if </tr>
# Line 4543  sub _tree_construction_main ($) { Line 5853  sub _tree_construction_main ($) {
5853                  my $i;                  my $i;
5854                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5855                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5856                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5857                      !!!cp ('t233');                      !!!cp ('t233');
5858                      $i = $_;                      $i = $_;
5859                      last INSCOPE;                      last INSCOPE;
5860                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5861                      !!!cp ('t234');                      !!!cp ('t234');
5862                      last INSCOPE;                      last INSCOPE;
5863                    }                    }
5864                  } # INSCOPE                  } # INSCOPE
5865                  unless (defined $i) {                  unless (defined $i) {
5866                    !!!cp ('t235');                    !!!cp ('t235');
5867                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});  ## TODO: The following is wrong.
5868                      !!!parse-error (type => 'unmatched end tag',
5869                                      text => $token->{type}, token => $token);
5870                    ## Ignore the token                    ## Ignore the token
5871                      !!!nack ('t236.1');
5872                    !!!next-token;                    !!!next-token;
5873                    redo B;                    next B;
5874                  }                  }
5875                                    
5876                  ## Clear back to table row context                  ## Clear back to table row context
5877                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5878                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5879                    !!!cp ('t236');                    !!!cp ('t236');
5880                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this state be reached?
5881                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5882                  }                  }
5883                                    
# Line 4581  sub _tree_construction_main ($) { Line 5891  sub _tree_construction_main ($) {
5891                  my $i;                  my $i;
5892                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5893                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5894                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5895                      !!!cp ('t237');                      !!!cp ('t237');
5896                      $i = $_;                      $i = $_;
5897                      last INSCOPE;                      last INSCOPE;
5898                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5899                      !!!cp ('t238');                      !!!cp ('t238');
5900                      last INSCOPE;                      last INSCOPE;
5901                    }                    }
5902                  } # INSCOPE                  } # INSCOPE
5903                  unless (defined $i) {                  unless (defined $i) {
5904                    !!!cp ('t239');                    !!!cp ('t239');
5905                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5906                                      text => $token->{tag_name}, token => $token);
5907                    ## Ignore the token                    ## Ignore the token
5908                      !!!nack ('t239.1');
5909                    !!!next-token;                    !!!next-token;
5910                    redo B;                    next B;
5911                  }                  }
5912                                    
5913                  ## Clear back to table body context                  ## Clear back to table body context
5914                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5915                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5916                    !!!cp ('t240');                    !!!cp ('t240');
                   !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
5917                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5918                  }                  }
5919                                    
# Line 4623  sub _tree_construction_main ($) { Line 5929  sub _tree_construction_main ($) {
5929                  ## reprocess in the "in table" insertion mode...                  ## reprocess in the "in table" insertion mode...
5930                }                }
5931    
5932                  ## NOTE: </table> in the "in table" insertion mode.
5933                  ## When you edit the code fragment below, please ensure that
5934                  ## the code for <table> in the "in table" insertion mode
5935                  ## is synced with it.
5936    
5937                ## have a table element in table scope                ## have a table element in table scope
5938                my $i;                my $i;
5939                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5940                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5941                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5942                    !!!cp ('t241');                    !!!cp ('t241');
5943                    $i = $_;                    $i = $_;
5944                    last INSCOPE;                    last INSCOPE;
5945                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5946                    !!!cp ('t242');                    !!!cp ('t242');
5947                    last INSCOPE;                    last INSCOPE;
5948                  }                  }
5949                } # INSCOPE                } # INSCOPE
5950                unless (defined $i) {                unless (defined $i) {
5951                  !!!cp ('t243');                  !!!cp ('t243');
5952                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5953                                    text => $token->{tag_name}, token => $token);
5954                  ## Ignore the token                  ## Ignore the token
5955                    !!!nack ('t243.1');
5956                  !!!next-token;                  !!!next-token;
5957                  redo B;                  next B;
               }  
   
               ## generate implied end tags  
               if ({  
                    dd => 1, dt => 1, li => 1, p => 1,  
                    td => 1, th => 1, tr => 1,  
                    tbody => 1, tfoot=> 1, thead => 1,  
                   }->{$self->{open_elements}->[-1]->[1]}) {  
                 !!!cp ('t244');  
                 !!!back-token;  
                 $token = {type => END_TAG_TOKEN,  
                           tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
                 redo B;  
               }  
                 
               if ($self->{open_elements}->[-1]->[1] ne 'table') {  
                 !!!cp ('t245');  
                 !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
               } else {  
                 !!!cp ('t246');  
5958                }                }
5959                                    
5960                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
5961                  pop @{$open_tables};
5962                                
5963                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5964                                
5965                !!!next-token;                !!!next-token;
5966                redo B;                next B;
5967              } elsif ({              } elsif ({
5968                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5969                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4681  sub _tree_construction_main ($) { Line 5973  sub _tree_construction_main ($) {
5973                  my $i;                  my $i;
5974                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5975                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5976                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5977                      !!!cp ('t247');                      !!!cp ('t247');
5978                      $i = $_;                      $i = $_;
5979                      last INSCOPE;                      last INSCOPE;
5980                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5981                      !!!cp ('t248');                      !!!cp ('t248');
5982                      last INSCOPE;                      last INSCOPE;
5983                    }                    }
5984                  } # INSCOPE                  } # INSCOPE
5985                    unless (defined $i) {                    unless (defined $i) {
5986                      !!!cp ('t249');                      !!!cp ('t249');
5987                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5988                                        text => $token->{tag_name}, token => $token);
5989                      ## Ignore the token                      ## Ignore the token
5990                        !!!nack ('t249.1');
5991                      !!!next-token;                      !!!next-token;
5992                      redo B;                      next B;
5993                    }                    }
5994                                    
5995                  ## As if </tr>                  ## As if </tr>
# Line 4705  sub _tree_construction_main ($) { Line 5997  sub _tree_construction_main ($) {
5997                  my $i;                  my $i;
5998                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5999                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6000                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6001                      !!!cp ('t250');                      !!!cp ('t250');
6002                      $i = $_;                      $i = $_;
6003                      last INSCOPE;                      last INSCOPE;
6004                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6005                      !!!cp ('t251');                      !!!cp ('t251');
6006                      last INSCOPE;                      last INSCOPE;
6007                    }                    }
6008                  } # INSCOPE                  } # INSCOPE
6009                    unless (defined $i) {                    unless (defined $i) {
6010                      !!!cp ('t252');                      !!!cp ('t252');
6011                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
6012                                        text => 'tr', token => $token);
6013                      ## Ignore the token                      ## Ignore the token
6014                        !!!nack ('t252.1');
6015                      !!!next-token;                      !!!next-token;
6016                      redo B;                      next B;
6017                    }                    }
6018                                    
6019                  ## Clear back to table row context                  ## Clear back to table row context
6020                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6021                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6022                    !!!cp ('t253');                    !!!cp ('t253');
6023                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6024                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
6025                  }                  }
6026                                    
# Line 4742  sub _tree_construction_main ($) { Line 6033  sub _tree_construction_main ($) {
6033                my $i;                my $i;
6034                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6035                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6036                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6037                    !!!cp ('t254');                    !!!cp ('t254');
6038                    $i = $_;                    $i = $_;
6039                    last INSCOPE;                    last INSCOPE;
6040                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6041                    !!!cp ('t255');                    !!!cp ('t255');
6042                    last INSCOPE;                    last INSCOPE;
6043                  }                  }
6044                } # INSCOPE                } # INSCOPE
6045                unless (defined $i) {                unless (defined $i) {
6046                  !!!cp ('t256');                  !!!cp ('t256');
6047                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6048                                    text => $token->{tag_name}, token => $token);
6049                  ## Ignore the token                  ## Ignore the token
6050                    !!!nack ('t256.1');
6051                  !!!next-token;                  !!!next-token;
6052                  redo B;                  next B;
6053                }                }
6054    
6055                ## Clear back to table body context                ## Clear back to table body context
6056                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6057                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
6058                  !!!cp ('t257');                  !!!cp ('t257');
6059                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  ## ISSUE: Can this case be reached?
6060                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
6061                }                }
6062    
6063                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6064                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6065                  !!!nack ('t257.1');
6066                !!!next-token;                !!!next-token;
6067                redo B;                next B;
6068              } elsif ({              } elsif ({
6069                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6070                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6071                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6072                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6073                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6074                !!!cp ('t258');            !!!cp ('t258');
6075                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6076                ## Ignore the token                            text => $token->{tag_name}, token => $token);
6077                !!!next-token;            ## Ignore the token
6078                redo B;            !!!nack ('t258.1');
6079               !!!next-token;
6080              next B;
6081          } else {          } else {
6082            !!!cp ('t259');            !!!cp ('t259');
6083            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
6084                              text => $token->{tag_name}, token => $token);
6085    
6086            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6087            #            #
6088          }          }
6089          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6090            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6091                    @{$self->{open_elements}} == 1) { # redundant, maybe
6092              !!!parse-error (type => 'in body:#eof', token => $token);
6093              !!!cp ('t259.1');
6094              #
6095            } else {
6096              !!!cp ('t259.2');
6097              #
6098            }
6099    
6100            ## Stop parsing
6101            last B;
6102        } else {        } else {
6103          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6104        }        }
# Line 4802  sub _tree_construction_main ($) { Line 6109  sub _tree_construction_main ($) {
6109                unless (length $token->{data}) {                unless (length $token->{data}) {
6110                  !!!cp ('t260');                  !!!cp ('t260');
6111                  !!!next-token;                  !!!next-token;
6112                  redo B;                  next B;
6113                }                }
6114              }              }
6115                            
# Line 4811  sub _tree_construction_main ($) { Line 6118  sub _tree_construction_main ($) {
6118            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6119              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6120                !!!cp ('t262');                !!!cp ('t262');
6121                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6122                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6123                  !!!ack ('t262.1');
6124                !!!next-token;                !!!next-token;
6125                redo B;                next B;
6126              } else {              } else {
6127                !!!cp ('t263');                !!!cp ('t263');
6128                #                #
6129              }              }
6130            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6131              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6132                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6133                  !!!cp ('t264');                  !!!cp ('t264');
6134                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
6135                                    text => 'colgroup', token => $token);
6136                  ## Ignore the token                  ## Ignore the token
6137                  !!!next-token;                  !!!next-token;
6138                  redo B;                  next B;
6139                } else {                } else {
6140                  !!!cp ('t265');                  !!!cp ('t265');
6141                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6142                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6143                  !!!next-token;                  !!!next-token;
6144                  redo B;                              next B;            
6145                }                }
6146              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6147                !!!cp ('t266');                !!!cp ('t266');
6148                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
6149                                  text => 'col', token => $token);
6150                ## Ignore the token                ## Ignore the token
6151                !!!next-token;                !!!next-token;
6152                redo B;                next B;
6153              } else {              } else {
6154                !!!cp ('t267');                !!!cp ('t267');
6155                #                #
6156              }              }
6157            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6158              !!!cp ('t268');          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6159              #              @{$self->{open_elements}} == 1) { # redundant, maybe
6160            }            !!!cp ('t270.2');
6161              ## Stop parsing.
6162              last B;
6163            } else {
6164              ## NOTE: As if </colgroup>.
6165              !!!cp ('t270.1');
6166              pop @{$self->{open_elements}}; # colgroup
6167              $self->{insertion_mode} = IN_TABLE_IM;
6168              ## Reprocess.
6169              next B;
6170            }
6171          } else {
6172            die "$0: $token->{type}: Unknown token type";
6173          }
6174    
6175            ## As if </colgroup>            ## As if </colgroup>
6176            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6177              !!!cp ('t269');              !!!cp ('t269');
6178              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
6179                !!!parse-error (type => 'unmatched end tag',
6180                                text => 'colgroup', token => $token);
6181              ## Ignore the token              ## Ignore the token
6182                !!!nack ('t269.1');
6183              !!!next-token;              !!!next-token;
6184              redo B;              next B;
6185            } else {            } else {
6186              !!!cp ('t270');              !!!cp ('t270');
6187              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6188              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6189                !!!ack-later;
6190              ## reprocess              ## reprocess
6191              redo B;              next B;
6192            }            }
6193      } elsif ($self->{insertion_mode} == IN_SELECT_IM) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6194        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6195          !!!cp ('t271');          !!!cp ('t271');
6196          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6197          !!!next-token;          !!!next-token;
6198          redo B;          next B;
6199        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6200              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6201                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6202                  !!!cp ('t272');              !!!cp ('t272');
6203                  ## As if </option>              ## As if </option>
6204                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6205                } else {            } else {
6206                  !!!cp ('t273');              !!!cp ('t273');
6207                }            }
6208    
6209                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6210                !!!next-token;            !!!nack ('t273.1');
6211                redo B;            !!!next-token;
6212              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6213                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6214                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6215                  ## As if </option>              !!!cp ('t274');
6216                  pop @{$self->{open_elements}};              ## As if </option>
6217                } else {              pop @{$self->{open_elements}};
6218                  !!!cp ('t275');            } else {
6219                }              !!!cp ('t275');
6220              }
6221    
6222                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6223                  !!!cp ('t276');              !!!cp ('t276');
6224                  ## As if </optgroup>              ## As if </optgroup>
6225                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6226                } else {            } else {
6227                  !!!cp ('t277');              !!!cp ('t277');
6228                }            }
6229    
6230                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6231                !!!next-token;            !!!nack ('t277.1');
6232                redo B;            !!!next-token;
6233              } elsif ($token->{tag_name} eq 'select') {            next B;
6234                !!!parse-error (type => 'not closed:select');          } elsif ({
6235                ## As if </select> instead                     select => 1, input => 1, textarea => 1,
6236                ## have an element in table scope                   }->{$token->{tag_name}} or
6237                my $i;                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6238                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                    {
6239                  my $node = $self->{open_elements}->[$_];                     caption => 1, table => 1,
6240                  if ($node->[1] eq $token->{tag_name}) {                     tbody => 1, tfoot => 1, thead => 1,
6241                    !!!cp ('t278');                     tr => 1, td => 1, th => 1,
6242                    $i = $_;                    }->{$token->{tag_name}})) {
6243                    last INSCOPE;            ## TODO: The type below is not good - <select> is replaced by </select>
6244                  } elsif ({            !!!parse-error (type => 'not closed', text => 'select',
6245                            table => 1, html => 1,                            token => $token);
6246                           }->{$node->[1]}) {            ## NOTE: As if the token were </select> (<select> case) or
6247                    !!!cp ('t279');            ## as if there were </select> (otherwise).
6248                    last INSCOPE;            ## have an element in table scope
6249                  }            my $i;
6250                } # INSCOPE            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6251                unless (defined $i) {              my $node = $self->{open_elements}->[$_];
6252                  !!!cp ('t280');              if ($node->[1] & SELECT_EL) {
6253                  !!!parse-error (type => 'unmatched end tag:select');                !!!cp ('t278');
6254                  ## Ignore the token                $i = $_;
6255                  !!!next-token;                last INSCOPE;
6256                  redo B;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6257                }                !!!cp ('t279');
6258                  last INSCOPE;
6259                }
6260              } # INSCOPE
6261              unless (defined $i) {
6262                !!!cp ('t280');
6263                !!!parse-error (type => 'unmatched end tag',
6264                                text => 'select', token => $token);
6265                ## Ignore the token
6266                !!!nack ('t280.1');
6267                !!!next-token;
6268                next B;
6269              }
6270                                
6271                !!!cp ('t281');            !!!cp ('t281');
6272                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6273    
6274                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6275    
6276                !!!next-token;            if ($token->{tag_name} eq 'select') {
6277                redo B;              !!!nack ('t281.2');
6278                !!!next-token;
6279                next B;
6280              } else {
6281                !!!cp ('t281.1');
6282                !!!ack-later;
6283                ## Reprocess the token.
6284                next B;
6285              }
6286          } else {          } else {
6287            !!!cp ('t282');            !!!cp ('t282');
6288            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
6289                              text => $token->{tag_name}, token => $token);
6290            ## Ignore the token            ## Ignore the token
6291              !!!nack ('t282.1');
6292            !!!next-token;            !!!next-token;
6293            redo B;            next B;
6294          }          }
6295        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6296              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6297                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6298                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6299                  !!!cp ('t283');              !!!cp ('t283');
6300                  ## As if </option>              ## As if </option>
6301                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6302                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6303                  !!!cp ('t284');              !!!cp ('t284');
6304                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6305                } else {            } else {
6306                  !!!cp ('t285');              !!!cp ('t285');
6307                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6308                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6309                }              ## Ignore the token
6310                !!!next-token;            }
6311                redo B;            !!!nack ('t285.1');
6312              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6313                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6314                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6315                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6316                } else {              !!!cp ('t286');
6317                  !!!cp ('t287');              pop @{$self->{open_elements}};
6318                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
6319                  ## Ignore the token              !!!cp ('t287');
6320                }              !!!parse-error (type => 'unmatched end tag',
6321                !!!next-token;                              text => $token->{tag_name}, token => $token);
6322                redo B;              ## Ignore the token
6323              } elsif ($token->{tag_name} eq 'select') {            }
6324                ## have an element in table scope            !!!nack ('t287.1');
6325                my $i;            !!!next-token;
6326                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6327                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6328                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6329                    !!!cp ('t288');            my $i;
6330                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6331                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6332                  } elsif ({              if ($node->[1] & SELECT_EL) {
6333                            table => 1, html => 1,                !!!cp ('t288');
6334                           }->{$node->[1]}) {                $i = $_;
6335                    !!!cp ('t289');                last INSCOPE;
6336                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6337                  }                !!!cp ('t289');
6338                } # INSCOPE                last INSCOPE;
6339                unless (defined $i) {              }
6340                  !!!cp ('t290');            } # INSCOPE
6341                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
6342                  ## Ignore the token              !!!cp ('t290');
6343                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6344                  redo B;                              text => $token->{tag_name}, token => $token);
6345                }              ## Ignore the token
6346                !!!nack ('t290.1');
6347                !!!next-token;
6348                next B;
6349              }
6350                                
6351                !!!cp ('t291');            !!!cp ('t291');
6352                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6353    
6354                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6355    
6356                !!!next-token;            !!!nack ('t291.1');
6357                redo B;            !!!next-token;
6358              } elsif ({            next B;
6359                        caption => 1, table => 1, tbody => 1,          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6360                        tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                   {
6361                       }->{$token->{tag_name}}) {                    caption => 1, table => 1, tbody => 1,
6362                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6363                     }->{$token->{tag_name}}) {
6364    ## TODO: The following is wrong?
6365              !!!parse-error (type => 'unmatched end tag',
6366                              text => $token->{tag_name}, token => $token);
6367                                
6368                ## have an element in table scope            ## have an element in table scope
6369                my $i;            my $i;
6370                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6371                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6372                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6373                    !!!cp ('t292');                !!!cp ('t292');
6374                    $i = $_;                $i = $_;
6375                    last INSCOPE;                last INSCOPE;
6376                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6377                            table => 1, html => 1,                !!!cp ('t293');
6378                           }->{$node->[1]}) {                last INSCOPE;
6379                    !!!cp ('t293');              }
6380                    last INSCOPE;            } # INSCOPE
6381                  }            unless (defined $i) {
6382                } # INSCOPE              !!!cp ('t294');
6383                unless (defined $i) {              ## Ignore the token
6384                  !!!cp ('t294');              !!!nack ('t294.1');
6385                  ## Ignore the token              !!!next-token;
6386                  !!!next-token;              next B;
6387                  redo B;            }
               }  
6388                                
6389                ## As if </select>            ## As if </select>
6390                ## have an element in table scope            ## have an element in table scope
6391                undef $i;            undef $i;
6392                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6393                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6394                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6395                    !!!cp ('t295');                !!!cp ('t295');
6396                    $i = $_;                $i = $_;
6397                    last INSCOPE;                last INSCOPE;
6398                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6399                            table => 1, html => 1,  ## ISSUE: Can this state be reached?
6400                           }->{$node->[1]}) {                !!!cp ('t296');
6401                    !!!cp ('t296');                last INSCOPE;
6402                    last INSCOPE;              }
6403                  }            } # INSCOPE
6404                } # INSCOPE            unless (defined $i) {
6405                unless (defined $i) {              !!!cp ('t297');
6406                  !!!cp ('t297');  ## TODO: The following error type is correct?
6407                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
6408                  ## Ignore the </select> token                              text => 'select', token => $token);
6409                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6410                  redo B;              !!!nack ('t297.1');
6411                }              !!!next-token; ## TODO: ok?
6412                next B;
6413              }
6414                                
6415                !!!cp ('t298');            !!!cp ('t298');
6416                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6417    
6418                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6419    
6420                ## reprocess            !!!ack-later;
6421                redo B;            ## reprocess
6422              next B;
6423          } else {          } else {
6424            !!!cp ('t299');            !!!cp ('t299');
6425            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
6426                              text => $token->{tag_name}, token => $token);
6427            ## Ignore the token            ## Ignore the token
6428              !!!nack ('t299.3');
6429            !!!next-token;            !!!next-token;
6430            redo B;            next B;
6431            }
6432          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6433            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6434                    @{$self->{open_elements}} == 1) { # redundant, maybe
6435              !!!cp ('t299.1');
6436              !!!parse-error (type => 'in body:#eof', token => $token);
6437            } else {
6438              !!!cp ('t299.2');
6439          }          }
6440    
6441            ## Stop parsing.
6442            last B;
6443        } else {        } else {
6444          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6445        }        }
# Line 5082  sub _tree_construction_main ($) { Line 6455  sub _tree_construction_main ($) {
6455            unless (length $token->{data}) {            unless (length $token->{data}) {
6456              !!!cp ('t300');              !!!cp ('t300');
6457              !!!next-token;              !!!next-token;
6458              redo B;              next B;
6459            }            }
6460          }          }
6461                    
6462          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6463            !!!cp ('t301');            !!!cp ('t301');
6464            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
6465    
6466            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6467          } else {          } else {
6468            !!!cp ('t302');            !!!cp ('t302');
6469          }          }
6470                    
6471          ## "after body" insertion mode          ## "after body" insertion mode
6472          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6473    
6474          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6475          ## reprocess          ## reprocess
6476          redo B;          next B;
6477        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6478          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6479            !!!cp ('t303');            !!!cp ('t303');
6480            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6481                              text => $token->{tag_name}, token => $token);
6482                        
6483            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6484          } else {          } else {
6485            !!!cp ('t304');            !!!cp ('t304');
6486          }          }
6487    
6488          ## "after body" insertion mode          ## "after body" insertion mode
6489          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6490                            text => $token->{tag_name}, token => $token);
6491    
6492          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6493            !!!ack-later;
6494          ## reprocess          ## reprocess
6495          redo B;          next B;
6496        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6497          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6498            !!!cp ('t305');            !!!cp ('t305');
6499            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6500                              text => $token->{tag_name}, token => $token);
6501                        
6502            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6503            ## Reprocess in the "main" phase, "after body" insertion mode...            ## Reprocess in the "after body" insertion mode.
6504          } else {          } else {
6505            !!!cp ('t306');            !!!cp ('t306');
6506          }          }
# Line 5132  sub _tree_construction_main ($) { Line 6509  sub _tree_construction_main ($) {
6509          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6510            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6511              !!!cp ('t307');              !!!cp ('t307');
6512              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
6513                                text => 'html', token => $token);
6514              ## Ignore the token              ## Ignore the token
6515              !!!next-token;              !!!next-token;
6516              redo B;              next B;
6517            } else {            } else {
6518              !!!cp ('t308');              !!!cp ('t308');
6519              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6520              !!!next-token;              !!!next-token;
6521              redo B;              next B;
6522            }            }
6523          } else {          } else {
6524            !!!cp ('t309');            !!!cp ('t309');
6525            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
6526                              text => $token->{tag_name}, token => $token);
6527    
6528            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6529            ## reprocess            ## reprocess
6530            redo B;            next B;
6531          }          }
6532          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6533            !!!cp ('t309.2');
6534            ## Stop parsing
6535            last B;
6536        } else {        } else {
6537          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6538        }        }
# Line 5161  sub _tree_construction_main ($) { Line 6544  sub _tree_construction_main ($) {
6544            unless (length $token->{data}) {            unless (length $token->{data}) {
6545              !!!cp ('t310');              !!!cp ('t310');
6546              !!!next-token;              !!!next-token;
6547              redo B;              next B;
6548            }            }
6549          }          }
6550                    
6551          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6552            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6553              !!!cp ('t311');              !!!cp ('t311');
6554              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
6555            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6556              !!!cp ('t312');              !!!cp ('t312');
6557              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6558            } else { # "after html frameset"            } else { # "after after frameset"
6559              !!!cp ('t313');              !!!cp ('t313');
6560              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "main" phase, "after frameset"...  
             !!!parse-error (type => 'after frameset:#character');  
6561            }            }
6562                        
6563            ## Ignore the token.            ## Ignore the token.
# Line 5189  sub _tree_construction_main ($) { Line 6568  sub _tree_construction_main ($) {
6568              !!!cp ('t315');              !!!cp ('t315');
6569              !!!next-token;              !!!next-token;
6570            }            }
6571            redo B;            next B;
6572          }          }
6573                    
6574          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6575        } 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 "main" phase, "after frameset" insertion mode...  
         } else {  
           !!!cp ('t317');  
         }  
   
6576          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6577              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6578            !!!cp ('t318');            !!!cp ('t318');
6579            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6580              !!!nack ('t318.1');
6581            !!!next-token;            !!!next-token;
6582            redo B;            next B;
6583          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6584                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6585            !!!cp ('t319');            !!!cp ('t319');
6586            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6587            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6588              !!!ack ('t319.1');
6589            !!!next-token;            !!!next-token;
6590            redo B;            next B;
6591          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6592            !!!cp ('t320');            !!!cp ('t320');
6593            ## NOTE: As if in body.            ## NOTE: As if in head.
6594            $parse_rcdata->(CDATA_CONTENT_MODEL, $insert_to_current);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6595            redo B;            next B;
6596    
6597              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6598              ## has no parse error.
6599          } else {          } else {
6600            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6601              !!!cp ('t321');              !!!cp ('t321');
6602              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
6603            } else {                              text => $token->{tag_name}, token => $token);
6604              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6605              !!!cp ('t322');              !!!cp ('t322');
6606              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
6607                                text => $token->{tag_name}, token => $token);
6608              } else { # "after after frameset"
6609                !!!cp ('t322.2');
6610                !!!parse-error (type => 'after after frameset',
6611                                text => $token->{tag_name}, token => $token);
6612            }            }
6613            ## Ignore the token            ## Ignore the token
6614              !!!nack ('t322.1');
6615            !!!next-token;            !!!next-token;
6616            redo B;            next B;
6617          }          }
6618        } 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 "main" phase, "after frameset" insertion mode...  
         } else {  
           !!!cp ('t324');  
         }  
   
6619          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6620              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6621            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6622                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6623              !!!cp ('t325');              !!!cp ('t325');
6624              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6625                                text => $token->{tag_name}, token => $token);
6626              ## Ignore the token              ## Ignore the token
6627              !!!next-token;              !!!next-token;
6628            } else {            } else {
# Line 5260  sub _tree_construction_main ($) { Line 6632  sub _tree_construction_main ($) {
6632            }            }
6633    
6634            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6635                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6636              !!!cp ('t327');              !!!cp ('t327');
6637              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6638            } else {            } else {
6639              !!!cp ('t328');              !!!cp ('t328');
6640            }            }
6641            redo B;            next B;
6642          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6643                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6644            !!!cp ('t329');            !!!cp ('t329');
6645            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6646            !!!next-token;            !!!next-token;
6647            redo B;            next B;
6648          } else {          } else {
6649            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6650              !!!cp ('t330');              !!!cp ('t330');
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 ('t330.1');
6655                !!!parse-error (type => 'after frameset:/',
6656                                text => $token->{tag_name}, token => $token);
6657              } else { # "after after html"
6658              !!!cp ('t331');              !!!cp ('t331');
6659              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after after frameset:/',
6660                                text => $token->{tag_name}, token => $token);
6661            }            }
6662            ## Ignore the token            ## Ignore the token
6663            !!!next-token;            !!!next-token;
6664            redo B;            next B;
6665          }          }
6666          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6667            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6668                    @{$self->{open_elements}} == 1) { # redundant, maybe
6669              !!!cp ('t331.1');
6670              !!!parse-error (type => 'in body:#eof', token => $token);
6671            } else {
6672              !!!cp ('t331.2');
6673            }
6674            
6675            ## Stop parsing
6676            last B;
6677        } else {        } else {
6678          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6679        }        }
# Line 5299  sub _tree_construction_main ($) { Line 6688  sub _tree_construction_main ($) {
6688        if ($token->{tag_name} eq 'script') {        if ($token->{tag_name} eq 'script') {
6689          !!!cp ('t332');          !!!cp ('t332');
6690          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6691          $script_start_tag->($insert);          $script_start_tag->();
6692          redo B;          next B;
6693        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6694          !!!cp ('t333');          !!!cp ('t333');
6695          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6696          $parse_rcdata->(CDATA_CONTENT_MODEL, $insert);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6697          redo B;          next B;
6698        } elsif ({        } elsif ({
6699                  base => 1, link => 1,                  base => 1, link => 1,
6700                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6701          !!!cp ('t334');          !!!cp ('t334');
6702          ## 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
6703          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6704          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6705            !!!ack ('t334.1');
6706          !!!next-token;          !!!next-token;
6707          redo B;          next B;
6708        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6709          ## 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
6710          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6711          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.
6712    
6713          unless ($self->{confident}) {          unless ($self->{confident}) {
6714            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6715              !!!cp ('t335');              !!!cp ('t335');
6716                ## NOTE: Whether the encoding is supported or not is handled
6717                ## in the {change_encoding} callback.
6718              $self->{change_encoding}              $self->{change_encoding}
6719                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6720                            
6721              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6722                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6723                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6724                                           ->{has_reference});                                           ->{has_reference});
6725            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6726              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6727                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6728                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6729                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6730                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6731                !!!cp ('t336');                !!!cp ('t336');
6732                  ## NOTE: Whether the encoding is supported or not is handled
6733                  ## in the {change_encoding} callback.
6734                $self->{change_encoding}                $self->{change_encoding}
6735                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6736                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6737                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6738                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5363  sub _tree_construction_main ($) { Line 6756  sub _tree_construction_main ($) {
6756            }            }
6757          }          }
6758    
6759            !!!ack ('t338.1');
6760          !!!next-token;          !!!next-token;
6761          redo B;          next B;
6762        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6763          !!!cp ('t341');          !!!cp ('t341');
         !!!parse-error (type => 'in body:title');  
6764          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6765          $parse_rcdata->(RCDATA_CONTENT_MODEL, sub {          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6766            if (defined $self->{head_element}) {          next B;
             !!!cp ('t339');  
             $self->{head_element}->append_child ($_[0]);  
           } else {  
             !!!cp ('t340');  
             $insert->($_[0]);  
           }  
         });  
         redo B;  
6767        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6768          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6769                                
6770          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6771              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6772            !!!cp ('t342');            !!!cp ('t342');
6773            ## Ignore the token            ## Ignore the token
6774          } else {          } else {
# Line 5397  sub _tree_construction_main ($) { Line 6782  sub _tree_construction_main ($) {
6782              }              }
6783            }            }
6784          }          }
6785            !!!nack ('t343.1');
6786          !!!next-token;          !!!next-token;
6787          redo B;          next B;
6788        } elsif ({        } elsif ({
6789                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6790                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1,
6791                    h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6792                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6793                  pre => 1,                  pre => 1, listing => 1,
6794                    form => 1,
6795                    table => 1,
6796                    hr => 1,
6797                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6798            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6799              !!!cp ('t350');
6800              !!!parse-error (type => 'in form:form', token => $token);
6801              ## Ignore the token
6802              !!!nack ('t350.1');
6803              !!!next-token;
6804              next B;
6805            }
6806    
6807          ## has a p element in scope          ## has a p element in scope
6808          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6809            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6810              !!!cp ('t344');              !!!cp ('t344');
6811              !!!back-token;              !!!back-token; # <form>
6812              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6813              redo B;                        line => $token->{line}, column => $token->{column}};
6814            } elsif ({              next B;
6815                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6816              !!!cp ('t345');              !!!cp ('t345');
6817              last INSCOPE;              last INSCOPE;
6818            }            }
6819          } # INSCOPE          } # INSCOPE
6820                        
6821          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6822          if ($token->{tag_name} eq 'pre') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6823              !!!nack ('t346.1');
6824            !!!next-token;            !!!next-token;
6825            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6826              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5435  sub _tree_construction_main ($) { Line 6833  sub _tree_construction_main ($) {
6833            } else {            } else {
6834              !!!cp ('t348');              !!!cp ('t348');
6835            }            }
6836          } else {          } elsif ($token->{tag_name} eq 'form') {
6837            !!!cp ('t347');            !!!cp ('t347.1');
6838              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6839    
6840              !!!nack ('t347.2');
6841            !!!next-token;            !!!next-token;
6842          }          } elsif ($token->{tag_name} eq 'table') {
6843          redo B;            !!!cp ('t382');
6844        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6845          if (defined $self->{form_element}) {            
6846            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6847            !!!parse-error (type => 'in form:form');  
6848            ## Ignore the token            !!!nack ('t382.1');
6849              !!!next-token;
6850            } elsif ($token->{tag_name} eq 'hr') {
6851              !!!cp ('t386');
6852              pop @{$self->{open_elements}};
6853            
6854              !!!nack ('t386.1');
6855            !!!next-token;            !!!next-token;
           redo B;  
6856          } else {          } else {
6857            ## 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];  
6858            !!!next-token;            !!!next-token;
           redo B;  
6859          }          }
6860        } elsif ($token->{tag_name} eq 'li') {          next B;
6861          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6862          ## has a p element in scope          ## has a p element in scope
6863          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6864            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6865              !!!cp ('t353');              !!!cp ('t353');
6866              !!!back-token;              !!!back-token; # <x>
6867              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6868              redo B;                        line => $token->{line}, column => $token->{column}};
6869            } elsif ({              next B;
6870                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6871              !!!cp ('t354');              !!!cp ('t354');
6872              last INSCOPE;              last INSCOPE;
6873            }            }
# Line 5489  sub _tree_construction_main ($) { Line 6876  sub _tree_construction_main ($) {
6876          ## Step 1          ## Step 1
6877          my $i = -1;          my $i = -1;
6878          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6879            my $li_or_dtdd = {li => {li => 1},
6880                              dt => {dt => 1, dd => 1},
6881                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6882          LI: {          LI: {
6883            ## Step 2            ## Step 2
6884            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6885              if ($i != -1) {              if ($i != -1) {
6886                !!!cp ('t355');                !!!cp ('t355');
6887                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6888                                $self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
6889                                      ->manakai_local_name,
6890                                  token => $token);
6891              } else {              } else {
6892                !!!cp ('t356');                !!!cp ('t356');
6893              }              }
# Line 5506  sub _tree_construction_main ($) { Line 6898  sub _tree_construction_main ($) {
6898            }            }
6899                        
6900            ## Step 3            ## Step 3
6901            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6902                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6903                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6904                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6905                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6906                  not ($node->[1] & DIV_EL)) {
6907              !!!cp ('t358');              !!!cp ('t358');
6908              last LI;              last LI;
6909            }            }
# Line 5522  sub _tree_construction_main ($) { Line 6915  sub _tree_construction_main ($) {
6915            redo LI;            redo LI;
6916          } # LI          } # LI
6917                        
6918          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6919          !!!next-token;          !!!nack ('t359.1');
         redo B;  
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
6920          !!!next-token;          !!!next-token;
6921          redo B;          next B;
6922        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6923          ## has a p element in scope          ## has a p element in scope
6924          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6925            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6926              !!!cp ('t367');              !!!cp ('t367');
6927              !!!back-token;              !!!back-token; # <plaintext>
6928              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6929              redo B;                        line => $token->{line}, column => $token->{column}};
6930            } elsif ({              next B;
6931                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6932              !!!cp ('t368');              !!!cp ('t368');
6933              last INSCOPE;              last INSCOPE;
6934            }            }
6935          } # INSCOPE          } # INSCOPE
6936                        
6937          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6938                        
6939          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6940                        
6941            !!!nack ('t368.1');
6942          !!!next-token;          !!!next-token;
6943          redo B;          next B;
       } elsif ({  
                 h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
                }->{$token->{tag_name}}) {  
         ## has a p element in scope  
         INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
           my $node = $self->{open_elements}->[$_];  
           if ($node->[1] eq 'p') {  
             !!!cp ('t369');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
             !!!cp ('t370');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## NOTE: See <http://html5.org/tools/web-apps-tracker?from=925&to=926>  
         ## has an element in scope  
         #my $i;  
         #INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
         #  my $node = $self->{open_elements}->[$_];  
         #  if ({  
         #       h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
         #      }->{$node->[1]}) {  
         #    $i = $_;  
         #    last INSCOPE;  
         #  } elsif ({  
         #            table => 1, caption => 1, td => 1, th => 1,  
         #            button => 1, marquee => 1, object => 1, html => 1,  
         #           }->{$node->[1]}) {  
         #    last INSCOPE;  
         #  }  
         #} # INSCOPE  
         #    
         #if (defined $i) {  
         #  !!! parse-error (type => 'in hn:hn');  
         #  splice @{$self->{open_elements}}, $i;  
         #}  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
             
         !!!next-token;  
         redo B;  
6944        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6945          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6946            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6947            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6948              !!!cp ('t371');              !!!cp ('t371');
6949              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6950                            
6951              !!!back-token;              !!!back-token; # <a>
6952              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6953              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6954                $formatting_end_tag->($token);
6955                            
6956              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6957                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5685  sub _tree_construction_main ($) { Line 6976  sub _tree_construction_main ($) {
6976                        
6977          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6978    
6979          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6980          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6981    
6982            !!!nack ('t374.1');
6983          !!!next-token;          !!!next-token;
6984          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;  
6985        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6986          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6987    
6988          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6989          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6990            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6991            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6992              !!!cp ('t376');              !!!cp ('t376');
6993              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6994              !!!back-token;              !!!back-token; # <nobr>
6995              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6996              redo B;                        line => $token->{line}, column => $token->{column}};
6997            } elsif ({              next B;
6998                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6999              !!!cp ('t377');              !!!cp ('t377');
7000              last INSCOPE;              last INSCOPE;
7001            }            }
7002          } # INSCOPE          } # INSCOPE
7003                    
7004          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7005          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7006                    
7007            !!!nack ('t377.1');
7008          !!!next-token;          !!!next-token;
7009          redo B;          next B;
7010        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7011          ## has a button element in scope          ## has a button element in scope
7012          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7013            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7014            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7015              !!!cp ('t378');              !!!cp ('t378');
7016              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
7017              !!!back-token;              !!!back-token; # <button>
7018              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7019              redo B;                        line => $token->{line}, column => $token->{column}};
7020            } elsif ({              next B;
7021                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7022              !!!cp ('t379');              !!!cp ('t379');
7023              last INSCOPE;              last INSCOPE;
7024            }            }
# Line 5750  sub _tree_construction_main ($) { Line 7026  sub _tree_construction_main ($) {
7026                        
7027          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7028                        
7029          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7030          push @$active_formatting_elements, ['#marker', ''];  
7031            ## TODO: associate with $self->{form_element} if defined
7032    
         !!!next-token;  
         redo 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});  
7033          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7034            
7035          !!!next-token;          !!!nack ('t379.1');
         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;  
             
7036          !!!next-token;          !!!next-token;
7037          redo B;          next B;
7038        } elsif ({        } elsif ({
7039                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7040                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7041                  image => 1,                  noembed => 1,
7042                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7043                    noscript => 0, ## TODO: 1 if scripting is enabled
7044                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7045          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7046            !!!cp ('t384');            !!!cp ('t381');
7047            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
7048          } else {          } else {
7049            !!!cp ('t385');            !!!cp ('t399');
7050          }          }
7051            ## NOTE: There is an "as if in body" code clone.
7052          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7053          $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;  
7054        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7055          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7056                    
7057          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7058            !!!cp ('t389');            !!!cp ('t389');
7059            ## Ignore the token            ## Ignore the token
7060              !!!nack ('t389'); ## NOTE: Not acknowledged.
7061            !!!next-token;            !!!next-token;
7062            redo B;            next B;
7063          } else {          } else {
7064              !!!ack ('t391.1');
7065    
7066            my $at = $token->{attributes};            my $at = $token->{attributes};
7067            my $form_attrs;            my $form_attrs;
7068            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5864  sub _tree_construction_main ($) { Line 7072  sub _tree_construction_main ($) {
7072            delete $at->{prompt};            delete $at->{prompt};
7073            my @tokens = (            my @tokens = (
7074                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7075                           attributes => $form_attrs},                           attributes => $form_attrs,
7076                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7077                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7078                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7079                            {type => START_TAG_TOKEN, tag_name => 'p',
7080                             line => $token->{line}, column => $token->{column}},
7081                            {type => START_TAG_TOKEN, tag_name => 'label',
7082                             line => $token->{line}, column => $token->{column}},
7083                         );                         );
7084            if ($prompt_attr) {            if ($prompt_attr) {
7085              !!!cp ('t390');              !!!cp ('t390');
7086              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7087                               #line => $token->{line}, column => $token->{column},
7088                              };
7089            } else {            } else {
7090              !!!cp ('t391');              !!!cp ('t391');
7091              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7092                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7093                               #line => $token->{line}, column => $token->{column},
7094                              }; # SHOULD
7095              ## TODO: make this configurable              ## TODO: make this configurable
7096            }            }
7097            push @tokens,            push @tokens,
7098                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7099                             line => $token->{line}, column => $token->{column}},
7100                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7101                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7102                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7103                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7104                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7105            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7106                             line => $token->{line}, column => $token->{column}},
7107                            {type => END_TAG_TOKEN, tag_name => 'form',
7108                             line => $token->{line}, column => $token->{column}};
7109            !!!back-token (@tokens);            !!!back-token (@tokens);
7110            redo B;            !!!next-token;
7111              next B;
7112          }          }
7113        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7114          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7115          my $el;          my $el;
7116          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7117                    
7118          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7119          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5901  sub _tree_construction_main ($) { Line 7122  sub _tree_construction_main ($) {
7122          $insert->($el);          $insert->($el);
7123                    
7124          my $text = '';          my $text = '';
7125            !!!nack ('t392.1');
7126          !!!next-token;          !!!next-token;
7127          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7128            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5931  sub _tree_construction_main ($) { Line 7153  sub _tree_construction_main ($) {
7153            ## Ignore the token            ## Ignore the token
7154          } else {          } else {
7155            !!!cp ('t398');            !!!cp ('t398');
7156            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7157          }          }
7158          !!!next-token;          !!!next-token;
7159            next B;
7160          } elsif ($token->{tag_name} eq 'rt' or
7161                   $token->{tag_name} eq 'rp') {
7162            ## has a |ruby| element in scope
7163            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7164              my $node = $self->{open_elements}->[$_];
7165              if ($node->[1] & RUBY_EL) {
7166                !!!cp ('t398.1');
7167                ## generate implied end tags
7168                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7169                  !!!cp ('t398.2');
7170                  pop @{$self->{open_elements}};
7171                }
7172                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7173                  !!!cp ('t398.3');
7174                  !!!parse-error (type => 'not closed',
7175                                  text => $self->{open_elements}->[-1]->[0]
7176                                      ->manakai_local_name,
7177                                  token => $token);
7178                  pop @{$self->{open_elements}}
7179                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7180                }
7181                last INSCOPE;
7182              } elsif ($node->[1] & SCOPING_EL) {
7183                !!!cp ('t398.4');
7184                last INSCOPE;
7185              }
7186            } # INSCOPE
7187    
7188            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7189    
7190            !!!nack ('t398.5');
7191            !!!next-token;
7192          redo B;          redo B;
7193        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7194                  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');  
7195          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7196    
7197            ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7198    
7199            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7200    
7201            ## "adjust foreign attributes" - done in insert-element-f
7202                    
7203          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7204                    
7205          $self->{insertion_mode} = IN_SELECT_IM;          if ($self->{self_closing}) {
7206              pop @{$self->{open_elements}};
7207              !!!ack ('t398.1');
7208            } else {
7209              !!!cp ('t398.2');
7210              $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7211              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7212              ## mode, "in body" (not "in foreign content") secondary insertion
7213              ## mode, maybe.
7214            }
7215    
7216          !!!next-token;          !!!next-token;
7217          redo B;          next B;
7218        } elsif ({        } elsif ({
7219                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7220                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 5961  sub _tree_construction_main ($) { Line 7222  sub _tree_construction_main ($) {
7222                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7223                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7224          !!!cp ('t401');          !!!cp ('t401');
7225          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
7226                            text => $token->{tag_name}, token => $token);
7227          ## Ignore the token          ## Ignore the token
7228            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7229          !!!next-token;          !!!next-token;
7230          redo B;          next B;
7231                    
7232          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7233        } else {        } else {
7234          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
7235              !!!cp ('t384');
7236              !!!parse-error (type => 'image', token => $token);
7237              $token->{tag_name} = 'img';
7238            } else {
7239              !!!cp ('t385');
7240            }
7241    
7242            ## NOTE: There is an "as if <br>" code clone.
7243          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7244                    
7245          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7246    
7247            if ({
7248                 applet => 1, marquee => 1, object => 1,
7249                }->{$token->{tag_name}}) {
7250              !!!cp ('t380');
7251              push @$active_formatting_elements, ['#marker', ''];
7252              !!!nack ('t380.1');
7253            } elsif ({
7254                      b => 1, big => 1, em => 1, font => 1, i => 1,
7255                      s => 1, small => 1, strile => 1,
7256                      strong => 1, tt => 1, u => 1,
7257                     }->{$token->{tag_name}}) {
7258              !!!cp ('t375');
7259              push @$active_formatting_elements, $self->{open_elements}->[-1];
7260              !!!nack ('t375.1');
7261            } elsif ($token->{tag_name} eq 'input') {
7262              !!!cp ('t388');
7263              ## TODO: associate with $self->{form_element} if defined
7264              pop @{$self->{open_elements}};
7265              !!!ack ('t388.2');
7266            } elsif ({
7267                      area => 1, basefont => 1, bgsound => 1, br => 1,
7268                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7269                      #image => 1,
7270                     }->{$token->{tag_name}}) {
7271              !!!cp ('t388.1');
7272              pop @{$self->{open_elements}};
7273              !!!ack ('t388.3');
7274            } elsif ($token->{tag_name} eq 'select') {
7275              ## TODO: associate with $self->{form_element} if defined
7276            
7277              if ($self->{insertion_mode} & TABLE_IMS or
7278                  $self->{insertion_mode} & BODY_TABLE_IMS or
7279                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7280                !!!cp ('t400.1');
7281                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7282              } else {
7283                !!!cp ('t400.2');
7284                $self->{insertion_mode} = IN_SELECT_IM;
7285              }
7286              !!!nack ('t400.3');
7287            } else {
7288              !!!nack ('t402');
7289            }
7290                    
7291          !!!next-token;          !!!next-token;
7292          redo B;          next B;
7293        }        }
7294      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7295        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7296          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7297              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7298            for (@{$self->{open_elements}}) {          INSCOPE: {
7299              unless ({            for (reverse @{$self->{open_elements}}) {
7300                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7301                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7302                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7303                      }->{$_->[1]}) {                last INSCOPE;
7304                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
7305                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
7306              } else {                last;
               !!!cp ('t404');  
7307              }              }
7308            }            }
7309    
7310            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7311            !!!next-token;                            text => $token->{tag_name}, token => $token);
7312            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
7313            !!!next-token;            !!!next-token;
7314            redo B;            next B;
7315            } # INSCOPE
7316    
7317            for (@{$self->{open_elements}}) {
7318              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7319                !!!cp ('t403');
7320                !!!parse-error (type => 'not closed',
7321                                text => $_->[0]->manakai_local_name,
7322                                token => $token);
7323                last;
7324              } else {
7325                !!!cp ('t404');
7326              }
7327          }          }
7328    
7329            $self->{insertion_mode} = AFTER_BODY_IM;
7330            !!!next-token;
7331            next B;
7332        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7333          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
7334            ## up-to-date, though it has same effect as speced.
7335            if (@{$self->{open_elements}} > 1 and
7336                $self->{open_elements}->[1]->[1] & BODY_EL) {
7337            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7338            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7339              !!!cp ('t406');              !!!cp ('t406');
7340              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
7341                                text => $self->{open_elements}->[1]->[0]
7342                                    ->manakai_local_name,
7343                                token => $token);
7344            } else {            } else {
7345              !!!cp ('t407');              !!!cp ('t407');
7346            }            }
7347            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7348            ## reprocess            ## reprocess
7349            redo B;            next B;
7350          } else {          } else {
7351            !!!cp ('t408');            !!!cp ('t408');
7352            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7353                              text => $token->{tag_name}, token => $token);
7354            ## Ignore the token            ## Ignore the token
7355            !!!next-token;            !!!next-token;
7356            redo B;            next B;
7357          }          }
7358        } elsif ({        } elsif ({
7359                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7360                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7361                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
                 p => 1,  
7362                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7363                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7364                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7365          ## has an element in scope          ## has an element in scope
7366          my $i;          my $i;
7367          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7368            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7369            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
             ## generate implied end tags  
             if ({  
                  dd => ($token->{tag_name} ne 'dd'),  
                  dt => ($token->{tag_name} ne 'dt'),  
                  li => ($token->{tag_name} ne 'li'),  
                  p => ($token->{tag_name} ne 'p'),  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t409');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
7370              !!!cp ('t410');              !!!cp ('t410');
7371              $i = $_;              $i = $_;
7372              last INSCOPE unless $token->{tag_name} eq 'p';              last INSCOPE;
7373            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7374              !!!cp ('t411');              !!!cp ('t411');
7375              last INSCOPE;              last INSCOPE;
7376            }            }
7377          } # INSCOPE          } # INSCOPE
7378            
7379          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          unless (defined $i) { # has an element in scope
7380            if (defined $i) {            !!!cp ('t413');
7381              !!!parse-error (type => 'unmatched end tag',
7382                              text => $token->{tag_name}, token => $token);
7383              ## NOTE: Ignore the token.
7384            } else {
7385              ## Step 1. generate implied end tags
7386              while ({
7387                      ## END_TAG_OPTIONAL_EL
7388                      dd => ($token->{tag_name} ne 'dd'),
7389                      dt => ($token->{tag_name} ne 'dt'),
7390                      li => ($token->{tag_name} ne 'li'),
7391                      p => 1,
7392                      rt => 1,
7393                      rp => 1,
7394                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7395                !!!cp ('t409');
7396                pop @{$self->{open_elements}};
7397              }
7398    
7399              ## Step 2.
7400              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7401                      ne $token->{tag_name}) {
7402              !!!cp ('t412');              !!!cp ('t412');
7403              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7404                                text => $self->{open_elements}->[-1]->[0]
7405                                    ->manakai_local_name,
7406                                token => $token);
7407            } else {            } else {
7408              !!!cp ('t413');              !!!cp ('t414');
             !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
7409            }            }
7410          }  
7411                      ## Step 3.
         if (defined $i) {  
           !!!cp ('t414');  
7412            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7413          } elsif ($token->{tag_name} eq 'p') {  
7414            !!!cp ('t415');            ## Step 4.
7415            ## As if <p>, then reprocess the current token            $clear_up_to_marker->()
7416            my $el;                if {
7417            !!!create-element ($el, 'p');                  applet => 1, button => 1, marquee => 1, object => 1,
7418            $insert->($el);                }->{$token->{tag_name}};
         } else {  
           !!!cp ('t416');  
7419          }          }
         $clear_up_to_marker->()  
           if {  
             button => 1, marquee => 1, object => 1,  
           }->{$token->{tag_name}};  
7420          !!!next-token;          !!!next-token;
7421          redo B;          next B;
7422        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7423            undef $self->{form_element};
7424    
7425          ## has an element in scope          ## has an element in scope
7426            my $i;
7427          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7428            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7429            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t417');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
               
7430              !!!cp ('t418');              !!!cp ('t418');
7431                $i = $_;
7432              last INSCOPE;              last INSCOPE;
7433            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7434              !!!cp ('t419');              !!!cp ('t419');
7435              last INSCOPE;              last INSCOPE;
7436            }            }
7437          } # INSCOPE          } # INSCOPE
7438            
7439          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 {  
7440            !!!cp ('t421');            !!!cp ('t421');
7441            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7442                              text => $token->{tag_name}, token => $token);
7443              ## NOTE: Ignore the token.
7444            } else {
7445              ## Step 1. generate implied end tags
7446              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7447                !!!cp ('t417');
7448                pop @{$self->{open_elements}};
7449              }
7450              
7451              ## Step 2.
7452              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7453                      ne $token->{tag_name}) {
7454                !!!cp ('t417.1');
7455                !!!parse-error (type => 'not closed',
7456                                text => $self->{open_elements}->[-1]->[0]
7457                                    ->manakai_local_name,
7458                                token => $token);
7459              } else {
7460                !!!cp ('t420');
7461              }  
7462              
7463              ## Step 3.
7464              splice @{$self->{open_elements}}, $i;
7465          }          }
7466    
         undef $self->{form_element};  
7467          !!!next-token;          !!!next-token;
7468          redo B;          next B;
7469        } elsif ({        } elsif ({
7470                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7471                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6138  sub _tree_construction_main ($) { Line 7473  sub _tree_construction_main ($) {
7473          my $i;          my $i;
7474          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7475            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7476            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
             ## generate implied end tags  
             if ({  
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot=> 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
               !!!cp ('t422');  
               !!!back-token;  
               $token = {type => END_TAG_TOKEN,  
                         tag_name => $self->{open_elements}->[-1]->[1]}; # MUST  
               redo B;  
             }  
   
7477              !!!cp ('t423');              !!!cp ('t423');
7478              $i = $_;              $i = $_;
7479              last INSCOPE;              last INSCOPE;
7480            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7481              !!!cp ('t424');              !!!cp ('t424');
7482              last INSCOPE;              last INSCOPE;
7483            }            }
7484          } # INSCOPE          } # INSCOPE
7485    
7486            unless (defined $i) { # has an element in scope
7487              !!!cp ('t425.1');
7488              !!!parse-error (type => 'unmatched end tag',
7489                              text => $token->{tag_name}, token => $token);
7490              ## NOTE: Ignore the token.
7491            } else {
7492              ## Step 1. generate implied end tags
7493              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7494                !!!cp ('t422');
7495                pop @{$self->{open_elements}};
7496              }
7497              
7498              ## Step 2.
7499              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7500                      ne $token->{tag_name}) {
7501                !!!cp ('t425');
7502                !!!parse-error (type => 'unmatched end tag',
7503                                text => $token->{tag_name}, token => $token);
7504              } else {
7505                !!!cp ('t426');
7506              }
7507    
7508              ## Step 3.
7509              splice @{$self->{open_elements}}, $i;
7510            }
7511                    
7512          if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {          !!!next-token;
7513            !!!cp ('t425');          next B;
7514            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});        } elsif ($token->{tag_name} eq 'p') {
7515            ## has an element in scope
7516            my $i;
7517            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7518              my $node = $self->{open_elements}->[$_];
7519              if ($node->[1] & P_EL) {
7520                !!!cp ('t410.1');
7521                $i = $_;
7522                last INSCOPE;
7523              } elsif ($node->[1] & SCOPING_EL) {
7524                !!!cp ('t411.1');
7525                last INSCOPE;
7526              }
7527            } # INSCOPE
7528    
7529            if (defined $i) {
7530              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7531                      ne $token->{tag_name}) {
7532                !!!cp ('t412.1');
7533                !!!parse-error (type => 'not closed',
7534                                text => $self->{open_elements}->[-1]->[0]
7535                                    ->manakai_local_name,
7536                                token => $token);
7537              } else {
7538                !!!cp ('t414.1');
7539              }
7540    
7541              splice @{$self->{open_elements}}, $i;
7542          } else {          } else {
7543            !!!cp ('t426');            !!!cp ('t413.1');
7544              !!!parse-error (type => 'unmatched end tag',
7545                              text => $token->{tag_name}, token => $token);
7546    
7547              !!!cp ('t415.1');
7548              ## As if <p>, then reprocess the current token
7549              my $el;
7550              !!!create-element ($el, $HTML_NS, 'p',, $token);
7551              $insert->($el);
7552              ## NOTE: Not inserted into |$self->{open_elements}|.
7553          }          }
7554            
         splice @{$self->{open_elements}}, $i if defined $i;  
7555          !!!next-token;          !!!next-token;
7556          redo B;          next B;
7557        } elsif ({        } elsif ({
7558                  a => 1,                  a => 1,
7559                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6183  sub _tree_construction_main ($) { Line 7561  sub _tree_construction_main ($) {
7561                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7562                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7563          !!!cp ('t427');          !!!cp ('t427');
7564          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7565          redo B;          next B;
7566        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7567          !!!cp ('t428');          !!!cp ('t428');
7568          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
7569                            text => 'br', token => $token);
7570    
7571          ## As if <br>          ## As if <br>
7572          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7573                    
7574          my $el;          my $el;
7575          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7576          $insert->($el);          $insert->($el);
7577                    
7578          ## Ignore the token.          ## Ignore the token.
7579          !!!next-token;          !!!next-token;
7580          redo B;          next B;
7581        } elsif ({        } elsif ({
7582                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7583                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6212  sub _tree_construction_main ($) { Line 7591  sub _tree_construction_main ($) {
7591                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7592                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7593          !!!cp ('t429');          !!!cp ('t429');
7594          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
7595                            text => $token->{tag_name}, token => $token);
7596          ## Ignore the token          ## Ignore the token
7597          !!!next-token;          !!!next-token;
7598          redo B;          next B;
7599                    
7600          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7601                    
# Line 6226  sub _tree_construction_main ($) { Line 7606  sub _tree_construction_main ($) {
7606    
7607          ## Step 2          ## Step 2
7608          S2: {          S2: {
7609            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7610              ## Step 1              ## Step 1
7611              ## generate implied end tags              ## generate implied end tags
7612              if ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                  dd => 1, dt => 1, li => 1, p => 1,  
                  td => 1, th => 1, tr => 1,  
                  tbody => 1, tfoot => 1, thead => 1,  
                 }->{$self->{open_elements}->[-1]->[1]}) {  
7613                !!!cp ('t430');                !!!cp ('t430');
7614                !!!back-token;                ## NOTE: |<ruby><rt></ruby>|.
7615                $token = {type => END_TAG_TOKEN,                ## ISSUE: <ruby><rt></rt> will also take this code path,
7616                          tag_name => $self->{open_elements}->[-1]->[1]}; # MUST                ## which seems wrong.
7617                redo B;                pop @{$self->{open_elements}};
7618                  $node_i++;
7619              }              }
7620                    
7621              ## Step 2              ## Step 2
7622              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7623                        ne $token->{tag_name}) {
7624                !!!cp ('t431');                !!!cp ('t431');
7625                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7626                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7627                                  text => $self->{open_elements}->[-1]->[0]
7628                                      ->manakai_local_name,
7629                                  token => $token);
7630              } else {              } else {
7631                !!!cp ('t432');                !!!cp ('t432');
7632              }              }
7633                            
7634              ## Step 3              ## Step 3
7635              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7636    
7637              !!!next-token;              !!!next-token;
7638              last S2;              last S2;
7639            } else {            } else {
7640              ## Step 3              ## Step 3
7641              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7642                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7643                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7644                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7645                !!!cp ('t433');                !!!cp ('t433');
7646                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
7647                                  text => $token->{tag_name}, token => $token);
7648                ## Ignore the token                ## Ignore the token
7649                !!!next-token;                !!!next-token;
7650                last S2;                last S2;
# Line 6278  sub _tree_construction_main ($) { Line 7660  sub _tree_construction_main ($) {
7660            ## Step 5;            ## Step 5;
7661            redo S2;            redo S2;
7662          } # S2          } # S2
7663          redo B;          next B;
7664        }        }
7665      }      }
7666      redo B;      next B;
7667      } continue { # B
7668        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7669          ## NOTE: The code below is executed in cases where it does not have
7670          ## to be, but it it is harmless even in those cases.
7671          ## has an element in scope
7672          INSCOPE: {
7673            for (reverse 0..$#{$self->{open_elements}}) {
7674              my $node = $self->{open_elements}->[$_];
7675              if ($node->[1] & FOREIGN_EL) {
7676                last INSCOPE;
7677              } elsif ($node->[1] & SCOPING_EL) {
7678                last;
7679              }
7680            }
7681            
7682            ## NOTE: No foreign element in scope.
7683            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7684          } # INSCOPE
7685        }
7686    } # B    } # B
7687    
   ## NOTE: The "trailing end" phase in HTML5 is split into  
   ## two insertion modes: "after html body" and "after html frameset".  
   ## NOTE: States in the main stage is preserved while  
   ## the parser stays in the trailing end phase. # MUST  
   
7688    ## Stop parsing # MUST    ## Stop parsing # MUST
7689        
7690    ## TODO: script stuffs    ## TODO: script stuffs
7691  } # _tree_construct_main  } # _tree_construct_main
7692    
7693  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7694    my $class = shift;    my $class = shift;
7695    my $node = shift;    my $node = shift;
7696    my $s = \$_[0];    my $s = \$_[0];
7697    my $onerror = $_[1];    my $onerror = $_[1];
7698      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7699    
7700    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7701    
# Line 6317  sub set_inner_html ($$$) { Line 7714  sub set_inner_html ($$$) {
7714      }      }
7715    
7716      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7717      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7718    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7719      ## TODO: If non-html element      ## TODO: If non-html element
7720    
7721      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7722    
7723    ## TODO: Support for $get_wrapper
7724    
7725      ## Step 1 # MUST      ## Step 1 # MUST
7726      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7727      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6330  sub set_inner_html ($$$) { Line 7729  sub set_inner_html ($$$) {
7729      my $p = $class->new;      my $p = $class->new;
7730      $p->{document} = $doc;      $p->{document} = $doc;
7731    
7732      ## Step 9 # MUST      ## Step 8 # MUST
7733      my $i = 0;      my $i = 0;
7734      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7735      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7736      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7737        my $self = shift;        my $self = shift;
7738    
# Line 6342  sub set_inner_html ($$$) { Line 7741  sub set_inner_html ($$$) {
7741    
7742        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7743        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7744        $column++;  
7745          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7746          $p->{column}++;
7747    
7748        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7749          $line++;          $p->{line}++;
7750          $column = 0;          $p->{column} = 0;
7751          !!!cp ('i1');          !!!cp ('i1');
7752        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7753          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7754          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7755          $line++;          $p->{line}++;
7756          $column = 0;          $p->{column} = 0;
7757          !!!cp ('i2');          !!!cp ('i2');
7758        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7759          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6361  sub set_inner_html ($$$) { Line 7762  sub set_inner_html ($$$) {
7762          !!!cp ('i4');          !!!cp ('i4');
7763          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7764          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7765          } elsif ($self->{next_char} <= 0x0008 or
7766                   (0x000E <= $self->{next_char} and
7767                    $self->{next_char} <= 0x001F) or
7768                   (0x007F <= $self->{next_char} and
7769                    $self->{next_char} <= 0x009F) or
7770                   (0xD800 <= $self->{next_char} and
7771                    $self->{next_char} <= 0xDFFF) or
7772                   (0xFDD0 <= $self->{next_char} and
7773                    $self->{next_char} <= 0xFDDF) or
7774                   {
7775                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7776                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7777                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7778                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7779                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7780                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7781                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7782                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7783                    0x10FFFE => 1, 0x10FFFF => 1,
7784                   }->{$self->{next_char}}) {
7785            !!!cp ('i4.1');
7786            if ($self->{next_char} < 0x10000) {
7787              !!!parse-error (type => 'control char',
7788                              text => (sprintf 'U+%04X', $self->{next_char}));
7789            } else {
7790              !!!parse-error (type => 'control char',
7791                              text => (sprintf 'U-%08X', $self->{next_char}));
7792            }
7793        }        }
7794      };      };
7795      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7796      $p->{next_char} = -1;      $p->{next_char} = -1;
7797        
7798        $p->{read_until} = sub {
7799          ## TODO: ...
7800          return 0;
7801        }; # $p->{read_until};
7802    
7803      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7804        my (%opt) = @_;        my (%opt) = @_;
7805        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7806          my $column = $opt{column};
7807          if (defined $opt{token} and defined $opt{token}->{line}) {
7808            $line = $opt{token}->{line};
7809            $column = $opt{token}->{column};
7810          }
7811          warn "Parse error ($opt{type}) at line $line column $column\n";
7812      };      };
7813      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7814        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7815      };      };
7816            
7817      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6395  sub set_inner_html ($$$) { Line 7835  sub set_inner_html ($$$) {
7835          unless defined $p->{content_model};          unless defined $p->{content_model};
7836          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7837    
7838      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7839          ## TODO: Foreign element OK?
7840    
7841      ## Step 4      ## Step 3
7842      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
7843        ('http://www.w3.org/1999/xhtml', [undef, 'html']);        ('http://www.w3.org/1999/xhtml', [undef, 'html']);
7844    
7845      ## Step 5 # MUST      ## Step 4 # MUST
7846      $doc->append_child ($root);      $doc->append_child ($root);
7847    
7848      ## Step 6 # MUST      ## Step 5 # MUST
7849      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7850    
7851      undef $p->{head_element};      undef $p->{head_element};
7852    
7853      ## Step 7 # MUST      ## Step 6 # MUST
7854      $p->_reset_insertion_mode;      $p->_reset_insertion_mode;
7855    
7856      ## Step 8 # MUST      ## Step 7 # MUST
7857      my $anode = $node;      my $anode = $node;
7858      AN: while (defined $anode) {      AN: while (defined $anode) {
7859        if ($anode->node_type == 1) {        if ($anode->node_type == 1) {
# Line 6428  sub set_inner_html ($$$) { Line 7869  sub set_inner_html ($$$) {
7869        $anode = $anode->parent_node;        $anode = $anode->parent_node;
7870      } # AN      } # AN
7871            
7872      ## Step 3 # MUST      ## Step 9 # MUST
     ## Step 10 # MUST  
7873      {      {
7874        my $self = $p;        my $self = $p;
7875        !!!next-token;        !!!next-token;
7876      }      }
7877      $p->_tree_construction_main;      $p->_tree_construction_main;
7878    
7879      ## Step 11 # MUST      ## Step 10 # MUST
7880      my @cn = @{$node->child_nodes};      my @cn = @{$node->child_nodes};
7881      for (@cn) {      for (@cn) {
7882        $node->remove_child ($_);        $node->remove_child ($_);
7883      }      }
7884      ## ISSUE: mutation events? read-only?      ## ISSUE: mutation events? read-only?
7885    
7886      ## Step 12 # MUST      ## Step 11 # MUST
7887      @cn = @{$root->child_nodes};      @cn = @{$root->child_nodes};
7888      for (@cn) {      for (@cn) {
7889        $this_doc->adopt_node ($_);        $this_doc->adopt_node ($_);
# Line 6452  sub set_inner_html ($$$) { Line 7892  sub set_inner_html ($$$) {
7892      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7893    
7894      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7895    
7896        delete $p->{parse_error}; # delete loop
7897    } else {    } else {
7898      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";
7899    }    }

Legend:
Removed from v.1.81  
changed lines
  Added in v.1.172

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24