/[suikacvs]/markup/html/whatpm/Whatpm/HTML.pm.src
Suika

Diff of /markup/html/whatpm/Whatpm/HTML.pm.src

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.102 by wakaba, Sun Mar 9 06:29:25 2008 UTC revision 1.179 by wakaba, Sun Sep 14 13:09:01 2008 UTC
# Line 8  use Error qw(:try); Line 8  use Error qw(:try);
8  ## doc.write ('');  ## doc.write ('');
9  ## alert (doc.compatMode);  ## alert (doc.compatMode);
10    
11  ## TODO: Control charcters and noncharacters are not allowed (HTML5 revision 1263)  require IO::Handle;
12  ## TODO: 1252 parse error (revision 1264)  
13  ## TODO: 8859-11 = 874 (revision 1271)  my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
14    my $MML_NS = q<http://www.w3.org/1998/Math/MathML>;
15  my $permitted_slash_tag_name = {  my $SVG_NS = q<http://www.w3.org/2000/svg>;
16    base => 1,  my $XLINK_NS = q<http://www.w3.org/1999/xlink>;
17    link => 1,  my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
18    meta => 1,  my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
19    hr => 1,  
20    br => 1,  sub A_EL () { 0b1 }
21    img => 1,  sub ADDRESS_EL () { 0b10 }
22    embed => 1,  sub BODY_EL () { 0b100 }
23    param => 1,  sub BUTTON_EL () { 0b1000 }
24    area => 1,  sub CAPTION_EL () { 0b10000 }
25    col => 1,  sub DD_EL () { 0b100000 }
26    input => 1,  sub DIV_EL () { 0b1000000 }
27    sub DT_EL () { 0b10000000 }
28    sub FORM_EL () { 0b100000000 }
29    sub FORMATTING_EL () { 0b1000000000 }
30    sub FRAMESET_EL () { 0b10000000000 }
31    sub HEADING_EL () { 0b100000000000 }
32    sub HTML_EL () { 0b1000000000000 }
33    sub LI_EL () { 0b10000000000000 }
34    sub NOBR_EL () { 0b100000000000000 }
35    sub OPTION_EL () { 0b1000000000000000 }
36    sub OPTGROUP_EL () { 0b10000000000000000 }
37    sub P_EL () { 0b100000000000000000 }
38    sub SELECT_EL () { 0b1000000000000000000 }
39    sub TABLE_EL () { 0b10000000000000000000 }
40    sub TABLE_CELL_EL () { 0b100000000000000000000 }
41    sub TABLE_ROW_EL () { 0b1000000000000000000000 }
42    sub TABLE_ROW_GROUP_EL () { 0b10000000000000000000000 }
43    sub MISC_SCOPING_EL () { 0b100000000000000000000000 }
44    sub MISC_SPECIAL_EL () { 0b1000000000000000000000000 }
45    sub FOREIGN_EL () { 0b10000000000000000000000000 }
46    sub FOREIGN_FLOW_CONTENT_EL () { 0b100000000000000000000000000 }
47    sub MML_AXML_EL () { 0b1000000000000000000000000000 }
48    sub RUBY_EL () { 0b10000000000000000000000000000 }
49    sub RUBY_COMPONENT_EL () { 0b100000000000000000000000000000 }
50    
51    sub TABLE_ROWS_EL () {
52      TABLE_EL |
53      TABLE_ROW_EL |
54      TABLE_ROW_GROUP_EL
55    }
56    
57    ## NOTE: Used in "generate implied end tags" algorithm.
58    ## NOTE: There is a code where a modified version of END_TAG_OPTIONAL_EL
59    ## is used in "generate implied end tags" implementation (search for the
60    ## function mae).
61    sub END_TAG_OPTIONAL_EL () {
62      DD_EL |
63      DT_EL |
64      LI_EL |
65      P_EL |
66      RUBY_COMPONENT_EL
67    }
68    
69    ## NOTE: Used in </body> and EOF algorithms.
70    sub ALL_END_TAG_OPTIONAL_EL () {
71      DD_EL |
72      DT_EL |
73      LI_EL |
74      P_EL |
75    
76      BODY_EL |
77      HTML_EL |
78      TABLE_CELL_EL |
79      TABLE_ROW_EL |
80      TABLE_ROW_GROUP_EL
81    }
82    
83    sub SCOPING_EL () {
84      BUTTON_EL |
85      CAPTION_EL |
86      HTML_EL |
87      TABLE_EL |
88      TABLE_CELL_EL |
89      MISC_SCOPING_EL
90    }
91    
92    sub TABLE_SCOPING_EL () {
93      HTML_EL |
94      TABLE_EL
95    }
96    
97    sub TABLE_ROWS_SCOPING_EL () {
98      HTML_EL |
99      TABLE_ROW_GROUP_EL
100    }
101    
102    sub TABLE_ROW_SCOPING_EL () {
103      HTML_EL |
104      TABLE_ROW_EL
105    }
106    
107    sub SPECIAL_EL () {
108      ADDRESS_EL |
109      BODY_EL |
110      DIV_EL |
111    
112      DD_EL |
113      DT_EL |
114      LI_EL |
115      P_EL |
116    
117      FORM_EL |
118      FRAMESET_EL |
119      HEADING_EL |
120      OPTION_EL |
121      OPTGROUP_EL |
122      SELECT_EL |
123      TABLE_ROW_EL |
124      TABLE_ROW_GROUP_EL |
125      MISC_SPECIAL_EL
126    }
127    
128    my $el_category = {
129      a => A_EL | FORMATTING_EL,
130      address => ADDRESS_EL,
131      applet => MISC_SCOPING_EL,
132      area => MISC_SPECIAL_EL,
133      b => FORMATTING_EL,
134      base => MISC_SPECIAL_EL,
135      basefont => MISC_SPECIAL_EL,
136      bgsound => MISC_SPECIAL_EL,
137      big => FORMATTING_EL,
138      blockquote => MISC_SPECIAL_EL,
139      body => BODY_EL,
140      br => MISC_SPECIAL_EL,
141      button => BUTTON_EL,
142      caption => CAPTION_EL,
143      center => MISC_SPECIAL_EL,
144      col => MISC_SPECIAL_EL,
145      colgroup => MISC_SPECIAL_EL,
146      dd => DD_EL,
147      dir => MISC_SPECIAL_EL,
148      div => DIV_EL,
149      dl => MISC_SPECIAL_EL,
150      dt => DT_EL,
151      em => FORMATTING_EL,
152      embed => MISC_SPECIAL_EL,
153      fieldset => MISC_SPECIAL_EL,
154      font => FORMATTING_EL,
155      form => FORM_EL,
156      frame => MISC_SPECIAL_EL,
157      frameset => FRAMESET_EL,
158      h1 => HEADING_EL,
159      h2 => HEADING_EL,
160      h3 => HEADING_EL,
161      h4 => HEADING_EL,
162      h5 => HEADING_EL,
163      h6 => HEADING_EL,
164      head => MISC_SPECIAL_EL,
165      hr => MISC_SPECIAL_EL,
166      html => HTML_EL,
167      i => FORMATTING_EL,
168      iframe => MISC_SPECIAL_EL,
169      img => MISC_SPECIAL_EL,
170      input => MISC_SPECIAL_EL,
171      isindex => MISC_SPECIAL_EL,
172      li => LI_EL,
173      link => MISC_SPECIAL_EL,
174      listing => MISC_SPECIAL_EL,
175      marquee => MISC_SCOPING_EL,
176      menu => MISC_SPECIAL_EL,
177      meta => MISC_SPECIAL_EL,
178      nobr => NOBR_EL | FORMATTING_EL,
179      noembed => MISC_SPECIAL_EL,
180      noframes => MISC_SPECIAL_EL,
181      noscript => MISC_SPECIAL_EL,
182      object => MISC_SCOPING_EL,
183      ol => MISC_SPECIAL_EL,
184      optgroup => OPTGROUP_EL,
185      option => OPTION_EL,
186      p => P_EL,
187      param => MISC_SPECIAL_EL,
188      plaintext => MISC_SPECIAL_EL,
189      pre => MISC_SPECIAL_EL,
190      rp => RUBY_COMPONENT_EL,
191      rt => RUBY_COMPONENT_EL,
192      ruby => RUBY_EL,
193      s => FORMATTING_EL,
194      script => MISC_SPECIAL_EL,
195      select => SELECT_EL,
196      small => FORMATTING_EL,
197      spacer => MISC_SPECIAL_EL,
198      strike => FORMATTING_EL,
199      strong => FORMATTING_EL,
200      style => MISC_SPECIAL_EL,
201      table => TABLE_EL,
202      tbody => TABLE_ROW_GROUP_EL,
203      td => TABLE_CELL_EL,
204      textarea => MISC_SPECIAL_EL,
205      tfoot => TABLE_ROW_GROUP_EL,
206      th => TABLE_CELL_EL,
207      thead => TABLE_ROW_GROUP_EL,
208      title => MISC_SPECIAL_EL,
209      tr => TABLE_ROW_EL,
210      tt => FORMATTING_EL,
211      u => FORMATTING_EL,
212      ul => MISC_SPECIAL_EL,
213      wbr => MISC_SPECIAL_EL,
214    };
215    
216    my $el_category_f = {
217      $MML_NS => {
218        'annotation-xml' => MML_AXML_EL,
219        mi => FOREIGN_FLOW_CONTENT_EL,
220        mo => FOREIGN_FLOW_CONTENT_EL,
221        mn => FOREIGN_FLOW_CONTENT_EL,
222        ms => FOREIGN_FLOW_CONTENT_EL,
223        mtext => FOREIGN_FLOW_CONTENT_EL,
224      },
225      $SVG_NS => {
226        foreignObject => FOREIGN_FLOW_CONTENT_EL,
227        desc => FOREIGN_FLOW_CONTENT_EL,
228        title => FOREIGN_FLOW_CONTENT_EL,
229      },
230      ## NOTE: In addition, FOREIGN_EL is set to non-HTML elements.
231    };
232    
233    my $svg_attr_name = {
234      attributename => 'attributeName',
235      attributetype => 'attributeType',
236      basefrequency => 'baseFrequency',
237      baseprofile => 'baseProfile',
238      calcmode => 'calcMode',
239      clippathunits => 'clipPathUnits',
240      contentscripttype => 'contentScriptType',
241      contentstyletype => 'contentStyleType',
242      diffuseconstant => 'diffuseConstant',
243      edgemode => 'edgeMode',
244      externalresourcesrequired => 'externalResourcesRequired',
245      filterres => 'filterRes',
246      filterunits => 'filterUnits',
247      glyphref => 'glyphRef',
248      gradienttransform => 'gradientTransform',
249      gradientunits => 'gradientUnits',
250      kernelmatrix => 'kernelMatrix',
251      kernelunitlength => 'kernelUnitLength',
252      keypoints => 'keyPoints',
253      keysplines => 'keySplines',
254      keytimes => 'keyTimes',
255      lengthadjust => 'lengthAdjust',
256      limitingconeangle => 'limitingConeAngle',
257      markerheight => 'markerHeight',
258      markerunits => 'markerUnits',
259      markerwidth => 'markerWidth',
260      maskcontentunits => 'maskContentUnits',
261      maskunits => 'maskUnits',
262      numoctaves => 'numOctaves',
263      pathlength => 'pathLength',
264      patterncontentunits => 'patternContentUnits',
265      patterntransform => 'patternTransform',
266      patternunits => 'patternUnits',
267      pointsatx => 'pointsAtX',
268      pointsaty => 'pointsAtY',
269      pointsatz => 'pointsAtZ',
270      preservealpha => 'preserveAlpha',
271      preserveaspectratio => 'preserveAspectRatio',
272      primitiveunits => 'primitiveUnits',
273      refx => 'refX',
274      refy => 'refY',
275      repeatcount => 'repeatCount',
276      repeatdur => 'repeatDur',
277      requiredextensions => 'requiredExtensions',
278      requiredfeatures => 'requiredFeatures',
279      specularconstant => 'specularConstant',
280      specularexponent => 'specularExponent',
281      spreadmethod => 'spreadMethod',
282      startoffset => 'startOffset',
283      stddeviation => 'stdDeviation',
284      stitchtiles => 'stitchTiles',
285      surfacescale => 'surfaceScale',
286      systemlanguage => 'systemLanguage',
287      tablevalues => 'tableValues',
288      targetx => 'targetX',
289      targety => 'targetY',
290      textlength => 'textLength',
291      viewbox => 'viewBox',
292      viewtarget => 'viewTarget',
293      xchannelselector => 'xChannelSelector',
294      ychannelselector => 'yChannelSelector',
295      zoomandpan => 'zoomAndPan',
296  };  };
297    
298    my $foreign_attr_xname = {
299      'xlink:actuate' => [$XLINK_NS, ['xlink', 'actuate']],
300      'xlink:arcrole' => [$XLINK_NS, ['xlink', 'arcrole']],
301      'xlink:href' => [$XLINK_NS, ['xlink', 'href']],
302      'xlink:role' => [$XLINK_NS, ['xlink', 'role']],
303      'xlink:show' => [$XLINK_NS, ['xlink', 'show']],
304      'xlink:title' => [$XLINK_NS, ['xlink', 'title']],
305      'xlink:type' => [$XLINK_NS, ['xlink', 'type']],
306      'xml:base' => [$XML_NS, ['xml', 'base']],
307      'xml:lang' => [$XML_NS, ['xml', 'lang']],
308      'xml:space' => [$XML_NS, ['xml', 'space']],
309      'xmlns' => [$XMLNS_NS, [undef, 'xmlns']],
310      'xmlns:xlink' => [$XMLNS_NS, ['xmlns', 'xlink']],
311    };
312    
313    ## ISSUE: xmlns:xlink="non-xlink-ns" is not an error.
314    
315  my $c1_entity_char = {  my $c1_entity_char = {
316    0x80 => 0x20AC,    0x80 => 0x20AC,
317    0x81 => 0xFFFD,    0x81 => 0xFFFD,
# Line 61  my $c1_entity_char = { Line 347  my $c1_entity_char = {
347    0x9F => 0x0178,    0x9F => 0x0178,
348  }; # $c1_entity_char  }; # $c1_entity_char
349    
 my $special_category = {  
   address => 1, area => 1, base => 1, basefont => 1, bgsound => 1,  
   blockquote => 1, body => 1, br => 1, center => 1, col => 1, colgroup => 1,  
   dd => 1, dir => 1, div => 1, dl => 1, dt => 1, embed => 1, fieldset => 1,  
   form => 1, frame => 1, frameset => 1, h1 => 1, h2 => 1, h3 => 1,  
   h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, iframe => 1, image => 1,  
   img => 1, input => 1, isindex => 1, li => 1, link => 1, listing => 1,  
   menu => 1, meta => 1, noembed => 1, noframes => 1, noscript => 1,  
   ol => 1, optgroup => 1, option => 1, p => 1, param => 1, plaintext => 1,  
   pre => 1, script => 1, select => 1, spacer => 1, style => 1, tbody => 1,  
   textarea => 1, tfoot => 1, thead => 1, title => 1, tr => 1, ul => 1, wbr => 1,  
 };  
 my $scoping_category = {  
   button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
360    my $charset = shift;    my $charset_name = shift;
361    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
362    
363    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
364      my $self = shift;      my (%opt) = @_;
365      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
366      ## TODO: if $charset is supported    };
367      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      ## "Change the encoding" algorithm:    my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      ## Step 1        };
372      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
373        $charset = 'utf-8';    ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403      }      }
404    
405      ## Step 2      ## Step 2
406      if (defined $self->{input_encoding} and      my $byte_buffer = '';
407          $self->{input_encoding} eq $charset) {      for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419        $self->{confident} = 1;        $self->{confident} = 1;
420        return;        last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435      }      }
436    
437      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
438          ':'.$charset, level => 'w');      ## TODO: <meta charset>
439    
440      ## Step 3      ## Step 5
441      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
442    
443      ## Step 4      ## Step 6
444      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559    }; # $self->{change_encoding}    }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        line => $self->{line}, column => $self->{column} + 1,
565                        %opt, type => $type);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
575    my $return;    my $return;
576    try {    try {
577      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
579      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
580      $s = \ (Encode::decode ($charset, $$bytes_s));      
581      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL ())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $return = $self->parse_char_string ($s, @args);  
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605    };    };
606    return $return;    return $return;
607  } # parse_byte_string  } # parse_byte_stream
608    
609  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 615  sub parse_byte_string ($$$$;$) {
615  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
616  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
617    
618  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      my $s = ref $_[0] ? $_[0] : \($_[0]);
622      require Whatpm::Charset::DecodeHandle;
623      my $input = Whatpm::Charset::DecodeHandle::CharString->new ($s);
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
633    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
636    
# Line 175  sub parse_string ($$$;$) { Line 639  sub parse_string ($$$;$) {
639    $self->{confident} = 1 unless exists $self->{confident};    $self->{confident} = 1 unless exists $self->{confident};
640    $self->{document}->input_encoding ($self->{input_encoding})    $self->{document}->input_encoding ($self->{input_encoding})
641        if defined $self->{input_encoding};        if defined $self->{input_encoding};
642    ## TODO: |{input_encoding}| is needless?
643    
644    my $i = 0;    my $i = 0;
645    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
646    my $column = 0;    $self->{column_prev} = -1;
647      $self->{column} = 0;
648    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
649      my $self = shift;      my $self = shift;
650    
651      pop @{$self->{prev_char}};      my $char = '';
652      unshift @{$self->{prev_char}}, $self->{next_char};      if (defined $self->{next_next_char}) {
653          $char = $self->{next_next_char};
654          delete $self->{next_next_char};
655          $self->{next_char} = ord $char;
656        } else {
657          $self->{char_buffer} = '';
658          $self->{char_buffer_pos} = 0;
659    
660      $self->{next_char} = -1 and return if $i >= length $$s;        my $count = $input->manakai_read_until
661      $self->{next_char} = ord substr $$s, $i++, 1;           ($self->{char_buffer},
662      $column++;            qr/(?!\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}]/,
663              $self->{char_buffer_pos});
664          if ($count) {
665            $self->{line_prev} = $self->{line};
666            $self->{column_prev} = $self->{column};
667            $self->{column}++;
668            $self->{next_char}
669                = ord substr ($self->{char_buffer}, $self->{char_buffer_pos}++, 1);
670            return;
671          }
672    
673          if ($input->read ($char, 1)) {
674            $self->{next_char} = ord $char;
675          } else {
676            $self->{next_char} = -1;
677            return;
678          }
679        }
680    
681        ($self->{line_prev}, $self->{column_prev})
682            = ($self->{line}, $self->{column});
683        $self->{column}++;
684            
685      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
686        $line++;        !!!cp ('j1');
687        $column = 0;        $self->{line}++;
688          $self->{column} = 0;
689      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
690        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
691    ## TODO: support for abort/streaming
692          my $next = '';
693          if ($input->read ($next, 1) and $next ne "\x0A") {
694            $self->{next_next_char} = $next;
695          }
696        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
697        $line++;        $self->{line}++;
698        $column = 0;        $self->{column} = 0;
699      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
700          !!!cp ('j3');
701        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
702      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
703          !!!cp ('j4');
704        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
705        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
706        } elsif ($self->{next_char} <= 0x0008 or
707                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
708                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
709                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
710                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
711    ## ISSUE: U+FDE0-U+FDEF are not excluded
712                 {
713                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
714                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
715                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
716                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
717                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
718                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
719                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
720                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
721                  0x10FFFE => 1, 0x10FFFF => 1,
722                 }->{$self->{next_char}}) {
723          !!!cp ('j5');
724          if ($self->{next_char} < 0x10000) {
725            !!!parse-error (type => 'control char',
726                            text => (sprintf 'U+%04X', $self->{next_char}));
727          } else {
728            !!!parse-error (type => 'control char',
729                            text => (sprintf 'U-%08X', $self->{next_char}));
730          }
731      }      }
732    };    };
733    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
734    $self->{next_char} = -1;    $self->{next_char} = -1;
735    
736      $self->{read_until} = sub {
737        #my ($scalar, $specials_range, $offset) = @_;
738        my $specials_range = $_[1];
739        return 0 if defined $self->{next_next_char};
740        my $count = $input->manakai_read_until
741           ($_[0],
742            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}]/,
743            $_[2]);
744        if ($count) {
745          $self->{column} += $count;
746          $self->{column_prev} += $count;
747          $self->{prev_char} = [-1, -1, -1];
748          $self->{next_char} = -1;
749        }
750        return $count;
751      }; # $self->{read_until}
752    $self->{read_until}=sub{0};
753    
754    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
755      my (%opt) = @_;      my (%opt) = @_;
756      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
757        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
758        warn "Parse error ($opt{type}) at line $line column $column\n";
759    };    };
760    $self->{parse_error} = sub {    $self->{parse_error} = sub {
761      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
762    };    };
763    
764    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 766  sub parse_string ($$$;$) {
766    $self->_construct_tree;    $self->_construct_tree;
767    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
768    
769      delete $self->{parse_error}; # remove loop
770    
771    return $self->{document};    return $self->{document};
772  } # parse_string  } # parse_char_stream
773    
774  sub new ($) {  sub new ($) {
775    my $class = shift;    my $class = shift;
776    my $self = bless {}, $class;    my $self = bless {
777        level => {must => 'm',
778                  should => 's',
779                  warn => 'w',
780                  info => 'i',
781                  uncertain => 'u'},
782      }, $class;
783    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
784      $self->{next_char} = -1;      $self->{next_char} = -1;
785    };    };
# Line 254  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 808  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
808  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
809    
810  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
811  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
812  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
813  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
814  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 265  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 819  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
819  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
820  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
821  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
822  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
823  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
824  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
825  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 841  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
841  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
842  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
843  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
844    sub SELF_CLOSING_START_TAG_STATE () { 34 }
845    sub CDATA_SECTION_STATE () { 35 }
846    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
847    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
848    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
849    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
850    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
851    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
852    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
853    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
854    ## NOTE: "Entity data state", "entity in attribute value state", and
855    ## "consume a character reference" algorithm are jointly implemented
856    ## using the following six states:
857    sub ENTITY_STATE () { 44 }
858    sub ENTITY_HASH_STATE () { 45 }
859    sub NCR_NUM_STATE () { 46 }
860    sub HEXREF_X_STATE () { 47 }
861    sub HEXREF_HEX_STATE () { 48 }
862    sub ENTITY_NAME_STATE () { 49 }
863    
864  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
865  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 304  sub ROW_IMS ()        { 0b10000000 } Line 877  sub ROW_IMS ()        { 0b10000000 }
877  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
878  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
879  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
880    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
881        ## NOTE: "in foreign content" insertion mode is special; it is combined
882        ## with the secondary insertion mode.  In this parser, they are stored
883        ## together in the bit-or'ed form.
884    
885  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
886    
# Line 335  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 912  sub IN_COLUMN_GROUP_IM () { 0b10 }
912  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
913    my $self = shift;    my $self = shift;
914    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
915      #$self->{state_keyword}; # initialized when used
916      #$self->{entity__value}; # initialized when used
917      #$self->{entity__match}; # initialized when used
918    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
919    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
920    undef $self->{current_attribute};    undef $self->{current_attribute};
921    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
922    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
923    $self->{char} = [];    delete $self->{self_closing};
924      $self->{char_buffer} = '';
925      $self->{char_buffer_pos} = 0;
926    # $self->{next_char}    # $self->{next_char}
927    !!!next-input-character;    !!!next-input-character;
928    $self->{token} = [];    $self->{token} = [];
# Line 360  sub _initialize_tokenizer ($) { Line 942  sub _initialize_tokenizer ($) {
942  ##        ->{value}  ##        ->{value}
943  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
944  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
945    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
946    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
947    ##     while the token is pushed back to the stack.
948    
949  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
950    
# Line 369  sub _initialize_tokenizer ($) { Line 954  sub _initialize_tokenizer ($) {
954  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
955  ## and removed from the list.  ## and removed from the list.
956    
957  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
958  ## 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,  
959    
960  sub _get_next_token ($) {  sub _get_next_token ($) {
961    my $self = shift;    my $self = shift;
962    
963      if ($self->{self_closing}) {
964        !!!parse-error (type => 'nestc', token => $self->{current_token});
965        ## NOTE: The |self_closing| flag is only set by start tag token.
966        ## In addition, when a start tag token is emitted, it is always set to
967        ## |current_token|.
968        delete $self->{self_closing};
969      }
970    
971    if (@{$self->{token}}) {    if (@{$self->{token}}) {
972        $self->{self_closing} = $self->{token}->[0]->{self_closing};
973      return shift @{$self->{token}};      return shift @{$self->{token}};
974    }    }
975    
# Line 396  sub _get_next_token ($) { Line 979  sub _get_next_token ($) {
979          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
980              not $self->{escape}) {              not $self->{escape}) {
981            !!!cp (1);            !!!cp (1);
982            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
983              ## "entity data state".  In this implementation, the tokenizer
984              ## is switched to the |ENTITY_STATE|, which is an implementation
985              ## of the "consume a character reference" algorithm.
986              $self->{entity_additional} = -1;
987              $self->{prev_state} = DATA_STATE;
988              $self->{state} = ENTITY_STATE;
989            !!!next-input-character;            !!!next-input-character;
990            redo A;            redo A;
991          } else {          } else {
# Line 449  sub _get_next_token ($) { Line 1038  sub _get_next_token ($) {
1038          #          #
1039        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1040          !!!cp (11);          !!!cp (11);
1041          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
1042                      line => $self->{line}, column => $self->{column}});
1043          last A; ## TODO: ok?          last A; ## TODO: ok?
1044        } else {        } else {
1045          !!!cp (12);          !!!cp (12);
1046        }        }
1047        # Anything else        # Anything else
1048        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1049                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
1050                       line => $self->{line}, column => $self->{column},
1051                      };
1052          $self->{read_until}->($token->{data}, q[-!<>&], length $token->{data});
1053    
1054        ## Stay in the data state        ## Stay in the data state
1055        !!!next-input-character;        !!!next-input-character;
1056    
1057        !!!emit ($token);        !!!emit ($token);
1058    
1059        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;  
1060      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1061        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1062          if ($self->{next_char} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
# Line 492  sub _get_next_token ($) { Line 1069  sub _get_next_token ($) {
1069            ## reconsume            ## reconsume
1070            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1071    
1072            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1073                        line => $self->{line_prev},
1074                        column => $self->{column_prev},
1075                       });
1076    
1077            redo A;            redo A;
1078          }          }
# Line 512  sub _get_next_token ($) { Line 1092  sub _get_next_token ($) {
1092            !!!cp (19);            !!!cp (19);
1093            $self->{current_token}            $self->{current_token}
1094              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1095                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1096                   line => $self->{line_prev},
1097                   column => $self->{column_prev}};
1098            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1099            !!!next-input-character;            !!!next-input-character;
1100            redo A;            redo A;
# Line 520  sub _get_next_token ($) { Line 1102  sub _get_next_token ($) {
1102                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1103            !!!cp (20);            !!!cp (20);
1104            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1105                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1106                                        line => $self->{line_prev},
1107                                        column => $self->{column_prev}};
1108            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1109            !!!next-input-character;            !!!next-input-character;
1110            redo A;            redo A;
1111          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1112            !!!cp (21);            !!!cp (21);
1113            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1114                              line => $self->{line_prev},
1115                              column => $self->{column_prev});
1116            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1117            !!!next-input-character;            !!!next-input-character;
1118    
1119            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1120                        line => $self->{line_prev},
1121                        column => $self->{column_prev},
1122                       });
1123    
1124            redo A;            redo A;
1125          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1126            !!!cp (22);            !!!cp (22);
1127            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1128                              line => $self->{line_prev},
1129                              column => $self->{column_prev});
1130            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1131              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1132                                        line => $self->{line_prev},
1133                                        column => $self->{column_prev},
1134                                       };
1135            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1136            redo A;            redo A;
1137          } else {          } else {
1138            !!!cp (23);            !!!cp (23);
1139            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1140                              line => $self->{line_prev},
1141                              column => $self->{column_prev});
1142            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1143            ## reconsume            ## reconsume
1144    
1145            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1146                        line => $self->{line_prev},
1147                        column => $self->{column_prev},
1148                       });
1149    
1150            redo A;            redo A;
1151          }          }
# Line 553  sub _get_next_token ($) { Line 1153  sub _get_next_token ($) {
1153          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1154        }        }
1155      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1156          ## NOTE: The "close tag open state" in the spec is implemented as
1157          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1158    
1159          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1160        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1161          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1162            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1163            my @next_char;            $self->{state_keyword} = '';
1164            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            ## Reconsume.
1165              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...  
           }  
1166          } else {          } else {
1167            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1168              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1169            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1170            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1171            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1172              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1173                        line => $l, column => $c,
1174                       });
1175            redo A;            redo A;
1176          }          }
1177        }        }
1178          
1179        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1180            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1181          !!!cp (29);          !!!cp (29);
1182          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1183                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1184                   tag_name => chr ($self->{next_char} + 0x0020),
1185                   line => $l, column => $c};
1186          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1187          !!!next-input-character;          !!!next-input-character;
1188          redo A;          redo A;
# Line 620  sub _get_next_token ($) { Line 1190  sub _get_next_token ($) {
1190                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1191          !!!cp (30);          !!!cp (30);
1192          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1193                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1194                                      line => $l, column => $c};
1195          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1196          !!!next-input-character;          !!!next-input-character;
1197          redo A;          redo A;
1198        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1199          !!!cp (31);          !!!cp (31);
1200          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1201                            line => $self->{line_prev}, ## "<" in "</>"
1202                            column => $self->{column_prev} - 1);
1203          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1204          !!!next-input-character;          !!!next-input-character;
1205          redo A;          redo A;
# Line 636  sub _get_next_token ($) { Line 1209  sub _get_next_token ($) {
1209          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1210          # reconsume          # reconsume
1211    
1212          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1213                      line => $l, column => $c,
1214                     });
1215    
1216          redo A;          redo A;
1217        } else {        } else {
1218          !!!cp (33);          !!!cp (33);
1219          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1220          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1221          ## $self->{next_char} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1222          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1223                                      column => $self->{column_prev} - 1,
1224                                     };
1225            ## NOTE: $self->{next_char} is intentionally left as is.
1226            ## Although the "anything else" case of the spec not explicitly
1227            ## states that the next input character is to be reconsumed,
1228            ## it will be included to the |data| of the comment token
1229            ## generated from the bogus end tag, as defined in the
1230            ## "bogus comment state" entry.
1231            redo A;
1232          }
1233        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1234          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1235          if (length $ch) {
1236            my $CH = $ch;
1237            $ch =~ tr/a-z/A-Z/;
1238            my $nch = chr $self->{next_char};
1239            if ($nch eq $ch or $nch eq $CH) {
1240              !!!cp (24);
1241              ## Stay in the state.
1242              $self->{state_keyword} .= $nch;
1243              !!!next-input-character;
1244              redo A;
1245            } else {
1246              !!!cp (25);
1247              $self->{state} = DATA_STATE;
1248              ## Reconsume.
1249              !!!emit ({type => CHARACTER_TOKEN,
1250                        data => '</' . $self->{state_keyword},
1251                        line => $self->{line_prev},
1252                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1253                       });
1254              redo A;
1255            }
1256          } else { # after "<{tag-name}"
1257            unless ({
1258                     0x0009 => 1, # HT
1259                     0x000A => 1, # LF
1260                     0x000B => 1, # VT
1261                     0x000C => 1, # FF
1262                     0x0020 => 1, # SP
1263                     0x003E => 1, # >
1264                     0x002F => 1, # /
1265                     -1 => 1, # EOF
1266                    }->{$self->{next_char}}) {
1267              !!!cp (26);
1268              ## Reconsume.
1269              $self->{state} = DATA_STATE;
1270              !!!emit ({type => CHARACTER_TOKEN,
1271                        data => '</' . $self->{state_keyword},
1272                        line => $self->{line_prev},
1273                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1274                       });
1275              redo A;
1276            } else {
1277              !!!cp (27);
1278              $self->{current_token}
1279                  = {type => END_TAG_TOKEN,
1280                     tag_name => $self->{last_emitted_start_tag_name},
1281                     line => $self->{line_prev},
1282                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1283              $self->{state} = TAG_NAME_STATE;
1284              ## Reconsume.
1285              redo A;
1286            }
1287        }        }
1288      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1289        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 659  sub _get_next_token ($) { Line 1298  sub _get_next_token ($) {
1298        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1299          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1300            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1301            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1302          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1303            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 692  sub _get_next_token ($) { Line 1329  sub _get_next_token ($) {
1329          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1330          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1331            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1332            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1333          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1334            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 714  sub _get_next_token ($) { Line 1349  sub _get_next_token ($) {
1349    
1350          redo A;          redo A;
1351        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1352            !!!cp (42);
1353            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1354          !!!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  
1355          redo A;          redo A;
1356        } else {        } else {
1357          !!!cp (44);          !!!cp (44);
# Line 749  sub _get_next_token ($) { Line 1374  sub _get_next_token ($) {
1374        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1375          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1376            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1377            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1378          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1379            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 772  sub _get_next_token ($) { Line 1395  sub _get_next_token ($) {
1395        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1396                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1397          !!!cp (49);          !!!cp (49);
1398          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1399                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1400                   value => '',
1401                   line => $self->{line}, column => $self->{column}};
1402          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1403          !!!next-input-character;          !!!next-input-character;
1404          redo A;          redo A;
1405        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1406            !!!cp (50);
1407            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1408          !!!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  
1409          redo A;          redo A;
1410        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1411          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1412          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1413            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1414            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1415          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1416            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 827  sub _get_next_token ($) { Line 1440  sub _get_next_token ($) {
1440          } else {          } else {
1441            !!!cp (56);            !!!cp (56);
1442          }          }
1443          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1444                                value => ''};              = {name => chr ($self->{next_char}),
1445                   value => '',
1446                   line => $self->{line}, column => $self->{column}};
1447          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1448          !!!next-input-character;          !!!next-input-character;
1449          redo A;          redo A;
# Line 838  sub _get_next_token ($) { Line 1453  sub _get_next_token ($) {
1453          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1454              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1455            !!!cp (57);            !!!cp (57);
1456            !!!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});
1457            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1458          } else {          } else {
1459            !!!cp (58);            !!!cp (58);
# Line 867  sub _get_next_token ($) { Line 1482  sub _get_next_token ($) {
1482          $before_leave->();          $before_leave->();
1483          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1484            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1485            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1486          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1487            !!!cp (62);            !!!cp (62);
# Line 893  sub _get_next_token ($) { Line 1506  sub _get_next_token ($) {
1506          !!!next-input-character;          !!!next-input-character;
1507          redo A;          redo A;
1508        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1509            !!!cp (64);
1510          $before_leave->();          $before_leave->();
1511            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1512          !!!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  
1513          redo A;          redo A;
1514        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1515          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1516          $before_leave->();          $before_leave->();
1517          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1518            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1519            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1520          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1521            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 965  sub _get_next_token ($) { Line 1566  sub _get_next_token ($) {
1566        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1567          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1568            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1569            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1570          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1571            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 989  sub _get_next_token ($) { Line 1588  sub _get_next_token ($) {
1588        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1589                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1590          !!!cp (76);          !!!cp (76);
1591          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1592                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1593                   value => '',
1594                   line => $self->{line}, column => $self->{column}};
1595          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1596          !!!next-input-character;          !!!next-input-character;
1597          redo A;          redo A;
1598        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1599            !!!cp (77);
1600            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1601          !!!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  
1602          redo A;          redo A;
1603        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1604          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1605          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1606            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1607            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1608          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1609            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1036  sub _get_next_token ($) { Line 1624  sub _get_next_token ($) {
1624    
1625          redo A;          redo A;
1626        } else {        } else {
1627          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1628          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1629                                value => ''};            !!!cp (78);
1630              !!!parse-error (type => 'bad attribute name');
1631            } else {
1632              !!!cp (82);
1633            }
1634            $self->{current_attribute}
1635                = {name => chr ($self->{next_char}),
1636                   value => '',
1637                   line => $self->{line}, column => $self->{column}};
1638          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1639          !!!next-input-character;          !!!next-input-character;
1640          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1665  sub _get_next_token ($) {
1665          !!!next-input-character;          !!!next-input-character;
1666          redo A;          redo A;
1667        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1668            !!!parse-error (type => 'empty unquoted attribute value');
1669          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1670            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1671            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1672          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1673            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1096  sub _get_next_token ($) { Line 1691  sub _get_next_token ($) {
1691          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1692          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1693            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1694            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1695          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1696            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1137  sub _get_next_token ($) { Line 1730  sub _get_next_token ($) {
1730          redo A;          redo A;
1731        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1732          !!!cp (96);          !!!cp (96);
1733          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1734          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1735            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1736            ## implementation of the "consume a character reference" algorithm.
1737            $self->{prev_state} = $self->{state};
1738            $self->{entity_additional} = 0x0022; # "
1739            $self->{state} = ENTITY_STATE;
1740          !!!next-input-character;          !!!next-input-character;
1741          redo A;          redo A;
1742        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1743          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1744          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1745            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1746            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1747          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1748            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1169  sub _get_next_token ($) { Line 1765  sub _get_next_token ($) {
1765        } else {        } else {
1766          !!!cp (100);          !!!cp (100);
1767          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1768            $self->{read_until}->($self->{current_attribute}->{value},
1769                                  q["&],
1770                                  length $self->{current_attribute}->{value});
1771    
1772          ## Stay in the state          ## Stay in the state
1773          !!!next-input-character;          !!!next-input-character;
1774          redo A;          redo A;
# Line 1181  sub _get_next_token ($) { Line 1781  sub _get_next_token ($) {
1781          redo A;          redo A;
1782        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1783          !!!cp (102);          !!!cp (102);
1784          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1785          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1786            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1787            ## implementation of the "consume a character reference" algorithm.
1788            $self->{entity_additional} = 0x0027; # '
1789            $self->{prev_state} = $self->{state};
1790            $self->{state} = ENTITY_STATE;
1791          !!!next-input-character;          !!!next-input-character;
1792          redo A;          redo A;
1793        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1794          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1795          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1796            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1797            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1798          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1799            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1213  sub _get_next_token ($) { Line 1816  sub _get_next_token ($) {
1816        } else {        } else {
1817          !!!cp (106);          !!!cp (106);
1818          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1819            $self->{read_until}->($self->{current_attribute}->{value},
1820                                  q['&],
1821                                  length $self->{current_attribute}->{value});
1822    
1823          ## Stay in the state          ## Stay in the state
1824          !!!next-input-character;          !!!next-input-character;
1825          redo A;          redo A;
# Line 1229  sub _get_next_token ($) { Line 1836  sub _get_next_token ($) {
1836          redo A;          redo A;
1837        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1838          !!!cp (108);          !!!cp (108);
1839          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1840          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1841            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1842            ## implementation of the "consume a character reference" algorithm.
1843            $self->{entity_additional} = -1;
1844            $self->{prev_state} = $self->{state};
1845            $self->{state} = ENTITY_STATE;
1846          !!!next-input-character;          !!!next-input-character;
1847          redo A;          redo A;
1848        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1849          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1850            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1851            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1852          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1853            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1261  sub _get_next_token ($) { Line 1871  sub _get_next_token ($) {
1871          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1872          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1873            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1874            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1875          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1876            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1294  sub _get_next_token ($) { Line 1902  sub _get_next_token ($) {
1902            !!!cp (116);            !!!cp (116);
1903          }          }
1904          $self->{current_attribute}->{value} .= chr ($self->{next_char});          $self->{current_attribute}->{value} .= chr ($self->{next_char});
1905            $self->{read_until}->($self->{current_attribute}->{value},
1906                                  q["'=& >],
1907                                  length $self->{current_attribute}->{value});
1908    
1909          ## Stay in the state          ## Stay in the state
1910          !!!next-input-character;          !!!next-input-character;
1911          redo A;          redo A;
1912        }        }
     } 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;  
1913      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1914        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1915            $self->{next_char} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
# Line 1333  sub _get_next_token ($) { Line 1923  sub _get_next_token ($) {
1923        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1924          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1925            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1926            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1927          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1928            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1355  sub _get_next_token ($) { Line 1943  sub _get_next_token ($) {
1943    
1944          redo A;          redo A;
1945        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1946            !!!cp (122);
1947            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1948          !!!next-input-character;          !!!next-input-character;
1949          if ($self->{next_char} == 0x003E and # >          redo A;
1950              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1951              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1952            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1953            !!!cp (122);            !!!cp (122.3);
1954            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1955            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1956              if ($self->{current_token}->{attributes}) {
1957                !!!cp (122.1);
1958                !!!parse-error (type => 'end tag attribute');
1959              } else {
1960                ## NOTE: This state should never be reached.
1961                !!!cp (122.2);
1962              }
1963          } else {          } else {
1964            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1965          }          }
1966          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1967          # next-input-character is already done          ## Reconsume.
1968            !!!emit ($self->{current_token}); # start tag or end tag
1969          redo A;          redo A;
1970        } else {        } else {
1971          !!!cp (124);          !!!cp ('124.1');
1972          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1973          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1974          ## reconsume          ## reconsume
1975          redo A;          redo A;
1976        }        }
1977      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1978        ## (only happen if PCDATA state)        if ($self->{next_char} == 0x003E) { # >
1979                  if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1980        my $token = {type => COMMENT_TOKEN, data => ''};            !!!cp ('124.2');
1981              !!!parse-error (type => 'nestc', token => $self->{current_token});
1982        BC: {            ## TODO: Different type than slash in start tag
1983          if ($self->{next_char} == 0x003E) { # >            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1984            !!!cp (124);            if ($self->{current_token}->{attributes}) {
1985            $self->{state} = DATA_STATE;              !!!cp ('124.4');
1986            !!!next-input-character;              !!!parse-error (type => 'end tag attribute');
1987              } else {
1988            !!!emit ($token);              !!!cp ('124.5');
1989              }
1990              ## TODO: Test |<title></title/>|
1991            } else {
1992              !!!cp ('124.3');
1993              $self->{self_closing} = 1;
1994            }
1995    
1996            redo A;          $self->{state} = DATA_STATE;
1997          } elsif ($self->{next_char} == -1) {          !!!next-input-character;
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
1998    
1999            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
2000    
2001            redo A;          redo A;
2002          } elsif ($self->{next_char} == -1) {
2003            !!!parse-error (type => 'unclosed tag');
2004            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
2005              !!!cp (124.7);
2006              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
2007            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
2008              if ($self->{current_token}->{attributes}) {
2009                !!!cp (124.5);
2010                !!!parse-error (type => 'end tag attribute');
2011              } else {
2012                ## NOTE: This state should never be reached.
2013                !!!cp (124.6);
2014              }
2015          } else {          } else {
2016            !!!cp (126);            die "$0: $self->{current_token}->{type}: Unknown token type";
           $token->{data} .= chr ($self->{next_char});  
           !!!next-input-character;  
           redo BC;  
2017          }          }
2018        } # BC          $self->{state} = DATA_STATE;
2019            ## Reconsume.
2020            !!!emit ($self->{current_token}); # start tag or end tag
2021            redo A;
2022          } else {
2023            !!!cp ('124.4');
2024            !!!parse-error (type => 'nestc');
2025            ## TODO: This error type is wrong.
2026            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
2027            ## Reconsume.
2028            redo A;
2029          }
2030        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
2031          ## (only happen if PCDATA state)
2032    
2033        die "$0: _get_next_token: unexpected case [BC]";        ## NOTE: Unlike spec's "bogus comment state", this implementation
2034          ## consumes characters one-by-one basis.
2035          
2036          if ($self->{next_char} == 0x003E) { # >
2037            !!!cp (124);
2038            $self->{state} = DATA_STATE;
2039            !!!next-input-character;
2040    
2041            !!!emit ($self->{current_token}); # comment
2042            redo A;
2043          } elsif ($self->{next_char} == -1) {
2044            !!!cp (125);
2045            $self->{state} = DATA_STATE;
2046            ## reconsume
2047    
2048            !!!emit ($self->{current_token}); # comment
2049            redo A;
2050          } else {
2051            !!!cp (126);
2052            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2053            $self->{read_until}->($self->{current_token}->{data},
2054                                  q[>],
2055                                  length $self->{current_token}->{data});
2056    
2057            ## Stay in the state.
2058            !!!next-input-character;
2059            redo A;
2060          }
2061      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2062        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my @next_char;  
       push @next_char, $self->{next_char};  
2063                
2064        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2065            !!!cp (133);
2066            $self->{state} = MD_HYPHEN_STATE;
2067          !!!next-input-character;          !!!next-input-character;
2068          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);  
         }  
2069        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2070                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2071            ## ASCII case-insensitive.
2072            !!!cp (130);
2073            $self->{state} = MD_DOCTYPE_STATE;
2074            $self->{state_keyword} = chr $self->{next_char};
2075          !!!next-input-character;          !!!next-input-character;
2076          push @next_char, $self->{next_char};          redo A;
2077          if ($self->{next_char} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2078              $self->{next_char} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2079            !!!next-input-character;                 $self->{next_char} == 0x005B) { # [
2080            push @next_char, $self->{next_char};          !!!cp (135.4);                
2081            if ($self->{next_char} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2082                $self->{next_char} == 0x0063) { # c          $self->{state_keyword} = '[';
2083              !!!next-input-character;          !!!next-input-character;
2084              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);  
         }  
2085        } else {        } else {
2086          !!!cp (136);          !!!cp (136);
2087        }        }
2088    
2089        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2090        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2091        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2092          ## Reconsume.
2093        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2094          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2095                                    line => $self->{line_prev},
2096                                    column => $self->{column_prev} - 1,
2097                                   };
2098        redo A;        redo A;
2099              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2100        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2101        ## 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);
2102            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2103                                      line => $self->{line_prev},
2104                                      column => $self->{column_prev} - 2,
2105                                     };
2106            $self->{state} = COMMENT_START_STATE;
2107            !!!next-input-character;
2108            redo A;
2109          } else {
2110            !!!cp (128);
2111            !!!parse-error (type => 'bogus comment',
2112                            line => $self->{line_prev},
2113                            column => $self->{column_prev} - 2);
2114            $self->{state} = BOGUS_COMMENT_STATE;
2115            ## Reconsume.
2116            $self->{current_token} = {type => COMMENT_TOKEN,
2117                                      data => '-',
2118                                      line => $self->{line_prev},
2119                                      column => $self->{column_prev} - 2,
2120                                     };
2121            redo A;
2122          }
2123        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2124          ## ASCII case-insensitive.
2125          if ($self->{next_char} == [
2126                undef,
2127                0x004F, # O
2128                0x0043, # C
2129                0x0054, # T
2130                0x0059, # Y
2131                0x0050, # P
2132              ]->[length $self->{state_keyword}] or
2133              $self->{next_char} == [
2134                undef,
2135                0x006F, # o
2136                0x0063, # c
2137                0x0074, # t
2138                0x0079, # y
2139                0x0070, # p
2140              ]->[length $self->{state_keyword}]) {
2141            !!!cp (131);
2142            ## Stay in the state.
2143            $self->{state_keyword} .= chr $self->{next_char};
2144            !!!next-input-character;
2145            redo A;
2146          } elsif ((length $self->{state_keyword}) == 6 and
2147                   ($self->{next_char} == 0x0045 or # E
2148                    $self->{next_char} == 0x0065)) { # e
2149            !!!cp (129);
2150            $self->{state} = DOCTYPE_STATE;
2151            $self->{current_token} = {type => DOCTYPE_TOKEN,
2152                                      quirks => 1,
2153                                      line => $self->{line_prev},
2154                                      column => $self->{column_prev} - 7,
2155                                     };
2156            !!!next-input-character;
2157            redo A;
2158          } else {
2159            !!!cp (132);        
2160            !!!parse-error (type => 'bogus comment',
2161                            line => $self->{line_prev},
2162                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2163            $self->{state} = BOGUS_COMMENT_STATE;
2164            ## Reconsume.
2165            $self->{current_token} = {type => COMMENT_TOKEN,
2166                                      data => $self->{state_keyword},
2167                                      line => $self->{line_prev},
2168                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2169                                     };
2170            redo A;
2171          }
2172        } elsif ($self->{state} == MD_CDATA_STATE) {
2173          if ($self->{next_char} == {
2174                '[' => 0x0043, # C
2175                '[C' => 0x0044, # D
2176                '[CD' => 0x0041, # A
2177                '[CDA' => 0x0054, # T
2178                '[CDAT' => 0x0041, # A
2179              }->{$self->{state_keyword}}) {
2180            !!!cp (135.1);
2181            ## Stay in the state.
2182            $self->{state_keyword} .= chr $self->{next_char};
2183            !!!next-input-character;
2184            redo A;
2185          } elsif ($self->{state_keyword} eq '[CDATA' and
2186                   $self->{next_char} == 0x005B) { # [
2187            !!!cp (135.2);
2188            $self->{current_token} = {type => CHARACTER_TOKEN,
2189                                      data => '',
2190                                      line => $self->{line_prev},
2191                                      column => $self->{column_prev} - 7};
2192            $self->{state} = CDATA_SECTION_STATE;
2193            !!!next-input-character;
2194            redo A;
2195          } else {
2196            !!!cp (135.3);
2197            !!!parse-error (type => 'bogus comment',
2198                            line => $self->{line_prev},
2199                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2200            $self->{state} = BOGUS_COMMENT_STATE;
2201            ## Reconsume.
2202            $self->{current_token} = {type => COMMENT_TOKEN,
2203                                      data => $self->{state_keyword},
2204                                      line => $self->{line_prev},
2205                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2206                                     };
2207            redo A;
2208          }
2209      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2210        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2211          !!!cp (137);          !!!cp (137);
# Line 1568  sub _get_next_token ($) { Line 2288  sub _get_next_token ($) {
2288        } else {        } else {
2289          !!!cp (147);          !!!cp (147);
2290          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
2291            $self->{read_until}->($self->{current_token}->{data},
2292                                  q[-],
2293                                  length $self->{current_token}->{data});
2294    
2295          ## Stay in the state          ## Stay in the state
2296          !!!next-input-character;          !!!next-input-character;
2297          redo A;          redo A;
# Line 1605  sub _get_next_token ($) { Line 2329  sub _get_next_token ($) {
2329          redo A;          redo A;
2330        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2331          !!!cp (152);          !!!cp (152);
2332          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2333                            line => $self->{line_prev},
2334                            column => $self->{column_prev});
2335          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2336          ## Stay in the state          ## Stay in the state
2337          !!!next-input-character;          !!!next-input-character;
# Line 1621  sub _get_next_token ($) { Line 2347  sub _get_next_token ($) {
2347          redo A;          redo A;
2348        } else {        } else {
2349          !!!cp (154);          !!!cp (154);
2350          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2351                            line => $self->{line_prev},
2352                            column => $self->{column_prev});
2353          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2354          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2355          !!!next-input-character;          !!!next-input-character;
# Line 1660  sub _get_next_token ($) { Line 2388  sub _get_next_token ($) {
2388          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2389          !!!next-input-character;          !!!next-input-character;
2390    
2391          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2392    
2393          redo A;          redo A;
2394        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1669  sub _get_next_token ($) { Line 2397  sub _get_next_token ($) {
2397          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2398          ## reconsume          ## reconsume
2399    
2400          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2401    
2402          redo A;          redo A;
2403        } else {        } else {
2404          !!!cp (160);          !!!cp (160);
2405          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2406              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2407  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2408          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2409          !!!next-input-character;          !!!next-input-character;
# Line 1751  sub _get_next_token ($) { Line 2476  sub _get_next_token ($) {
2476          redo A;          redo A;
2477        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2478                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2479            $self->{state} = PUBLIC_STATE;
2480            $self->{state_keyword} = chr $self->{next_char};
2481          !!!next-input-character;          !!!next-input-character;
2482          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);  
         }  
   
         #  
2483        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2484                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2485            $self->{state} = SYSTEM_STATE;
2486            $self->{state_keyword} = chr $self->{next_char};
2487          !!!next-input-character;          !!!next-input-character;
2488          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);  
         }  
   
         #  
2489        } else {        } else {
2490          !!!cp (180);          !!!cp (180);
2491            !!!parse-error (type => 'string after DOCTYPE name');
2492            $self->{current_token}->{quirks} = 1;
2493    
2494            $self->{state} = BOGUS_DOCTYPE_STATE;
2495          !!!next-input-character;          !!!next-input-character;
2496          #          redo A;
2497        }        }
2498        } elsif ($self->{state} == PUBLIC_STATE) {
2499          ## ASCII case-insensitive
2500          if ($self->{next_char} == [
2501                undef,
2502                0x0055, # U
2503                0x0042, # B
2504                0x004C, # L
2505                0x0049, # I
2506              ]->[length $self->{state_keyword}] or
2507              $self->{next_char} == [
2508                undef,
2509                0x0075, # u
2510                0x0062, # b
2511                0x006C, # l
2512                0x0069, # i
2513              ]->[length $self->{state_keyword}]) {
2514            !!!cp (175);
2515            ## Stay in the state.
2516            $self->{state_keyword} .= chr $self->{next_char};
2517            !!!next-input-character;
2518            redo A;
2519          } elsif ((length $self->{state_keyword}) == 5 and
2520                   ($self->{next_char} == 0x0043 or # C
2521                    $self->{next_char} == 0x0063)) { # c
2522            !!!cp (168);
2523            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2524            !!!next-input-character;
2525            redo A;
2526          } else {
2527            !!!cp (169);
2528            !!!parse-error (type => 'string after DOCTYPE name',
2529                            line => $self->{line_prev},
2530                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2531            $self->{current_token}->{quirks} = 1;
2532    
2533        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2534        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2535            redo A;
2536          }
2537        } elsif ($self->{state} == SYSTEM_STATE) {
2538          ## ASCII case-insensitive
2539          if ($self->{next_char} == [
2540                undef,
2541                0x0059, # Y
2542                0x0053, # S
2543                0x0054, # T
2544                0x0045, # E
2545              ]->[length $self->{state_keyword}] or
2546              $self->{next_char} == [
2547                undef,
2548                0x0079, # y
2549                0x0073, # s
2550                0x0074, # t
2551                0x0065, # e
2552              ]->[length $self->{state_keyword}]) {
2553            !!!cp (170);
2554            ## Stay in the state.
2555            $self->{state_keyword} .= chr $self->{next_char};
2556            !!!next-input-character;
2557            redo A;
2558          } elsif ((length $self->{state_keyword}) == 5 and
2559                   ($self->{next_char} == 0x004D or # M
2560                    $self->{next_char} == 0x006D)) { # m
2561            !!!cp (171);
2562            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2563            !!!next-input-character;
2564            redo A;
2565          } else {
2566            !!!cp (172);
2567            !!!parse-error (type => 'string after DOCTYPE name',
2568                            line => $self->{line_prev},
2569                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2570            $self->{current_token}->{quirks} = 1;
2571    
2572        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2573        # next-input-character is already done          ## Reconsume.
2574        redo A;          redo A;
2575          }
2576      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2577        if ({        if ({
2578              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 1921  sub _get_next_token ($) { Line 2657  sub _get_next_token ($) {
2657          !!!cp (190);          !!!cp (190);
2658          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2659              .= chr $self->{next_char};              .= chr $self->{next_char};
2660            $self->{read_until}->($self->{current_token}->{public_identifier},
2661                                  q[">],
2662                                  length $self->{current_token}->{public_identifier});
2663    
2664          ## Stay in the state          ## Stay in the state
2665          !!!next-input-character;          !!!next-input-character;
2666          redo A;          redo A;
# Line 1957  sub _get_next_token ($) { Line 2697  sub _get_next_token ($) {
2697          !!!cp (194);          !!!cp (194);
2698          $self->{current_token}->{public_identifier} # DOCTYPE          $self->{current_token}->{public_identifier} # DOCTYPE
2699              .= chr $self->{next_char};              .= chr $self->{next_char};
2700            $self->{read_until}->($self->{current_token}->{public_identifier},
2701                                  q['>],
2702                                  length $self->{current_token}->{public_identifier});
2703    
2704          ## Stay in the state          ## Stay in the state
2705          !!!next-input-character;          !!!next-input-character;
2706          redo A;          redo A;
# Line 2069  sub _get_next_token ($) { Line 2813  sub _get_next_token ($) {
2813          redo A;          redo A;
2814        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2815          !!!cp (208);          !!!cp (208);
2816          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2817    
2818          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2819          !!!next-input-character;          !!!next-input-character;
# Line 2093  sub _get_next_token ($) { Line 2837  sub _get_next_token ($) {
2837          !!!cp (210);          !!!cp (210);
2838          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2839              .= chr $self->{next_char};              .= chr $self->{next_char};
2840            $self->{read_until}->($self->{current_token}->{system_identifier},
2841                                  q[">],
2842                                  length $self->{current_token}->{system_identifier});
2843    
2844          ## Stay in the state          ## Stay in the state
2845          !!!next-input-character;          !!!next-input-character;
2846          redo A;          redo A;
# Line 2105  sub _get_next_token ($) { Line 2853  sub _get_next_token ($) {
2853          redo A;          redo A;
2854        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2855          !!!cp (212);          !!!cp (212);
2856          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2857    
2858          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2859          !!!next-input-character;          !!!next-input-character;
# Line 2129  sub _get_next_token ($) { Line 2877  sub _get_next_token ($) {
2877          !!!cp (214);          !!!cp (214);
2878          $self->{current_token}->{system_identifier} # DOCTYPE          $self->{current_token}->{system_identifier} # DOCTYPE
2879              .= chr $self->{next_char};              .= chr $self->{next_char};
2880            $self->{read_until}->($self->{current_token}->{system_identifier},
2881                                  q['>],
2882                                  length $self->{current_token}->{system_identifier});
2883    
2884          ## Stay in the state          ## Stay in the state
2885          !!!next-input-character;          !!!next-input-character;
2886          redo A;          redo A;
# Line 2153  sub _get_next_token ($) { Line 2905  sub _get_next_token ($) {
2905        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2906          !!!cp (217);          !!!cp (217);
2907          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2908          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2909          ## reconsume          ## reconsume
2910    
# Line 2190  sub _get_next_token ($) { Line 2941  sub _get_next_token ($) {
2941          redo A;          redo A;
2942        } else {        } else {
2943          !!!cp (221);          !!!cp (221);
2944            my $s = '';
2945            $self->{read_until}->($s, q[>], 0);
2946    
2947          ## Stay in the state          ## Stay in the state
2948          !!!next-input-character;          !!!next-input-character;
2949          redo A;          redo A;
2950        }        }
2951      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2952        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2953      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2954    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2955          
2956    die "$0: _get_next_token: unexpected case";        if ($self->{next_char} == 0x005D) { # ]
2957  } # _get_next_token          !!!cp (221.1);
2958            $self->{state} = CDATA_SECTION_MSE1_STATE;
2959            !!!next-input-character;
2960            redo A;
2961          } elsif ($self->{next_char} == -1) {
2962            $self->{state} = DATA_STATE;
2963            !!!next-input-character;
2964            if (length $self->{current_token}->{data}) { # character
2965              !!!cp (221.2);
2966              !!!emit ($self->{current_token}); # character
2967            } else {
2968              !!!cp (221.3);
2969              ## No token to emit. $self->{current_token} is discarded.
2970            }        
2971            redo A;
2972          } else {
2973            !!!cp (221.4);
2974            $self->{current_token}->{data} .= chr $self->{next_char};
2975            $self->{read_until}->($self->{current_token}->{data},
2976                                  q<]>,
2977                                  length $self->{current_token}->{data});
2978    
2979  sub _tokenize_attempt_to_consume_an_entity ($$$) {          ## Stay in the state.
2980    my ($self, $in_attr, $additional) = @_;          !!!next-input-character;
2981            redo A;
2982          }
2983    
2984    if ({        ## ISSUE: "text tokens" in spec.
2985         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2986         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{next_char} == 0x005D) { # ]
2987         $additional => 1,          !!!cp (221.5);
2988        }->{$self->{next_char}}) {          $self->{state} = CDATA_SECTION_MSE2_STATE;
2989      !!!cp (1001);          !!!next-input-character;
2990      ## Don't consume          redo A;
2991      ## No error        } else {
2992      return undef;          !!!cp (221.6);
2993    } elsif ($self->{next_char} == 0x0023) { # #          $self->{current_token}->{data} .= ']';
2994      !!!next-input-character;          $self->{state} = CDATA_SECTION_STATE;
2995      if ($self->{next_char} == 0x0078 or # x          ## Reconsume.
2996          $self->{next_char} == 0x0058) { # X          redo A;
2997        my $code;        }
2998        X: {      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2999          my $x_char = $self->{next_char};        if ($self->{next_char} == 0x003E) { # >
3000          !!!next-input-character;          $self->{state} = DATA_STATE;
3001          if (0x0030 <= $self->{next_char} and          !!!next-input-character;
3002              $self->{next_char} <= 0x0039) { # 0..9          if (length $self->{current_token}->{data}) { # character
3003            !!!cp (1002);            !!!cp (221.7);
3004            $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;  
3005          } else {          } else {
3006            !!!cp (1007);            !!!cp (221.8);
3007            !!!parse-error (type => 'no refc');            ## No token to emit. $self->{current_token} is discarded.
3008          }          }
3009            redo A;
3010          } elsif ($self->{next_char} == 0x005D) { # ]
3011            !!!cp (221.9); # character
3012            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
3013            ## Stay in the state.
3014            !!!next-input-character;
3015            redo A;
3016          } else {
3017            !!!cp (221.11);
3018            $self->{current_token}->{data} .= ']]'; # character
3019            $self->{state} = CDATA_SECTION_STATE;
3020            ## Reconsume.
3021            redo A;
3022          }
3023        } elsif ($self->{state} == ENTITY_STATE) {
3024          if ({
3025            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
3026            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
3027            $self->{entity_additional} => 1,
3028          }->{$self->{next_char}}) {
3029            !!!cp (1001);
3030            ## Don't consume
3031            ## No error
3032            ## Return nothing.
3033            #
3034          } elsif ($self->{next_char} == 0x0023) { # #
3035            !!!cp (999);
3036            $self->{state} = ENTITY_HASH_STATE;
3037            $self->{state_keyword} = '#';
3038            !!!next-input-character;
3039            redo A;
3040          } elsif ((0x0041 <= $self->{next_char} and
3041                    $self->{next_char} <= 0x005A) or # A..Z
3042                   (0x0061 <= $self->{next_char} and
3043                    $self->{next_char} <= 0x007A)) { # a..z
3044            !!!cp (998);
3045            require Whatpm::_NamedEntityList;
3046            $self->{state} = ENTITY_NAME_STATE;
3047            $self->{state_keyword} = chr $self->{next_char};
3048            $self->{entity__value} = $self->{state_keyword};
3049            $self->{entity__match} = 0;
3050            !!!next-input-character;
3051            redo A;
3052          } else {
3053            !!!cp (1027);
3054            !!!parse-error (type => 'bare ero');
3055            ## Return nothing.
3056            #
3057          }
3058    
3059          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
3060            !!!cp (1008);        ## reference" algorithm.  In other word, there is an "&" character
3061            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## that does not introduce a character reference, which would be
3062            $code = 0xFFFD;        ## appended to the parent element or the attribute value in later
3063          } elsif ($code > 0x10FFFF) {        ## process of the tokenizer.
3064            !!!cp (1009);  
3065            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        if ($self->{prev_state} == DATA_STATE) {
3066            $code = 0xFFFD;          !!!cp (997);
3067          } elsif ($code == 0x000D) {          $self->{state} = $self->{prev_state};
3068            !!!cp (1010);          ## Reconsume.
3069            !!!parse-error (type => 'CR character reference');          !!!emit ({type => CHARACTER_TOKEN, data => '&',
3070            $code = 0x000A;                    line => $self->{line_prev},
3071          } elsif (0x80 <= $code and $code <= 0x9F) {                    column => $self->{column_prev},
3072            !!!cp (1011);                   });
3073            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          redo A;
3074            $code = $c1_entity_char->{$code};        } else {
3075          }          !!!cp (996);
3076            $self->{current_attribute}->{value} .= '&';
3077          return {type => CHARACTER_TOKEN, data => chr $code,          $self->{state} = $self->{prev_state};
3078                  has_reference => 1};          ## Reconsume.
3079        } # X          redo A;
3080      } elsif (0x0030 <= $self->{next_char} and        }
3081               $self->{next_char} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
3082        my $code = $self->{next_char} - 0x0030;        if ($self->{next_char} == 0x0078 or # x
3083        !!!next-input-character;            $self->{next_char} == 0x0058) { # X
3084                  !!!cp (995);
3085        while (0x0030 <= $self->{next_char} and          $self->{state} = HEXREF_X_STATE;
3086                  $self->{next_char} <= 0x0039) { # 0..9          $self->{state_keyword} .= chr $self->{next_char};
3087            !!!next-input-character;
3088            redo A;
3089          } elsif (0x0030 <= $self->{next_char} and
3090                   $self->{next_char} <= 0x0039) { # 0..9
3091            !!!cp (994);
3092            $self->{state} = NCR_NUM_STATE;
3093            $self->{state_keyword} = $self->{next_char} - 0x0030;
3094            !!!next-input-character;
3095            redo A;
3096          } else {
3097            !!!parse-error (type => 'bare nero',
3098                            line => $self->{line_prev},
3099                            column => $self->{column_prev} - 1);
3100    
3101            ## NOTE: According to the spec algorithm, nothing is returned,
3102            ## and then "&#" is appended to the parent element or the attribute
3103            ## value in the later processing.
3104    
3105            if ($self->{prev_state} == DATA_STATE) {
3106              !!!cp (1019);
3107              $self->{state} = $self->{prev_state};
3108              ## Reconsume.
3109              !!!emit ({type => CHARACTER_TOKEN,
3110                        data => '&#',
3111                        line => $self->{line_prev},
3112                        column => $self->{column_prev} - 1,
3113                       });
3114              redo A;
3115            } else {
3116              !!!cp (993);
3117              $self->{current_attribute}->{value} .= '&#';
3118              $self->{state} = $self->{prev_state};
3119              ## Reconsume.
3120              redo A;
3121            }
3122          }
3123        } elsif ($self->{state} == NCR_NUM_STATE) {
3124          if (0x0030 <= $self->{next_char} and
3125              $self->{next_char} <= 0x0039) { # 0..9
3126          !!!cp (1012);          !!!cp (1012);
3127          $code *= 10;          $self->{state_keyword} *= 10;
3128          $code += $self->{next_char} - 0x0030;          $self->{state_keyword} += $self->{next_char} - 0x0030;
3129                    
3130            ## Stay in the state.
3131          !!!next-input-character;          !!!next-input-character;
3132        }          redo A;
3133          } elsif ($self->{next_char} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3134          !!!cp (1013);          !!!cp (1013);
3135          !!!next-input-character;          !!!next-input-character;
3136            #
3137        } else {        } else {
3138          !!!cp (1014);          !!!cp (1014);
3139          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3140            ## Reconsume.
3141            #
3142        }        }
3143    
3144          my $code = $self->{state_keyword};
3145          my $l = $self->{line_prev};
3146          my $c = $self->{column_prev};
3147        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3148          !!!cp (1015);          !!!cp (1015);
3149          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
3150                            text => (sprintf 'U+%04X', $code),
3151                            line => $l, column => $c);
3152          $code = 0xFFFD;          $code = 0xFFFD;
3153        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3154          !!!cp (1016);          !!!cp (1016);
3155          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
3156                            text => (sprintf 'U-%08X', $code),
3157                            line => $l, column => $c);
3158          $code = 0xFFFD;          $code = 0xFFFD;
3159        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3160          !!!cp (1017);          !!!cp (1017);
3161          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
3162                            line => $l, column => $c);
3163          $code = 0x000A;          $code = 0x000A;
3164        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3165          !!!cp (1018);          !!!cp (1018);
3166          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3167                            text => (sprintf 'U+%04X', $code),
3168                            line => $l, column => $c);
3169          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3170        }        }
3171          
3172        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        if ($self->{prev_state} == DATA_STATE) {
3173      } else {          !!!cp (992);
3174        !!!cp (1019);          $self->{state} = $self->{prev_state};
3175        !!!parse-error (type => 'bare nero');          ## Reconsume.
3176        !!!back-next-input-character ($self->{next_char});          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3177        $self->{next_char} = 0x0023; # #                    line => $l, column => $c,
3178        return undef;                   });
3179      }          redo A;
3180    } elsif ((0x0041 <= $self->{next_char} and        } else {
3181              $self->{next_char} <= 0x005A) or          !!!cp (991);
3182             (0x0061 <= $self->{next_char} and          $self->{current_attribute}->{value} .= chr $code;
3183              $self->{next_char} <= 0x007A)) {          $self->{current_attribute}->{has_reference} = 1;
3184      my $entity_name = chr $self->{next_char};          $self->{state} = $self->{prev_state};
3185      !!!next-input-character;          ## Reconsume.
3186            redo A;
3187      my $value = $entity_name;        }
3188      my $match = 0;      } elsif ($self->{state} == HEXREF_X_STATE) {
3189      require Whatpm::_NamedEntityList;        if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3190      our $EntityChar;            (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3191              (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3192      while (length $entity_name < 10 and          # 0..9, A..F, a..f
3193             ## NOTE: Some number greater than the maximum length of entity name          !!!cp (990);
3194             ((0x0041 <= $self->{next_char} and # a          $self->{state} = HEXREF_HEX_STATE;
3195               $self->{next_char} <= 0x005A) or # x          $self->{state_keyword} = 0;
3196              (0x0061 <= $self->{next_char} and # a          ## Reconsume.
3197               $self->{next_char} <= 0x007A) or # z          redo A;
3198              (0x0030 <= $self->{next_char} and # 0        } else {
3199               $self->{next_char} <= 0x0039) or # 9          !!!parse-error (type => 'bare hcro',
3200              $self->{next_char} == 0x003B)) { # ;                          line => $self->{line_prev},
3201        $entity_name .= chr $self->{next_char};                          column => $self->{column_prev} - 2);
3202        if (defined $EntityChar->{$entity_name}) {  
3203          if ($self->{next_char} == 0x003B) { # ;          ## NOTE: According to the spec algorithm, nothing is returned,
3204            !!!cp (1020);          ## and then "&#" followed by "X" or "x" is appended to the parent
3205            $value = $EntityChar->{$entity_name};          ## element or the attribute value in the later processing.
3206            $match = 1;  
3207            !!!next-input-character;          if ($self->{prev_state} == DATA_STATE) {
3208            last;            !!!cp (1005);
3209              $self->{state} = $self->{prev_state};
3210              ## Reconsume.
3211              !!!emit ({type => CHARACTER_TOKEN,
3212                        data => '&' . $self->{state_keyword},
3213                        line => $self->{line_prev},
3214                        column => $self->{column_prev} - length $self->{state_keyword},
3215                       });
3216              redo A;
3217          } else {          } else {
3218            !!!cp (1021);            !!!cp (989);
3219            $value = $EntityChar->{$entity_name};            $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3220            $match = -1;            $self->{state} = $self->{prev_state};
3221            !!!next-input-character;            ## Reconsume.
3222              redo A;
3223          }          }
3224        } else {        }
3225          !!!cp (1022);      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3226          $value .= chr $self->{next_char};        if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3227          $match *= 2;          # 0..9
3228            !!!cp (1002);
3229            $self->{state_keyword} *= 0x10;
3230            $self->{state_keyword} += $self->{next_char} - 0x0030;
3231            ## Stay in the state.
3232            !!!next-input-character;
3233            redo A;
3234          } elsif (0x0061 <= $self->{next_char} and
3235                   $self->{next_char} <= 0x0066) { # a..f
3236            !!!cp (1003);
3237            $self->{state_keyword} *= 0x10;
3238            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3239            ## Stay in the state.
3240            !!!next-input-character;
3241            redo A;
3242          } elsif (0x0041 <= $self->{next_char} and
3243                   $self->{next_char} <= 0x0046) { # A..F
3244            !!!cp (1004);
3245            $self->{state_keyword} *= 0x10;
3246            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3247            ## Stay in the state.
3248          !!!next-input-character;          !!!next-input-character;
3249            redo A;
3250          } elsif ($self->{next_char} == 0x003B) { # ;
3251            !!!cp (1006);
3252            !!!next-input-character;
3253            #
3254          } else {
3255            !!!cp (1007);
3256            !!!parse-error (type => 'no refc',
3257                            line => $self->{line},
3258                            column => $self->{column});
3259            ## Reconsume.
3260            #
3261        }        }
3262      }  
3263              my $code = $self->{state_keyword};
3264      if ($match > 0) {        my $l = $self->{line_prev};
3265        !!!cp (1023);        my $c = $self->{column_prev};
3266        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3267      } elsif ($match < 0) {          !!!cp (1008);
3268        !!!parse-error (type => 'no refc');          !!!parse-error (type => 'invalid character reference',
3269        if ($in_attr and $match < -1) {                          text => (sprintf 'U+%04X', $code),
3270          !!!cp (1024);                          line => $l, column => $c);
3271          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          $code = 0xFFFD;
3272          } elsif ($code > 0x10FFFF) {
3273            !!!cp (1009);
3274            !!!parse-error (type => 'invalid character reference',
3275                            text => (sprintf 'U-%08X', $code),
3276                            line => $l, column => $c);
3277            $code = 0xFFFD;
3278          } elsif ($code == 0x000D) {
3279            !!!cp (1010);
3280            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3281            $code = 0x000A;
3282          } elsif (0x80 <= $code and $code <= 0x9F) {
3283            !!!cp (1011);
3284            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3285            $code = $c1_entity_char->{$code};
3286          }
3287    
3288          if ($self->{prev_state} == DATA_STATE) {
3289            !!!cp (988);
3290            $self->{state} = $self->{prev_state};
3291            ## Reconsume.
3292            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3293                      line => $l, column => $c,
3294                     });
3295            redo A;
3296          } else {
3297            !!!cp (987);
3298            $self->{current_attribute}->{value} .= chr $code;
3299            $self->{current_attribute}->{has_reference} = 1;
3300            $self->{state} = $self->{prev_state};
3301            ## Reconsume.
3302            redo A;
3303          }
3304        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3305          if (length $self->{state_keyword} < 30 and
3306              ## NOTE: Some number greater than the maximum length of entity name
3307              ((0x0041 <= $self->{next_char} and # a
3308                $self->{next_char} <= 0x005A) or # x
3309               (0x0061 <= $self->{next_char} and # a
3310                $self->{next_char} <= 0x007A) or # z
3311               (0x0030 <= $self->{next_char} and # 0
3312                $self->{next_char} <= 0x0039) or # 9
3313               $self->{next_char} == 0x003B)) { # ;
3314            our $EntityChar;
3315            $self->{state_keyword} .= chr $self->{next_char};
3316            if (defined $EntityChar->{$self->{state_keyword}}) {
3317              if ($self->{next_char} == 0x003B) { # ;
3318                !!!cp (1020);
3319                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3320                $self->{entity__match} = 1;
3321                !!!next-input-character;
3322                #
3323              } else {
3324                !!!cp (1021);
3325                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3326                $self->{entity__match} = -1;
3327                ## Stay in the state.
3328                !!!next-input-character;
3329                redo A;
3330              }
3331            } else {
3332              !!!cp (1022);
3333              $self->{entity__value} .= chr $self->{next_char};
3334              $self->{entity__match} *= 2;
3335              ## Stay in the state.
3336              !!!next-input-character;
3337              redo A;
3338            }
3339          }
3340    
3341          my $data;
3342          my $has_ref;
3343          if ($self->{entity__match} > 0) {
3344            !!!cp (1023);
3345            $data = $self->{entity__value};
3346            $has_ref = 1;
3347            #
3348          } elsif ($self->{entity__match} < 0) {
3349            !!!parse-error (type => 'no refc');
3350            if ($self->{prev_state} != DATA_STATE and # in attribute
3351                $self->{entity__match} < -1) {
3352              !!!cp (1024);
3353              $data = '&' . $self->{state_keyword};
3354              #
3355            } else {
3356              !!!cp (1025);
3357              $data = $self->{entity__value};
3358              $has_ref = 1;
3359              #
3360            }
3361        } else {        } else {
3362          !!!cp (1025);          !!!cp (1026);
3363          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          !!!parse-error (type => 'bare ero',
3364                            line => $self->{line_prev},
3365                            column => $self->{column_prev} - length $self->{state_keyword});
3366            $data = '&' . $self->{state_keyword};
3367            #
3368          }
3369      
3370          ## NOTE: In these cases, when a character reference is found,
3371          ## it is consumed and a character token is returned, or, otherwise,
3372          ## nothing is consumed and returned, according to the spec algorithm.
3373          ## In this implementation, anything that has been examined by the
3374          ## tokenizer is appended to the parent element or the attribute value
3375          ## as string, either literal string when no character reference or
3376          ## entity-replaced string otherwise, in this stage, since any characters
3377          ## that would not be consumed are appended in the data state or in an
3378          ## appropriate attribute value state anyway.
3379    
3380          if ($self->{prev_state} == DATA_STATE) {
3381            !!!cp (986);
3382            $self->{state} = $self->{prev_state};
3383            ## Reconsume.
3384            !!!emit ({type => CHARACTER_TOKEN,
3385                      data => $data,
3386                      line => $self->{line_prev},
3387                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3388                     });
3389            redo A;
3390          } else {
3391            !!!cp (985);
3392            $self->{current_attribute}->{value} .= $data;
3393            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3394            $self->{state} = $self->{prev_state};
3395            ## Reconsume.
3396            redo A;
3397        }        }
3398      } else {      } else {
3399        !!!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};  
3400      }      }
3401    } else {    } # A  
3402      !!!cp (1027);  
3403      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3404      !!!parse-error (type => 'bare ero');  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3405    
3406  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3407    my $self = shift;    my $self = shift;
# Line 2402  sub _initialize_tree_constructor ($) { Line 3410  sub _initialize_tree_constructor ($) {
3410    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3411    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3412    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3413      $self->{document}->set_user_data (manakai_source_line => 1);
3414      $self->{document}->set_user_data (manakai_source_column => 1);
3415  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3416    
3417  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2456  sub _tree_construction_initial ($) { Line 3466  sub _tree_construction_initial ($) {
3466        ## language.        ## language.
3467        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3468        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3469        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3470        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3471            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3472          !!!cp ('t1');          !!!cp ('t1');
3473          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3474        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3475          !!!cp ('t2');          !!!cp ('t2');
3476          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!parse-error (type => 'not HTML5', token => $token);
3477          !!!parse-error (type => 'not HTML5');        } elsif (defined $token->{public_identifier}) {
3478            if ($token->{public_identifier} eq 'XSLT-compat') {
3479              !!!cp ('t1.2');
3480              !!!parse-error (type => 'XSLT-compat', token => $token,
3481                              level => $self->{level}->{should});
3482            } else {
3483              !!!parse-error (type => 'not HTML5', token => $token);
3484            }
3485        } else {        } else {
3486          !!!cp ('t3');          !!!cp ('t3');
3487            #
3488        }        }
3489                
3490        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3491          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3492          ## NOTE: Default value for both |public_id| and |system_id| attributes
3493          ## are empty strings, so that we don't set any value in missing cases.
3494        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3495            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3496        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2486  sub _tree_construction_initial ($) { Line 3505  sub _tree_construction_initial ($) {
3505        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3506          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3507          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3508          if ({          my $prefix = [
3509            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3510            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3511            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3512            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3513            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3514            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3515            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3516            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3517            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3518            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3519            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3520            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3521            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3522            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3523            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3524            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3525            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3526            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3527            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3528            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3529            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3530            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3531            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3532            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3533            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3534            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3535            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3536            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3537            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3538            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3539            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3540            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3541            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3542            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3543            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3544            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3545            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3546            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3547            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3548            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3549            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3550            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3551            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3552            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3553            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3554            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3555            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3556            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3557            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3558            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3559            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3560            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3561            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3562            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3563            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3564            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3565            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3566            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3567            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3568            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3569            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3570            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3571            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3572            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3573            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3574            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3575            "-//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}) {  
3576            !!!cp ('t5');            !!!cp ('t5');
3577            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3578          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3579                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3580            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3581              !!!cp ('t6');              !!!cp ('t6');
3582              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2571  sub _tree_construction_initial ($) { Line 3584  sub _tree_construction_initial ($) {
3584              !!!cp ('t7');              !!!cp ('t7');
3585              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3586            }            }
3587          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3588                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3589            !!!cp ('t8');            !!!cp ('t8');
3590            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3591          } else {          } else {
# Line 2585  sub _tree_construction_initial ($) { Line 3598  sub _tree_construction_initial ($) {
3598          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3599          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3600          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") {
3601            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3602              ## marked as quirks.
3603            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3604            !!!cp ('t11');            !!!cp ('t11');
3605          } else {          } else {
# Line 2604  sub _tree_construction_initial ($) { Line 3618  sub _tree_construction_initial ($) {
3618                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3619               }->{$token->{type}}) {               }->{$token->{type}}) {
3620        !!!cp ('t14');        !!!cp ('t14');
3621        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3622        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3623        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3624        ## reprocess        ## reprocess
3625          !!!ack-later;
3626        return;        return;
3627      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3628        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D        if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) { # \x0D
# Line 2625  sub _tree_construction_initial ($) { Line 3640  sub _tree_construction_initial ($) {
3640          !!!cp ('t17');          !!!cp ('t17');
3641        }        }
3642    
3643        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3644        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3645        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3646        ## reprocess        ## reprocess
# Line 2654  sub _tree_construction_root_element ($) Line 3669  sub _tree_construction_root_element ($)
3669    B: {    B: {
3670        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3671          !!!cp ('t19');          !!!cp ('t19');
3672          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3673          ## Ignore the token          ## Ignore the token
3674          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3675          !!!next-token;          !!!next-token;
# Line 2688  sub _tree_construction_root_element ($) Line 3703  sub _tree_construction_root_element ($)
3703        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3704          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3705            my $root_element;            my $root_element;
3706            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3707            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3708            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3709                  [$root_element, $el_category->{html}];
3710    
3711            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3712              !!!cp ('t24');              !!!cp ('t24');
3713              $self->{application_cache_selection}              $self->{application_cache_selection}
3714                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3715              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3716                ## According to Hixie (#whatwg 2008-03-19), it should be
3717                ## resolved against the base URI of the document in HTML
3718                ## or xml:base of the element in XHTML.
3719            } else {            } else {
3720              !!!cp ('t25');              !!!cp ('t25');
3721              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3722            }            }
3723    
3724              !!!nack ('t25c');
3725    
3726            !!!next-token;            !!!next-token;
3727            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3728          } else {          } else {
# Line 2718  sub _tree_construction_root_element ($) Line 3739  sub _tree_construction_root_element ($)
3739          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3740        }        }
3741    
3742      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3743        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3744      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3745      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3746    
3747      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3748    
3749      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3750        !!!ack-later;
3751      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3752    
3753      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2748  sub _reset_insertion_mode ($) { Line 3771  sub _reset_insertion_mode ($) {
3771        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3772          $last = 1;          $last = 1;
3773          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3774            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3775                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3776              !!!cp ('t27');          } else {
3777              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3778          }          }
3779        }        }
3780              
3781        ## Step 4..13        ## Step 4..14
3782        my $new_mode = {        my $new_mode;
3783          if ($node->[1] & FOREIGN_EL) {
3784            !!!cp ('t28.1');
3785            ## NOTE: Strictly spaking, the line below only applies to MathML and
3786            ## SVG elements.  Currently the HTML syntax supports only MathML and
3787            ## SVG elements as foreigners.
3788            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3789          } elsif ($node->[1] & TABLE_CELL_EL) {
3790            if ($last) {
3791              !!!cp ('t28.2');
3792              #
3793            } else {
3794              !!!cp ('t28.3');
3795              $new_mode = IN_CELL_IM;
3796            }
3797          } else {
3798            !!!cp ('t28.4');
3799            $new_mode = {
3800                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3801                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3802                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3803                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3804                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3805                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2776  sub _reset_insertion_mode ($) { Line 3810  sub _reset_insertion_mode ($) {
3810                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3811                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3812                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3813                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3814          }
3815        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3816                
3817        ## Step 14        ## Step 15
3818        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3819          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3820            !!!cp ('t29');            !!!cp ('t29');
3821            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2794  sub _reset_insertion_mode ($) { Line 3829  sub _reset_insertion_mode ($) {
3829          !!!cp ('t31');          !!!cp ('t31');
3830        }        }
3831                
3832        ## Step 15        ## Step 16
3833        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3834                
3835        ## Step 16        ## Step 17
3836        $i--;        $i--;
3837        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3838                
3839        ## Step 17        ## Step 18
3840        redo S3;        redo S3;
3841      } # S3      } # S3
3842    
# Line 2913  sub _tree_construction_main ($) { Line 3948  sub _tree_construction_main ($) {
3948      ## Step 1      ## Step 1
3949      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3950      my $el;      my $el;
3951      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3952    
3953      ## Step 2      ## Step 2
3954      $insert->($el);      $insert->($el);
# Line 2924  sub _tree_construction_main ($) { Line 3959  sub _tree_construction_main ($) {
3959    
3960      ## Step 4      ## Step 4
3961      my $text = '';      my $text = '';
3962        !!!nack ('t40.1');
3963      !!!next-token;      !!!next-token;
3964      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3965        !!!cp ('t40');        !!!cp ('t40');
# Line 2950  sub _tree_construction_main ($) { Line 3986  sub _tree_construction_main ($) {
3986        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3987        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3988          !!!cp ('t43');          !!!cp ('t43');
3989          !!!parse-error (type => 'in CDATA:#'.$token->{type});          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3990        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3991          !!!cp ('t44');          !!!cp ('t44');
3992          !!!parse-error (type => 'in RCDATA:#'.$token->{type});          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3993        } else {        } else {
3994          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3995        }        }
# Line 2963  sub _tree_construction_main ($) { Line 3999  sub _tree_construction_main ($) {
3999    
4000    my $script_start_tag = sub () {    my $script_start_tag = sub () {
4001      my $script_el;      my $script_el;
4002      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
4003      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
4004    
4005      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
4006      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
4007            
4008      my $text = '';      my $text = '';
4009        !!!nack ('t45.1');
4010      !!!next-token;      !!!next-token;
4011      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
4012        !!!cp ('t45');        !!!cp ('t45');
# Line 2989  sub _tree_construction_main ($) { Line 4026  sub _tree_construction_main ($) {
4026        ## Ignore the token        ## Ignore the token
4027      } else {      } else {
4028        !!!cp ('t48');        !!!cp ('t48');
4029        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#eof', token => $token);
4030        ## ISSUE: And ignore?        ## ISSUE: And ignore?
4031        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
4032      }      }
# Line 3017  sub _tree_construction_main ($) { Line 4054  sub _tree_construction_main ($) {
4054    my $open_tables = [[$self->{open_elements}->[0]->[0]]];    my $open_tables = [[$self->{open_elements}->[0]->[0]]];
4055    
4056    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
4057      my $tag_name = shift;      my $end_tag_token = shift;
4058        my $tag_name = $end_tag_token->{tag_name};
4059    
4060      ## NOTE: The adoptioon agency algorithm (AAA).      ## NOTE: The adoption agency algorithm (AAA).
4061    
4062      FET: {      FET: {
4063        ## Step 1        ## Step 1
4064        my $formatting_element;        my $formatting_element;
4065        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
4066        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
4067          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
4068              !!!cp ('t52');
4069              last AFE;
4070            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
4071                         eq $tag_name) {
4072            !!!cp ('t51');            !!!cp ('t51');
4073            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
4074            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
4075            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
4076          }          }
4077        } # AFE        } # AFE
4078        unless (defined $formatting_element) {        unless (defined $formatting_element) {
4079          !!!cp ('t53');          !!!cp ('t53');
4080          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
4081          ## Ignore the token          ## Ignore the token
4082          !!!next-token;          !!!next-token;
4083          return;          return;
# Line 3055  sub _tree_construction_main ($) { Line 4094  sub _tree_construction_main ($) {
4094              last INSCOPE;              last INSCOPE;
4095            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4096              !!!cp ('t55');              !!!cp ('t55');
4097              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
4098                                text => $token->{tag_name},
4099                                token => $end_tag_token);
4100              ## Ignore the token              ## Ignore the token
4101              !!!next-token;              !!!next-token;
4102              return;              return;
4103            }            }
4104          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4105            !!!cp ('t56');            !!!cp ('t56');
4106            $in_scope = 0;            $in_scope = 0;
4107          }          }
4108        } # INSCOPE        } # INSCOPE
4109        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4110          !!!cp ('t57');          !!!cp ('t57');
4111          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
4112                            text => $token->{tag_name},
4113                            token => $end_tag_token);
4114          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4115          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4116          return;          return;
4117        }        }
4118        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4119          !!!cp ('t58');          !!!cp ('t58');
4120          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
4121                            text => $self->{open_elements}->[-1]->[0]
4122                                ->manakai_local_name,
4123                            token => $end_tag_token);
4124        }        }
4125                
4126        ## Step 2        ## Step 2
# Line 3085  sub _tree_construction_main ($) { Line 4128  sub _tree_construction_main ($) {
4128        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4129        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4130          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4131          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4132              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4133              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4134               $scoping_category->{$node->[1]})) {               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4135            !!!cp ('t59');            !!!cp ('t59');
4136            $furthest_block = $node;            $furthest_block = $node;
4137            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3174  sub _tree_construction_main ($) { Line 4217  sub _tree_construction_main ($) {
4217        } # S7          } # S7  
4218                
4219        ## Step 8        ## Step 8
4220        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
4221          my $foster_parent_element;          my $foster_parent_element;
4222          my $next_sibling;          my $next_sibling;
4223                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
4224                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4225                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4226                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4227                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3253  sub _tree_construction_main ($) { Line 4294  sub _tree_construction_main ($) {
4294    
4295    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4296      my $child = shift;      my $child = shift;
4297      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]}) {  
4298        # MUST        # MUST
4299        my $foster_parent_element;        my $foster_parent_element;
4300        my $next_sibling;        my $next_sibling;
4301                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4302                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4303                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4304                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4305                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3285  sub _tree_construction_main ($) { Line 4324  sub _tree_construction_main ($) {
4324      }      }
4325    }; # $insert_to_foster    }; # $insert_to_foster
4326    
4327    B: {    B: while (1) {
4328      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4329        !!!cp ('t73');        !!!cp ('t73');
4330        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4331        ## Ignore the token        ## Ignore the token
4332        ## Stay in the phase        ## Stay in the phase
4333        !!!next-token;        !!!next-token;
4334        redo B;        next B;
     } elsif ($token->{type} == END_OF_FILE_TOKEN) {  
       if ($self->{insertion_mode} & AFTER_HTML_IMS) {  
         !!!cp ('t74');  
         #  
       } else {  
         ## Generate implied end tags  
         while ({  
                 dd => 1, dt => 1, li => 1, p => 1,  
                }->{$self->{open_elements}->[-1]->[1]}) {  
           !!!cp ('t75');  
           pop @{$self->{open_elements}};  
         }  
           
         if (@{$self->{open_elements}} > 2 or  
             (@{$self->{open_elements}} == 2 and $self->{open_elements}->[1]->[1] ne 'body')) {  
           !!!cp ('t76');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } elsif (defined $self->{inner_html_node} and  
                  @{$self->{open_elements}} > 1 and  
                  $self->{open_elements}->[1]->[1] ne 'body') {  
 ## ISSUE: This case is never reached.  
           !!!cp ('t77');  
           !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);  
         } else {  
           !!!cp ('t78');  
         }  
   
         ## ISSUE: There is an issue in the spec.  
       }  
   
       ## Stop parsing  
       last B;  
4335      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4336               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4337        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4338          !!!cp ('t79');          !!!cp ('t79');
4339          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4340          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4341        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4342          !!!cp ('t80');          !!!cp ('t80');
4343          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4344          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4345        } else {        } else {
4346          !!!cp ('t81');          !!!cp ('t81');
4347        }        }
4348    
4349        !!!cp ('t82');        !!!cp ('t82');
4350        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
4351        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4352        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4353          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3350  sub _tree_construction_main ($) { Line 4357  sub _tree_construction_main ($) {
4357               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4358          }          }
4359        }        }
4360          !!!nack ('t84.1');
4361        !!!next-token;        !!!next-token;
4362        redo B;        next B;
4363      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4364        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4365        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3365  sub _tree_construction_main ($) { Line 4373  sub _tree_construction_main ($) {
4373          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4374        }        }
4375        !!!next-token;        !!!next-token;
4376        redo B;        next B;
4377      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4378          if ($token->{type} == CHARACTER_TOKEN) {
4379            !!!cp ('t87.1');
4380            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4381            !!!next-token;
4382            next B;
4383          } elsif ($token->{type} == START_TAG_TOKEN) {
4384            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4385                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4386                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4387                ($token->{tag_name} eq 'svg' and
4388                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4389              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4390              !!!cp ('t87.2');
4391              #
4392            } elsif ({
4393                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4394                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4395                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4396                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4397                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4398                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4399                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4400                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4401                     }->{$token->{tag_name}}) {
4402              !!!cp ('t87.2');
4403              !!!parse-error (type => 'not closed',
4404                              text => $self->{open_elements}->[-1]->[0]
4405                                  ->manakai_local_name,
4406                              token => $token);
4407    
4408              pop @{$self->{open_elements}}
4409                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4410    
4411              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4412              ## Reprocess.
4413              next B;
4414            } else {
4415              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4416              my $tag_name = $token->{tag_name};
4417              if ($nsuri eq $SVG_NS) {
4418                $tag_name = {
4419                   altglyph => 'altGlyph',
4420                   altglyphdef => 'altGlyphDef',
4421                   altglyphitem => 'altGlyphItem',
4422                   animatecolor => 'animateColor',
4423                   animatemotion => 'animateMotion',
4424                   animatetransform => 'animateTransform',
4425                   clippath => 'clipPath',
4426                   feblend => 'feBlend',
4427                   fecolormatrix => 'feColorMatrix',
4428                   fecomponenttransfer => 'feComponentTransfer',
4429                   fecomposite => 'feComposite',
4430                   feconvolvematrix => 'feConvolveMatrix',
4431                   fediffuselighting => 'feDiffuseLighting',
4432                   fedisplacementmap => 'feDisplacementMap',
4433                   fedistantlight => 'feDistantLight',
4434                   feflood => 'feFlood',
4435                   fefunca => 'feFuncA',
4436                   fefuncb => 'feFuncB',
4437                   fefuncg => 'feFuncG',
4438                   fefuncr => 'feFuncR',
4439                   fegaussianblur => 'feGaussianBlur',
4440                   feimage => 'feImage',
4441                   femerge => 'feMerge',
4442                   femergenode => 'feMergeNode',
4443                   femorphology => 'feMorphology',
4444                   feoffset => 'feOffset',
4445                   fepointlight => 'fePointLight',
4446                   fespecularlighting => 'feSpecularLighting',
4447                   fespotlight => 'feSpotLight',
4448                   fetile => 'feTile',
4449                   feturbulence => 'feTurbulence',
4450                   foreignobject => 'foreignObject',
4451                   glyphref => 'glyphRef',
4452                   lineargradient => 'linearGradient',
4453                   radialgradient => 'radialGradient',
4454                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4455                   textpath => 'textPath',  
4456                }->{$tag_name} || $tag_name;
4457              }
4458    
4459              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4460    
4461              ## "adjust foreign attributes" - done in insert-element-f
4462    
4463              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4464    
4465              if ($self->{self_closing}) {
4466                pop @{$self->{open_elements}};
4467                !!!ack ('t87.3');
4468              } else {
4469                !!!cp ('t87.4');
4470              }
4471    
4472              !!!next-token;
4473              next B;
4474            }
4475          } elsif ($token->{type} == END_TAG_TOKEN) {
4476            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4477            !!!cp ('t87.5');
4478            #
4479          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4480            !!!cp ('t87.6');
4481            !!!parse-error (type => 'not closed',
4482                            text => $self->{open_elements}->[-1]->[0]
4483                                ->manakai_local_name,
4484                            token => $token);
4485    
4486            pop @{$self->{open_elements}}
4487                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4488    
4489            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4490            ## Reprocess.
4491            next B;
4492          } else {
4493            die "$0: $token->{type}: Unknown token type";        
4494          }
4495        }
4496    
4497        if ($self->{insertion_mode} & HEAD_IMS) {
4498        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4499          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4500            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4501              !!!cp ('t88.2');              !!!cp ('t88.2');
4502              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);              $self->{open_elements}->[-1]->[0]->manakai_append_text ($1);
4503                #
4504            } else {            } else {
4505              !!!cp ('t88.1');              !!!cp ('t88.1');
4506              ## Ignore the token.              ## Ignore the token.
4507              !!!next-token;              #
             redo B;  
4508            }            }
4509            unless (length $token->{data}) {            unless (length $token->{data}) {
4510              !!!cp ('t88');              !!!cp ('t88');
4511              !!!next-token;              !!!next-token;
4512              redo B;              next B;
4513            }            }
4514    ## TODO: set $token->{column} appropriately
4515          }          }
4516    
4517          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4518            !!!cp ('t89');            !!!cp ('t89');
4519            ## As if <head>            ## As if <head>
4520            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4521            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4522            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4523                  [$self->{head_element}, $el_category->{head}];
4524    
4525            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4526            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3400  sub _tree_construction_main ($) { Line 4530  sub _tree_construction_main ($) {
4530            !!!cp ('t90');            !!!cp ('t90');
4531            ## As if </noscript>            ## As if </noscript>
4532            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4533            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4534                        
4535            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4536            ## As if </head>            ## As if </head>
# Line 3416  sub _tree_construction_main ($) { Line 4546  sub _tree_construction_main ($) {
4546            !!!cp ('t92');            !!!cp ('t92');
4547          }          }
4548    
4549              ## "after head" insertion mode          ## "after head" insertion mode
4550              ## As if <body>          ## As if <body>
4551              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4552              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4553              ## reprocess          ## reprocess
4554              redo B;          next B;
4555            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4556              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4557                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4558                  !!!cp ('t93');              !!!cp ('t93');
4559                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4560                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4561                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4562                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4563                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4564                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4565                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4566                  !!!cp ('t94');              !!!next-token;
4567                  #              next B;
4568                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4569                  !!!cp ('t95');              !!!cp ('t93.2');
4570                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4571                  ## Ignore the token                              token => $token);
4572                  !!!next-token;              ## Ignore the token
4573                  redo B;              !!!nack ('t93.3');
4574                }              !!!next-token;
4575              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4576                !!!cp ('t96');            } else {
4577                ## As if <head>              !!!cp ('t95');
4578                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4579                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4580                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4581                !!!nack ('t95.1');
4582                !!!next-token;
4583                next B;
4584              }
4585            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4586              !!!cp ('t96');
4587              ## As if <head>
4588              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4589              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4590              push @{$self->{open_elements}},
4591                  [$self->{head_element}, $el_category->{head}];
4592    
4593                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4594                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4595              } else {          } else {
4596                !!!cp ('t97');            !!!cp ('t97');
4597              }          }
4598    
4599              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4600                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4601                  !!!cp ('t98');                  !!!cp ('t98');
4602                  ## As if </noscript>                  ## As if </noscript>
4603                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4604                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4605                                    token => $token);
4606                                
4607                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4608                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3471  sub _tree_construction_main ($) { Line 4613  sub _tree_construction_main ($) {
4613                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4614                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4615                  !!!cp ('t100');                  !!!cp ('t100');
4616                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4617                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4618                    push @{$self->{open_elements}},
4619                        [$self->{head_element}, $el_category->{head}];
4620                } else {                } else {
4621                  !!!cp ('t101');                  !!!cp ('t101');
4622                }                }
4623                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4624                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4625                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4626                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4627                  !!!nack ('t101.1');
4628                !!!next-token;                !!!next-token;
4629                redo B;                next B;
4630              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4631                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4632                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4633                  !!!cp ('t102');                  !!!cp ('t102');
4634                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4635                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4636                    push @{$self->{open_elements}},
4637                        [$self->{head_element}, $el_category->{head}];
4638                } else {                } else {
4639                  !!!cp ('t103');                  !!!cp ('t103');
4640                }                }
4641                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4642                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4643                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4644                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4645                  !!!ack ('t103.1');
4646                !!!next-token;                !!!next-token;
4647                redo B;                next B;
4648              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4649                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4650                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4651                  !!!cp ('t104');                  !!!cp ('t104');
4652                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4653                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4654                    push @{$self->{open_elements}},
4655                        [$self->{head_element}, $el_category->{head}];
4656                } else {                } else {
4657                  !!!cp ('t105');                  !!!cp ('t105');
4658                }                }
4659                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4660                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.
4661    
4662                unless ($self->{confident}) {                unless ($self->{confident}) {
4663                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4664                    !!!cp ('t106');                    !!!cp ('t106');
4665                      ## NOTE: Whether the encoding is supported or not is handled
4666                      ## in the {change_encoding} callback.
4667                    $self->{change_encoding}                    $self->{change_encoding}
4668                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4669                             $token);
4670                                        
4671                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4672                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4673                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4674                                                 ->{has_reference});                                                 ->{has_reference});
4675                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4676                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4677                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4678                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4679                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4680                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4681                      !!!cp ('t107');                      !!!cp ('t107');
4682                        ## NOTE: Whether the encoding is supported or not is handled
4683                        ## in the {change_encoding} callback.
4684                      $self->{change_encoding}                      $self->{change_encoding}
4685                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4686                               $token);
4687                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4688                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4689                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3556  sub _tree_construction_main ($) { Line 4711  sub _tree_construction_main ($) {
4711    
4712                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4713                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4714                  !!!ack ('t110.1');
4715                !!!next-token;                !!!next-token;
4716                redo B;                next B;
4717              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4718                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4719                  !!!cp ('t111');                  !!!cp ('t111');
4720                  ## As if </noscript>                  ## As if </noscript>
4721                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4722                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
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 ('t112');                  !!!cp ('t112');
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 ('t113');                  !!!cp ('t113');
4735                }                }
# Line 3581  sub _tree_construction_main ($) { Line 4740  sub _tree_construction_main ($) {
4740                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4741                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4742                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4743                redo B;                next B;
4744              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4745                         $token->{tag_name} eq 'noframes') {
4746                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4747                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4748                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4749                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4750                  !!!cp ('t114');                  !!!cp ('t114');
4751                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4752                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4753                    push @{$self->{open_elements}},
4754                        [$self->{head_element}, $el_category->{head}];
4755                } else {                } else {
4756                  !!!cp ('t115');                  !!!cp ('t115');
4757                }                }
4758                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4759                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4760                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4761                redo B;                next B;
4762              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4763                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4764                  !!!cp ('t116');                  !!!cp ('t116');
4765                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4766                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4767                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4768                    !!!nack ('t116.1');
4769                  !!!next-token;                  !!!next-token;
4770                  redo B;                  next B;
4771                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4772                  !!!cp ('t117');                  !!!cp ('t117');
4773                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
4774                                    token => $token);
4775                  ## Ignore the token                  ## Ignore the token
4776                    !!!nack ('t117.1');
4777                  !!!next-token;                  !!!next-token;
4778                  redo B;                  next B;
4779                } else {                } else {
4780                  !!!cp ('t118');                  !!!cp ('t118');
4781                  #                  #
# Line 3620  sub _tree_construction_main ($) { Line 4785  sub _tree_construction_main ($) {
4785                  !!!cp ('t119');                  !!!cp ('t119');
4786                  ## As if </noscript>                  ## As if </noscript>
4787                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4788                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4789                                    token => $token);
4790                                
4791                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4792                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4793                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4794                  !!!cp ('t120');                  !!!cp ('t120');
4795                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4796                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4797                    push @{$self->{open_elements}},
4798                        [$self->{head_element}, $el_category->{head}];
4799                } else {                } else {
4800                  !!!cp ('t121');                  !!!cp ('t121');
4801                }                }
# Line 3636  sub _tree_construction_main ($) { Line 4804  sub _tree_construction_main ($) {
4804                $script_start_tag->();                $script_start_tag->();
4805                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4806                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4807                redo B;                next B;
4808              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4809                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4810                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4811                  !!!cp ('t122');                  !!!cp ('t122');
4812                  ## As if </noscript>                  ## As if </noscript>
4813                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4814                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4815                                    text => $token->{tag_name}, token => $token);
4816                                    
4817                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4818                  ## As if </head>                  ## As if </head>
# Line 3660  sub _tree_construction_main ($) { Line 4829  sub _tree_construction_main ($) {
4829                }                }
4830    
4831                ## "after head" insertion mode                ## "after head" insertion mode
4832                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4833                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4834                  !!!cp ('t126');                  !!!cp ('t126');
4835                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3670  sub _tree_construction_main ($) { Line 4839  sub _tree_construction_main ($) {
4839                } else {                } else {
4840                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4841                }                }
4842                  !!!nack ('t127.1');
4843                !!!next-token;                !!!next-token;
4844                redo B;                next B;
4845              } else {              } else {
4846                !!!cp ('t128');                !!!cp ('t128');
4847                #                #
# Line 3681  sub _tree_construction_main ($) { Line 4851  sub _tree_construction_main ($) {
4851                !!!cp ('t129');                !!!cp ('t129');
4852                ## As if </noscript>                ## As if </noscript>
4853                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4854                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4855                                  text => $token->{tag_name}, token => $token);
4856                                
4857                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4858                ## As if </head>                ## As if </head>
# Line 3700  sub _tree_construction_main ($) { Line 4871  sub _tree_construction_main ($) {
4871    
4872              ## "after head" insertion mode              ## "after head" insertion mode
4873              ## As if <body>              ## As if <body>
4874              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4875              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4876              ## reprocess              ## reprocess
4877              redo B;              !!!ack-later;
4878                next B;
4879            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4880              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4881                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4882                  !!!cp ('t132');                  !!!cp ('t132');
4883                  ## As if <head>                  ## As if <head>
4884                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4885                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4886                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4887                        [$self->{head_element}, $el_category->{head}];
4888    
4889                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4890                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4891                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4892                  !!!next-token;                  !!!next-token;
4893                  redo B;                  next B;
4894                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4895                  !!!cp ('t133');                  !!!cp ('t133');
4896                  ## As if </noscript>                  ## As if </noscript>
4897                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4898                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/',
4899                                    text => 'head', token => $token);
4900                                    
4901                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4902                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4903                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4904                  !!!next-token;                  !!!next-token;
4905                  redo B;                  next B;
4906                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4907                  !!!cp ('t134');                  !!!cp ('t134');
4908                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4909                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4910                  !!!next-token;                  !!!next-token;
4911                  redo B;                  next B;
4912                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4913                    !!!cp ('t134.1');
4914                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4915                                    token => $token);
4916                    ## Ignore the token
4917                    !!!next-token;
4918                    next B;
4919                } else {                } else {
4920                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4921                }                }
4922              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4923                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3745  sub _tree_construction_main ($) { Line 4925  sub _tree_construction_main ($) {
4925                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4926                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4927                  !!!next-token;                  !!!next-token;
4928                  redo B;                  next B;
4929                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4930                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4931                  !!!cp ('t137');                  !!!cp ('t137');
4932                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
4933                                    text => 'noscript', token => $token);
4934                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4935                  !!!next-token;                  !!!next-token;
4936                  redo B;                  next B;
4937                } else {                } else {
4938                  !!!cp ('t138');                  !!!cp ('t138');
4939                  #                  #
# Line 3759  sub _tree_construction_main ($) { Line 4941  sub _tree_construction_main ($) {
4941              } elsif ({              } elsif ({
4942                        body => 1, html => 1,                        body => 1, html => 1,
4943                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4944                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4945                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4946                  ## 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) {  
4947                  !!!cp ('t140');                  !!!cp ('t140');
4948                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4949                                    text => $token->{tag_name}, token => $token);
4950                  ## Ignore the token                  ## Ignore the token
4951                  !!!next-token;                  !!!next-token;
4952                  redo B;                  next B;
4953                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4954                    !!!cp ('t140.1');
4955                    !!!parse-error (type => 'unmatched end tag',
4956                                    text => $token->{tag_name}, token => $token);
4957                    ## Ignore the token
4958                    !!!next-token;
4959                    next B;
4960                } else {                } else {
4961                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4962                }                }
4963                              } elsif ($token->{tag_name} eq 'p') {
4964                #                !!!cp ('t142');
4965              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4966                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4967                       }->{$token->{tag_name}}) {                ## Ignore the token
4968                  !!!next-token;
4969                  next B;
4970                } elsif ($token->{tag_name} eq 'br') {
4971                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4972                  !!!cp ('t142');                  !!!cp ('t142.2');
4973                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4974                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4975                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4976                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4977      
4978                    ## Reprocess in the "after head" insertion mode...
4979                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4980                    !!!cp ('t143.2');
4981                    ## As if </head>
4982                    pop @{$self->{open_elements}};
4983                    $self->{insertion_mode} = AFTER_HEAD_IM;
4984      
4985                    ## Reprocess in the "after head" insertion mode...
4986                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4987                    !!!cp ('t143.3');
4988                    ## ISSUE: Two parse errors for <head><noscript></br>
4989                    !!!parse-error (type => 'unmatched end tag',
4990                                    text => 'br', token => $token);
4991                    ## As if </noscript>
4992                    pop @{$self->{open_elements}};
4993                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4994    
4995                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4996                } else {                  ## As if </head>
4997                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4998                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4999    
5000                #                  ## Reprocess in the "after head" insertion mode...
5001              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
5002                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
5003                  #                  #
5004                } else {                } else {
5005                  !!!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;  
5006                }                }
5007    
5008                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
5009                  !!!parse-error (type => 'unmatched end tag',
5010                                  text => 'br', token => $token);
5011                  ## Ignore the token
5012                  !!!next-token;
5013                  next B;
5014                } else {
5015                  !!!cp ('t145');
5016                  !!!parse-error (type => 'unmatched end tag',
5017                                  text => $token->{tag_name}, token => $token);
5018                  ## Ignore the token
5019                  !!!next-token;
5020                  next B;
5021              }              }
5022    
5023              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5024                !!!cp ('t146');                !!!cp ('t146');
5025                ## As if </noscript>                ## As if </noscript>
5026                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5027                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
5028                                  text => $token->{tag_name}, token => $token);
5029                                
5030                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
5031                ## As if </head>                ## As if </head>
# Line 3829  sub _tree_construction_main ($) { Line 5041  sub _tree_construction_main ($) {
5041              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5042  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
5043                !!!cp ('t148');                !!!cp ('t148');
5044                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5045                                  text => $token->{tag_name}, token => $token);
5046                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
5047                !!!next-token;                !!!next-token;
5048                redo B;                next B;
5049              } else {              } else {
5050                !!!cp ('t149');                !!!cp ('t149');
5051              }              }
5052    
5053              ## "after head" insertion mode              ## "after head" insertion mode
5054              ## As if <body>              ## As if <body>
5055              !!!insert-element ('body');              !!!insert-element ('body',, $token);
5056              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
5057              ## reprocess              ## reprocess
5058              redo B;              next B;
5059            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5060              die "$0: $token->{type}: Unknown token type";          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
5061            }            !!!cp ('t149.1');
5062    
5063              ## NOTE: As if <head>
5064              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
5065              $self->{open_elements}->[-1]->[0]->append_child
5066                  ($self->{head_element});
5067              #push @{$self->{open_elements}},
5068              #    [$self->{head_element}, $el_category->{head}];
5069              #$self->{insertion_mode} = IN_HEAD_IM;
5070              ## NOTE: Reprocess.
5071    
5072              ## NOTE: As if </head>
5073              #pop @{$self->{open_elements}};
5074              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5075              ## NOTE: Reprocess.
5076              
5077              #
5078            } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
5079              !!!cp ('t149.2');
5080    
5081              ## NOTE: As if </head>
5082              pop @{$self->{open_elements}};
5083              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5084              ## NOTE: Reprocess.
5085    
5086              #
5087            } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5088              !!!cp ('t149.3');
5089    
5090              !!!parse-error (type => 'in noscript:#eof', token => $token);
5091    
5092              ## As if </noscript>
5093              pop @{$self->{open_elements}};
5094              #$self->{insertion_mode} = IN_HEAD_IM;
5095              ## NOTE: Reprocess.
5096    
5097              ## NOTE: As if </head>
5098              pop @{$self->{open_elements}};
5099              #$self->{insertion_mode} = IN_AFTER_HEAD_IM;
5100              ## NOTE: Reprocess.
5101    
5102              #
5103            } else {
5104              !!!cp ('t149.4');
5105              #
5106            }
5107    
5108            ## NOTE: As if <body>
5109            !!!insert-element ('body',, $token);
5110            $self->{insertion_mode} = IN_BODY_IM;
5111            ## NOTE: Reprocess.
5112            next B;
5113          } else {
5114            die "$0: $token->{type}: Unknown token type";
5115          }
5116    
5117            ## ISSUE: An issue in the spec.            ## ISSUE: An issue in the spec.
5118      } elsif ($self->{insertion_mode} & BODY_IMS) {      } elsif ($self->{insertion_mode} & BODY_IMS) {
# Line 3857  sub _tree_construction_main ($) { Line 5124  sub _tree_construction_main ($) {
5124              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5125    
5126              !!!next-token;              !!!next-token;
5127              redo B;              next B;
5128            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5129              if ({              if ({
5130                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3865  sub _tree_construction_main ($) { Line 5132  sub _tree_construction_main ($) {
5132                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5133                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5134                  ## have an element in table scope                  ## have an element in table scope
5135                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5136                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5137                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5138                      !!!cp ('t151');                      !!!cp ('t151');
5139                      $tn = $node->[1];  
5140                      last INSCOPE;                      ## Close the cell
5141                    } elsif ({                      !!!back-token; # <x>
5142                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
5143                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
5144                                  line => $token->{line},
5145                                  column => $token->{column}};
5146                        next B;
5147                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5148                      !!!cp ('t152');                      !!!cp ('t152');
5149                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
5150                        last;
5151                    }                    }
5152                  } # INSCOPE                  }
5153                    unless (defined $tn) {  
5154                      !!!cp ('t153');                  !!!cp ('t153');
5155  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
5156                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      text => $token->{tag_name}, token => $token);
5157                      ## Ignore the token                  ## Ignore the token
5158                      !!!next-token;                  !!!nack ('t153.1');
5159                      redo B;                  !!!next-token;
5160                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
5161                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5162                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5163                                    token => $token);
5164                                    
5165                  ## As if </caption>                  ## NOTE: As if </caption>.
5166                  ## have a table element in table scope                  ## have a table element in table scope
5167                  my $i;                  my $i;
5168                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5169                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5170                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5171                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
5172                      $i = $_;                        !!!cp ('t155');
5173                      last INSCOPE;                        $i = $_;
5174                    } elsif ({                        last INSCOPE;
5175                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5176                             }->{$node->[1]}) {                        !!!cp ('t156');
5177                      !!!cp ('t156');                        last;
5178                      last INSCOPE;                      }
5179                    }                    }
5180    
5181                      !!!cp ('t157');
5182                      !!!parse-error (type => 'start tag not allowed',
5183                                      text => $token->{tag_name}, token => $token);
5184                      ## Ignore the token
5185                      !!!nack ('t157.1');
5186                      !!!next-token;
5187                      next B;
5188                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t157');  
 ## TODO: this type is wrong.  
                     !!!parse-error (type => 'unmatched end tag:caption');  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5189                                    
5190                  ## generate implied end tags                  ## generate implied end tags
5191                  while ({                  while ($self->{open_elements}->[-1]->[1]
5192                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5193                    !!!cp ('t158');                    !!!cp ('t158');
5194                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5195                  }                  }
5196    
5197                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5198                    !!!cp ('t159');                    !!!cp ('t159');
5199                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5200                                      text => $self->{open_elements}->[-1]->[0]
5201                                          ->manakai_local_name,
5202                                      token => $token);
5203                  } else {                  } else {
5204                    !!!cp ('t160');                    !!!cp ('t160');
5205                  }                  }
# Line 3943  sub _tree_construction_main ($) { Line 5211  sub _tree_construction_main ($) {
5211                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5212                                    
5213                  ## reprocess                  ## reprocess
5214                  redo B;                  !!!ack-later;
5215                    next B;
5216                } else {                } else {
5217                  !!!cp ('t161');                  !!!cp ('t161');
5218                  #                  #
# Line 3959  sub _tree_construction_main ($) { Line 5228  sub _tree_construction_main ($) {
5228                  my $i;                  my $i;
5229                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5230                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5231                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5232                      !!!cp ('t163');                      !!!cp ('t163');
5233                      $i = $_;                      $i = $_;
5234                      last INSCOPE;                      last INSCOPE;
5235                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5236                      !!!cp ('t164');                      !!!cp ('t164');
5237                      last INSCOPE;                      last INSCOPE;
5238                    }                    }
5239                  } # INSCOPE                  } # INSCOPE
5240                    unless (defined $i) {                    unless (defined $i) {
5241                      !!!cp ('t165');                      !!!cp ('t165');
5242                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5243                                        text => $token->{tag_name},
5244                                        token => $token);
5245                      ## Ignore the token                      ## Ignore the token
5246                      !!!next-token;                      !!!next-token;
5247                      redo B;                      next B;
5248                    }                    }
5249                                    
5250                  ## generate implied end tags                  ## generate implied end tags
5251                  while ({                  while ($self->{open_elements}->[-1]->[1]
5252                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5253                    !!!cp ('t166');                    !!!cp ('t166');
5254                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5255                  }                  }
5256    
5257                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5258                            ne $token->{tag_name}) {
5259                    !!!cp ('t167');                    !!!cp ('t167');
5260                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5261                                      text => $self->{open_elements}->[-1]->[0]
5262                                          ->manakai_local_name,
5263                                      token => $token);
5264                  } else {                  } else {
5265                    !!!cp ('t168');                    !!!cp ('t168');
5266                  }                  }
# Line 4000  sub _tree_construction_main ($) { Line 5272  sub _tree_construction_main ($) {
5272                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5273                                    
5274                  !!!next-token;                  !!!next-token;
5275                  redo B;                  next B;
5276                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5277                  !!!cp ('t169');                  !!!cp ('t169');
5278                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5279                                    text => $token->{tag_name}, token => $token);
5280                  ## Ignore the token                  ## Ignore the token
5281                  !!!next-token;                  !!!next-token;
5282                  redo B;                  next B;
5283                } else {                } else {
5284                  !!!cp ('t170');                  !!!cp ('t170');
5285                  #                  #
# Line 4015  sub _tree_construction_main ($) { Line 5288  sub _tree_construction_main ($) {
5288                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5289                  ## have a table element in table scope                  ## have a table element in table scope
5290                  my $i;                  my $i;
5291                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5292                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5293                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5294                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
5295                      $i = $_;                        !!!cp ('t171');
5296                      last INSCOPE;                        $i = $_;
5297                    } elsif ({                        last INSCOPE;
5298                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5299                             }->{$node->[1]}) {                        !!!cp ('t172');
5300                      !!!cp ('t172');                        last;
5301                      last INSCOPE;                      }
5302                    }                    }
5303    
5304                      !!!cp ('t173');
5305                      !!!parse-error (type => 'unmatched end tag',
5306                                      text => $token->{tag_name}, token => $token);
5307                      ## Ignore the token
5308                      !!!next-token;
5309                      next B;
5310                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5311                                    
5312                  ## generate implied end tags                  ## generate implied end tags
5313                  while ({                  while ($self->{open_elements}->[-1]->[1]
5314                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5315                    !!!cp ('t174');                    !!!cp ('t174');
5316                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5317                  }                  }
5318                                    
5319                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5320                    !!!cp ('t175');                    !!!cp ('t175');
5321                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5322                                      text => $self->{open_elements}->[-1]->[0]
5323                                          ->manakai_local_name,
5324                                      token => $token);
5325                  } else {                  } else {
5326                    !!!cp ('t176');                    !!!cp ('t176');
5327                  }                  }
# Line 4058  sub _tree_construction_main ($) { Line 5333  sub _tree_construction_main ($) {
5333                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5334                                    
5335                  !!!next-token;                  !!!next-token;
5336                  redo B;                  next B;
5337                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5338                  !!!cp ('t177');                  !!!cp ('t177');
5339                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5340                                    text => $token->{tag_name}, token => $token);
5341                  ## Ignore the token                  ## Ignore the token
5342                  !!!next-token;                  !!!next-token;
5343                  redo B;                  next B;
5344                } else {                } else {
5345                  !!!cp ('t178');                  !!!cp ('t178');
5346                  #                  #
# Line 4077  sub _tree_construction_main ($) { Line 5353  sub _tree_construction_main ($) {
5353                ## have an element in table scope                ## have an element in table scope
5354                my $i;                my $i;
5355                my $tn;                my $tn;
5356                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5357                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5358                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5359                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5360                    $i = $_;                      !!!cp ('t179');
5361                    last INSCOPE;                      $i = $_;
5362                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
5363                    !!!cp ('t180');                      ## Close the cell
5364                    $tn = $node->[1];                      !!!back-token; # </x>
5365                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5366                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
5367                  } elsif ({                                column => $token->{column}};
5368                            table => 1, html => 1,                      next B;
5369                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5370                    !!!cp ('t181');                      !!!cp ('t180');
5371                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
5372                        ## NOTE: There is exactly one |td| or |th| element
5373                        ## in scope in the stack of open elements by definition.
5374                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5375                        ## ISSUE: Can this be reached?
5376                        !!!cp ('t181');
5377                        last;
5378                      }
5379                  }                  }
5380                } # INSCOPE  
               unless (defined $i) {  
5381                  !!!cp ('t182');                  !!!cp ('t182');
5382                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5383                        text => $token->{tag_name}, token => $token);
5384                  ## Ignore the token                  ## Ignore the token
5385                  !!!next-token;                  !!!next-token;
5386                  redo B;                  next B;
5387                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5388              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5389                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5390                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5391                                  token => $token);
5392    
5393                ## As if </caption>                ## As if </caption>
5394                ## have a table element in table scope                ## have a table element in table scope
5395                my $i;                my $i;
5396                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5397                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5398                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5399                    !!!cp ('t184');                    !!!cp ('t184');
5400                    $i = $_;                    $i = $_;
5401                    last INSCOPE;                    last INSCOPE;
5402                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5403                    !!!cp ('t185');                    !!!cp ('t185');
5404                    last INSCOPE;                    last INSCOPE;
5405                  }                  }
5406                } # INSCOPE                } # INSCOPE
5407                unless (defined $i) {                unless (defined $i) {
5408                  !!!cp ('t186');                  !!!cp ('t186');
5409                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag',
5410                                    text => 'caption', token => $token);
5411                  ## Ignore the token                  ## Ignore the token
5412                  !!!next-token;                  !!!next-token;
5413                  redo B;                  next B;
5414                }                }
5415                                
5416                ## generate implied end tags                ## generate implied end tags
5417                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5418                  !!!cp ('t187');                  !!!cp ('t187');
5419                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5420                }                }
5421    
5422                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5423                  !!!cp ('t188');                  !!!cp ('t188');
5424                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5425                                    text => $self->{open_elements}->[-1]->[0]
5426                                        ->manakai_local_name,
5427                                    token => $token);
5428                } else {                } else {
5429                  !!!cp ('t189');                  !!!cp ('t189');
5430                }                }
# Line 4159  sub _tree_construction_main ($) { Line 5436  sub _tree_construction_main ($) {
5436                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5437    
5438                ## reprocess                ## reprocess
5439                redo B;                next B;
5440              } elsif ({              } elsif ({
5441                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5442                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5443                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5444                  !!!cp ('t190');                  !!!cp ('t190');
5445                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5446                                    text => $token->{tag_name}, token => $token);
5447                  ## Ignore the token                  ## Ignore the token
5448                  !!!next-token;                  !!!next-token;
5449                  redo B;                  next B;
5450                } else {                } else {
5451                  !!!cp ('t191');                  !!!cp ('t191');
5452                  #                  #
# Line 4179  sub _tree_construction_main ($) { Line 5457  sub _tree_construction_main ($) {
5457                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5458                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5459                !!!cp ('t192');                !!!cp ('t192');
5460                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5461                                  text => $token->{tag_name}, token => $token);
5462                ## Ignore the token                ## Ignore the token
5463                !!!next-token;                !!!next-token;
5464                redo B;                next B;
5465              } else {              } else {
5466                !!!cp ('t193');                !!!cp ('t193');
5467                #                #
5468              }              }
5469          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5470            for my $entry (@{$self->{open_elements}}) {
5471              unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
5472                !!!cp ('t75');
5473                !!!parse-error (type => 'in body:#eof', token => $token);
5474                last;
5475              }
5476            }
5477    
5478            ## Stop parsing.
5479            last B;
5480        } else {        } else {
5481          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5482        }        }
# Line 4202  sub _tree_construction_main ($) { Line 5492  sub _tree_construction_main ($) {
5492            unless (length $token->{data}) {            unless (length $token->{data}) {
5493              !!!cp ('t194');              !!!cp ('t194');
5494              !!!next-token;              !!!next-token;
5495              redo B;              next B;
5496            } else {            } else {
5497              !!!cp ('t195');              !!!cp ('t195');
5498            }            }
5499          }          }
5500    
5501              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5502    
5503              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5504              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4216  sub _tree_construction_main ($) { Line 5506  sub _tree_construction_main ($) {
5506              ## result in a new Text node.              ## result in a new Text node.
5507              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5508                            
5509              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]}) {  
5510                # MUST                # MUST
5511                my $foster_parent_element;                my $foster_parent_element;
5512                my $next_sibling;                my $next_sibling;
5513                my $prev_sibling;                my $prev_sibling;
5514                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5515                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5516                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5517                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5518                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4260  sub _tree_construction_main ($) { Line 5547  sub _tree_construction_main ($) {
5547          }          }
5548                            
5549          !!!next-token;          !!!next-token;
5550          redo B;          next B;
5551        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5552              if ({          if ({
5553                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5554                   th => 1, td => 1,               th => 1, td => 1,
5555                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5556                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5557                  ## Clear back to table context              ## Clear back to table context
5558                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5559                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5560                    !!!cp ('t201');                !!!cp ('t201');
5561                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5562                  }              }
5563                                
5564                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5565                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5566                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5567                }            }
5568              
5569                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5570                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5571                    !!!cp ('t202');                !!!cp ('t202');
5572                    !!!parse-error (type => 'missing start tag:tr');                !!!parse-error (type => 'missing start tag:tr', token => $token);
5573                  }              }
5574                                    
5575                  ## Clear back to table body context              ## Clear back to table body context
5576                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5577                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5578                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5579                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5580                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5581                    pop @{$self->{open_elements}};              }
                 }  
5582                                    
5583                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5584                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5585                    !!!cp ('t204');                    !!!cp ('t204');
5586                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5587                      !!!nack ('t204');
5588                    !!!next-token;                    !!!next-token;
5589                    redo B;                    next B;
5590                  } else {                  } else {
5591                    !!!cp ('t205');                    !!!cp ('t205');
5592                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5593                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5594                  }                  }
5595                } else {                } else {
# Line 4310  sub _tree_construction_main ($) { Line 5597  sub _tree_construction_main ($) {
5597                }                }
5598    
5599                ## Clear back to table row context                ## Clear back to table row context
5600                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5601                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5602                  !!!cp ('t207');                  !!!cp ('t207');
5603                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5604                }                }
5605                                
5606                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5607                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5608    
5609                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5610                                
5611                  !!!nack ('t207.1');
5612                !!!next-token;                !!!next-token;
5613                redo B;                next B;
5614              } elsif ({              } elsif ({
5615                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5616                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4335  sub _tree_construction_main ($) { Line 5622  sub _tree_construction_main ($) {
5622                  my $i;                  my $i;
5623                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5624                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5625                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5626                      !!!cp ('t208');                      !!!cp ('t208');
5627                      $i = $_;                      $i = $_;
5628                      last INSCOPE;                      last INSCOPE;
5629                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5630                      !!!cp ('t209');                      !!!cp ('t209');
5631                      last INSCOPE;                      last INSCOPE;
5632                    }                    }
5633                  } # INSCOPE                  } # INSCOPE
5634                  unless (defined $i) {                  unless (defined $i) {
5635                   !!!cp ('t210');                    !!!cp ('t210');
5636  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5637                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag',
5638                                      text => $token->{tag_name}, token => $token);
5639                    ## Ignore the token                    ## Ignore the token
5640                      !!!nack ('t210.1');
5641                    !!!next-token;                    !!!next-token;
5642                    redo B;                    next B;
5643                  }                  }
5644                                    
5645                  ## Clear back to table row context                  ## Clear back to table row context
5646                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5647                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5648                    !!!cp ('t211');                    !!!cp ('t211');
5649                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5650                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4372  sub _tree_construction_main ($) { Line 5655  sub _tree_construction_main ($) {
5655                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5656                    !!!cp ('t212');                    !!!cp ('t212');
5657                    ## reprocess                    ## reprocess
5658                    redo B;                    !!!ack-later;
5659                      next B;
5660                  } else {                  } else {
5661                    !!!cp ('t213');                    !!!cp ('t213');
5662                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4384  sub _tree_construction_main ($) { Line 5668  sub _tree_construction_main ($) {
5668                  my $i;                  my $i;
5669                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5670                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5671                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5672                      !!!cp ('t214');                      !!!cp ('t214');
5673                      $i = $_;                      $i = $_;
5674                      last INSCOPE;                      last INSCOPE;
5675                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5676                      !!!cp ('t215');                      !!!cp ('t215');
5677                      last INSCOPE;                      last INSCOPE;
5678                    }                    }
5679                  } # INSCOPE                  } # INSCOPE
5680                  unless (defined $i) {                  unless (defined $i) {
5681                    !!!cp ('t216');                    !!!cp ('t216');
5682  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5683                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5684                                      text => $token->{tag_name}, token => $token);
5685                    ## Ignore the token                    ## Ignore the token
5686                      !!!nack ('t216.1');
5687                    !!!next-token;                    !!!next-token;
5688                    redo B;                    next B;
5689                  }                  }
5690    
5691                  ## Clear back to table body context                  ## Clear back to table body context
5692                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5693                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5694                    !!!cp ('t217');                    !!!cp ('t217');
5695                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5696                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4431  sub _tree_construction_main ($) { Line 5712  sub _tree_construction_main ($) {
5712    
5713                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5714                  ## Clear back to table context                  ## Clear back to table context
5715                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5716                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5717                    !!!cp ('t219');                    !!!cp ('t219');
5718                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5719                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5720                  }                  }
5721                                    
5722                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5723                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5724                  ## reprocess                  ## reprocess
5725                  redo B;                  !!!ack-later;
5726                    next B;
5727                } elsif ({                } elsif ({
5728                          caption => 1,                          caption => 1,
5729                          colgroup => 1,                          colgroup => 1,
5730                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5731                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5732                  ## Clear back to table context                  ## Clear back to table context
5733                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5734                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5735                    !!!cp ('t220');                    !!!cp ('t220');
5736                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5737                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4458  sub _tree_construction_main ($) { Line 5740  sub _tree_construction_main ($) {
5740                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5741                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5742                                    
5743                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5744                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5745                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5746                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4467  sub _tree_construction_main ($) { Line 5749  sub _tree_construction_main ($) {
5749                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5750                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5751                  !!!next-token;                  !!!next-token;
5752                  redo B;                  !!!nack ('t220.1');
5753                    next B;
5754                } else {                } else {
5755                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5756                }                }
5757              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5758                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5759                                  text => $self->{open_elements}->[-1]->[0]
5760                                      ->manakai_local_name,
5761                                  token => $token);
5762    
5763                ## As if </table>                ## As if </table>
5764                ## have a table element in table scope                ## have a table element in table scope
5765                my $i;                my $i;
5766                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5767                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5768                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5769                    !!!cp ('t221');                    !!!cp ('t221');
5770                    $i = $_;                    $i = $_;
5771                    last INSCOPE;                    last INSCOPE;
5772                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5773                    !!!cp ('t222');                    !!!cp ('t222');
5774                    last INSCOPE;                    last INSCOPE;
5775                  }                  }
# Line 4494  sub _tree_construction_main ($) { Line 5777  sub _tree_construction_main ($) {
5777                unless (defined $i) {                unless (defined $i) {
5778                  !!!cp ('t223');                  !!!cp ('t223');
5779  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5780                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5781                                    token => $token);
5782                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5783                    !!!nack ('t223.1');
5784                  !!!next-token;                  !!!next-token;
5785                  redo B;                  next B;
5786                }                }
5787                                
5788    ## TODO: Followings are removed from the latest spec.
5789                ## generate implied end tags                ## generate implied end tags
5790                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5791                  !!!cp ('t224');                  !!!cp ('t224');
5792                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5793                }                }
5794    
5795                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5796                  !!!cp ('t225');                  !!!cp ('t225');
5797  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5798                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5799                                    text => $self->{open_elements}->[-1]->[0]
5800                                        ->manakai_local_name,
5801                                    token => $token);
5802                } else {                } else {
5803                  !!!cp ('t226');                  !!!cp ('t226');
5804                }                }
# Line 4521  sub _tree_construction_main ($) { Line 5808  sub _tree_construction_main ($) {
5808    
5809                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5810    
5811                ## reprocess            ## reprocess
5812                redo B;            !!!ack-later;
5813              next B;
5814          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5815            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5816              !!!cp ('t227.8');              !!!cp ('t227.8');
5817              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5818              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5819              redo B;              next B;
5820            } else {            } else {
5821              !!!cp ('t227.7');              !!!cp ('t227.7');
5822              #              #
# Line 4538  sub _tree_construction_main ($) { Line 5826  sub _tree_construction_main ($) {
5826              !!!cp ('t227.6');              !!!cp ('t227.6');
5827              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5828              $script_start_tag->();              $script_start_tag->();
5829              redo B;              next B;
5830            } else {            } else {
5831              !!!cp ('t227.5');              !!!cp ('t227.5');
5832              #              #
# Line 4549  sub _tree_construction_main ($) { Line 5837  sub _tree_construction_main ($) {
5837                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5838                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5839                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5840                  !!!parse-error (type => 'in table:'.$token->{tag_name});                  !!!parse-error (type => 'in table',
5841                                    text => $token->{tag_name}, token => $token);
5842    
5843                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5844    
5845                  ## TODO: form element pointer                  ## TODO: form element pointer
5846    
5847                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5848    
5849                  !!!next-token;                  !!!next-token;
5850                  redo B;                  !!!ack ('t227.2.1');
5851                    next B;
5852                } else {                } else {
5853                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5854                  #                  #
# Line 4576  sub _tree_construction_main ($) { Line 5866  sub _tree_construction_main ($) {
5866            #            #
5867          }          }
5868    
5869          !!!parse-error (type => 'in table:'.$token->{tag_name});          !!!parse-error (type => 'in table', text => $token->{tag_name},
5870                            token => $token);
5871    
5872          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5873          #          #
# Line 4587  sub _tree_construction_main ($) { Line 5878  sub _tree_construction_main ($) {
5878                my $i;                my $i;
5879                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5880                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5881                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5882                    !!!cp ('t228');                    !!!cp ('t228');
5883                    $i = $_;                    $i = $_;
5884                    last INSCOPE;                    last INSCOPE;
5885                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5886                    !!!cp ('t229');                    !!!cp ('t229');
5887                    last INSCOPE;                    last INSCOPE;
5888                  }                  }
5889                } # INSCOPE                } # INSCOPE
5890                unless (defined $i) {                unless (defined $i) {
5891                  !!!cp ('t230');                  !!!cp ('t230');
5892                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5893                                    text => $token->{tag_name}, token => $token);
5894                  ## Ignore the token                  ## Ignore the token
5895                    !!!nack ('t230.1');
5896                  !!!next-token;                  !!!next-token;
5897                  redo B;                  next B;
5898                } else {                } else {
5899                  !!!cp ('t232');                  !!!cp ('t232');
5900                }                }
5901    
5902                ## Clear back to table row context                ## Clear back to table row context
5903                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5904                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5905                  !!!cp ('t231');                  !!!cp ('t231');
5906  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5907                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4620  sub _tree_construction_main ($) { Line 5910  sub _tree_construction_main ($) {
5910                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5911                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5912                !!!next-token;                !!!next-token;
5913                redo B;                !!!nack ('t231.1');
5914                  next B;
5915              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5916                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5917                  ## As if </tr>                  ## As if </tr>
# Line 4628  sub _tree_construction_main ($) { Line 5919  sub _tree_construction_main ($) {
5919                  my $i;                  my $i;
5920                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5921                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5922                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5923                      !!!cp ('t233');                      !!!cp ('t233');
5924                      $i = $_;                      $i = $_;
5925                      last INSCOPE;                      last INSCOPE;
5926                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5927                      !!!cp ('t234');                      !!!cp ('t234');
5928                      last INSCOPE;                      last INSCOPE;
5929                    }                    }
# Line 4642  sub _tree_construction_main ($) { Line 5931  sub _tree_construction_main ($) {
5931                  unless (defined $i) {                  unless (defined $i) {
5932                    !!!cp ('t235');                    !!!cp ('t235');
5933  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5934                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag',
5935                                      text => $token->{type}, token => $token);
5936                    ## Ignore the token                    ## Ignore the token
5937                      !!!nack ('t236.1');
5938                    !!!next-token;                    !!!next-token;
5939                    redo B;                    next B;
5940                  }                  }
5941                                    
5942                  ## Clear back to table row context                  ## Clear back to table row context
5943                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5944                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5945                    !!!cp ('t236');                    !!!cp ('t236');
5946  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5947                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4667  sub _tree_construction_main ($) { Line 5957  sub _tree_construction_main ($) {
5957                  my $i;                  my $i;
5958                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5959                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5960                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5961                      !!!cp ('t237');                      !!!cp ('t237');
5962                      $i = $_;                      $i = $_;
5963                      last INSCOPE;                      last INSCOPE;
5964                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5965                      !!!cp ('t238');                      !!!cp ('t238');
5966                      last INSCOPE;                      last INSCOPE;
5967                    }                    }
5968                  } # INSCOPE                  } # INSCOPE
5969                  unless (defined $i) {                  unless (defined $i) {
5970                    !!!cp ('t239');                    !!!cp ('t239');
5971                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5972                                      text => $token->{tag_name}, token => $token);
5973                    ## Ignore the token                    ## Ignore the token
5974                      !!!nack ('t239.1');
5975                    !!!next-token;                    !!!next-token;
5976                    redo B;                    next B;
5977                  }                  }
5978                                    
5979                  ## Clear back to table body context                  ## Clear back to table body context
5980                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5981                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5982                    !!!cp ('t240');                    !!!cp ('t240');
5983                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5984                  }                  }
# Line 4717  sub _tree_construction_main ($) { Line 6004  sub _tree_construction_main ($) {
6004                my $i;                my $i;
6005                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6006                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6007                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
6008                    !!!cp ('t241');                    !!!cp ('t241');
6009                    $i = $_;                    $i = $_;
6010                    last INSCOPE;                    last INSCOPE;
6011                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6012                    !!!cp ('t242');                    !!!cp ('t242');
6013                    last INSCOPE;                    last INSCOPE;
6014                  }                  }
6015                } # INSCOPE                } # INSCOPE
6016                unless (defined $i) {                unless (defined $i) {
6017                  !!!cp ('t243');                  !!!cp ('t243');
6018                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6019                                    text => $token->{tag_name}, token => $token);
6020                  ## Ignore the token                  ## Ignore the token
6021                    !!!nack ('t243.1');
6022                  !!!next-token;                  !!!next-token;
6023                  redo B;                  next B;
6024                }                }
6025                                    
6026                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4742  sub _tree_construction_main ($) { Line 6029  sub _tree_construction_main ($) {
6029                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
6030                                
6031                !!!next-token;                !!!next-token;
6032                redo B;                next B;
6033              } elsif ({              } elsif ({
6034                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
6035                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4752  sub _tree_construction_main ($) { Line 6039  sub _tree_construction_main ($) {
6039                  my $i;                  my $i;
6040                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6041                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6042                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6043                      !!!cp ('t247');                      !!!cp ('t247');
6044                      $i = $_;                      $i = $_;
6045                      last INSCOPE;                      last INSCOPE;
6046                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6047                      !!!cp ('t248');                      !!!cp ('t248');
6048                      last INSCOPE;                      last INSCOPE;
6049                    }                    }
6050                  } # INSCOPE                  } # INSCOPE
6051                    unless (defined $i) {                    unless (defined $i) {
6052                      !!!cp ('t249');                      !!!cp ('t249');
6053                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
6054                                        text => $token->{tag_name}, token => $token);
6055                      ## Ignore the token                      ## Ignore the token
6056                        !!!nack ('t249.1');
6057                      !!!next-token;                      !!!next-token;
6058                      redo B;                      next B;
6059                    }                    }
6060                                    
6061                  ## As if </tr>                  ## As if </tr>
# Line 4776  sub _tree_construction_main ($) { Line 6063  sub _tree_construction_main ($) {
6063                  my $i;                  my $i;
6064                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6065                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
6066                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
6067                      !!!cp ('t250');                      !!!cp ('t250');
6068                      $i = $_;                      $i = $_;
6069                      last INSCOPE;                      last INSCOPE;
6070                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
6071                      !!!cp ('t251');                      !!!cp ('t251');
6072                      last INSCOPE;                      last INSCOPE;
6073                    }                    }
6074                  } # INSCOPE                  } # INSCOPE
6075                    unless (defined $i) {                    unless (defined $i) {
6076                      !!!cp ('t252');                      !!!cp ('t252');
6077                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
6078                                        text => 'tr', token => $token);
6079                      ## Ignore the token                      ## Ignore the token
6080                        !!!nack ('t252.1');
6081                      !!!next-token;                      !!!next-token;
6082                      redo B;                      next B;
6083                    }                    }
6084                                    
6085                  ## Clear back to table row context                  ## Clear back to table row context
6086                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6087                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6088                    !!!cp ('t253');                    !!!cp ('t253');
6089  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
6090                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4813  sub _tree_construction_main ($) { Line 6099  sub _tree_construction_main ($) {
6099                my $i;                my $i;
6100                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6101                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6102                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6103                    !!!cp ('t254');                    !!!cp ('t254');
6104                    $i = $_;                    $i = $_;
6105                    last INSCOPE;                    last INSCOPE;
6106                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6107                    !!!cp ('t255');                    !!!cp ('t255');
6108                    last INSCOPE;                    last INSCOPE;
6109                  }                  }
6110                } # INSCOPE                } # INSCOPE
6111                unless (defined $i) {                unless (defined $i) {
6112                  !!!cp ('t256');                  !!!cp ('t256');
6113                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6114                                    text => $token->{tag_name}, token => $token);
6115                  ## Ignore the token                  ## Ignore the token
6116                    !!!nack ('t256.1');
6117                  !!!next-token;                  !!!next-token;
6118                  redo B;                  next B;
6119                }                }
6120    
6121                ## Clear back to table body context                ## Clear back to table body context
6122                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6123                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
6124                  !!!cp ('t257');                  !!!cp ('t257');
6125  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
6126                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4843  sub _tree_construction_main ($) { Line 6128  sub _tree_construction_main ($) {
6128    
6129                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6130                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6131                  !!!nack ('t257.1');
6132                !!!next-token;                !!!next-token;
6133                redo B;                next B;
6134              } elsif ({              } elsif ({
6135                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6136                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6137                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6138                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6139                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6140                !!!cp ('t258');            !!!cp ('t258');
6141                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6142                ## Ignore the token                            text => $token->{tag_name}, token => $token);
6143                !!!next-token;            ## Ignore the token
6144                redo B;            !!!nack ('t258.1');
6145               !!!next-token;
6146              next B;
6147          } else {          } else {
6148            !!!cp ('t259');            !!!cp ('t259');
6149            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
6150                              text => $token->{tag_name}, token => $token);
6151    
6152            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6153            #            #
6154          }          }
6155          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6156            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6157                    @{$self->{open_elements}} == 1) { # redundant, maybe
6158              !!!parse-error (type => 'in body:#eof', token => $token);
6159              !!!cp ('t259.1');
6160              #
6161            } else {
6162              !!!cp ('t259.2');
6163              #
6164            }
6165    
6166            ## Stop parsing
6167            last B;
6168        } else {        } else {
6169          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6170        }        }
# Line 4873  sub _tree_construction_main ($) { Line 6175  sub _tree_construction_main ($) {
6175                unless (length $token->{data}) {                unless (length $token->{data}) {
6176                  !!!cp ('t260');                  !!!cp ('t260');
6177                  !!!next-token;                  !!!next-token;
6178                  redo B;                  next B;
6179                }                }
6180              }              }
6181                            
# Line 4882  sub _tree_construction_main ($) { Line 6184  sub _tree_construction_main ($) {
6184            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6185              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6186                !!!cp ('t262');                !!!cp ('t262');
6187                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6188                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6189                  !!!ack ('t262.1');
6190                !!!next-token;                !!!next-token;
6191                redo B;                next B;
6192              } else {              } else {
6193                !!!cp ('t263');                !!!cp ('t263');
6194                #                #
6195              }              }
6196            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6197              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6198                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6199                  !!!cp ('t264');                  !!!cp ('t264');
6200                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
6201                                    text => 'colgroup', token => $token);
6202                  ## Ignore the token                  ## Ignore the token
6203                  !!!next-token;                  !!!next-token;
6204                  redo B;                  next B;
6205                } else {                } else {
6206                  !!!cp ('t265');                  !!!cp ('t265');
6207                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6208                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6209                  !!!next-token;                  !!!next-token;
6210                  redo B;                              next B;            
6211                }                }
6212              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6213                !!!cp ('t266');                !!!cp ('t266');
6214                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
6215                                  text => 'col', token => $token);
6216                ## Ignore the token                ## Ignore the token
6217                !!!next-token;                !!!next-token;
6218                redo B;                next B;
6219              } else {              } else {
6220                !!!cp ('t267');                !!!cp ('t267');
6221                #                #
6222              }              }
6223            } else {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6224              die "$0: $token->{type}: Unknown token type";          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6225            }              @{$self->{open_elements}} == 1) { # redundant, maybe
6226              !!!cp ('t270.2');
6227              ## Stop parsing.
6228              last B;
6229            } else {
6230              ## NOTE: As if </colgroup>.
6231              !!!cp ('t270.1');
6232              pop @{$self->{open_elements}}; # colgroup
6233              $self->{insertion_mode} = IN_TABLE_IM;
6234              ## Reprocess.
6235              next B;
6236            }
6237          } else {
6238            die "$0: $token->{type}: Unknown token type";
6239          }
6240    
6241            ## As if </colgroup>            ## As if </colgroup>
6242            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6243              !!!cp ('t269');              !!!cp ('t269');
6244              !!!parse-error (type => 'unmatched end tag:colgroup');  ## TODO: Wrong error type?
6245                !!!parse-error (type => 'unmatched end tag',
6246                                text => 'colgroup', token => $token);
6247              ## Ignore the token              ## Ignore the token
6248                !!!nack ('t269.1');
6249              !!!next-token;              !!!next-token;
6250              redo B;              next B;
6251            } else {            } else {
6252              !!!cp ('t270');              !!!cp ('t270');
6253              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6254              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6255                !!!ack-later;
6256              ## reprocess              ## reprocess
6257              redo B;              next B;
6258            }            }
6259      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6260        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6261          !!!cp ('t271');          !!!cp ('t271');
6262          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6263          !!!next-token;          !!!next-token;
6264          redo B;          next B;
6265        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6266              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6267                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6268                  !!!cp ('t272');              !!!cp ('t272');
6269                  ## As if </option>              ## As if </option>
6270                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6271                } else {            } else {
6272                  !!!cp ('t273');              !!!cp ('t273');
6273                }            }
6274    
6275                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6276                !!!next-token;            !!!nack ('t273.1');
6277                redo B;            !!!next-token;
6278              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6279                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6280                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6281                  ## As if </option>              !!!cp ('t274');
6282                  pop @{$self->{open_elements}};              ## As if </option>
6283                } else {              pop @{$self->{open_elements}};
6284                  !!!cp ('t275');            } else {
6285                }              !!!cp ('t275');
6286              }
6287    
6288                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6289                  !!!cp ('t276');              !!!cp ('t276');
6290                  ## As if </optgroup>              ## As if </optgroup>
6291                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6292                } else {            } else {
6293                  !!!cp ('t277');              !!!cp ('t277');
6294                }            }
6295    
6296                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6297                !!!next-token;            !!!nack ('t277.1');
6298                redo B;            !!!next-token;
6299          } elsif ($token->{tag_name} eq 'select' or            next B;
6300                   $token->{tag_name} eq 'input' or          } elsif ({
6301                       select => 1, input => 1, textarea => 1,
6302                     }->{$token->{tag_name}} or
6303                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6304                    {                    {
6305                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 4981  sub _tree_construction_main ($) { Line 6307  sub _tree_construction_main ($) {
6307                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6308                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6309            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6310            !!!parse-error (type => 'not closed:select');            !!!parse-error (type => 'not closed', text => 'select',
6311                              token => $token);
6312            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6313            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6314                ## have an element in table scope            ## have an element in table scope
6315                my $i;            my $i;
6316                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6317                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6318                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6319                    !!!cp ('t278');                !!!cp ('t278');
6320                    $i = $_;                $i = $_;
6321                    last INSCOPE;                last INSCOPE;
6322                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6323                            table => 1, html => 1,                !!!cp ('t279');
6324                           }->{$node->[1]}) {                last INSCOPE;
6325                    !!!cp ('t279');              }
6326                    last INSCOPE;            } # INSCOPE
6327                  }            unless (defined $i) {
6328                } # INSCOPE              !!!cp ('t280');
6329                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag',
6330                  !!!cp ('t280');                              text => 'select', token => $token);
6331                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6332                  ## Ignore the token              !!!nack ('t280.1');
6333                  !!!next-token;              !!!next-token;
6334                  redo B;              next B;
6335                }            }
6336                                
6337                !!!cp ('t281');            !!!cp ('t281');
6338                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6339    
6340                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6341    
6342            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
6343              !!!cp ('t281.2');              !!!nack ('t281.2');
6344              !!!next-token;              !!!next-token;
6345              redo B;              next B;
6346            } else {            } else {
6347              !!!cp ('t281.1');              !!!cp ('t281.1');
6348                !!!ack-later;
6349              ## Reprocess the token.              ## Reprocess the token.
6350              redo B;              next B;
6351            }            }
6352          } else {          } else {
6353            !!!cp ('t282');            !!!cp ('t282');
6354            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
6355                              text => $token->{tag_name}, token => $token);
6356            ## Ignore the token            ## Ignore the token
6357              !!!nack ('t282.1');
6358            !!!next-token;            !!!next-token;
6359            redo B;            next B;
6360          }          }
6361        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6362              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6363                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6364                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6365                  !!!cp ('t283');              !!!cp ('t283');
6366                  ## As if </option>              ## As if </option>
6367                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6368                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6369                  !!!cp ('t284');              !!!cp ('t284');
6370                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6371                } else {            } else {
6372                  !!!cp ('t285');              !!!cp ('t285');
6373                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6374                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6375                }              ## Ignore the token
6376                !!!next-token;            }
6377                redo B;            !!!nack ('t285.1');
6378              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6379                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6380                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6381                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6382                } else {              !!!cp ('t286');
6383                  !!!cp ('t287');              pop @{$self->{open_elements}};
6384                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
6385                  ## Ignore the token              !!!cp ('t287');
6386                }              !!!parse-error (type => 'unmatched end tag',
6387                !!!next-token;                              text => $token->{tag_name}, token => $token);
6388                redo B;              ## Ignore the token
6389              } elsif ($token->{tag_name} eq 'select') {            }
6390                ## have an element in table scope            !!!nack ('t287.1');
6391                my $i;            !!!next-token;
6392                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6393                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6394                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6395                    !!!cp ('t288');            my $i;
6396                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6397                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6398                  } elsif ({              if ($node->[1] & SELECT_EL) {
6399                            table => 1, html => 1,                !!!cp ('t288');
6400                           }->{$node->[1]}) {                $i = $_;
6401                    !!!cp ('t289');                last INSCOPE;
6402                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6403                  }                !!!cp ('t289');
6404                } # INSCOPE                last INSCOPE;
6405                unless (defined $i) {              }
6406                  !!!cp ('t290');            } # INSCOPE
6407                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
6408                  ## Ignore the token              !!!cp ('t290');
6409                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6410                  redo B;                              text => $token->{tag_name}, token => $token);
6411                }              ## Ignore the token
6412                !!!nack ('t290.1');
6413                !!!next-token;
6414                next B;
6415              }
6416                                
6417                !!!cp ('t291');            !!!cp ('t291');
6418                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6419    
6420                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6421    
6422                !!!next-token;            !!!nack ('t291.1');
6423                redo B;            !!!next-token;
6424              next B;
6425          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6426                   {                   {
6427                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6428                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6429                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6430  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6431                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6432                              text => $token->{tag_name}, token => $token);
6433                                
6434                ## have an element in table scope            ## have an element in table scope
6435                my $i;            my $i;
6436                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6437                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6438                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6439                    !!!cp ('t292');                !!!cp ('t292');
6440                    $i = $_;                $i = $_;
6441                    last INSCOPE;                last INSCOPE;
6442                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6443                            table => 1, html => 1,                !!!cp ('t293');
6444                           }->{$node->[1]}) {                last INSCOPE;
6445                    !!!cp ('t293');              }
6446                    last INSCOPE;            } # INSCOPE
6447                  }            unless (defined $i) {
6448                } # INSCOPE              !!!cp ('t294');
6449                unless (defined $i) {              ## Ignore the token
6450                  !!!cp ('t294');              !!!nack ('t294.1');
6451                  ## Ignore the token              !!!next-token;
6452                  !!!next-token;              next B;
6453                  redo B;            }
               }  
6454                                
6455                ## As if </select>            ## As if </select>
6456                ## have an element in table scope            ## have an element in table scope
6457                undef $i;            undef $i;
6458                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6459                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6460                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6461                    !!!cp ('t295');                !!!cp ('t295');
6462                    $i = $_;                $i = $_;
6463                    last INSCOPE;                last INSCOPE;
6464                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6465  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6466                    !!!cp ('t296');                !!!cp ('t296');
6467                    last INSCOPE;                last INSCOPE;
6468                  }              }
6469                } # INSCOPE            } # INSCOPE
6470                unless (defined $i) {            unless (defined $i) {
6471                  !!!cp ('t297');              !!!cp ('t297');
6472  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6473                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
6474                  ## Ignore the </select> token                              text => 'select', token => $token);
6475                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6476                  redo B;              !!!nack ('t297.1');
6477                }              !!!next-token; ## TODO: ok?
6478                next B;
6479              }
6480                                
6481                !!!cp ('t298');            !!!cp ('t298');
6482                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6483    
6484                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6485    
6486                ## reprocess            !!!ack-later;
6487                redo B;            ## reprocess
6488              next B;
6489          } else {          } else {
6490            !!!cp ('t299');            !!!cp ('t299');
6491            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
6492                              text => $token->{tag_name}, token => $token);
6493            ## Ignore the token            ## Ignore the token
6494              !!!nack ('t299.3');
6495            !!!next-token;            !!!next-token;
6496            redo B;            next B;
6497            }
6498          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6499            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6500                    @{$self->{open_elements}} == 1) { # redundant, maybe
6501              !!!cp ('t299.1');
6502              !!!parse-error (type => 'in body:#eof', token => $token);
6503            } else {
6504              !!!cp ('t299.2');
6505          }          }
6506    
6507            ## Stop parsing.
6508            last B;
6509        } else {        } else {
6510          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6511        }        }
# Line 5172  sub _tree_construction_main ($) { Line 6521  sub _tree_construction_main ($) {
6521            unless (length $token->{data}) {            unless (length $token->{data}) {
6522              !!!cp ('t300');              !!!cp ('t300');
6523              !!!next-token;              !!!next-token;
6524              redo B;              next B;
6525            }            }
6526          }          }
6527                    
6528          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6529            !!!cp ('t301');            !!!cp ('t301');
6530            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
6531    
6532            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6533          } else {          } else {
# Line 5186  sub _tree_construction_main ($) { Line 6535  sub _tree_construction_main ($) {
6535          }          }
6536                    
6537          ## "after body" insertion mode          ## "after body" insertion mode
6538          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6539    
6540          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6541          ## reprocess          ## reprocess
6542          redo B;          next B;
6543        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6544          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6545            !!!cp ('t303');            !!!cp ('t303');
6546            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6547                              text => $token->{tag_name}, token => $token);
6548                        
6549            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6550          } else {          } else {
# Line 5202  sub _tree_construction_main ($) { Line 6552  sub _tree_construction_main ($) {
6552          }          }
6553    
6554          ## "after body" insertion mode          ## "after body" insertion mode
6555          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6556                            text => $token->{tag_name}, token => $token);
6557    
6558          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6559            !!!ack-later;
6560          ## reprocess          ## reprocess
6561          redo B;          next B;
6562        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6563          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6564            !!!cp ('t305');            !!!cp ('t305');
6565            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6566                              text => $token->{tag_name}, token => $token);
6567                        
6568            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6569            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5222  sub _tree_construction_main ($) { Line 6575  sub _tree_construction_main ($) {
6575          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6576            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6577              !!!cp ('t307');              !!!cp ('t307');
6578              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
6579                                text => 'html', token => $token);
6580              ## Ignore the token              ## Ignore the token
6581              !!!next-token;              !!!next-token;
6582              redo B;              next B;
6583            } else {            } else {
6584              !!!cp ('t308');              !!!cp ('t308');
6585              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6586              !!!next-token;              !!!next-token;
6587              redo B;              next B;
6588            }            }
6589          } else {          } else {
6590            !!!cp ('t309');            !!!cp ('t309');
6591            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
6592                              text => $token->{tag_name}, token => $token);
6593    
6594            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6595            ## reprocess            ## reprocess
6596            redo B;            next B;
6597          }          }
6598          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6599            !!!cp ('t309.2');
6600            ## Stop parsing
6601            last B;
6602        } else {        } else {
6603          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6604        }        }
# Line 5251  sub _tree_construction_main ($) { Line 6610  sub _tree_construction_main ($) {
6610            unless (length $token->{data}) {            unless (length $token->{data}) {
6611              !!!cp ('t310');              !!!cp ('t310');
6612              !!!next-token;              !!!next-token;
6613              redo B;              next B;
6614            }            }
6615          }          }
6616                    
6617          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6618            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6619              !!!cp ('t311');              !!!cp ('t311');
6620              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
6621            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6622              !!!cp ('t312');              !!!cp ('t312');
6623              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6624            } else { # "after html frameset"            } else { # "after after frameset"
6625              !!!cp ('t313');              !!!cp ('t313');
6626              !!!parse-error (type => 'after html:#character');              !!!parse-error (type => 'after html:#text', token => $token);
   
             $self->{insertion_mode} = AFTER_FRAMESET_IM;  
             ## Reprocess in the "after frameset" insertion mode.  
             !!!parse-error (type => 'after frameset:#character');  
6627            }            }
6628                        
6629            ## Ignore the token.            ## Ignore the token.
# Line 5279  sub _tree_construction_main ($) { Line 6634  sub _tree_construction_main ($) {
6634              !!!cp ('t315');              !!!cp ('t315');
6635              !!!next-token;              !!!next-token;
6636            }            }
6637            redo B;            next B;
6638          }          }
6639                    
6640          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6641        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t316');  
           !!!parse-error (type => 'after html:'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t317');  
         }  
   
6642          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6643              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6644            !!!cp ('t318');            !!!cp ('t318');
6645            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6646              !!!nack ('t318.1');
6647            !!!next-token;            !!!next-token;
6648            redo B;            next B;
6649          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6650                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6651            !!!cp ('t319');            !!!cp ('t319');
6652            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6653            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6654              !!!ack ('t319.1');
6655            !!!next-token;            !!!next-token;
6656            redo B;            next B;
6657          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6658            !!!cp ('t320');            !!!cp ('t320');
6659            ## NOTE: As if in body.            ## NOTE: As if in head.
6660            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6661            redo B;            next B;
6662    
6663              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6664              ## has no parse error.
6665          } else {          } else {
6666            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6667              !!!cp ('t321');              !!!cp ('t321');
6668              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
6669            } else {                              text => $token->{tag_name}, token => $token);
6670              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6671              !!!cp ('t322');              !!!cp ('t322');
6672              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
6673                                text => $token->{tag_name}, token => $token);
6674              } else { # "after after frameset"
6675                !!!cp ('t322.2');
6676                !!!parse-error (type => 'after after frameset',
6677                                text => $token->{tag_name}, token => $token);
6678            }            }
6679            ## Ignore the token            ## Ignore the token
6680              !!!nack ('t322.1');
6681            !!!next-token;            !!!next-token;
6682            redo B;            next B;
6683          }          }
6684        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
         if ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {  
           !!!cp ('t323');  
           !!!parse-error (type => 'after html:/'.$token->{tag_name});  
   
           $self->{insertion_mode} = AFTER_FRAMESET_IM;  
           ## Process in the "after frameset" insertion mode.  
         } else {  
           !!!cp ('t324');  
         }  
   
6685          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6686              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6687            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6688                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6689              !!!cp ('t325');              !!!cp ('t325');
6690              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6691                                text => $token->{tag_name}, token => $token);
6692              ## Ignore the token              ## Ignore the token
6693              !!!next-token;              !!!next-token;
6694            } else {            } else {
# Line 5350  sub _tree_construction_main ($) { Line 6698  sub _tree_construction_main ($) {
6698            }            }
6699    
6700            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6701                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6702              !!!cp ('t327');              !!!cp ('t327');
6703              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6704            } else {            } else {
6705              !!!cp ('t328');              !!!cp ('t328');
6706            }            }
6707            redo B;            next B;
6708          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6709                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6710            !!!cp ('t329');            !!!cp ('t329');
6711            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6712            !!!next-token;            !!!next-token;
6713            redo B;            next B;
6714          } else {          } else {
6715            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6716              !!!cp ('t330');              !!!cp ('t330');
6717              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/',
6718            } else {                              text => $token->{tag_name}, token => $token);
6719              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6720                !!!cp ('t330.1');
6721                !!!parse-error (type => 'after frameset:/',
6722                                text => $token->{tag_name}, token => $token);
6723              } else { # "after after html"
6724              !!!cp ('t331');              !!!cp ('t331');
6725              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after after frameset:/',
6726                                text => $token->{tag_name}, token => $token);
6727            }            }
6728            ## Ignore the token            ## Ignore the token
6729            !!!next-token;            !!!next-token;
6730            redo B;            next B;
6731            }
6732          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6733            unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6734                    @{$self->{open_elements}} == 1) { # redundant, maybe
6735              !!!cp ('t331.1');
6736              !!!parse-error (type => 'in body:#eof', token => $token);
6737            } else {
6738              !!!cp ('t331.2');
6739          }          }
6740            
6741            ## Stop parsing
6742            last B;
6743        } else {        } else {
6744          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6745        }        }
# Line 5390  sub _tree_construction_main ($) { Line 6755  sub _tree_construction_main ($) {
6755          !!!cp ('t332');          !!!cp ('t332');
6756          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6757          $script_start_tag->();          $script_start_tag->();
6758          redo B;          next B;
6759        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6760          !!!cp ('t333');          !!!cp ('t333');
6761          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6762          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6763          redo B;          next B;
6764        } elsif ({        } elsif ({
6765                  base => 1, link => 1,                  base => 1, link => 1,
6766                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6767          !!!cp ('t334');          !!!cp ('t334');
6768          ## 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
6769          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6770          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6771            !!!ack ('t334.1');
6772          !!!next-token;          !!!next-token;
6773          redo B;          next B;
6774        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6775          ## 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
6776          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6777          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.
6778    
6779          unless ($self->{confident}) {          unless ($self->{confident}) {
6780            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6781              !!!cp ('t335');              !!!cp ('t335');
6782                ## NOTE: Whether the encoding is supported or not is handled
6783                ## in the {change_encoding} callback.
6784              $self->{change_encoding}              $self->{change_encoding}
6785                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6786                            
6787              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6788                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6789                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6790                                           ->{has_reference});                                           ->{has_reference});
6791            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6792              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6793                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6794                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6795                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6796                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6797                !!!cp ('t336');                !!!cp ('t336');
6798                  ## NOTE: Whether the encoding is supported or not is handled
6799                  ## in the {change_encoding} callback.
6800                $self->{change_encoding}                $self->{change_encoding}
6801                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6802                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6803                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6804                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5453  sub _tree_construction_main ($) { Line 6822  sub _tree_construction_main ($) {
6822            }            }
6823          }          }
6824    
6825            !!!ack ('t338.1');
6826          !!!next-token;          !!!next-token;
6827          redo B;          next B;
6828        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6829          !!!cp ('t341');          !!!cp ('t341');
6830          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6831          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6832          redo B;          next B;
6833        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6834          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6835                                
6836          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6837              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6838            !!!cp ('t342');            !!!cp ('t342');
6839            ## Ignore the token            ## Ignore the token
6840          } else {          } else {
# Line 5478  sub _tree_construction_main ($) { Line 6848  sub _tree_construction_main ($) {
6848              }              }
6849            }            }
6850          }          }
6851            !!!nack ('t343.1');
6852          !!!next-token;          !!!next-token;
6853          redo B;          next B;
6854        } elsif ({        } elsif ({
6855                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6856                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6857                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6858                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6859                  pre => 1, listing => 1,                  pre => 1, listing => 1,
6860                    form => 1,
6861                    table => 1,
6862                    hr => 1,
6863                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6864            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6865              !!!cp ('t350');
6866              !!!parse-error (type => 'in form:form', token => $token);
6867              ## Ignore the token
6868              !!!nack ('t350.1');
6869              !!!next-token;
6870              next B;
6871            }
6872    
6873          ## has a p element in scope          ## has a p element in scope
6874          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6875            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6876              !!!cp ('t344');              !!!cp ('t344');
6877              !!!back-token;              !!!back-token; # <form>
6878              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6879              redo B;                        line => $token->{line}, column => $token->{column}};
6880            } elsif ({              next B;
6881                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6882              !!!cp ('t345');              !!!cp ('t345');
6883              last INSCOPE;              last INSCOPE;
6884            }            }
6885          } # INSCOPE          } # INSCOPE
6886                        
6887          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6888          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6889              !!!nack ('t346.1');
6890            !!!next-token;            !!!next-token;
6891            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6892              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5517  sub _tree_construction_main ($) { Line 6899  sub _tree_construction_main ($) {
6899            } else {            } else {
6900              !!!cp ('t348');              !!!cp ('t348');
6901            }            }
6902          } else {          } elsif ($token->{tag_name} eq 'form') {
6903            !!!cp ('t347');            !!!cp ('t347.1');
6904              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6905    
6906              !!!nack ('t347.2');
6907            !!!next-token;            !!!next-token;
6908          }          } elsif ($token->{tag_name} eq 'table') {
6909          redo B;            !!!cp ('t382');
6910        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6911          if (defined $self->{form_element}) {            
6912            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6913            !!!parse-error (type => 'in form:form');  
6914            ## Ignore the token            !!!nack ('t382.1');
6915              !!!next-token;
6916            } elsif ($token->{tag_name} eq 'hr') {
6917              !!!cp ('t386');
6918              pop @{$self->{open_elements}};
6919            
6920              !!!nack ('t386.1');
6921            !!!next-token;            !!!next-token;
           redo B;  
6922          } else {          } else {
6923            ## 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];  
6924            !!!next-token;            !!!next-token;
           redo B;  
6925          }          }
6926        } elsif ($token->{tag_name} eq 'li') {          next B;
6927          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6928          ## has a p element in scope          ## has a p element in scope
6929          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6930            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6931              !!!cp ('t353');              !!!cp ('t353');
6932              !!!back-token;              !!!back-token; # <x>
6933              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6934              redo B;                        line => $token->{line}, column => $token->{column}};
6935            } elsif ({              next B;
6936                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6937              !!!cp ('t354');              !!!cp ('t354');
6938              last INSCOPE;              last INSCOPE;
6939            }            }
# Line 5571  sub _tree_construction_main ($) { Line 6942  sub _tree_construction_main ($) {
6942          ## Step 1          ## Step 1
6943          my $i = -1;          my $i = -1;
6944          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6945            my $li_or_dtdd = {li => {li => 1},
6946                              dt => {dt => 1, dd => 1},
6947                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6948          LI: {          LI: {
6949            ## Step 2            ## Step 2
6950            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6951              if ($i != -1) {              if ($i != -1) {
6952                !!!cp ('t355');                !!!cp ('t355');
6953                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6954                                $self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
6955                                      ->manakai_local_name,
6956                                  token => $token);
6957              } else {              } else {
6958                !!!cp ('t356');                !!!cp ('t356');
6959              }              }
# Line 5588  sub _tree_construction_main ($) { Line 6964  sub _tree_construction_main ($) {
6964            }            }
6965                        
6966            ## Step 3            ## Step 3
6967            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6968                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6969                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6970                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6971                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6972                  not ($node->[1] & DIV_EL)) {
6973              !!!cp ('t358');              !!!cp ('t358');
6974              last LI;              last LI;
6975            }            }
# Line 5604  sub _tree_construction_main ($) { Line 6981  sub _tree_construction_main ($) {
6981            redo LI;            redo LI;
6982          } # LI          } # LI
6983                        
6984          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6985          !!!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});  
6986          !!!next-token;          !!!next-token;
6987          redo B;          next B;
6988        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6989          ## has a p element in scope          ## has a p element in scope
6990          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6991            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6992              !!!cp ('t367');              !!!cp ('t367');
6993              !!!back-token;              !!!back-token; # <plaintext>
6994              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6995              redo B;                        line => $token->{line}, column => $token->{column}};
6996            } elsif ({              next B;
6997                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6998              !!!cp ('t368');              !!!cp ('t368');
6999              last INSCOPE;              last INSCOPE;
7000            }            }
7001          } # INSCOPE          } # INSCOPE
7002                        
7003          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7004                        
7005          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
7006                        
7007            !!!nack ('t368.1');
7008          !!!next-token;          !!!next-token;
7009          redo B;          next B;
7010        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
7011          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
7012            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
7013            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
7014              !!!cp ('t371');              !!!cp ('t371');
7015              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
7016                            
7017              !!!back-token;              !!!back-token; # <a>
7018              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
7019              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
7020                $formatting_end_tag->($token);
7021                            
7022              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
7023                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5720  sub _tree_construction_main ($) { Line 7042  sub _tree_construction_main ($) {
7042                        
7043          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7044    
7045          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7046          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7047    
7048            !!!nack ('t374.1');
7049          !!!next-token;          !!!next-token;
7050          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;  
7051        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
7052          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7053    
7054          ## has a |nobr| element in scope          ## has a |nobr| element in scope
7055          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7056            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7057            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
7058              !!!cp ('t376');              !!!cp ('t376');
7059              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
7060              !!!back-token;              !!!back-token; # <nobr>
7061              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
7062              redo B;                        line => $token->{line}, column => $token->{column}};
7063            } elsif ({              next B;
7064                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7065              !!!cp ('t377');              !!!cp ('t377');
7066              last INSCOPE;              last INSCOPE;
7067            }            }
7068          } # INSCOPE          } # INSCOPE
7069                    
7070          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7071          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
7072                    
7073            !!!nack ('t377.1');
7074          !!!next-token;          !!!next-token;
7075          redo B;          next B;
7076        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
7077          ## has a button element in scope          ## has a button element in scope
7078          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7079            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7080            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
7081              !!!cp ('t378');              !!!cp ('t378');
7082              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
7083              !!!back-token;              !!!back-token; # <button>
7084              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
7085              redo B;                        line => $token->{line}, column => $token->{column}};
7086            } elsif ({              next B;
7087                      table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7088              !!!cp ('t379');              !!!cp ('t379');
7089              last INSCOPE;              last INSCOPE;
7090            }            }
# Line 5785  sub _tree_construction_main ($) { Line 7092  sub _tree_construction_main ($) {
7092                        
7093          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7094                        
7095          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7096    
7097          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
7098    
7099          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7100    
7101            !!!nack ('t379.1');
7102          !!!next-token;          !!!next-token;
7103          redo B;          next B;
       } elsif ($token->{tag_name} eq 'marquee' or  
                $token->{tag_name} eq 'object') {  
         !!!cp ('t380');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @$active_formatting_elements, ['#marker', ''];  
           
         !!!next-token;  
         redo B;  
       } elsif ($token->{tag_name} eq 'xmp') {  
         !!!cp ('t381');  
         $reconstruct_active_formatting_elements->($insert_to_current);  
         $parse_rcdata->(CDATA_CONTENT_MODEL);  
         redo B;  
       } elsif ($token->{tag_name} eq 'table') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t382');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t383');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];  
   
         $self->{insertion_mode} = IN_TABLE_IM;  
             
         !!!next-token;  
         redo B;  
7104        } elsif ({        } elsif ({
7105                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7106                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7107                  image => 1,                  noembed => 1,
7108                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7109                    noscript => 0, ## TODO: 1 if scripting is enabled
7110                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7111          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7112            !!!cp ('t384');            !!!cp ('t381');
7113            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
7114          } else {          } else {
7115            !!!cp ('t385');            !!!cp ('t399');
7116          }          }
7117            ## NOTE: There is an "as if in body" code clone.
7118          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7119          $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;  
7120        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7121          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7122                    
7123          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7124            !!!cp ('t389');            !!!cp ('t389');
7125            ## Ignore the token            ## Ignore the token
7126              !!!nack ('t389'); ## NOTE: Not acknowledged.
7127            !!!next-token;            !!!next-token;
7128            redo B;            next B;
7129          } else {          } else {
7130              !!!ack ('t391.1');
7131    
7132            my $at = $token->{attributes};            my $at = $token->{attributes};
7133            my $form_attrs;            my $form_attrs;
7134            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5903  sub _tree_construction_main ($) { Line 7138  sub _tree_construction_main ($) {
7138            delete $at->{prompt};            delete $at->{prompt};
7139            my @tokens = (            my @tokens = (
7140                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7141                           attributes => $form_attrs},                           attributes => $form_attrs,
7142                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7143                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7144                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7145                            {type => START_TAG_TOKEN, tag_name => 'p',
7146                             line => $token->{line}, column => $token->{column}},
7147                            {type => START_TAG_TOKEN, tag_name => 'label',
7148                             line => $token->{line}, column => $token->{column}},
7149                         );                         );
7150            if ($prompt_attr) {            if ($prompt_attr) {
7151              !!!cp ('t390');              !!!cp ('t390');
7152              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7153                               #line => $token->{line}, column => $token->{column},
7154                              };
7155            } else {            } else {
7156              !!!cp ('t391');              !!!cp ('t391');
7157              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7158                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7159                               #line => $token->{line}, column => $token->{column},
7160                              }; # SHOULD
7161              ## TODO: make this configurable              ## TODO: make this configurable
7162            }            }
7163            push @tokens,            push @tokens,
7164                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7165                             line => $token->{line}, column => $token->{column}},
7166                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7167                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7168                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7169                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7170                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7171            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7172                             line => $token->{line}, column => $token->{column}},
7173                            {type => END_TAG_TOKEN, tag_name => 'form',
7174                             line => $token->{line}, column => $token->{column}};
7175            !!!back-token (@tokens);            !!!back-token (@tokens);
7176            redo B;            !!!next-token;
7177              next B;
7178          }          }
7179        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7180          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7181          my $el;          my $el;
7182          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7183                    
7184          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7185          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 5940  sub _tree_construction_main ($) { Line 7188  sub _tree_construction_main ($) {
7188          $insert->($el);          $insert->($el);
7189                    
7190          my $text = '';          my $text = '';
7191            !!!nack ('t392.1');
7192          !!!next-token;          !!!next-token;
7193          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7194            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 5970  sub _tree_construction_main ($) { Line 7219  sub _tree_construction_main ($) {
7219            ## Ignore the token            ## Ignore the token
7220          } else {          } else {
7221            !!!cp ('t398');            !!!cp ('t398');
7222            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7223          }          }
7224          !!!next-token;          !!!next-token;
7225            next B;
7226          } elsif ($token->{tag_name} eq 'rt' or
7227                   $token->{tag_name} eq 'rp') {
7228            ## has a |ruby| element in scope
7229            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7230              my $node = $self->{open_elements}->[$_];
7231              if ($node->[1] & RUBY_EL) {
7232                !!!cp ('t398.1');
7233                ## generate implied end tags
7234                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7235                  !!!cp ('t398.2');
7236                  pop @{$self->{open_elements}};
7237                }
7238                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7239                  !!!cp ('t398.3');
7240                  !!!parse-error (type => 'not closed',
7241                                  text => $self->{open_elements}->[-1]->[0]
7242                                      ->manakai_local_name,
7243                                  token => $token);
7244                  pop @{$self->{open_elements}}
7245                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7246                }
7247                last INSCOPE;
7248              } elsif ($node->[1] & SCOPING_EL) {
7249                !!!cp ('t398.4');
7250                last INSCOPE;
7251              }
7252            } # INSCOPE
7253    
7254            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7255    
7256            !!!nack ('t398.5');
7257            !!!next-token;
7258          redo B;          redo B;
7259        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7260                  iframe => 1,                 $token->{tag_name} eq 'svg') {
                 noembed => 1,  
                 noframes => 1,  
                 noscript => 0, ## TODO: 1 if scripting is enabled  
                }->{$token->{tag_name}}) {  
         !!!cp ('t399');  
         ## NOTE: There is an "as if in body" code clone.  
         $parse_rcdata->(CDATA_CONTENT_MODEL);  
         redo B;  
       } elsif ($token->{tag_name} eq 'select') {  
         !!!cp ('t400');  
7261          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
7262    
7263          ## TODO: associate with $self->{form_element} if defined          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7264    
7265            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7266    
7267            ## "adjust foreign attributes" - done in insert-element-f
7268                    
7269          if ($self->{insertion_mode} & TABLE_IMS or          !!!insert-element-f ($token->{tag_name} eq 'math' ? $MML_NS : $SVG_NS, $token->{tag_name}, $token->{attributes}, $token);
7270              $self->{insertion_mode} & BODY_TABLE_IMS or          
7271              $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {          if ($self->{self_closing}) {
7272            !!!cp ('t400.1');            pop @{$self->{open_elements}};
7273            $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;            !!!ack ('t398.1');
7274          } else {          } else {
7275            !!!cp ('t400.2');            !!!cp ('t398.2');
7276            $self->{insertion_mode} = IN_SELECT_IM;            $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7277              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7278              ## mode, "in body" (not "in foreign content") secondary insertion
7279              ## mode, maybe.
7280          }          }
7281    
7282          !!!next-token;          !!!next-token;
7283          redo B;          next B;
7284        } elsif ({        } elsif ({
7285                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7286                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6010  sub _tree_construction_main ($) { Line 7288  sub _tree_construction_main ($) {
7288                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7289                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7290          !!!cp ('t401');          !!!cp ('t401');
7291          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
7292                            text => $token->{tag_name}, token => $token);
7293          ## Ignore the token          ## Ignore the token
7294            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7295          !!!next-token;          !!!next-token;
7296          redo B;          next B;
7297                    
7298          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7299        } else {        } else {
7300          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
7301              !!!cp ('t384');
7302              !!!parse-error (type => 'image', token => $token);
7303              $token->{tag_name} = 'img';
7304            } else {
7305              !!!cp ('t385');
7306            }
7307    
7308            ## NOTE: There is an "as if <br>" code clone.
7309          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7310                    
7311          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7312    
7313            if ({
7314                 applet => 1, marquee => 1, object => 1,
7315                }->{$token->{tag_name}}) {
7316              !!!cp ('t380');
7317              push @$active_formatting_elements, ['#marker', ''];
7318              !!!nack ('t380.1');
7319            } elsif ({
7320                      b => 1, big => 1, em => 1, font => 1, i => 1,
7321                      s => 1, small => 1, strile => 1,
7322                      strong => 1, tt => 1, u => 1,
7323                     }->{$token->{tag_name}}) {
7324              !!!cp ('t375');
7325              push @$active_formatting_elements, $self->{open_elements}->[-1];
7326              !!!nack ('t375.1');
7327            } elsif ($token->{tag_name} eq 'input') {
7328              !!!cp ('t388');
7329              ## TODO: associate with $self->{form_element} if defined
7330              pop @{$self->{open_elements}};
7331              !!!ack ('t388.2');
7332            } elsif ({
7333                      area => 1, basefont => 1, bgsound => 1, br => 1,
7334                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7335                      #image => 1,
7336                     }->{$token->{tag_name}}) {
7337              !!!cp ('t388.1');
7338              pop @{$self->{open_elements}};
7339              !!!ack ('t388.3');
7340            } elsif ($token->{tag_name} eq 'select') {
7341              ## TODO: associate with $self->{form_element} if defined
7342            
7343              if ($self->{insertion_mode} & TABLE_IMS or
7344                  $self->{insertion_mode} & BODY_TABLE_IMS or
7345                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7346                !!!cp ('t400.1');
7347                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7348              } else {
7349                !!!cp ('t400.2');
7350                $self->{insertion_mode} = IN_SELECT_IM;
7351              }
7352              !!!nack ('t400.3');
7353            } else {
7354              !!!nack ('t402');
7355            }
7356                    
7357          !!!next-token;          !!!next-token;
7358          redo B;          next B;
7359        }        }
7360      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7361        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7362          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7363              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7364            for (@{$self->{open_elements}}) {          INSCOPE: {
7365              unless ({            for (reverse @{$self->{open_elements}}) {
7366                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7367                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7368                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7369                      }->{$_->[1]}) {                last INSCOPE;
7370                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
7371                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
7372              } else {                last;
               !!!cp ('t404');  
7373              }              }
7374            }            }
7375    
7376            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7377                              text => $token->{tag_name}, token => $token);
7378              ## NOTE: Ignore the token.
7379            !!!next-token;            !!!next-token;
7380            redo B;            next B;
7381          } else {          } # INSCOPE
7382            !!!cp ('t405');  
7383            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          for (@{$self->{open_elements}}) {
7384            ## Ignore the token            unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7385            !!!next-token;              !!!cp ('t403');
7386            redo B;              !!!parse-error (type => 'not closed',
7387                                text => $_->[0]->manakai_local_name,
7388                                token => $token);
7389                last;
7390              } else {
7391                !!!cp ('t404');
7392              }
7393          }          }
7394    
7395            $self->{insertion_mode} = AFTER_BODY_IM;
7396            !!!next-token;
7397            next B;
7398        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7399          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
7400            ## up-to-date, though it has same effect as speced.
7401            if (@{$self->{open_elements}} > 1 and
7402                $self->{open_elements}->[1]->[1] & BODY_EL) {
7403            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7404            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7405              !!!cp ('t406');              !!!cp ('t406');
7406              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
7407                                text => $self->{open_elements}->[1]->[0]
7408                                    ->manakai_local_name,
7409                                token => $token);
7410            } else {            } else {
7411              !!!cp ('t407');              !!!cp ('t407');
7412            }            }
7413            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7414            ## reprocess            ## reprocess
7415            redo B;            next B;
7416          } else {          } else {
7417            !!!cp ('t408');            !!!cp ('t408');
7418            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7419                              text => $token->{tag_name}, token => $token);
7420            ## Ignore the token            ## Ignore the token
7421            !!!next-token;            !!!next-token;
7422            redo B;            next B;
7423          }          }
7424        } elsif ({        } elsif ({
7425                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
7426                  div => 1, dl => 1, fieldset => 1, listing => 1,                  div => 1, dl => 1, fieldset => 1, listing => 1,
7427                  menu => 1, ol => 1, pre => 1, ul => 1,                  menu => 1, ol => 1, pre => 1, ul => 1,
7428                  dd => 1, dt => 1, li => 1,                  dd => 1, dt => 1, li => 1,
7429                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7430                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7431          ## has an element in scope          ## has an element in scope
7432          my $i;          my $i;
7433          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7434            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7435            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7436              !!!cp ('t410');              !!!cp ('t410');
7437              $i = $_;              $i = $_;
7438              last INSCOPE;              last INSCOPE;
7439            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7440              !!!cp ('t411');              !!!cp ('t411');
7441              last INSCOPE;              last INSCOPE;
7442            }            }
# Line 6097  sub _tree_construction_main ($) { Line 7444  sub _tree_construction_main ($) {
7444    
7445          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7446            !!!cp ('t413');            !!!cp ('t413');
7447            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7448                              text => $token->{tag_name}, token => $token);
7449              ## NOTE: Ignore the token.
7450          } else {          } else {
7451            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7452            while ({            while ({
7453                      ## END_TAG_OPTIONAL_EL
7454                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7455                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7456                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7457                    p => 1,                    p => 1,
7458                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7459                      rp => 1,
7460                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7461              !!!cp ('t409');              !!!cp ('t409');
7462              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7463            }            }
7464    
7465            ## Step 2.            ## Step 2.
7466            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7467                      ne $token->{tag_name}) {
7468              !!!cp ('t412');              !!!cp ('t412');
7469              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7470                                text => $self->{open_elements}->[-1]->[0]
7471                                    ->manakai_local_name,
7472                                token => $token);
7473            } else {            } else {
7474              !!!cp ('t414');              !!!cp ('t414');
7475            }            }
# Line 6124  sub _tree_construction_main ($) { Line 7480  sub _tree_construction_main ($) {
7480            ## Step 4.            ## Step 4.
7481            $clear_up_to_marker->()            $clear_up_to_marker->()
7482                if {                if {
7483                  button => 1, marquee => 1, object => 1,                  applet => 1, button => 1, marquee => 1, object => 1,
7484                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7485          }          }
7486          !!!next-token;          !!!next-token;
7487          redo B;          next B;
7488        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7489          undef $self->{form_element};          undef $self->{form_element};
7490    
# Line 6136  sub _tree_construction_main ($) { Line 7492  sub _tree_construction_main ($) {
7492          my $i;          my $i;
7493          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7494            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7495            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7496              !!!cp ('t418');              !!!cp ('t418');
7497              $i = $_;              $i = $_;
7498              last INSCOPE;              last INSCOPE;
7499            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7500              !!!cp ('t419');              !!!cp ('t419');
7501              last INSCOPE;              last INSCOPE;
7502            }            }
# Line 6151  sub _tree_construction_main ($) { Line 7504  sub _tree_construction_main ($) {
7504    
7505          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7506            !!!cp ('t421');            !!!cp ('t421');
7507            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7508                              text => $token->{tag_name}, token => $token);
7509              ## NOTE: Ignore the token.
7510          } else {          } else {
7511            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7512            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7513              !!!cp ('t417');              !!!cp ('t417');
7514              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7515            }            }
7516                        
7517            ## Step 2.            ## Step 2.
7518            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7519                      ne $token->{tag_name}) {
7520              !!!cp ('t417.1');              !!!cp ('t417.1');
7521              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7522                                text => $self->{open_elements}->[-1]->[0]
7523                                    ->manakai_local_name,
7524                                token => $token);
7525            } else {            } else {
7526              !!!cp ('t420');              !!!cp ('t420');
7527            }              }  
# Line 6174  sub _tree_construction_main ($) { Line 7531  sub _tree_construction_main ($) {
7531          }          }
7532    
7533          !!!next-token;          !!!next-token;
7534          redo B;          next B;
7535        } elsif ({        } elsif ({
7536                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7537                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6182  sub _tree_construction_main ($) { Line 7539  sub _tree_construction_main ($) {
7539          my $i;          my $i;
7540          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7541            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7542            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7543              !!!cp ('t423');              !!!cp ('t423');
7544              $i = $_;              $i = $_;
7545              last INSCOPE;              last INSCOPE;
7546            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7547              !!!cp ('t424');              !!!cp ('t424');
7548              last INSCOPE;              last INSCOPE;
7549            }            }
# Line 6199  sub _tree_construction_main ($) { Line 7551  sub _tree_construction_main ($) {
7551    
7552          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7553            !!!cp ('t425.1');            !!!cp ('t425.1');
7554            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7555                              text => $token->{tag_name}, token => $token);
7556              ## NOTE: Ignore the token.
7557          } else {          } else {
7558            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7559            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7560              !!!cp ('t422');              !!!cp ('t422');
7561              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7562            }            }
7563                        
7564            ## Step 2.            ## Step 2.
7565            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7566                      ne $token->{tag_name}) {
7567              !!!cp ('t425');              !!!cp ('t425');
7568              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
7569                                text => $token->{tag_name}, token => $token);
7570            } else {            } else {
7571              !!!cp ('t426');              !!!cp ('t426');
7572            }            }
# Line 6222  sub _tree_construction_main ($) { Line 7576  sub _tree_construction_main ($) {
7576          }          }
7577                    
7578          !!!next-token;          !!!next-token;
7579          redo B;          next B;
7580        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7581          ## has an element in scope          ## has an element in scope
7582          my $i;          my $i;
7583          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7584            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7585            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7586              !!!cp ('t410.1');              !!!cp ('t410.1');
7587              $i = $_;              $i = $_;
7588              last INSCOPE;              last INSCOPE;
7589            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7590              !!!cp ('t411.1');              !!!cp ('t411.1');
7591              last INSCOPE;              last INSCOPE;
7592            }            }
7593          } # INSCOPE          } # INSCOPE
7594    
7595          if (defined $i) {          if (defined $i) {
7596            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7597                      ne $token->{tag_name}) {
7598              !!!cp ('t412.1');              !!!cp ('t412.1');
7599              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7600                                text => $self->{open_elements}->[-1]->[0]
7601                                    ->manakai_local_name,
7602                                token => $token);
7603            } else {            } else {
7604              !!!cp ('t414.1');              !!!cp ('t414.1');
7605            }            }
# Line 6252  sub _tree_construction_main ($) { Line 7607  sub _tree_construction_main ($) {
7607            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7608          } else {          } else {
7609            !!!cp ('t413.1');            !!!cp ('t413.1');
7610            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7611                              text => $token->{tag_name}, token => $token);
7612    
7613            !!!cp ('t415.1');            !!!cp ('t415.1');
7614            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7615            my $el;            my $el;
7616            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
7617            $insert->($el);            $insert->($el);
7618            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7619          }          }
7620    
7621          !!!next-token;          !!!next-token;
7622          redo B;          next B;
7623        } elsif ({        } elsif ({
7624                  a => 1,                  a => 1,
7625                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6271  sub _tree_construction_main ($) { Line 7627  sub _tree_construction_main ($) {
7627                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7628                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7629          !!!cp ('t427');          !!!cp ('t427');
7630          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7631          redo B;          next B;
7632        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7633          !!!cp ('t428');          !!!cp ('t428');
7634          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
7635                            text => 'br', token => $token);
7636    
7637          ## As if <br>          ## As if <br>
7638          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7639                    
7640          my $el;          my $el;
7641          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7642          $insert->($el);          $insert->($el);
7643                    
7644          ## Ignore the token.          ## Ignore the token.
7645          !!!next-token;          !!!next-token;
7646          redo B;          next B;
7647        } elsif ({        } elsif ({
7648                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7649                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6300  sub _tree_construction_main ($) { Line 7657  sub _tree_construction_main ($) {
7657                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7658                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7659          !!!cp ('t429');          !!!cp ('t429');
7660          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
7661                            text => $token->{tag_name}, token => $token);
7662          ## Ignore the token          ## Ignore the token
7663          !!!next-token;          !!!next-token;
7664          redo B;          next B;
7665                    
7666          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7667                    
# Line 6314  sub _tree_construction_main ($) { Line 7672  sub _tree_construction_main ($) {
7672    
7673          ## Step 2          ## Step 2
7674          S2: {          S2: {
7675            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7676              ## Step 1              ## Step 1
7677              ## generate implied end tags              ## generate implied end tags
7678              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7679                !!!cp ('t430');                !!!cp ('t430');
7680                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7681                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7682                  ## which seems wrong.
7683                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7684                  $node_i++;
7685              }              }
7686                    
7687              ## Step 2              ## Step 2
7688              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7689                        ne $token->{tag_name}) {
7690                !!!cp ('t431');                !!!cp ('t431');
7691                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7692                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7693                                  text => $self->{open_elements}->[-1]->[0]
7694                                      ->manakai_local_name,
7695                                  token => $token);
7696              } else {              } else {
7697                !!!cp ('t432');                !!!cp ('t432');
7698              }              }
7699                            
7700              ## Step 3              ## Step 3
7701              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7702    
7703              !!!next-token;              !!!next-token;
7704              last S2;              last S2;
7705            } else {            } else {
7706              ## Step 3              ## Step 3
7707              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7708                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7709                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7710                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7711                !!!cp ('t433');                !!!cp ('t433');
7712                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
7713                                  text => $token->{tag_name}, token => $token);
7714                ## Ignore the token                ## Ignore the token
7715                !!!next-token;                !!!next-token;
7716                last S2;                last S2;
# Line 6362  sub _tree_construction_main ($) { Line 7726  sub _tree_construction_main ($) {
7726            ## Step 5;            ## Step 5;
7727            redo S2;            redo S2;
7728          } # S2          } # S2
7729          redo B;          next B;
7730        }        }
7731      }      }
7732      redo B;      next B;
7733      } continue { # B
7734        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7735          ## NOTE: The code below is executed in cases where it does not have
7736          ## to be, but it it is harmless even in those cases.
7737          ## has an element in scope
7738          INSCOPE: {
7739            for (reverse 0..$#{$self->{open_elements}}) {
7740              my $node = $self->{open_elements}->[$_];
7741              if ($node->[1] & FOREIGN_EL) {
7742                last INSCOPE;
7743              } elsif ($node->[1] & SCOPING_EL) {
7744                last;
7745              }
7746            }
7747            
7748            ## NOTE: No foreign element in scope.
7749            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7750          } # INSCOPE
7751        }
7752    } # B    } # B
7753    
7754    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6373  sub _tree_construction_main ($) { Line 7756  sub _tree_construction_main ($) {
7756    ## TODO: script stuffs    ## TODO: script stuffs
7757  } # _tree_construct_main  } # _tree_construct_main
7758    
7759  sub set_inner_html ($$$) {  sub set_inner_html ($$$$;$) {
7760    my $class = shift;    my $class = shift;
7761    my $node = shift;    my $node = shift;
7762    my $s = \$_[0];    #my $s = \$_[0];
7763    my $onerror = $_[1];    my $onerror = $_[1];
7764      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7765    
7766    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7767    
# Line 6396  sub set_inner_html ($$$) { Line 7780  sub set_inner_html ($$$) {
7780      }      }
7781    
7782      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7783      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($_[0] => $node, $onerror, $get_wrapper);
7784    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7785      ## TODO: If non-html element      ## TODO: If non-html element
7786    
7787      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7788    
7789    ## TODO: Support for $get_wrapper
7790    
7791      ## Step 1 # MUST      ## Step 1 # MUST
7792      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7793      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6411  sub set_inner_html ($$$) { Line 7797  sub set_inner_html ($$$) {
7797    
7798      ## Step 8 # MUST      ## Step 8 # MUST
7799      my $i = 0;      my $i = 0;
7800      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7801      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7802        require Whatpm::Charset::DecodeHandle;
7803        my $input = Whatpm::Charset::DecodeHandle::CharString->new (\($_[0]));
7804        $input = $get_wrapper->($input);
7805      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7806        my $self = shift;        my $self = shift;
7807    
7808        pop @{$self->{prev_char}};        pop @{$self->{prev_char}};
7809        unshift @{$self->{prev_char}}, $self->{next_char};        unshift @{$self->{prev_char}}, $self->{next_char};
7810    
7811        $self->{next_char} = -1 and return if $i >= length $$s;        my $char = '';
7812        $self->{next_char} = ord substr $$s, $i++, 1;        if (defined $self->{next_next_char}) {
7813        $column++;          $char = $self->{next_next_char};
7814            delete $self->{next_next_char};
7815            $self->{next_char} = ord $char;
7816          } else {
7817            if ($input->read ($char, 1)) {
7818              $self->{next_char} = ord $char;
7819            } else {
7820              $self->{next_char} = -1;
7821              return;
7822            }
7823          }
7824    
7825          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7826          $p->{column}++;
7827    
7828        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7829          $line++;          $p->{line}++;
7830          $column = 0;          $p->{column} = 0;
7831          !!!cp ('i1');          !!!cp ('i1');
7832        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7833          $i++ if substr ($$s, $i, 1) eq "\x0A";  ## TODO: support for abort/streaming
7834            my $next = '';
7835            if ($input->read ($next, 1) and $next ne "\x0A") {
7836              $self->{next_next_char} = $next;
7837            }
7838          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7839          $line++;          $p->{line}++;
7840          $column = 0;          $p->{column} = 0;
7841          !!!cp ('i2');          !!!cp ('i2');
7842        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7843          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6440  sub set_inner_html ($$$) { Line 7846  sub set_inner_html ($$$) {
7846          !!!cp ('i4');          !!!cp ('i4');
7847          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7848          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7849          } elsif ($self->{next_char} <= 0x0008 or
7850                   (0x000E <= $self->{next_char} and
7851                    $self->{next_char} <= 0x001F) or
7852                   (0x007F <= $self->{next_char} and
7853                    $self->{next_char} <= 0x009F) or
7854                   (0xD800 <= $self->{next_char} and
7855                    $self->{next_char} <= 0xDFFF) or
7856                   (0xFDD0 <= $self->{next_char} and
7857                    $self->{next_char} <= 0xFDDF) or
7858                   {
7859                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7860                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7861                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7862                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7863                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7864                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7865                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7866                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7867                    0x10FFFE => 1, 0x10FFFF => 1,
7868                   }->{$self->{next_char}}) {
7869            !!!cp ('i4.1');
7870            if ($self->{next_char} < 0x10000) {
7871              !!!parse-error (type => 'control char',
7872                              text => (sprintf 'U+%04X', $self->{next_char}));
7873            } else {
7874              !!!parse-error (type => 'control char',
7875                              text => (sprintf 'U-%08X', $self->{next_char}));
7876            }
7877        }        }
7878      };      };
7879      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
7880      $p->{next_char} = -1;      $p->{next_char} = -1;
7881        
7882        $p->{read_until} = sub {
7883          #my ($scalar, $specials_range, $offset) = @_;
7884          my $specials_range = $_[1];
7885          return 0 if defined $p->{next_next_char};
7886          my $count = $input->manakai_read_until
7887            ($_[0],
7888             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}]/,
7889             $_[2]);
7890          if ($count) {
7891            $p->{column} += $count;
7892            $p->{column_prev} += $count;
7893            $p->{prev_char} = [-1, -1, -1];
7894            $p->{next_char} = -1;
7895          }
7896          return $count;
7897        }; # $p->{read_until}
7898    
7899      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7900        my (%opt) = @_;        my (%opt) = @_;
7901        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7902          my $column = $opt{column};
7903          if (defined $opt{token} and defined $opt{token}->{line}) {
7904            $line = $opt{token}->{line};
7905            $column = $opt{token}->{column};
7906          }
7907          warn "Parse error ($opt{type}) at line $line column $column\n";
7908      };      };
7909      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7910        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7911      };      };
7912            
7913        my $char_onerror = sub {
7914          my (undef, $type, %opt) = @_;
7915          $ponerror->(layer => 'encode',
7916                      line => $p->{line}, column => $p->{column} + 1,
7917                      %opt, type => $type);
7918        }; # $char_onerror
7919        $input->onerror ($char_onerror);
7920    
7921      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
7922      $p->_initialize_tree_constructor;      $p->_initialize_tree_constructor;
7923    
# Line 6474  sub set_inner_html ($$$) { Line 7939  sub set_inner_html ($$$) {
7939          unless defined $p->{content_model};          unless defined $p->{content_model};
7940          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7941    
7942      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7943          ## TODO: Foreign element OK?
7944    
7945      ## Step 3      ## Step 3
7946      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6484  sub set_inner_html ($$$) { Line 7950  sub set_inner_html ($$$) {
7950      $doc->append_child ($root);      $doc->append_child ($root);
7951    
7952      ## Step 5 # MUST      ## Step 5 # MUST
7953      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7954    
7955      undef $p->{head_element};      undef $p->{head_element};
7956    
# Line 6530  sub set_inner_html ($$$) { Line 7996  sub set_inner_html ($$$) {
7996      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7997    
7998      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7999    
8000        delete $p->{parse_error}; # delete loop
8001    } else {    } else {
8002      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";
8003    }    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24