/[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.106 by wakaba, Sun Mar 9 10:31:19 2008 UTC revision 1.170 by wakaba, Sat Sep 13 12:25:44 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 = {  
   applet => 1, button => 1, caption => 1, html => 1, marquee => 1, object => 1,  
   table => 1, td => 1, th => 1,  
 };  
 my $formatting_category = {  
   a => 1, b => 1, big => 1, em => 1, font => 1, i => 1, nobr => 1,  
   s => 1, small => 1, strile => 1, strong => 1, tt => 1, u => 1,  
 };  
 # $phrasing_category: all other elements  
   
350  sub parse_byte_string ($$$$;$) {  sub parse_byte_string ($$$$;$) {
351      my $self = shift;
352      my $charset_name = shift;
353      open my $input, '<', ref $_[0] ? $_[0] : \($_[0]);
354      return $self->parse_byte_stream ($charset_name, $input, @_[1..$#_]);
355    } # parse_byte_string
356    
357    sub parse_byte_stream ($$$$;$$) {
358      # my ($self, $charset_name, $byte_stream, $doc, $onerror, $get_wrapper) = @_;
359    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
360    my $charset = shift;    my $charset_name = shift;
361    my $bytes_s = ref $_[0] ? $_[0] : \($_[0]);    my $byte_stream = $_[0];
   my $s;  
     
   if (defined $charset) {  
     require Encode; ## TODO: decode(utf8) don't delete BOM  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = lc $charset; ## TODO: normalize name  
     $self->{confident} = 1;  
   } else {  
     ## TODO: Implement HTML5 detection algorithm  
     require Whatpm::Charset::UniversalCharDet;  
     $charset = Whatpm::Charset::UniversalCharDet->detect_byte_string  
         (substr ($$bytes_s, 0, 1024));  
     $charset ||= 'windows-1252';  
     $s = \ (Encode::decode ($charset, $$bytes_s));  
     $self->{input_encoding} = $charset;  
     $self->{confident} = 0;  
   }  
362    
363    $self->{change_encoding} = sub {    my $onerror = $_[2] || sub {
364      my $self = shift;      my (%opt) = @_;
365      my $charset = lc shift;      warn "Parse error ($opt{type})\n";
366      ## TODO: if $charset is supported    };
367      ## TODO: normalize charset name    $self->{parse_error} = $onerror; # updated later by parse_char_string
368    
369      ## "Change the encoding" algorithm:    my $get_wrapper = $_[3] || sub ($) {
370        return $_[0]; # $_[0] = byte stream handle, returned = arg to char handle
371      ## Step 1        };
372      if ($charset eq 'utf-16') { ## ISSUE: UTF-16BE -> UTF-8? UTF-16LE -> UTF-8?  
373        $charset = 'utf-8';    ## HTML5 encoding sniffing algorithm
374      require Message::Charset::Info;
375      my $charset;
376      my $buffer;
377      my ($char_stream, $e_status);
378    
379      SNIFFING: {
380        ## NOTE: By setting |allow_fallback| option true when the
381        ## |get_decode_handle| method is invoked, we ignore what the HTML5
382        ## spec requires, i.e. unsupported encoding should be ignored.
383          ## TODO: We should not do this unless the parser is invoked
384          ## in the conformance checking mode, in which this behavior
385          ## would be useful.
386    
387        ## Step 1
388        if (defined $charset_name) {
389          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
390              ## TODO: Is this ok?  Transfer protocol's parameter should be
391              ## interpreted in its semantics?
392    
393          ## ISSUE: Unsupported encoding is not ignored according to the spec.
394          ($char_stream, $e_status) = $charset->get_decode_handle
395              ($byte_stream, allow_error_reporting => 1,
396               allow_fallback => 1);
397          if ($char_stream) {
398            $self->{confident} = 1;
399            last SNIFFING;
400          } else {
401            ## TODO: unsupported error
402          }
403      }      }
404    
405      ## Step 2      ## Step 2
406      if (defined $self->{input_encoding} and      my $byte_buffer = '';
407          $self->{input_encoding} eq $charset) {      for (1..1024) {
408          my $char = $byte_stream->getc;
409          last unless defined $char;
410          $byte_buffer .= $char;
411        } ## TODO: timeout
412    
413        ## Step 3
414        if ($byte_buffer =~ /^\xFE\xFF/) {
415          $charset = Message::Charset::Info->get_by_html_name ('utf-16be');
416          ($char_stream, $e_status) = $charset->get_decode_handle
417              ($byte_stream, allow_error_reporting => 1,
418               allow_fallback => 1, byte_buffer => \$byte_buffer);
419        $self->{confident} = 1;        $self->{confident} = 1;
420        return;        last SNIFFING;
421        } elsif ($byte_buffer =~ /^\xFF\xFE/) {
422          $charset = Message::Charset::Info->get_by_html_name ('utf-16le');
423          ($char_stream, $e_status) = $charset->get_decode_handle
424              ($byte_stream, allow_error_reporting => 1,
425               allow_fallback => 1, byte_buffer => \$byte_buffer);
426          $self->{confident} = 1;
427          last SNIFFING;
428        } elsif ($byte_buffer =~ /^\xEF\xBB\xBF/) {
429          $charset = Message::Charset::Info->get_by_html_name ('utf-8');
430          ($char_stream, $e_status) = $charset->get_decode_handle
431              ($byte_stream, allow_error_reporting => 1,
432               allow_fallback => 1, byte_buffer => \$byte_buffer);
433          $self->{confident} = 1;
434          last SNIFFING;
435      }      }
436    
437      !!!parse-error (type => 'charset label detected:'.$self->{input_encoding}.      ## Step 4
438          ':'.$charset, level => 'w');      ## TODO: <meta charset>
439    
440      ## Step 3      ## Step 5
441      # if (can) {      ## TODO: from history
       ## change the encoding on the fly.  
       #$self->{confident} = 1;  
       #return;  
     # }  
442    
443      ## Step 4      ## Step 6
444      throw Whatpm::HTML::RestartParser (charset => $charset);      require Whatpm::Charset::UniversalCharDet;
445        $charset_name = Whatpm::Charset::UniversalCharDet->detect_byte_string
446            ($byte_buffer);
447        if (defined $charset_name) {
448          $charset = Message::Charset::Info->get_by_html_name ($charset_name);
449    
450          ## ISSUE: Unsupported encoding is not ignored according to the spec.
451          require Whatpm::Charset::DecodeHandle;
452          $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
453              ($byte_stream);
454          ($char_stream, $e_status) = $charset->get_decode_handle
455              ($buffer, allow_error_reporting => 1,
456               allow_fallback => 1, byte_buffer => \$byte_buffer);
457          if ($char_stream) {
458            $buffer->{buffer} = $byte_buffer;
459            !!!parse-error (type => 'sniffing:chardet',
460                            text => $charset_name,
461                            level => $self->{level}->{info},
462                            layer => 'encode',
463                            line => 1, column => 1);
464            $self->{confident} = 0;
465            last SNIFFING;
466          }
467        }
468    
469        ## Step 7: default
470        ## TODO: Make this configurable.
471        $charset = Message::Charset::Info->get_by_html_name ('windows-1252');
472            ## NOTE: We choose |windows-1252| here, since |utf-8| should be
473            ## detectable in the step 6.
474        require Whatpm::Charset::DecodeHandle;
475        $buffer = Whatpm::Charset::DecodeHandle::ByteBuffer->new
476            ($byte_stream);
477        ($char_stream, $e_status)
478            = $charset->get_decode_handle ($buffer,
479                                           allow_error_reporting => 1,
480                                           allow_fallback => 1,
481                                           byte_buffer => \$byte_buffer);
482        $buffer->{buffer} = $byte_buffer;
483        !!!parse-error (type => 'sniffing:default',
484                        text => 'windows-1252',
485                        level => $self->{level}->{info},
486                        line => 1, column => 1,
487                        layer => 'encode');
488        $self->{confident} = 0;
489      } # SNIFFING
490    
491      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
492        $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
493        !!!parse-error (type => 'chardecode:fallback',
494                        #text => $self->{input_encoding},
495                        level => $self->{level}->{uncertain},
496                        line => 1, column => 1,
497                        layer => 'encode');
498      } elsif (not ($e_status &
499                    Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
500        $self->{input_encoding} = $charset->get_iana_name;
501        !!!parse-error (type => 'chardecode:no error',
502                        text => $self->{input_encoding},
503                        level => $self->{level}->{uncertain},
504                        line => 1, column => 1,
505                        layer => 'encode');
506      } else {
507        $self->{input_encoding} = $charset->get_iana_name;
508      }
509    
510      $self->{change_encoding} = sub {
511        my $self = shift;
512        $charset_name = shift;
513        my $token = shift;
514    
515        $charset = Message::Charset::Info->get_by_html_name ($charset_name);
516        ($char_stream, $e_status) = $charset->get_decode_handle
517            ($byte_stream, allow_error_reporting => 1, allow_fallback => 1,
518             byte_buffer => \ $buffer->{buffer});
519        
520        if ($char_stream) { # if supported
521          ## "Change the encoding" algorithm:
522    
523          ## Step 1    
524          if ($charset->{category} &
525              Message::Charset::Info::CHARSET_CATEGORY_UTF16 ()) {
526            $charset = Message::Charset::Info->get_by_html_name ('utf-8');
527            ($char_stream, $e_status) = $charset->get_decode_handle
528                ($byte_stream,
529                 byte_buffer => \ $buffer->{buffer});
530          }
531          $charset_name = $charset->get_iana_name;
532          
533          ## Step 2
534          if (defined $self->{input_encoding} and
535              $self->{input_encoding} eq $charset_name) {
536            !!!parse-error (type => 'charset label:matching',
537                            text => $charset_name,
538                            level => $self->{level}->{info});
539            $self->{confident} = 1;
540            return;
541          }
542    
543          !!!parse-error (type => 'charset label detected',
544                          text => $self->{input_encoding},
545                          value => $charset_name,
546                          level => $self->{level}->{warn},
547                          token => $token);
548          
549          ## Step 3
550          # if (can) {
551            ## change the encoding on the fly.
552            #$self->{confident} = 1;
553            #return;
554          # }
555          
556          ## Step 4
557          throw Whatpm::HTML::RestartParser ();
558        }
559    }; # $self->{change_encoding}    }; # $self->{change_encoding}
560    
561      my $char_onerror = sub {
562        my (undef, $type, %opt) = @_;
563        !!!parse-error (layer => 'encode',
564                        %opt, type => $type,
565                        line => $self->{line}, column => $self->{column} + 1);
566        if ($opt{octets}) {
567          ${$opt{octets}} = "\x{FFFD}"; # relacement character
568        }
569      };
570    
571      my $wrapped_char_stream = $get_wrapper->($char_stream);
572      $wrapped_char_stream->onerror ($char_onerror);
573    
574    my @args = @_; shift @args; # $s    my @args = @_; shift @args; # $s
575    my $return;    my $return;
576    try {    try {
577      $return = $self->parse_char_string ($s, @args);        $return = $self->parse_char_stream ($wrapped_char_stream, @args);  
578    } catch Whatpm::HTML::RestartParser with {    } catch Whatpm::HTML::RestartParser with {
579      my $charset = shift->{charset};      ## NOTE: Invoked after {change_encoding}.
580      $s = \ (Encode::decode ($charset, $$bytes_s));      
581      $self->{input_encoding} = $charset; ## TODO: normalize      if ($e_status & Message::Charset::Info::FALLBACK_ENCODING_IMPL ()) {
582          $self->{input_encoding} = $charset->get_iana_name; ## TODO: Should we set actual charset decoder's encoding name?
583          !!!parse-error (type => 'chardecode:fallback',
584                          level => $self->{level}->{uncertain},
585                          #text => $self->{input_encoding},
586                          line => 1, column => 1,
587                          layer => 'encode');
588        } elsif (not ($e_status &
589                      Message::Charset::Info::ERROR_REPORTING_ENCODING_IMPL())) {
590          $self->{input_encoding} = $charset->get_iana_name;
591          !!!parse-error (type => 'chardecode:no error',
592                          text => $self->{input_encoding},
593                          level => $self->{level}->{uncertain},
594                          line => 1, column => 1,
595                          layer => 'encode');
596        } else {
597          $self->{input_encoding} = $charset->get_iana_name;
598        }
599      $self->{confident} = 1;      $self->{confident} = 1;
600      $return = $self->parse_char_string ($s, @args);  
601        $wrapped_char_stream = $get_wrapper->($char_stream);
602        $wrapped_char_stream->onerror ($char_onerror);
603    
604        $return = $self->parse_char_stream ($wrapped_char_stream, @args);
605    };    };
606    return $return;    return $return;
607  } # parse_byte_string  } # parse_byte_stream
608    
609  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM  ## NOTE: HTML5 spec says that the encoding layer MUST NOT strip BOM
610  ## and the HTML layer MUST ignore it.  However, we does strip BOM in  ## and the HTML layer MUST ignore it.  However, we does strip BOM in
# Line 162  sub parse_byte_string ($$$$;$) { Line 615  sub parse_byte_string ($$$$;$) {
615  ## such as |parse_byte_string| in this module, must ensure that it does  ## such as |parse_byte_string| in this module, must ensure that it does
616  ## strip the BOM and never strip any ZWNBSP.  ## strip the BOM and never strip any ZWNBSP.
617    
618  *parse_char_string = \&parse_string;  sub parse_char_string ($$$;$$) {
619      #my ($self, $s, $doc, $onerror, $get_wrapper) = @_;
620      my $self = shift;
621      require utf8;
622      my $s = ref $_[0] ? $_[0] : \($_[0]);
623      open my $input, '<' . (utf8::is_utf8 ($$s) ? ':utf8' : ''), $s;
624      if ($_[3]) {
625        $input = $_[3]->($input);
626      }
627      return $self->parse_char_stream ($input, @_[1..$#_]);
628    } # parse_char_string
629    *parse_string = \&parse_char_string; ## NOTE: Alias for backward compatibility.
630    
631  sub parse_string ($$$;$) {  sub parse_char_stream ($$$;$) {
632    my $self = ref $_[0] ? shift : shift->new;    my $self = ref $_[0] ? shift : shift->new;
633    my $s = ref $_[0] ? $_[0] : \($_[0]);    my $input = $_[0];
634    $self->{document} = $_[1];    $self->{document} = $_[1];
635    @{$self->{document}->child_nodes} = ();    @{$self->{document}->child_nodes} = ();
636    
# Line 177  sub parse_string ($$$;$) { Line 641  sub parse_string ($$$;$) {
641        if defined $self->{input_encoding};        if defined $self->{input_encoding};
642    
643    my $i = 0;    my $i = 0;
644    my $line = 1;    $self->{line_prev} = $self->{line} = 1;
645    my $column = 0;    $self->{column_prev} = $self->{column} = 0;
646    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
647      my $self = shift;      my $self = shift;
648    
649      pop @{$self->{prev_char}};      pop @{$self->{prev_char}};
650      unshift @{$self->{prev_char}}, $self->{next_char};      unshift @{$self->{prev_char}}, $self->{next_char};
651    
652      $self->{next_char} = -1 and return if $i >= length $$s;      my $char;
653      $self->{next_char} = ord substr $$s, $i++, 1;      if (defined $self->{next_next_char}) {
654      $column++;        $char = $self->{next_next_char};
655          delete $self->{next_next_char};
656        } else {
657          $char = $input->getc;
658        }
659        $self->{next_char} = -1 and return unless defined $char;
660        $self->{next_char} = ord $char;
661    
662        ($self->{line_prev}, $self->{column_prev})
663            = ($self->{line}, $self->{column});
664        $self->{column}++;
665            
666      if ($self->{next_char} == 0x000A) { # LF      if ($self->{next_char} == 0x000A) { # LF
667        $line++;        !!!cp ('j1');
668        $column = 0;        $self->{line}++;
669          $self->{column} = 0;
670      } elsif ($self->{next_char} == 0x000D) { # CR      } elsif ($self->{next_char} == 0x000D) { # CR
671        $i++ if substr ($$s, $i, 1) eq "\x0A";        !!!cp ('j2');
672    ## TODO: support for abort/streaming
673          my $next = $input->getc;
674          if (defined $next and $next ne "\x0A") {
675            $self->{next_next_char} = $next;
676          }
677        $self->{next_char} = 0x000A; # LF # MUST        $self->{next_char} = 0x000A; # LF # MUST
678        $line++;        $self->{line}++;
679        $column = 0;        $self->{column} = 0;
680      } elsif ($self->{next_char} > 0x10FFFF) {      } elsif ($self->{next_char} > 0x10FFFF) {
681          !!!cp ('j3');
682        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
683      } elsif ($self->{next_char} == 0x0000) { # NULL      } elsif ($self->{next_char} == 0x0000) { # NULL
684          !!!cp ('j4');
685        !!!parse-error (type => 'NULL');        !!!parse-error (type => 'NULL');
686        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST        $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
687        } elsif ($self->{next_char} <= 0x0008 or
688                 (0x000E <= $self->{next_char} and $self->{next_char} <= 0x001F) or
689                 (0x007F <= $self->{next_char} and $self->{next_char} <= 0x009F) or
690                 (0xD800 <= $self->{next_char} and $self->{next_char} <= 0xDFFF) or
691                 (0xFDD0 <= $self->{next_char} and $self->{next_char} <= 0xFDDF) or
692                 {
693                  0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
694                  0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
695                  0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
696                  0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
697                  0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
698                  0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
699                  0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
700                  0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
701                  0x10FFFE => 1, 0x10FFFF => 1,
702                 }->{$self->{next_char}}) {
703          !!!cp ('j5');
704          if ($self->{next_char} < 0x10000) {
705            !!!parse-error (type => 'control char',
706                            text => (sprintf 'U+%04X', $self->{next_char}));
707          } else {
708            !!!parse-error (type => 'control char',
709                            text => (sprintf 'U-%08X', $self->{next_char}));
710          }
711      }      }
712    };    };
713    $self->{prev_char} = [-1, -1, -1];    $self->{prev_char} = [-1, -1, -1];
# Line 209  sub parse_string ($$$;$) { Line 715  sub parse_string ($$$;$) {
715    
716    my $onerror = $_[2] || sub {    my $onerror = $_[2] || sub {
717      my (%opt) = @_;      my (%opt) = @_;
718      warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";      my $line = $opt{token} ? $opt{token}->{line} : $opt{line};
719        my $column = $opt{token} ? $opt{token}->{column} : $opt{column};
720        warn "Parse error ($opt{type}) at line $line column $column\n";
721    };    };
722    $self->{parse_error} = sub {    $self->{parse_error} = sub {
723      $onerror->(@_, line => $line, column => $column);      $onerror->(line => $self->{line}, column => $self->{column}, @_);
724    };    };
725    
726    $self->_initialize_tokenizer;    $self->_initialize_tokenizer;
# Line 220  sub parse_string ($$$;$) { Line 728  sub parse_string ($$$;$) {
728    $self->_construct_tree;    $self->_construct_tree;
729    $self->_terminate_tree_constructor;    $self->_terminate_tree_constructor;
730    
731      delete $self->{parse_error}; # remove loop
732    
733    return $self->{document};    return $self->{document};
734  } # parse_string  } # parse_char_stream
735    
736  sub new ($) {  sub new ($) {
737    my $class = shift;    my $class = shift;
738    my $self = bless {}, $class;    my $self = bless {
739        level => {must => 'm',
740                  should => 's',
741                  warn => 'w',
742                  info => 'i',
743                  uncertain => 'u'},
744      }, $class;
745    $self->{set_next_char} = sub {    $self->{set_next_char} = sub {
746      $self->{next_char} = -1;      $self->{next_char} = -1;
747    };    };
# Line 254  sub RCDATA_CONTENT_MODEL () { CM_ENTITY Line 770  sub RCDATA_CONTENT_MODEL () { CM_ENTITY
770  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }  sub PCDATA_CONTENT_MODEL () { CM_ENTITY | CM_FULL_MARKUP }
771    
772  sub DATA_STATE () { 0 }  sub DATA_STATE () { 0 }
773  sub ENTITY_DATA_STATE () { 1 }  #sub ENTITY_DATA_STATE () { 1 }
774  sub TAG_OPEN_STATE () { 2 }  sub TAG_OPEN_STATE () { 2 }
775  sub CLOSE_TAG_OPEN_STATE () { 3 }  sub CLOSE_TAG_OPEN_STATE () { 3 }
776  sub TAG_NAME_STATE () { 4 }  sub TAG_NAME_STATE () { 4 }
# Line 265  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8 Line 781  sub BEFORE_ATTRIBUTE_VALUE_STATE () { 8
781  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }  sub ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE () { 9 }
782  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }  sub ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE () { 10 }
783  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }  sub ATTRIBUTE_VALUE_UNQUOTED_STATE () { 11 }
784  sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }  #sub ENTITY_IN_ATTRIBUTE_VALUE_STATE () { 12 }
785  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }  sub MARKUP_DECLARATION_OPEN_STATE () { 13 }
786  sub COMMENT_START_STATE () { 14 }  sub COMMENT_START_STATE () { 14 }
787  sub COMMENT_START_DASH_STATE () { 15 }  sub COMMENT_START_DASH_STATE () { 15 }
# Line 287  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO Line 803  sub DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUO
803  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }  sub AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE () { 31 }
804  sub BOGUS_DOCTYPE_STATE () { 32 }  sub BOGUS_DOCTYPE_STATE () { 32 }
805  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }  sub AFTER_ATTRIBUTE_VALUE_QUOTED_STATE () { 33 }
806    sub SELF_CLOSING_START_TAG_STATE () { 34 }
807    sub CDATA_SECTION_STATE () { 35 }
808    sub MD_HYPHEN_STATE () { 36 } # "markup declaration open state" in the spec
809    sub MD_DOCTYPE_STATE () { 37 } # "markup declaration open state" in the spec
810    sub MD_CDATA_STATE () { 38 } # "markup declaration open state" in the spec
811    sub CDATA_PCDATA_CLOSE_TAG_STATE () { 39 } # "close tag open state" in the spec
812    sub CDATA_SECTION_MSE1_STATE () { 40 } # "CDATA section state" in the spec
813    sub CDATA_SECTION_MSE2_STATE () { 41 } # "CDATA section state" in the spec
814    sub PUBLIC_STATE () { 42 } # "after DOCTYPE name state" in the spec
815    sub SYSTEM_STATE () { 43 } # "after DOCTYPE name state" in the spec
816    ## NOTE: "Entity data state", "entity in attribute value state", and
817    ## "consume a character reference" algorithm are jointly implemented
818    ## using the following six states:
819    sub ENTITY_STATE () { 44 }
820    sub ENTITY_HASH_STATE () { 45 }
821    sub NCR_NUM_STATE () { 46 }
822    sub HEXREF_X_STATE () { 47 }
823    sub HEXREF_HEX_STATE () { 48 }
824    sub ENTITY_NAME_STATE () { 49 }
825    
826  sub DOCTYPE_TOKEN () { 1 }  sub DOCTYPE_TOKEN () { 1 }
827  sub COMMENT_TOKEN () { 2 }  sub COMMENT_TOKEN () { 2 }
# Line 304  sub ROW_IMS ()        { 0b10000000 } Line 839  sub ROW_IMS ()        { 0b10000000 }
839  sub BODY_AFTER_IMS () { 0b100000000 }  sub BODY_AFTER_IMS () { 0b100000000 }
840  sub FRAME_IMS ()      { 0b1000000000 }  sub FRAME_IMS ()      { 0b1000000000 }
841  sub SELECT_IMS ()     { 0b10000000000 }  sub SELECT_IMS ()     { 0b10000000000 }
842    sub IN_FOREIGN_CONTENT_IM () { 0b100000000000 }
843        ## NOTE: "in foreign content" insertion mode is special; it is combined
844        ## with the secondary insertion mode.  In this parser, they are stored
845        ## together in the bit-or'ed form.
846    
847  ## NOTE: "initial" and "before html" insertion modes have no constants.  ## NOTE: "initial" and "before html" insertion modes have no constants.
848    
# Line 335  sub IN_COLUMN_GROUP_IM () { 0b10 } Line 874  sub IN_COLUMN_GROUP_IM () { 0b10 }
874  sub _initialize_tokenizer ($) {  sub _initialize_tokenizer ($) {
875    my $self = shift;    my $self = shift;
876    $self->{state} = DATA_STATE; # MUST    $self->{state} = DATA_STATE; # MUST
877      #$self->{state_keyword}; # initialized when used
878      #$self->{entity__value}; # initialized when used
879      #$self->{entity__match}; # initialized when used
880    $self->{content_model} = PCDATA_CONTENT_MODEL; # be    $self->{content_model} = PCDATA_CONTENT_MODEL; # be
881    undef $self->{current_token}; # start tag, end tag, comment, or DOCTYPE    undef $self->{current_token};
882    undef $self->{current_attribute};    undef $self->{current_attribute};
883    undef $self->{last_emitted_start_tag_name};    undef $self->{last_emitted_start_tag_name};
884    undef $self->{last_attribute_value_state};    #$self->{prev_state}; # initialized when used
885    $self->{char} = [];    delete $self->{self_closing};
886    # $self->{next_char}    # $self->{next_char}
887    !!!next-input-character;    !!!next-input-character;
888    $self->{token} = [];    $self->{token} = [];
# Line 360  sub _initialize_tokenizer ($) { Line 902  sub _initialize_tokenizer ($) {
902  ##        ->{value}  ##        ->{value}
903  ##        ->{has_reference} == 1 or 0  ##        ->{has_reference} == 1 or 0
904  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)  ##   ->{data} (COMMENT_TOKEN, CHARACTER_TOKEN)
905    ## NOTE: The "self-closing flag" is hold as |$self->{self_closing}|.
906    ##     |->{self_closing}| is used to save the value of |$self->{self_closing}|
907    ##     while the token is pushed back to the stack.
908    
909  ## Emitted token MUST immediately be handled by the tree construction state.  ## Emitted token MUST immediately be handled by the tree construction state.
910    
# Line 369  sub _initialize_tokenizer ($) { Line 914  sub _initialize_tokenizer ($) {
914  ## has completed loading.  If one has, then it MUST be executed  ## has completed loading.  If one has, then it MUST be executed
915  ## and removed from the list.  ## and removed from the list.
916    
917  ## NOTE: HTML5 "Writing HTML documents" section, applied to  ## TODO: Polytheistic slash SHOULD NOT be used. (Applied only to atheists.)
918  ## 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,  
919    
920  sub _get_next_token ($) {  sub _get_next_token ($) {
921    my $self = shift;    my $self = shift;
922    
923      if ($self->{self_closing}) {
924        !!!parse-error (type => 'nestc', token => $self->{current_token});
925        ## NOTE: The |self_closing| flag is only set by start tag token.
926        ## In addition, when a start tag token is emitted, it is always set to
927        ## |current_token|.
928        delete $self->{self_closing};
929      }
930    
931    if (@{$self->{token}}) {    if (@{$self->{token}}) {
932        $self->{self_closing} = $self->{token}->[0]->{self_closing};
933      return shift @{$self->{token}};      return shift @{$self->{token}};
934    }    }
935    
# Line 396  sub _get_next_token ($) { Line 939  sub _get_next_token ($) {
939          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA          if ($self->{content_model} & CM_ENTITY and # PCDATA | RCDATA
940              not $self->{escape}) {              not $self->{escape}) {
941            !!!cp (1);            !!!cp (1);
942            $self->{state} = ENTITY_DATA_STATE;            ## NOTE: In the spec, the tokenizer is switched to the
943              ## "entity data state".  In this implementation, the tokenizer
944              ## is switched to the |ENTITY_STATE|, which is an implementation
945              ## of the "consume a character reference" algorithm.
946              $self->{entity_additional} = -1;
947              $self->{prev_state} = DATA_STATE;
948              $self->{state} = ENTITY_STATE;
949            !!!next-input-character;            !!!next-input-character;
950            redo A;            redo A;
951          } else {          } else {
# Line 449  sub _get_next_token ($) { Line 998  sub _get_next_token ($) {
998          #          #
999        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1000          !!!cp (11);          !!!cp (11);
1001          !!!emit ({type => END_OF_FILE_TOKEN});          !!!emit ({type => END_OF_FILE_TOKEN,
1002                      line => $self->{line}, column => $self->{column}});
1003          last A; ## TODO: ok?          last A; ## TODO: ok?
1004        } else {        } else {
1005          !!!cp (12);          !!!cp (12);
1006        }        }
1007        # Anything else        # Anything else
1008        my $token = {type => CHARACTER_TOKEN,        my $token = {type => CHARACTER_TOKEN,
1009                     data => chr $self->{next_char}};                     data => chr $self->{next_char},
1010                       line => $self->{line}, column => $self->{column},
1011                      };
1012        ## Stay in the data state        ## Stay in the data state
1013        !!!next-input-character;        !!!next-input-character;
1014    
1015        !!!emit ($token);        !!!emit ($token);
1016    
1017        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;  
1018      } elsif ($self->{state} == TAG_OPEN_STATE) {      } elsif ($self->{state} == TAG_OPEN_STATE) {
1019        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1020          if ($self->{next_char} == 0x002F) { # /          if ($self->{next_char} == 0x002F) { # /
# Line 492  sub _get_next_token ($) { Line 1027  sub _get_next_token ($) {
1027            ## reconsume            ## reconsume
1028            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1029    
1030            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1031                        line => $self->{line_prev},
1032                        column => $self->{column_prev},
1033                       });
1034    
1035            redo A;            redo A;
1036          }          }
# Line 512  sub _get_next_token ($) { Line 1050  sub _get_next_token ($) {
1050            !!!cp (19);            !!!cp (19);
1051            $self->{current_token}            $self->{current_token}
1052              = {type => START_TAG_TOKEN,              = {type => START_TAG_TOKEN,
1053                 tag_name => chr ($self->{next_char} + 0x0020)};                 tag_name => chr ($self->{next_char} + 0x0020),
1054                   line => $self->{line_prev},
1055                   column => $self->{column_prev}};
1056            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1057            !!!next-input-character;            !!!next-input-character;
1058            redo A;            redo A;
# Line 520  sub _get_next_token ($) { Line 1060  sub _get_next_token ($) {
1060                   $self->{next_char} <= 0x007A) { # a..z                   $self->{next_char} <= 0x007A) { # a..z
1061            !!!cp (20);            !!!cp (20);
1062            $self->{current_token} = {type => START_TAG_TOKEN,            $self->{current_token} = {type => START_TAG_TOKEN,
1063                              tag_name => chr ($self->{next_char})};                                      tag_name => chr ($self->{next_char}),
1064                                        line => $self->{line_prev},
1065                                        column => $self->{column_prev}};
1066            $self->{state} = TAG_NAME_STATE;            $self->{state} = TAG_NAME_STATE;
1067            !!!next-input-character;            !!!next-input-character;
1068            redo A;            redo A;
1069          } elsif ($self->{next_char} == 0x003E) { # >          } elsif ($self->{next_char} == 0x003E) { # >
1070            !!!cp (21);            !!!cp (21);
1071            !!!parse-error (type => 'empty start tag');            !!!parse-error (type => 'empty start tag',
1072                              line => $self->{line_prev},
1073                              column => $self->{column_prev});
1074            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1075            !!!next-input-character;            !!!next-input-character;
1076    
1077            !!!emit ({type => CHARACTER_TOKEN, data => '<>'});            !!!emit ({type => CHARACTER_TOKEN, data => '<>',
1078                        line => $self->{line_prev},
1079                        column => $self->{column_prev},
1080                       });
1081    
1082            redo A;            redo A;
1083          } elsif ($self->{next_char} == 0x003F) { # ?          } elsif ($self->{next_char} == 0x003F) { # ?
1084            !!!cp (22);            !!!cp (22);
1085            !!!parse-error (type => 'pio');            !!!parse-error (type => 'pio',
1086                              line => $self->{line_prev},
1087                              column => $self->{column_prev});
1088            $self->{state} = BOGUS_COMMENT_STATE;            $self->{state} = BOGUS_COMMENT_STATE;
1089              $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1090                                        line => $self->{line_prev},
1091                                        column => $self->{column_prev},
1092                                       };
1093            ## $self->{next_char} is intentionally left as is            ## $self->{next_char} is intentionally left as is
1094            redo A;            redo A;
1095          } else {          } else {
1096            !!!cp (23);            !!!cp (23);
1097            !!!parse-error (type => 'bare stago');            !!!parse-error (type => 'bare stago',
1098                              line => $self->{line_prev},
1099                              column => $self->{column_prev});
1100            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1101            ## reconsume            ## reconsume
1102    
1103            !!!emit ({type => CHARACTER_TOKEN, data => '<'});            !!!emit ({type => CHARACTER_TOKEN, data => '<',
1104                        line => $self->{line_prev},
1105                        column => $self->{column_prev},
1106                       });
1107    
1108            redo A;            redo A;
1109          }          }
# Line 553  sub _get_next_token ($) { Line 1111  sub _get_next_token ($) {
1111          die "$0: $self->{content_model} in tag open";          die "$0: $self->{content_model} in tag open";
1112        }        }
1113      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {      } elsif ($self->{state} == CLOSE_TAG_OPEN_STATE) {
1114          ## NOTE: The "close tag open state" in the spec is implemented as
1115          ## |CLOSE_TAG_OPEN_STATE| and |CDATA_PCDATA_CLOSE_TAG_STATE|.
1116    
1117          my ($l, $c) = ($self->{line_prev}, $self->{column_prev} - 1); # "<"of"</"
1118        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA        if ($self->{content_model} & CM_LIMITED_MARKUP) { # RCDATA | CDATA
1119          if (defined $self->{last_emitted_start_tag_name}) {          if (defined $self->{last_emitted_start_tag_name}) {
1120            ## NOTE: <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>            $self->{state} = CDATA_PCDATA_CLOSE_TAG_STATE;
1121            my @next_char;            $self->{state_keyword} = '';
1122            TAGNAME: for (my $i = 0; $i < length $self->{last_emitted_start_tag_name}; $i++) {            ## Reconsume.
1123              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...  
           }  
1124          } else {          } else {
1125            ## No start tag token has ever been emitted            ## No start tag token has ever been emitted
1126              ## NOTE: See <http://krijnhoetmer.nl/irc-logs/whatwg/20070626#l-564>.
1127            !!!cp (28);            !!!cp (28);
           # next-input-character is already done  
1128            $self->{state} = DATA_STATE;            $self->{state} = DATA_STATE;
1129            !!!emit ({type => CHARACTER_TOKEN, data => '</'});            ## Reconsume.
1130              !!!emit ({type => CHARACTER_TOKEN, data => '</',
1131                        line => $l, column => $c,
1132                       });
1133            redo A;            redo A;
1134          }          }
1135        }        }
1136          
1137        if (0x0041 <= $self->{next_char} and        if (0x0041 <= $self->{next_char} and
1138            $self->{next_char} <= 0x005A) { # A..Z            $self->{next_char} <= 0x005A) { # A..Z
1139          !!!cp (29);          !!!cp (29);
1140          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token}
1141                            tag_name => chr ($self->{next_char} + 0x0020)};              = {type => END_TAG_TOKEN,
1142                   tag_name => chr ($self->{next_char} + 0x0020),
1143                   line => $l, column => $c};
1144          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1145          !!!next-input-character;          !!!next-input-character;
1146          redo A;          redo A;
# Line 620  sub _get_next_token ($) { Line 1148  sub _get_next_token ($) {
1148                 $self->{next_char} <= 0x007A) { # a..z                 $self->{next_char} <= 0x007A) { # a..z
1149          !!!cp (30);          !!!cp (30);
1150          $self->{current_token} = {type => END_TAG_TOKEN,          $self->{current_token} = {type => END_TAG_TOKEN,
1151                            tag_name => chr ($self->{next_char})};                                    tag_name => chr ($self->{next_char}),
1152                                      line => $l, column => $c};
1153          $self->{state} = TAG_NAME_STATE;          $self->{state} = TAG_NAME_STATE;
1154          !!!next-input-character;          !!!next-input-character;
1155          redo A;          redo A;
1156        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1157          !!!cp (31);          !!!cp (31);
1158          !!!parse-error (type => 'empty end tag');          !!!parse-error (type => 'empty end tag',
1159                            line => $self->{line_prev}, ## "<" in "</>"
1160                            column => $self->{column_prev} - 1);
1161          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1162          !!!next-input-character;          !!!next-input-character;
1163          redo A;          redo A;
# Line 636  sub _get_next_token ($) { Line 1167  sub _get_next_token ($) {
1167          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
1168          # reconsume          # reconsume
1169    
1170          !!!emit ({type => CHARACTER_TOKEN, data => '</'});          !!!emit ({type => CHARACTER_TOKEN, data => '</',
1171                      line => $l, column => $c,
1172                     });
1173    
1174          redo A;          redo A;
1175        } else {        } else {
1176          !!!cp (33);          !!!cp (33);
1177          !!!parse-error (type => 'bogus end tag');          !!!parse-error (type => 'bogus end tag');
1178          $self->{state} = BOGUS_COMMENT_STATE;          $self->{state} = BOGUS_COMMENT_STATE;
1179          ## $self->{next_char} is intentionally left as is          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
1180          redo A;                                    line => $self->{line_prev}, # "<" of "</"
1181                                      column => $self->{column_prev} - 1,
1182                                     };
1183            ## NOTE: $self->{next_char} is intentionally left as is.
1184            ## Although the "anything else" case of the spec not explicitly
1185            ## states that the next input character is to be reconsumed,
1186            ## it will be included to the |data| of the comment token
1187            ## generated from the bogus end tag, as defined in the
1188            ## "bogus comment state" entry.
1189            redo A;
1190          }
1191        } elsif ($self->{state} == CDATA_PCDATA_CLOSE_TAG_STATE) {
1192          my $ch = substr $self->{last_emitted_start_tag_name}, length $self->{state_keyword}, 1;
1193          if (length $ch) {
1194            my $CH = $ch;
1195            $ch =~ tr/a-z/A-Z/;
1196            my $nch = chr $self->{next_char};
1197            if ($nch eq $ch or $nch eq $CH) {
1198              !!!cp (24);
1199              ## Stay in the state.
1200              $self->{state_keyword} .= $nch;
1201              !!!next-input-character;
1202              redo A;
1203            } else {
1204              !!!cp (25);
1205              $self->{state} = DATA_STATE;
1206              ## Reconsume.
1207              !!!emit ({type => CHARACTER_TOKEN,
1208                        data => '</' . $self->{state_keyword},
1209                        line => $self->{line_prev},
1210                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1211                       });
1212              redo A;
1213            }
1214          } else { # after "<{tag-name}"
1215            unless ({
1216                     0x0009 => 1, # HT
1217                     0x000A => 1, # LF
1218                     0x000B => 1, # VT
1219                     0x000C => 1, # FF
1220                     0x0020 => 1, # SP
1221                     0x003E => 1, # >
1222                     0x002F => 1, # /
1223                     -1 => 1, # EOF
1224                    }->{$self->{next_char}}) {
1225              !!!cp (26);
1226              ## Reconsume.
1227              $self->{state} = DATA_STATE;
1228              !!!emit ({type => CHARACTER_TOKEN,
1229                        data => '</' . $self->{state_keyword},
1230                        line => $self->{line_prev},
1231                        column => $self->{column_prev} - 1 - length $self->{state_keyword},
1232                       });
1233              redo A;
1234            } else {
1235              !!!cp (27);
1236              $self->{current_token}
1237                  = {type => END_TAG_TOKEN,
1238                     tag_name => $self->{last_emitted_start_tag_name},
1239                     line => $self->{line_prev},
1240                     column => $self->{column_prev} - 1 - length $self->{state_keyword}};
1241              $self->{state} = TAG_NAME_STATE;
1242              ## Reconsume.
1243              redo A;
1244            }
1245        }        }
1246      } elsif ($self->{state} == TAG_NAME_STATE) {      } elsif ($self->{state} == TAG_NAME_STATE) {
1247        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
# Line 659  sub _get_next_token ($) { Line 1256  sub _get_next_token ($) {
1256        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1257          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1258            !!!cp (35);            !!!cp (35);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1259            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1260          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1261            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 692  sub _get_next_token ($) { Line 1287  sub _get_next_token ($) {
1287          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1288          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1289            !!!cp (39);            !!!cp (39);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1290            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1291          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1292            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 714  sub _get_next_token ($) { Line 1307  sub _get_next_token ($) {
1307    
1308          redo A;          redo A;
1309        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1310            !!!cp (42);
1311            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1312          !!!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  
1313          redo A;          redo A;
1314        } else {        } else {
1315          !!!cp (44);          !!!cp (44);
# Line 749  sub _get_next_token ($) { Line 1332  sub _get_next_token ($) {
1332        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1333          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1334            !!!cp (46);            !!!cp (46);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1335            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1336          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1337            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 772  sub _get_next_token ($) { Line 1353  sub _get_next_token ($) {
1353        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1354                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1355          !!!cp (49);          !!!cp (49);
1356          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1357                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1358                   value => '',
1359                   line => $self->{line}, column => $self->{column}};
1360          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1361          !!!next-input-character;          !!!next-input-character;
1362          redo A;          redo A;
1363        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1364            !!!cp (50);
1365            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1366          !!!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  
1367          redo A;          redo A;
1368        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1369          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1370          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1371            !!!cp (52);            !!!cp (52);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1372            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1373          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1374            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 827  sub _get_next_token ($) { Line 1398  sub _get_next_token ($) {
1398          } else {          } else {
1399            !!!cp (56);            !!!cp (56);
1400          }          }
1401          $self->{current_attribute} = {name => chr ($self->{next_char}),          $self->{current_attribute}
1402                                value => ''};              = {name => chr ($self->{next_char}),
1403                   value => '',
1404                   line => $self->{line}, column => $self->{column}};
1405          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1406          !!!next-input-character;          !!!next-input-character;
1407          redo A;          redo A;
# Line 838  sub _get_next_token ($) { Line 1411  sub _get_next_token ($) {
1411          if (exists $self->{current_token}->{attributes} # start tag or end tag          if (exists $self->{current_token}->{attributes} # start tag or end tag
1412              ->{$self->{current_attribute}->{name}}) { # MUST              ->{$self->{current_attribute}->{name}}) { # MUST
1413            !!!cp (57);            !!!cp (57);
1414            !!!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});
1415            ## Discard $self->{current_attribute} # MUST            ## Discard $self->{current_attribute} # MUST
1416          } else {          } else {
1417            !!!cp (58);            !!!cp (58);
# Line 867  sub _get_next_token ($) { Line 1440  sub _get_next_token ($) {
1440          $before_leave->();          $before_leave->();
1441          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1442            !!!cp (61);            !!!cp (61);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1443            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1444          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1445            !!!cp (62);            !!!cp (62);
# Line 893  sub _get_next_token ($) { Line 1464  sub _get_next_token ($) {
1464          !!!next-input-character;          !!!next-input-character;
1465          redo A;          redo A;
1466        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1467            !!!cp (64);
1468          $before_leave->();          $before_leave->();
1469            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1470          !!!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  
1471          redo A;          redo A;
1472        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1473          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1474          $before_leave->();          $before_leave->();
1475          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1476            !!!cp (66);            !!!cp (66);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1477            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1478          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1479            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 965  sub _get_next_token ($) { Line 1524  sub _get_next_token ($) {
1524        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1525          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1526            !!!cp (73);            !!!cp (73);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1527            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1528          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1529            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 989  sub _get_next_token ($) { Line 1546  sub _get_next_token ($) {
1546        } elsif (0x0041 <= $self->{next_char} and        } elsif (0x0041 <= $self->{next_char} and
1547                 $self->{next_char} <= 0x005A) { # A..Z                 $self->{next_char} <= 0x005A) { # A..Z
1548          !!!cp (76);          !!!cp (76);
1549          $self->{current_attribute} = {name => chr ($self->{next_char} + 0x0020),          $self->{current_attribute}
1550                                value => ''};              = {name => chr ($self->{next_char} + 0x0020),
1551                   value => '',
1552                   line => $self->{line}, column => $self->{column}};
1553          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1554          !!!next-input-character;          !!!next-input-character;
1555          redo A;          redo A;
1556        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1557            !!!cp (77);
1558            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1559          !!!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  
1560          redo A;          redo A;
1561        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1562          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1563          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1564            !!!cp (79);            !!!cp (79);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1565            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1566          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1567            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1036  sub _get_next_token ($) { Line 1582  sub _get_next_token ($) {
1582    
1583          redo A;          redo A;
1584        } else {        } else {
1585          !!!cp (82);          if ($self->{next_char} == 0x0022 or # "
1586          $self->{current_attribute} = {name => chr ($self->{next_char}),              $self->{next_char} == 0x0027) { # '
1587                                value => ''};            !!!cp (78);
1588              !!!parse-error (type => 'bad attribute name');
1589            } else {
1590              !!!cp (82);
1591            }
1592            $self->{current_attribute}
1593                = {name => chr ($self->{next_char}),
1594                   value => '',
1595                   line => $self->{line}, column => $self->{column}};
1596          $self->{state} = ATTRIBUTE_NAME_STATE;          $self->{state} = ATTRIBUTE_NAME_STATE;
1597          !!!next-input-character;          !!!next-input-character;
1598          redo A;                  redo A;        
# Line 1069  sub _get_next_token ($) { Line 1623  sub _get_next_token ($) {
1623          !!!next-input-character;          !!!next-input-character;
1624          redo A;          redo A;
1625        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1626            !!!parse-error (type => 'empty unquoted attribute value');
1627          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1628            !!!cp (87);            !!!cp (87);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1629            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1630          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1631            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1096  sub _get_next_token ($) { Line 1649  sub _get_next_token ($) {
1649          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1650          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1651            !!!cp (90);            !!!cp (90);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1652            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1653          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1654            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1137  sub _get_next_token ($) { Line 1688  sub _get_next_token ($) {
1688          redo A;          redo A;
1689        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1690          !!!cp (96);          !!!cp (96);
1691          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1692          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1693            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1694            ## implementation of the "consume a character reference" algorithm.
1695            $self->{prev_state} = $self->{state};
1696            $self->{entity_additional} = 0x0022; # "
1697            $self->{state} = ENTITY_STATE;
1698          !!!next-input-character;          !!!next-input-character;
1699          redo A;          redo A;
1700        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1701          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1702          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1703            !!!cp (97);            !!!cp (97);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1704            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1705          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1706            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1181  sub _get_next_token ($) { Line 1735  sub _get_next_token ($) {
1735          redo A;          redo A;
1736        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1737          !!!cp (102);          !!!cp (102);
1738          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1739          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1740            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1741            ## implementation of the "consume a character reference" algorithm.
1742            $self->{entity_additional} = 0x0027; # '
1743            $self->{prev_state} = $self->{state};
1744            $self->{state} = ENTITY_STATE;
1745          !!!next-input-character;          !!!next-input-character;
1746          redo A;          redo A;
1747        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
1748          !!!parse-error (type => 'unclosed attribute value');          !!!parse-error (type => 'unclosed attribute value');
1749          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1750            !!!cp (103);            !!!cp (103);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1751            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1752          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1753            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1229  sub _get_next_token ($) { Line 1786  sub _get_next_token ($) {
1786          redo A;          redo A;
1787        } elsif ($self->{next_char} == 0x0026) { # &        } elsif ($self->{next_char} == 0x0026) { # &
1788          !!!cp (108);          !!!cp (108);
1789          $self->{last_attribute_value_state} = $self->{state};          ## NOTE: In the spec, the tokenizer is switched to the
1790          $self->{state} = ENTITY_IN_ATTRIBUTE_VALUE_STATE;          ## "entity in attribute value state".  In this implementation, the
1791            ## tokenizer is switched to the |ENTITY_STATE|, which is an
1792            ## implementation of the "consume a character reference" algorithm.
1793            $self->{entity_additional} = -1;
1794            $self->{prev_state} = $self->{state};
1795            $self->{state} = ENTITY_STATE;
1796          !!!next-input-character;          !!!next-input-character;
1797          redo A;          redo A;
1798        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1799          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1800            !!!cp (109);            !!!cp (109);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1801            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1802          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1803            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1261  sub _get_next_token ($) { Line 1821  sub _get_next_token ($) {
1821          !!!parse-error (type => 'unclosed tag');          !!!parse-error (type => 'unclosed tag');
1822          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1823            !!!cp (112);            !!!cp (112);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1824            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1825          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1826            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1298  sub _get_next_token ($) { Line 1856  sub _get_next_token ($) {
1856          !!!next-input-character;          !!!next-input-character;
1857          redo A;          redo A;
1858        }        }
     } 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;  
1859      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {      } elsif ($self->{state} == AFTER_ATTRIBUTE_VALUE_QUOTED_STATE) {
1860        if ($self->{next_char} == 0x0009 or # HT        if ($self->{next_char} == 0x0009 or # HT
1861            $self->{next_char} == 0x000A or # LF            $self->{next_char} == 0x000A or # LF
# Line 1333  sub _get_next_token ($) { Line 1869  sub _get_next_token ($) {
1869        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
1870          if ($self->{current_token}->{type} == START_TAG_TOKEN) {          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1871            !!!cp (119);            !!!cp (119);
           $self->{current_token}->{first_start_tag}  
               = not defined $self->{last_emitted_start_tag_name};  
1872            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1873          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {          } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1874            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
# Line 1355  sub _get_next_token ($) { Line 1889  sub _get_next_token ($) {
1889    
1890          redo A;          redo A;
1891        } elsif ($self->{next_char} == 0x002F) { # /        } elsif ($self->{next_char} == 0x002F) { # /
1892            !!!cp (122);
1893            $self->{state} = SELF_CLOSING_START_TAG_STATE;
1894          !!!next-input-character;          !!!next-input-character;
1895          if ($self->{next_char} == 0x003E and # >          redo A;
1896              $self->{current_token}->{type} == START_TAG_TOKEN and        } elsif ($self->{next_char} == -1) {
1897              $permitted_slash_tag_name->{$self->{current_token}->{tag_name}}) {          !!!parse-error (type => 'unclosed tag');
1898            # permitted slash          if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1899            !!!cp (122);            !!!cp (122.3);
1900            #            $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1901            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1902              if ($self->{current_token}->{attributes}) {
1903                !!!cp (122.1);
1904                !!!parse-error (type => 'end tag attribute');
1905              } else {
1906                ## NOTE: This state should never be reached.
1907                !!!cp (122.2);
1908              }
1909          } else {          } else {
1910            !!!cp (123);            die "$0: $self->{current_token}->{type}: Unknown token type";
           !!!parse-error (type => 'nestc');  
1911          }          }
1912          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = DATA_STATE;
1913          # next-input-character is already done          ## Reconsume.
1914            !!!emit ($self->{current_token}); # start tag or end tag
1915          redo A;          redo A;
1916        } else {        } else {
1917          !!!cp (124);          !!!cp ('124.1');
1918          !!!parse-error (type => 'no space between attributes');          !!!parse-error (type => 'no space between attributes');
1919          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;          $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1920          ## reconsume          ## reconsume
1921          redo A;          redo A;
1922        }        }
1923      } elsif ($self->{state} == BOGUS_COMMENT_STATE) {      } elsif ($self->{state} == SELF_CLOSING_START_TAG_STATE) {
1924        ## (only happen if PCDATA state)        if ($self->{next_char} == 0x003E) { # >
1925                  if ($self->{current_token}->{type} == END_TAG_TOKEN) {
1926        my $token = {type => COMMENT_TOKEN, data => ''};            !!!cp ('124.2');
1927              !!!parse-error (type => 'nestc', token => $self->{current_token});
1928        BC: {            ## TODO: Different type than slash in start tag
1929          if ($self->{next_char} == 0x003E) { # >            $self->{content_model} = PCDATA_CONTENT_MODEL; # MUST
1930            !!!cp (124);            if ($self->{current_token}->{attributes}) {
1931            $self->{state} = DATA_STATE;              !!!cp ('124.4');
1932            !!!next-input-character;              !!!parse-error (type => 'end tag attribute');
1933              } else {
1934            !!!emit ($token);              !!!cp ('124.5');
1935              }
1936              ## TODO: Test |<title></title/>|
1937            } else {
1938              !!!cp ('124.3');
1939              $self->{self_closing} = 1;
1940            }
1941    
1942            redo A;          $self->{state} = DATA_STATE;
1943          } elsif ($self->{next_char} == -1) {          !!!next-input-character;
           !!!cp (125);  
           $self->{state} = DATA_STATE;  
           ## reconsume  
1944    
1945            !!!emit ($token);          !!!emit ($self->{current_token}); # start tag or end tag
1946    
1947            redo A;          redo A;
1948          } elsif ($self->{next_char} == -1) {
1949            !!!parse-error (type => 'unclosed tag');
1950            if ($self->{current_token}->{type} == START_TAG_TOKEN) {
1951              !!!cp (124.7);
1952              $self->{last_emitted_start_tag_name} = $self->{current_token}->{tag_name};
1953            } elsif ($self->{current_token}->{type} == END_TAG_TOKEN) {
1954              if ($self->{current_token}->{attributes}) {
1955                !!!cp (124.5);
1956                !!!parse-error (type => 'end tag attribute');
1957              } else {
1958                ## NOTE: This state should never be reached.
1959                !!!cp (124.6);
1960              }
1961          } else {          } else {
1962            !!!cp (126);            die "$0: $self->{current_token}->{type}: Unknown token type";
           $token->{data} .= chr ($self->{next_char});  
           !!!next-input-character;  
           redo BC;  
1963          }          }
1964        } # BC          $self->{state} = DATA_STATE;
1965            ## Reconsume.
1966            !!!emit ($self->{current_token}); # start tag or end tag
1967            redo A;
1968          } else {
1969            !!!cp ('124.4');
1970            !!!parse-error (type => 'nestc');
1971            ## TODO: This error type is wrong.
1972            $self->{state} = BEFORE_ATTRIBUTE_NAME_STATE;
1973            ## Reconsume.
1974            redo A;
1975          }
1976        } elsif ($self->{state} == BOGUS_COMMENT_STATE) {
1977          ## (only happen if PCDATA state)
1978    
1979        die "$0: _get_next_token: unexpected case [BC]";        ## NOTE: Unlike spec's "bogus comment state", this implementation
1980          ## consumes characters one-by-one basis.
1981          
1982          if ($self->{next_char} == 0x003E) { # >
1983            !!!cp (124);
1984            $self->{state} = DATA_STATE;
1985            !!!next-input-character;
1986    
1987            !!!emit ($self->{current_token}); # comment
1988            redo A;
1989          } elsif ($self->{next_char} == -1) {
1990            !!!cp (125);
1991            $self->{state} = DATA_STATE;
1992            ## reconsume
1993    
1994            !!!emit ($self->{current_token}); # comment
1995            redo A;
1996          } else {
1997            !!!cp (126);
1998            $self->{current_token}->{data} .= chr ($self->{next_char}); # comment
1999            ## Stay in the state.
2000            !!!next-input-character;
2001            redo A;
2002          }
2003      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {      } elsif ($self->{state} == MARKUP_DECLARATION_OPEN_STATE) {
2004        ## (only happen if PCDATA state)        ## (only happen if PCDATA state)
   
       my @next_char;  
       push @next_char, $self->{next_char};  
2005                
2006        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2007            !!!cp (133);
2008            $self->{state} = MD_HYPHEN_STATE;
2009          !!!next-input-character;          !!!next-input-character;
2010          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);  
         }  
2011        } elsif ($self->{next_char} == 0x0044 or # D        } elsif ($self->{next_char} == 0x0044 or # D
2012                 $self->{next_char} == 0x0064) { # d                 $self->{next_char} == 0x0064) { # d
2013            ## ASCII case-insensitive.
2014            !!!cp (130);
2015            $self->{state} = MD_DOCTYPE_STATE;
2016            $self->{state_keyword} = chr $self->{next_char};
2017          !!!next-input-character;          !!!next-input-character;
2018          push @next_char, $self->{next_char};          redo A;
2019          if ($self->{next_char} == 0x004F or # O        } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM and
2020              $self->{next_char} == 0x006F) { # o                 $self->{open_elements}->[-1]->[1] & FOREIGN_EL and
2021            !!!next-input-character;                 $self->{next_char} == 0x005B) { # [
2022            push @next_char, $self->{next_char};          !!!cp (135.4);                
2023            if ($self->{next_char} == 0x0043 or # C          $self->{state} = MD_CDATA_STATE;
2024                $self->{next_char} == 0x0063) { # c          $self->{state_keyword} = '[';
2025              !!!next-input-character;          !!!next-input-character;
2026              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);  
         }  
2027        } else {        } else {
2028          !!!cp (136);          !!!cp (136);
2029        }        }
2030    
2031        !!!parse-error (type => 'bogus comment');        !!!parse-error (type => 'bogus comment',
2032        $self->{next_char} = shift @next_char;                        line => $self->{line_prev},
2033        !!!back-next-input-character (@next_char);                        column => $self->{column_prev} - 1);
2034          ## Reconsume.
2035        $self->{state} = BOGUS_COMMENT_STATE;        $self->{state} = BOGUS_COMMENT_STATE;
2036          $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2037                                    line => $self->{line_prev},
2038                                    column => $self->{column_prev} - 1,
2039                                   };
2040        redo A;        redo A;
2041              } elsif ($self->{state} == MD_HYPHEN_STATE) {
2042        ## ISSUE: typos in spec: chacacters, is is a parse error        if ($self->{next_char} == 0x002D) { # -
2043        ## 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);
2044            $self->{current_token} = {type => COMMENT_TOKEN, data => '',
2045                                      line => $self->{line_prev},
2046                                      column => $self->{column_prev} - 2,
2047                                     };
2048            $self->{state} = COMMENT_START_STATE;
2049            !!!next-input-character;
2050            redo A;
2051          } else {
2052            !!!cp (128);
2053            !!!parse-error (type => 'bogus comment',
2054                            line => $self->{line_prev},
2055                            column => $self->{column_prev} - 2);
2056            $self->{state} = BOGUS_COMMENT_STATE;
2057            ## Reconsume.
2058            $self->{current_token} = {type => COMMENT_TOKEN,
2059                                      data => '-',
2060                                      line => $self->{line_prev},
2061                                      column => $self->{column_prev} - 2,
2062                                     };
2063            redo A;
2064          }
2065        } elsif ($self->{state} == MD_DOCTYPE_STATE) {
2066          ## ASCII case-insensitive.
2067          if ($self->{next_char} == [
2068                undef,
2069                0x004F, # O
2070                0x0043, # C
2071                0x0054, # T
2072                0x0059, # Y
2073                0x0050, # P
2074              ]->[length $self->{state_keyword}] or
2075              $self->{next_char} == [
2076                undef,
2077                0x006F, # o
2078                0x0063, # c
2079                0x0074, # t
2080                0x0079, # y
2081                0x0070, # p
2082              ]->[length $self->{state_keyword}]) {
2083            !!!cp (131);
2084            ## Stay in the state.
2085            $self->{state_keyword} .= chr $self->{next_char};
2086            !!!next-input-character;
2087            redo A;
2088          } elsif ((length $self->{state_keyword}) == 6 and
2089                   ($self->{next_char} == 0x0045 or # E
2090                    $self->{next_char} == 0x0065)) { # e
2091            !!!cp (129);
2092            $self->{state} = DOCTYPE_STATE;
2093            $self->{current_token} = {type => DOCTYPE_TOKEN,
2094                                      quirks => 1,
2095                                      line => $self->{line_prev},
2096                                      column => $self->{column_prev} - 7,
2097                                     };
2098            !!!next-input-character;
2099            redo A;
2100          } else {
2101            !!!cp (132);        
2102            !!!parse-error (type => 'bogus comment',
2103                            line => $self->{line_prev},
2104                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2105            $self->{state} = BOGUS_COMMENT_STATE;
2106            ## Reconsume.
2107            $self->{current_token} = {type => COMMENT_TOKEN,
2108                                      data => $self->{state_keyword},
2109                                      line => $self->{line_prev},
2110                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2111                                     };
2112            redo A;
2113          }
2114        } elsif ($self->{state} == MD_CDATA_STATE) {
2115          if ($self->{next_char} == {
2116                '[' => 0x0043, # C
2117                '[C' => 0x0044, # D
2118                '[CD' => 0x0041, # A
2119                '[CDA' => 0x0054, # T
2120                '[CDAT' => 0x0041, # A
2121              }->{$self->{state_keyword}}) {
2122            !!!cp (135.1);
2123            ## Stay in the state.
2124            $self->{state_keyword} .= chr $self->{next_char};
2125            !!!next-input-character;
2126            redo A;
2127          } elsif ($self->{state_keyword} eq '[CDATA' and
2128                   $self->{next_char} == 0x005B) { # [
2129            !!!cp (135.2);
2130            $self->{current_token} = {type => CHARACTER_TOKEN,
2131                                      data => '',
2132                                      line => $self->{line_prev},
2133                                      column => $self->{column_prev} - 7};
2134            $self->{state} = CDATA_SECTION_STATE;
2135            !!!next-input-character;
2136            redo A;
2137          } else {
2138            !!!cp (135.3);
2139            !!!parse-error (type => 'bogus comment',
2140                            line => $self->{line_prev},
2141                            column => $self->{column_prev} - 1 - length $self->{state_keyword});
2142            $self->{state} = BOGUS_COMMENT_STATE;
2143            ## Reconsume.
2144            $self->{current_token} = {type => COMMENT_TOKEN,
2145                                      data => $self->{state_keyword},
2146                                      line => $self->{line_prev},
2147                                      column => $self->{column_prev} - 1 - length $self->{state_keyword},
2148                                     };
2149            redo A;
2150          }
2151      } elsif ($self->{state} == COMMENT_START_STATE) {      } elsif ($self->{state} == COMMENT_START_STATE) {
2152        if ($self->{next_char} == 0x002D) { # -        if ($self->{next_char} == 0x002D) { # -
2153          !!!cp (137);          !!!cp (137);
# Line 1605  sub _get_next_token ($) { Line 2267  sub _get_next_token ($) {
2267          redo A;          redo A;
2268        } elsif ($self->{next_char} == 0x002D) { # -        } elsif ($self->{next_char} == 0x002D) { # -
2269          !!!cp (152);          !!!cp (152);
2270          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2271                            line => $self->{line_prev},
2272                            column => $self->{column_prev});
2273          $self->{current_token}->{data} .= '-'; # comment          $self->{current_token}->{data} .= '-'; # comment
2274          ## Stay in the state          ## Stay in the state
2275          !!!next-input-character;          !!!next-input-character;
# Line 1621  sub _get_next_token ($) { Line 2285  sub _get_next_token ($) {
2285          redo A;          redo A;
2286        } else {        } else {
2287          !!!cp (154);          !!!cp (154);
2288          !!!parse-error (type => 'dash in comment');          !!!parse-error (type => 'dash in comment',
2289                            line => $self->{line_prev},
2290                            column => $self->{column_prev});
2291          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment          $self->{current_token}->{data} .= '--' . chr ($self->{next_char}); # comment
2292          $self->{state} = COMMENT_STATE;          $self->{state} = COMMENT_STATE;
2293          !!!next-input-character;          !!!next-input-character;
# Line 1660  sub _get_next_token ($) { Line 2326  sub _get_next_token ($) {
2326          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2327          !!!next-input-character;          !!!next-input-character;
2328    
2329          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2330    
2331          redo A;          redo A;
2332        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
# Line 1669  sub _get_next_token ($) { Line 2335  sub _get_next_token ($) {
2335          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2336          ## reconsume          ## reconsume
2337    
2338          !!!emit ({type => DOCTYPE_TOKEN, quirks => 1});          !!!emit ($self->{current_token}); # DOCTYPE (quirks)
2339    
2340          redo A;          redo A;
2341        } else {        } else {
2342          !!!cp (160);          !!!cp (160);
2343          $self->{current_token}          $self->{current_token}->{name} = chr $self->{next_char};
2344              = {type => DOCTYPE_TOKEN,          delete $self->{current_token}->{quirks};
                name => chr ($self->{next_char}),  
                #quirks => 0,  
               };  
2345  ## ISSUE: "Set the token's name name to the" in the spec  ## ISSUE: "Set the token's name name to the" in the spec
2346          $self->{state} = DOCTYPE_NAME_STATE;          $self->{state} = DOCTYPE_NAME_STATE;
2347          !!!next-input-character;          !!!next-input-character;
# Line 1751  sub _get_next_token ($) { Line 2414  sub _get_next_token ($) {
2414          redo A;          redo A;
2415        } elsif ($self->{next_char} == 0x0050 or # P        } elsif ($self->{next_char} == 0x0050 or # P
2416                 $self->{next_char} == 0x0070) { # p                 $self->{next_char} == 0x0070) { # p
2417            $self->{state} = PUBLIC_STATE;
2418            $self->{state_keyword} = chr $self->{next_char};
2419          !!!next-input-character;          !!!next-input-character;
2420          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);  
         }  
   
         #  
2421        } elsif ($self->{next_char} == 0x0053 or # S        } elsif ($self->{next_char} == 0x0053 or # S
2422                 $self->{next_char} == 0x0073) { # s                 $self->{next_char} == 0x0073) { # s
2423            $self->{state} = SYSTEM_STATE;
2424            $self->{state_keyword} = chr $self->{next_char};
2425          !!!next-input-character;          !!!next-input-character;
2426          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);  
         }  
   
         #  
2427        } else {        } else {
2428          !!!cp (180);          !!!cp (180);
2429            !!!parse-error (type => 'string after DOCTYPE name');
2430            $self->{current_token}->{quirks} = 1;
2431    
2432            $self->{state} = BOGUS_DOCTYPE_STATE;
2433          !!!next-input-character;          !!!next-input-character;
2434          #          redo A;
2435        }        }
2436        } elsif ($self->{state} == PUBLIC_STATE) {
2437          ## ASCII case-insensitive
2438          if ($self->{next_char} == [
2439                undef,
2440                0x0055, # U
2441                0x0042, # B
2442                0x004C, # L
2443                0x0049, # I
2444              ]->[length $self->{state_keyword}] or
2445              $self->{next_char} == [
2446                undef,
2447                0x0075, # u
2448                0x0062, # b
2449                0x006C, # l
2450                0x0069, # i
2451              ]->[length $self->{state_keyword}]) {
2452            !!!cp (175);
2453            ## Stay in the state.
2454            $self->{state_keyword} .= chr $self->{next_char};
2455            !!!next-input-character;
2456            redo A;
2457          } elsif ((length $self->{state_keyword}) == 5 and
2458                   ($self->{next_char} == 0x0043 or # C
2459                    $self->{next_char} == 0x0063)) { # c
2460            !!!cp (168);
2461            $self->{state} = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
2462            !!!next-input-character;
2463            redo A;
2464          } else {
2465            !!!cp (169);
2466            !!!parse-error (type => 'string after DOCTYPE name',
2467                            line => $self->{line_prev},
2468                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2469            $self->{current_token}->{quirks} = 1;
2470    
2471        !!!parse-error (type => 'string after DOCTYPE name');          $self->{state} = BOGUS_DOCTYPE_STATE;
2472        $self->{current_token}->{quirks} = 1;          ## Reconsume.
2473            redo A;
2474          }
2475        } elsif ($self->{state} == SYSTEM_STATE) {
2476          ## ASCII case-insensitive
2477          if ($self->{next_char} == [
2478                undef,
2479                0x0059, # Y
2480                0x0053, # S
2481                0x0054, # T
2482                0x0045, # E
2483              ]->[length $self->{state_keyword}] or
2484              $self->{next_char} == [
2485                undef,
2486                0x0079, # y
2487                0x0073, # s
2488                0x0074, # t
2489                0x0065, # e
2490              ]->[length $self->{state_keyword}]) {
2491            !!!cp (170);
2492            ## Stay in the state.
2493            $self->{state_keyword} .= chr $self->{next_char};
2494            !!!next-input-character;
2495            redo A;
2496          } elsif ((length $self->{state_keyword}) == 5 and
2497                   ($self->{next_char} == 0x004D or # M
2498                    $self->{next_char} == 0x006D)) { # m
2499            !!!cp (171);
2500            $self->{state} = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
2501            !!!next-input-character;
2502            redo A;
2503          } else {
2504            !!!cp (172);
2505            !!!parse-error (type => 'string after DOCTYPE name',
2506                            line => $self->{line_prev},
2507                            column => $self->{column_prev} + 1 - length $self->{state_keyword});
2508            $self->{current_token}->{quirks} = 1;
2509    
2510        $self->{state} = BOGUS_DOCTYPE_STATE;          $self->{state} = BOGUS_DOCTYPE_STATE;
2511        # next-input-character is already done          ## Reconsume.
2512        redo A;          redo A;
2513          }
2514      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {      } elsif ($self->{state} == BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE) {
2515        if ({        if ({
2516              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,              0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, 0x0020 => 1,
# Line 2069  sub _get_next_token ($) { Line 2743  sub _get_next_token ($) {
2743          redo A;          redo A;
2744        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2745          !!!cp (208);          !!!cp (208);
2746          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2747    
2748          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2749          !!!next-input-character;          !!!next-input-character;
# Line 2105  sub _get_next_token ($) { Line 2779  sub _get_next_token ($) {
2779          redo A;          redo A;
2780        } elsif ($self->{next_char} == 0x003E) { # >        } elsif ($self->{next_char} == 0x003E) { # >
2781          !!!cp (212);          !!!cp (212);
2782          !!!parse-error (type => 'unclosed PUBLIC literal');          !!!parse-error (type => 'unclosed SYSTEM literal');
2783    
2784          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2785          !!!next-input-character;          !!!next-input-character;
# Line 2153  sub _get_next_token ($) { Line 2827  sub _get_next_token ($) {
2827        } elsif ($self->{next_char} == -1) {        } elsif ($self->{next_char} == -1) {
2828          !!!cp (217);          !!!cp (217);
2829          !!!parse-error (type => 'unclosed DOCTYPE');          !!!parse-error (type => 'unclosed DOCTYPE');
   
2830          $self->{state} = DATA_STATE;          $self->{state} = DATA_STATE;
2831          ## reconsume          ## reconsume
2832    
# Line 2194  sub _get_next_token ($) { Line 2867  sub _get_next_token ($) {
2867          !!!next-input-character;          !!!next-input-character;
2868          redo A;          redo A;
2869        }        }
2870      } else {      } elsif ($self->{state} == CDATA_SECTION_STATE) {
2871        die "$0: $self->{state}: Unknown state";        ## NOTE: "CDATA section state" in the state is jointly implemented
2872      }        ## by three states, |CDATA_SECTION_STATE|, |CDATA_SECTION_MSE1_STATE|,
2873    } # A          ## and |CDATA_SECTION_MSE2_STATE|.
2874          
2875    die "$0: _get_next_token: unexpected case";        if ($self->{next_char} == 0x005D) { # ]
2876  } # _get_next_token          !!!cp (221.1);
2877            $self->{state} = CDATA_SECTION_MSE1_STATE;
2878  sub _tokenize_attempt_to_consume_an_entity ($$$) {          !!!next-input-character;
2879    my ($self, $in_attr, $additional) = @_;          redo A;
2880          } elsif ($self->{next_char} == -1) {
2881            $self->{state} = DATA_STATE;
2882            !!!next-input-character;
2883            if (length $self->{current_token}->{data}) { # character
2884              !!!cp (221.2);
2885              !!!emit ($self->{current_token}); # character
2886            } else {
2887              !!!cp (221.3);
2888              ## No token to emit. $self->{current_token} is discarded.
2889            }        
2890            redo A;
2891          } else {
2892            !!!cp (221.4);
2893            $self->{current_token}->{data} .= chr $self->{next_char};
2894            ## Stay in the state.
2895            !!!next-input-character;
2896            redo A;
2897          }
2898    
2899    if ({        ## ISSUE: "text tokens" in spec.
2900         0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,      } elsif ($self->{state} == CDATA_SECTION_MSE1_STATE) {
2901         0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, & # 0x000D # CR        if ($self->{next_char} == 0x005D) { # ]
2902         $additional => 1,          !!!cp (221.5);
2903        }->{$self->{next_char}}) {          $self->{state} = CDATA_SECTION_MSE2_STATE;
2904      !!!cp (1001);          !!!next-input-character;
2905      ## Don't consume          redo A;
2906      ## No error        } else {
2907      return undef;          !!!cp (221.6);
2908    } elsif ($self->{next_char} == 0x0023) { # #          $self->{current_token}->{data} .= ']';
2909      !!!next-input-character;          $self->{state} = CDATA_SECTION_STATE;
2910      if ($self->{next_char} == 0x0078 or # x          ## Reconsume.
2911          $self->{next_char} == 0x0058) { # X          redo A;
2912        my $code;        }
2913        X: {      } elsif ($self->{state} == CDATA_SECTION_MSE2_STATE) {
2914          my $x_char = $self->{next_char};        if ($self->{next_char} == 0x003E) { # >
2915          !!!next-input-character;          $self->{state} = DATA_STATE;
2916          if (0x0030 <= $self->{next_char} and          !!!next-input-character;
2917              $self->{next_char} <= 0x0039) { # 0..9          if (length $self->{current_token}->{data}) { # character
2918            !!!cp (1002);            !!!cp (221.7);
2919            $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;  
2920          } else {          } else {
2921            !!!cp (1007);            !!!cp (221.8);
2922            !!!parse-error (type => 'no refc');            ## No token to emit. $self->{current_token} is discarded.
2923          }          }
2924            redo A;
2925          } elsif ($self->{next_char} == 0x005D) { # ]
2926            !!!cp (221.9); # character
2927            $self->{current_token}->{data} .= ']'; ## Add first "]" of "]]]".
2928            ## Stay in the state.
2929            !!!next-input-character;
2930            redo A;
2931          } else {
2932            !!!cp (221.11);
2933            $self->{current_token}->{data} .= ']]'; # character
2934            $self->{state} = CDATA_SECTION_STATE;
2935            ## Reconsume.
2936            redo A;
2937          }
2938        } elsif ($self->{state} == ENTITY_STATE) {
2939          if ({
2940            0x0009 => 1, 0x000A => 1, 0x000B => 1, 0x000C => 1, # HT, LF, VT, FF,
2941            0x0020 => 1, 0x003C => 1, 0x0026 => 1, -1 => 1, # SP, <, &
2942            $self->{entity_additional} => 1,
2943          }->{$self->{next_char}}) {
2944            !!!cp (1001);
2945            ## Don't consume
2946            ## No error
2947            ## Return nothing.
2948            #
2949          } elsif ($self->{next_char} == 0x0023) { # #
2950            !!!cp (999);
2951            $self->{state} = ENTITY_HASH_STATE;
2952            $self->{state_keyword} = '#';
2953            !!!next-input-character;
2954            redo A;
2955          } elsif ((0x0041 <= $self->{next_char} and
2956                    $self->{next_char} <= 0x005A) or # A..Z
2957                   (0x0061 <= $self->{next_char} and
2958                    $self->{next_char} <= 0x007A)) { # a..z
2959            !!!cp (998);
2960            require Whatpm::_NamedEntityList;
2961            $self->{state} = ENTITY_NAME_STATE;
2962            $self->{state_keyword} = chr $self->{next_char};
2963            $self->{entity__value} = $self->{state_keyword};
2964            $self->{entity__match} = 0;
2965            !!!next-input-character;
2966            redo A;
2967          } else {
2968            !!!cp (1027);
2969            !!!parse-error (type => 'bare ero');
2970            ## Return nothing.
2971            #
2972          }
2973    
2974          if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        ## NOTE: No character is consumed by the "consume a character
2975            !!!cp (1008);        ## reference" algorithm.  In other word, there is an "&" character
2976            !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);        ## that does not introduce a character reference, which would be
2977            $code = 0xFFFD;        ## appended to the parent element or the attribute value in later
2978          } elsif ($code > 0x10FFFF) {        ## process of the tokenizer.
2979            !!!cp (1009);  
2980            !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);        if ($self->{prev_state} == DATA_STATE) {
2981            $code = 0xFFFD;          !!!cp (997);
2982          } elsif ($code == 0x000D) {          $self->{state} = $self->{prev_state};
2983            !!!cp (1010);          ## Reconsume.
2984            !!!parse-error (type => 'CR character reference');          !!!emit ({type => CHARACTER_TOKEN, data => '&',
2985            $code = 0x000A;                    line => $self->{line_prev},
2986          } elsif (0x80 <= $code and $code <= 0x9F) {                    column => $self->{column_prev},
2987            !!!cp (1011);                   });
2988            !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          redo A;
2989            $code = $c1_entity_char->{$code};        } else {
2990          }          !!!cp (996);
2991            $self->{current_attribute}->{value} .= '&';
2992          return {type => CHARACTER_TOKEN, data => chr $code,          $self->{state} = $self->{prev_state};
2993                  has_reference => 1};          ## Reconsume.
2994        } # X          redo A;
2995      } elsif (0x0030 <= $self->{next_char} and        }
2996               $self->{next_char} <= 0x0039) { # 0..9      } elsif ($self->{state} == ENTITY_HASH_STATE) {
2997        my $code = $self->{next_char} - 0x0030;        if ($self->{next_char} == 0x0078 or # x
2998        !!!next-input-character;            $self->{next_char} == 0x0058) { # X
2999                  !!!cp (995);
3000        while (0x0030 <= $self->{next_char} and          $self->{state} = HEXREF_X_STATE;
3001                  $self->{next_char} <= 0x0039) { # 0..9          $self->{state_keyword} .= chr $self->{next_char};
3002            !!!next-input-character;
3003            redo A;
3004          } elsif (0x0030 <= $self->{next_char} and
3005                   $self->{next_char} <= 0x0039) { # 0..9
3006            !!!cp (994);
3007            $self->{state} = NCR_NUM_STATE;
3008            $self->{state_keyword} = $self->{next_char} - 0x0030;
3009            !!!next-input-character;
3010            redo A;
3011          } else {
3012            !!!parse-error (type => 'bare nero',
3013                            line => $self->{line_prev},
3014                            column => $self->{column_prev} - 1);
3015    
3016            ## NOTE: According to the spec algorithm, nothing is returned,
3017            ## and then "&#" is appended to the parent element or the attribute
3018            ## value in the later processing.
3019    
3020            if ($self->{prev_state} == DATA_STATE) {
3021              !!!cp (1019);
3022              $self->{state} = $self->{prev_state};
3023              ## Reconsume.
3024              !!!emit ({type => CHARACTER_TOKEN,
3025                        data => '&#',
3026                        line => $self->{line_prev},
3027                        column => $self->{column_prev} - 1,
3028                       });
3029              redo A;
3030            } else {
3031              !!!cp (993);
3032              $self->{current_attribute}->{value} .= '&#';
3033              $self->{state} = $self->{prev_state};
3034              ## Reconsume.
3035              redo A;
3036            }
3037          }
3038        } elsif ($self->{state} == NCR_NUM_STATE) {
3039          if (0x0030 <= $self->{next_char} and
3040              $self->{next_char} <= 0x0039) { # 0..9
3041          !!!cp (1012);          !!!cp (1012);
3042          $code *= 10;          $self->{state_keyword} *= 10;
3043          $code += $self->{next_char} - 0x0030;          $self->{state_keyword} += $self->{next_char} - 0x0030;
3044                    
3045            ## Stay in the state.
3046          !!!next-input-character;          !!!next-input-character;
3047        }          redo A;
3048          } elsif ($self->{next_char} == 0x003B) { # ;
       if ($self->{next_char} == 0x003B) { # ;  
3049          !!!cp (1013);          !!!cp (1013);
3050          !!!next-input-character;          !!!next-input-character;
3051            #
3052        } else {        } else {
3053          !!!cp (1014);          !!!cp (1014);
3054          !!!parse-error (type => 'no refc');          !!!parse-error (type => 'no refc');
3055            ## Reconsume.
3056            #
3057        }        }
3058    
3059          my $code = $self->{state_keyword};
3060          my $l = $self->{line_prev};
3061          my $c = $self->{column_prev};
3062        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3063          !!!cp (1015);          !!!cp (1015);
3064          !!!parse-error (type => sprintf 'invalid character reference:U+%04X', $code);          !!!parse-error (type => 'invalid character reference',
3065                            text => (sprintf 'U+%04X', $code),
3066                            line => $l, column => $c);
3067          $code = 0xFFFD;          $code = 0xFFFD;
3068        } elsif ($code > 0x10FFFF) {        } elsif ($code > 0x10FFFF) {
3069          !!!cp (1016);          !!!cp (1016);
3070          !!!parse-error (type => sprintf 'invalid character reference:U-%08X', $code);          !!!parse-error (type => 'invalid character reference',
3071                            text => (sprintf 'U-%08X', $code),
3072                            line => $l, column => $c);
3073          $code = 0xFFFD;          $code = 0xFFFD;
3074        } elsif ($code == 0x000D) {        } elsif ($code == 0x000D) {
3075          !!!cp (1017);          !!!cp (1017);
3076          !!!parse-error (type => 'CR character reference');          !!!parse-error (type => 'CR character reference',
3077                            line => $l, column => $c);
3078          $code = 0x000A;          $code = 0x000A;
3079        } elsif (0x80 <= $code and $code <= 0x9F) {        } elsif (0x80 <= $code and $code <= 0x9F) {
3080          !!!cp (1018);          !!!cp (1018);
3081          !!!parse-error (type => sprintf 'C1 character reference:U+%04X', $code);          !!!parse-error (type => 'C1 character reference',
3082                            text => (sprintf 'U+%04X', $code),
3083                            line => $l, column => $c);
3084          $code = $c1_entity_char->{$code};          $code = $c1_entity_char->{$code};
3085        }        }
3086          
3087        return {type => CHARACTER_TOKEN, data => chr $code, has_reference => 1};        if ($self->{prev_state} == DATA_STATE) {
3088      } else {          !!!cp (992);
3089        !!!cp (1019);          $self->{state} = $self->{prev_state};
3090        !!!parse-error (type => 'bare nero');          ## Reconsume.
3091        !!!back-next-input-character ($self->{next_char});          !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3092        $self->{next_char} = 0x0023; # #                    line => $l, column => $c,
3093        return undef;                   });
3094      }          redo A;
3095    } elsif ((0x0041 <= $self->{next_char} and        } else {
3096              $self->{next_char} <= 0x005A) or          !!!cp (991);
3097             (0x0061 <= $self->{next_char} and          $self->{current_attribute}->{value} .= chr $code;
3098              $self->{next_char} <= 0x007A)) {          $self->{current_attribute}->{has_reference} = 1;
3099      my $entity_name = chr $self->{next_char};          $self->{state} = $self->{prev_state};
3100      !!!next-input-character;          ## Reconsume.
3101            redo A;
3102      my $value = $entity_name;        }
3103      my $match = 0;      } elsif ($self->{state} == HEXREF_X_STATE) {
3104      require Whatpm::_NamedEntityList;        if ((0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) or
3105      our $EntityChar;            (0x0041 <= $self->{next_char} and $self->{next_char} <= 0x0046) or
3106              (0x0061 <= $self->{next_char} and $self->{next_char} <= 0x0066)) {
3107      while (length $entity_name < 10 and          # 0..9, A..F, a..f
3108             ## NOTE: Some number greater than the maximum length of entity name          !!!cp (990);
3109             ((0x0041 <= $self->{next_char} and # a          $self->{state} = HEXREF_HEX_STATE;
3110               $self->{next_char} <= 0x005A) or # x          $self->{state_keyword} = 0;
3111              (0x0061 <= $self->{next_char} and # a          ## Reconsume.
3112               $self->{next_char} <= 0x007A) or # z          redo A;
3113              (0x0030 <= $self->{next_char} and # 0        } else {
3114               $self->{next_char} <= 0x0039) or # 9          !!!parse-error (type => 'bare hcro',
3115              $self->{next_char} == 0x003B)) { # ;                          line => $self->{line_prev},
3116        $entity_name .= chr $self->{next_char};                          column => $self->{column_prev} - 2);
3117        if (defined $EntityChar->{$entity_name}) {  
3118          if ($self->{next_char} == 0x003B) { # ;          ## NOTE: According to the spec algorithm, nothing is returned,
3119            !!!cp (1020);          ## and then "&#" followed by "X" or "x" is appended to the parent
3120            $value = $EntityChar->{$entity_name};          ## element or the attribute value in the later processing.
3121            $match = 1;  
3122            !!!next-input-character;          if ($self->{prev_state} == DATA_STATE) {
3123            last;            !!!cp (1005);
3124              $self->{state} = $self->{prev_state};
3125              ## Reconsume.
3126              !!!emit ({type => CHARACTER_TOKEN,
3127                        data => '&' . $self->{state_keyword},
3128                        line => $self->{line_prev},
3129                        column => $self->{column_prev} - length $self->{state_keyword},
3130                       });
3131              redo A;
3132          } else {          } else {
3133            !!!cp (1021);            !!!cp (989);
3134            $value = $EntityChar->{$entity_name};            $self->{current_attribute}->{value} .= '&' . $self->{state_keyword};
3135            $match = -1;            $self->{state} = $self->{prev_state};
3136            !!!next-input-character;            ## Reconsume.
3137              redo A;
3138          }          }
3139        } else {        }
3140          !!!cp (1022);      } elsif ($self->{state} == HEXREF_HEX_STATE) {
3141          $value .= chr $self->{next_char};        if (0x0030 <= $self->{next_char} and $self->{next_char} <= 0x0039) {
3142          $match *= 2;          # 0..9
3143            !!!cp (1002);
3144            $self->{state_keyword} *= 0x10;
3145            $self->{state_keyword} += $self->{next_char} - 0x0030;
3146            ## Stay in the state.
3147            !!!next-input-character;
3148            redo A;
3149          } elsif (0x0061 <= $self->{next_char} and
3150                   $self->{next_char} <= 0x0066) { # a..f
3151            !!!cp (1003);
3152            $self->{state_keyword} *= 0x10;
3153            $self->{state_keyword} += $self->{next_char} - 0x0060 + 9;
3154            ## Stay in the state.
3155          !!!next-input-character;          !!!next-input-character;
3156            redo A;
3157          } elsif (0x0041 <= $self->{next_char} and
3158                   $self->{next_char} <= 0x0046) { # A..F
3159            !!!cp (1004);
3160            $self->{state_keyword} *= 0x10;
3161            $self->{state_keyword} += $self->{next_char} - 0x0040 + 9;
3162            ## Stay in the state.
3163            !!!next-input-character;
3164            redo A;
3165          } elsif ($self->{next_char} == 0x003B) { # ;
3166            !!!cp (1006);
3167            !!!next-input-character;
3168            #
3169          } else {
3170            !!!cp (1007);
3171            !!!parse-error (type => 'no refc',
3172                            line => $self->{line},
3173                            column => $self->{column});
3174            ## Reconsume.
3175            #
3176        }        }
3177      }  
3178              my $code = $self->{state_keyword};
3179      if ($match > 0) {        my $l = $self->{line_prev};
3180        !!!cp (1023);        my $c = $self->{column_prev};
3181        return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};        if ($code == 0 or (0xD800 <= $code and $code <= 0xDFFF)) {
3182      } elsif ($match < 0) {          !!!cp (1008);
3183        !!!parse-error (type => 'no refc');          !!!parse-error (type => 'invalid character reference',
3184        if ($in_attr and $match < -1) {                          text => (sprintf 'U+%04X', $code),
3185          !!!cp (1024);                          line => $l, column => $c);
3186          return {type => CHARACTER_TOKEN, data => '&'.$entity_name};          $code = 0xFFFD;
3187          } elsif ($code > 0x10FFFF) {
3188            !!!cp (1009);
3189            !!!parse-error (type => 'invalid character reference',
3190                            text => (sprintf 'U-%08X', $code),
3191                            line => $l, column => $c);
3192            $code = 0xFFFD;
3193          } elsif ($code == 0x000D) {
3194            !!!cp (1010);
3195            !!!parse-error (type => 'CR character reference', line => $l, column => $c);
3196            $code = 0x000A;
3197          } elsif (0x80 <= $code and $code <= 0x9F) {
3198            !!!cp (1011);
3199            !!!parse-error (type => 'C1 character reference', text => (sprintf 'U+%04X', $code), line => $l, column => $c);
3200            $code = $c1_entity_char->{$code};
3201          }
3202    
3203          if ($self->{prev_state} == DATA_STATE) {
3204            !!!cp (988);
3205            $self->{state} = $self->{prev_state};
3206            ## Reconsume.
3207            !!!emit ({type => CHARACTER_TOKEN, data => chr $code,
3208                      line => $l, column => $c,
3209                     });
3210            redo A;
3211          } else {
3212            !!!cp (987);
3213            $self->{current_attribute}->{value} .= chr $code;
3214            $self->{current_attribute}->{has_reference} = 1;
3215            $self->{state} = $self->{prev_state};
3216            ## Reconsume.
3217            redo A;
3218          }
3219        } elsif ($self->{state} == ENTITY_NAME_STATE) {
3220          if (length $self->{state_keyword} < 30 and
3221              ## NOTE: Some number greater than the maximum length of entity name
3222              ((0x0041 <= $self->{next_char} and # a
3223                $self->{next_char} <= 0x005A) or # x
3224               (0x0061 <= $self->{next_char} and # a
3225                $self->{next_char} <= 0x007A) or # z
3226               (0x0030 <= $self->{next_char} and # 0
3227                $self->{next_char} <= 0x0039) or # 9
3228               $self->{next_char} == 0x003B)) { # ;
3229            our $EntityChar;
3230            $self->{state_keyword} .= chr $self->{next_char};
3231            if (defined $EntityChar->{$self->{state_keyword}}) {
3232              if ($self->{next_char} == 0x003B) { # ;
3233                !!!cp (1020);
3234                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3235                $self->{entity__match} = 1;
3236                !!!next-input-character;
3237                #
3238              } else {
3239                !!!cp (1021);
3240                $self->{entity__value} = $EntityChar->{$self->{state_keyword}};
3241                $self->{entity__match} = -1;
3242                ## Stay in the state.
3243                !!!next-input-character;
3244                redo A;
3245              }
3246            } else {
3247              !!!cp (1022);
3248              $self->{entity__value} .= chr $self->{next_char};
3249              $self->{entity__match} *= 2;
3250              ## Stay in the state.
3251              !!!next-input-character;
3252              redo A;
3253            }
3254          }
3255    
3256          my $data;
3257          my $has_ref;
3258          if ($self->{entity__match} > 0) {
3259            !!!cp (1023);
3260            $data = $self->{entity__value};
3261            $has_ref = 1;
3262            #
3263          } elsif ($self->{entity__match} < 0) {
3264            !!!parse-error (type => 'no refc');
3265            if ($self->{prev_state} != DATA_STATE and # in attribute
3266                $self->{entity__match} < -1) {
3267              !!!cp (1024);
3268              $data = '&' . $self->{state_keyword};
3269              #
3270            } else {
3271              !!!cp (1025);
3272              $data = $self->{entity__value};
3273              $has_ref = 1;
3274              #
3275            }
3276        } else {        } else {
3277          !!!cp (1025);          !!!cp (1026);
3278          return {type => CHARACTER_TOKEN, data => $value, has_reference => 1};          !!!parse-error (type => 'bare ero',
3279                            line => $self->{line_prev},
3280                            column => $self->{column_prev});
3281            $data = '&' . $self->{state_keyword};
3282            #
3283          }
3284      
3285          ## NOTE: In these cases, when a character reference is found,
3286          ## it is consumed and a character token is returned, or, otherwise,
3287          ## nothing is consumed and returned, according to the spec algorithm.
3288          ## In this implementation, anything that has been examined by the
3289          ## tokenizer is appended to the parent element or the attribute value
3290          ## as string, either literal string when no character reference or
3291          ## entity-replaced string otherwise, in this stage, since any characters
3292          ## that would not be consumed are appended in the data state or in an
3293          ## appropriate attribute value state anyway.
3294    
3295          if ($self->{prev_state} == DATA_STATE) {
3296            !!!cp (986);
3297            $self->{state} = $self->{prev_state};
3298            ## Reconsume.
3299            !!!emit ({type => CHARACTER_TOKEN,
3300                      data => $data,
3301                      line => $self->{line_prev},
3302                      column => $self->{column_prev} + 1 - length $self->{state_keyword},
3303                     });
3304            redo A;
3305          } else {
3306            !!!cp (985);
3307            $self->{current_attribute}->{value} .= $data;
3308            $self->{current_attribute}->{has_reference} = 1 if $has_ref;
3309            $self->{state} = $self->{prev_state};
3310            ## Reconsume.
3311            redo A;
3312        }        }
3313      } else {      } else {
3314        !!!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};  
3315      }      }
3316    } else {    } # A  
3317      !!!cp (1027);  
3318      ## no characters are consumed    die "$0: _get_next_token: unexpected case";
3319      !!!parse-error (type => 'bare ero');  } # _get_next_token
     return undef;  
   }  
 } # _tokenize_attempt_to_consume_an_entity  
3320    
3321  sub _initialize_tree_constructor ($) {  sub _initialize_tree_constructor ($) {
3322    my $self = shift;    my $self = shift;
# Line 2402  sub _initialize_tree_constructor ($) { Line 3325  sub _initialize_tree_constructor ($) {
3325    ## TODO: Turn mutation events off # MUST    ## TODO: Turn mutation events off # MUST
3326    ## TODO: Turn loose Document option (manakai extension) on    ## TODO: Turn loose Document option (manakai extension) on
3327    $self->{document}->manakai_is_html (1); # MUST    $self->{document}->manakai_is_html (1); # MUST
3328      $self->{document}->set_user_data (manakai_source_line => 1);
3329      $self->{document}->set_user_data (manakai_source_column => 1);
3330  } # _initialize_tree_constructor  } # _initialize_tree_constructor
3331    
3332  sub _terminate_tree_constructor ($) {  sub _terminate_tree_constructor ($) {
# Line 2456  sub _tree_construction_initial ($) { Line 3381  sub _tree_construction_initial ($) {
3381        ## language.        ## language.
3382        my $doctype_name = $token->{name};        my $doctype_name = $token->{name};
3383        $doctype_name = '' unless defined $doctype_name;        $doctype_name = '' unless defined $doctype_name;
3384        $doctype_name =~ tr/a-z/A-Z/;        $doctype_name =~ tr/a-z/A-Z/; # ASCII case-insensitive
3385        if (not defined $token->{name} or # <!DOCTYPE>        if (not defined $token->{name} or # <!DOCTYPE>
           defined $token->{public_identifier} or  
3386            defined $token->{system_identifier}) {            defined $token->{system_identifier}) {
3387          !!!cp ('t1');          !!!cp ('t1');
3388          !!!parse-error (type => 'not HTML5');          !!!parse-error (type => 'not HTML5', token => $token);
3389        } elsif ($doctype_name ne 'HTML') {        } elsif ($doctype_name ne 'HTML') {
3390          !!!cp ('t2');          !!!cp ('t2');
3391          ## ISSUE: ASCII case-insensitive? (in fact it does not matter)          !!!parse-error (type => 'not HTML5', token => $token);
3392          !!!parse-error (type => 'not HTML5');        } elsif (defined $token->{public_identifier}) {
3393            if ($token->{public_identifier} eq 'XSLT-compat') {
3394              !!!cp ('t1.2');
3395              !!!parse-error (type => 'XSLT-compat', token => $token,
3396                              level => $self->{level}->{should});
3397            } else {
3398              !!!parse-error (type => 'not HTML5', token => $token);
3399            }
3400        } else {        } else {
3401          !!!cp ('t3');          !!!cp ('t3');
3402            #
3403        }        }
3404                
3405        my $doctype = $self->{document}->create_document_type_definition        my $doctype = $self->{document}->create_document_type_definition
3406          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?          ($token->{name}); ## ISSUE: If name is missing (e.g. <!DOCTYPE>)?
3407          ## NOTE: Default value for both |public_id| and |system_id| attributes
3408          ## are empty strings, so that we don't set any value in missing cases.
3409        $doctype->public_id ($token->{public_identifier})        $doctype->public_id ($token->{public_identifier})
3410            if defined $token->{public_identifier};            if defined $token->{public_identifier};
3411        $doctype->system_id ($token->{system_identifier})        $doctype->system_id ($token->{system_identifier})
# Line 2486  sub _tree_construction_initial ($) { Line 3420  sub _tree_construction_initial ($) {
3420        } elsif (defined $token->{public_identifier}) {        } elsif (defined $token->{public_identifier}) {
3421          my $pubid = $token->{public_identifier};          my $pubid = $token->{public_identifier};
3422          $pubid =~ tr/a-z/A-z/;          $pubid =~ tr/a-z/A-z/;
3423          if ({          my $prefix = [
3424            "+//SILMARIL//DTD HTML PRO V0R11 19970101//EN" => 1,            "+//SILMARIL//DTD HTML PRO V0R11 19970101//",
3425            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//ADVASOFT LTD//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3426            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//EN" => 1,            "-//AS//DTD HTML 3.0 ASWEDIT + EXTENSIONS//",
3427            "-//IETF//DTD HTML 2.0 LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 1//",
3428            "-//IETF//DTD HTML 2.0 LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 LEVEL 2//",
3429            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 1//",
3430            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT LEVEL 2//",
3431            "-//IETF//DTD HTML 2.0 STRICT//EN" => 1,            "-//IETF//DTD HTML 2.0 STRICT//",
3432            "-//IETF//DTD HTML 2.0//EN" => 1,            "-//IETF//DTD HTML 2.0//",
3433            "-//IETF//DTD HTML 2.1E//EN" => 1,            "-//IETF//DTD HTML 2.1E//",
3434            "-//IETF//DTD HTML 3.0//EN" => 1,            "-//IETF//DTD HTML 3.0//",
3435            "-//IETF//DTD HTML 3.0//EN//" => 1,            "-//IETF//DTD HTML 3.2 FINAL//",
3436            "-//IETF//DTD HTML 3.2 FINAL//EN" => 1,            "-//IETF//DTD HTML 3.2//",
3437            "-//IETF//DTD HTML 3.2//EN" => 1,            "-//IETF//DTD HTML 3//",
3438            "-//IETF//DTD HTML 3//EN" => 1,            "-//IETF//DTD HTML LEVEL 0//",
3439            "-//IETF//DTD HTML LEVEL 0//EN" => 1,            "-//IETF//DTD HTML LEVEL 1//",
3440            "-//IETF//DTD HTML LEVEL 0//EN//2.0" => 1,            "-//IETF//DTD HTML LEVEL 2//",
3441            "-//IETF//DTD HTML LEVEL 1//EN" => 1,            "-//IETF//DTD HTML LEVEL 3//",
3442            "-//IETF//DTD HTML LEVEL 1//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 0//",
3443            "-//IETF//DTD HTML LEVEL 2//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 1//",
3444            "-//IETF//DTD HTML LEVEL 2//EN//2.0" => 1,            "-//IETF//DTD HTML STRICT LEVEL 2//",
3445            "-//IETF//DTD HTML LEVEL 3//EN" => 1,            "-//IETF//DTD HTML STRICT LEVEL 3//",
3446            "-//IETF//DTD HTML LEVEL 3//EN//3.0" => 1,            "-//IETF//DTD HTML STRICT//",
3447            "-//IETF//DTD HTML STRICT LEVEL 0//EN" => 1,            "-//IETF//DTD HTML//",
3448            "-//IETF//DTD HTML STRICT LEVEL 0//EN//2.0" => 1,            "-//METRIUS//DTD METRIUS PRESENTATIONAL//",
3449            "-//IETF//DTD HTML STRICT LEVEL 1//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//",
3450            "-//IETF//DTD HTML STRICT LEVEL 1//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//",
3451            "-//IETF//DTD HTML STRICT LEVEL 2//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//",
3452            "-//IETF//DTD HTML STRICT LEVEL 2//EN//2.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//",
3453            "-//IETF//DTD HTML STRICT LEVEL 3//EN" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//",
3454            "-//IETF//DTD HTML STRICT LEVEL 3//EN//3.0" => 1,            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//",
3455            "-//IETF//DTD HTML STRICT//EN" => 1,            "-//NETSCAPE COMM. CORP.//DTD HTML//",
3456            "-//IETF//DTD HTML STRICT//EN//2.0" => 1,            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//",
3457            "-//IETF//DTD HTML STRICT//EN//3.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//",
3458            "-//IETF//DTD HTML//EN" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//",
3459            "-//IETF//DTD HTML//EN//2.0" => 1,            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//",
3460            "-//IETF//DTD HTML//EN//3.0" => 1,            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//",
3461            "-//METRIUS//DTD METRIUS PRESENTATIONAL//EN" => 1,            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//",
3462            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML STRICT//EN" => 1,            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//",
3463            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 HTML//EN" => 1,            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//",
3464            "-//MICROSOFT//DTD INTERNET EXPLORER 2.0 TABLES//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//",
3465            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML STRICT//EN" => 1,            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//",
3466            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 HTML//EN" => 1,            "-//W3C//DTD HTML 3 1995-03-24//",
3467            "-//MICROSOFT//DTD INTERNET EXPLORER 3.0 TABLES//EN" => 1,            "-//W3C//DTD HTML 3.2 DRAFT//",
3468            "-//NETSCAPE COMM. CORP.//DTD HTML//EN" => 1,            "-//W3C//DTD HTML 3.2 FINAL//",
3469            "-//NETSCAPE COMM. CORP.//DTD STRICT HTML//EN" => 1,            "-//W3C//DTD HTML 3.2//",
3470            "-//O'REILLY AND ASSOCIATES//DTD HTML 2.0//EN" => 1,            "-//W3C//DTD HTML 3.2S DRAFT//",
3471            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 FRAMESET//",
3472            "-//O'REILLY AND ASSOCIATES//DTD HTML EXTENDED RELAXED 1.0//EN" => 1,            "-//W3C//DTD HTML 4.0 TRANSITIONAL//",
3473            "-//SOFTQUAD SOFTWARE//DTD HOTMETAL PRO 6.0::19990601::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMETNAL 19960712//",
3474            "-//SOFTQUAD//DTD HOTMETAL PRO 4.0::19971010::EXTENSIONS TO HTML 4.0//EN" => 1,            "-//W3C//DTD HTML EXPERIMENTAL 970421//",
3475            "-//SPYGLASS//DTD HTML 2.0 EXTENDED//EN" => 1,            "-//W3C//DTD W3 HTML//",
3476            "-//SQ//DTD HTML 2.0 HOTMETAL + EXTENSIONS//EN" => 1,            "-//W3O//DTD W3 HTML 3.0//",
3477            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML 2.0//",
3478            "-//SUN MICROSYSTEMS CORP.//DTD HOTJAVA STRICT HTML//EN" => 1,            "-//WEBTECHS//DTD MOZILLA HTML//",
3479            "-//W3C//DTD HTML 3 1995-03-24//EN" => 1,          ]; # $prefix
3480            "-//W3C//DTD HTML 3.2 DRAFT//EN" => 1,          my $match;
3481            "-//W3C//DTD HTML 3.2 FINAL//EN" => 1,          for (@$prefix) {
3482            "-//W3C//DTD HTML 3.2//EN" => 1,            if (substr ($prefix, 0, length $_) eq $_) {
3483            "-//W3C//DTD HTML 3.2S DRAFT//EN" => 1,              $match = 1;
3484            "-//W3C//DTD HTML 4.0 FRAMESET//EN" => 1,              last;
3485            "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN" => 1,            }
3486            "-//W3C//DTD HTML EXPERIMETNAL 19960712//EN" => 1,          }
3487            "-//W3C//DTD HTML EXPERIMENTAL 970421//EN" => 1,          if ($match or
3488            "-//W3C//DTD W3 HTML//EN" => 1,              $pubid eq "-//W3O//DTD W3 HTML STRICT 3.0//EN//" or
3489            "-//W3O//DTD W3 HTML 3.0//EN" => 1,              $pubid eq "-/W3C/DTD HTML 4.0 TRANSITIONAL/EN" or
3490            "-//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}) {  
3491            !!!cp ('t5');            !!!cp ('t5');
3492            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3493          } elsif ($pubid eq "-//W3C//DTD HTML 4.01 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD HTML 4.01 FRAMESET//] or
3494                   $pubid eq "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD HTML 4.01 TRANSITIONAL//]) {
3495            if (defined $token->{system_identifier}) {            if (defined $token->{system_identifier}) {
3496              !!!cp ('t6');              !!!cp ('t6');
3497              $self->{document}->manakai_compat_mode ('quirks');              $self->{document}->manakai_compat_mode ('quirks');
# Line 2571  sub _tree_construction_initial ($) { Line 3499  sub _tree_construction_initial ($) {
3499              !!!cp ('t7');              !!!cp ('t7');
3500              $self->{document}->manakai_compat_mode ('limited quirks');              $self->{document}->manakai_compat_mode ('limited quirks');
3501            }            }
3502          } elsif ($pubid eq "-//W3C//DTD XHTML 1.0 FRAMESET//EN" or          } elsif ($pubid =~ m[^-//W3C//DTD XHTML 1.0 FRAMESET//] or
3503                   $pubid eq "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN") {                   $pubid =~ m[^-//W3C//DTD XHTML 1.0 TRANSITIONAL//]) {
3504            !!!cp ('t8');            !!!cp ('t8');
3505            $self->{document}->manakai_compat_mode ('limited quirks');            $self->{document}->manakai_compat_mode ('limited quirks');
3506          } else {          } else {
# Line 2585  sub _tree_construction_initial ($) { Line 3513  sub _tree_construction_initial ($) {
3513          my $sysid = $token->{system_identifier};          my $sysid = $token->{system_identifier};
3514          $sysid =~ tr/A-Z/a-z/;          $sysid =~ tr/A-Z/a-z/;
3515          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") {
3516            ## TODO: Check the spec: PUBLIC "(limited quirks)" "(quirks)"            ## NOTE: Ensure that |PUBLIC "(limited quirks)" "(quirks)"| is
3517              ## marked as quirks.
3518            $self->{document}->manakai_compat_mode ('quirks');            $self->{document}->manakai_compat_mode ('quirks');
3519            !!!cp ('t11');            !!!cp ('t11');
3520          } else {          } else {
# Line 2604  sub _tree_construction_initial ($) { Line 3533  sub _tree_construction_initial ($) {
3533                END_OF_FILE_TOKEN, 1,                END_OF_FILE_TOKEN, 1,
3534               }->{$token->{type}}) {               }->{$token->{type}}) {
3535        !!!cp ('t14');        !!!cp ('t14');
3536        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3537        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3538        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3539        ## reprocess        ## reprocess
3540          !!!ack-later;
3541        return;        return;
3542      } elsif ($token->{type} == CHARACTER_TOKEN) {      } elsif ($token->{type} == CHARACTER_TOKEN) {
3543        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 3555  sub _tree_construction_initial ($) {
3555          !!!cp ('t17');          !!!cp ('t17');
3556        }        }
3557    
3558        !!!parse-error (type => 'no DOCTYPE');        !!!parse-error (type => 'no DOCTYPE', token => $token);
3559        $self->{document}->manakai_compat_mode ('quirks');        $self->{document}->manakai_compat_mode ('quirks');
3560        ## Go to the "before html" insertion mode.        ## Go to the "before html" insertion mode.
3561        ## reprocess        ## reprocess
# Line 2654  sub _tree_construction_root_element ($) Line 3584  sub _tree_construction_root_element ($)
3584    B: {    B: {
3585        if ($token->{type} == DOCTYPE_TOKEN) {        if ($token->{type} == DOCTYPE_TOKEN) {
3586          !!!cp ('t19');          !!!cp ('t19');
3587          !!!parse-error (type => 'in html:#DOCTYPE');          !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
3588          ## Ignore the token          ## Ignore the token
3589          ## Stay in the insertion mode.          ## Stay in the insertion mode.
3590          !!!next-token;          !!!next-token;
# Line 2688  sub _tree_construction_root_element ($) Line 3618  sub _tree_construction_root_element ($)
3618        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
3619          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
3620            my $root_element;            my $root_element;
3621            !!!create-element ($root_element, $token->{tag_name}, $token->{attributes});            !!!create-element ($root_element, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
3622            $self->{document}->append_child ($root_element);            $self->{document}->append_child ($root_element);
3623            push @{$self->{open_elements}}, [$root_element, 'html'];            push @{$self->{open_elements}},
3624                  [$root_element, $el_category->{html}];
3625    
3626            if ($token->{attributes}->{manifest}) {            if ($token->{attributes}->{manifest}) {
3627              !!!cp ('t24');              !!!cp ('t24');
3628              $self->{application_cache_selection}              $self->{application_cache_selection}
3629                  ->($token->{attributes}->{manifest}->{value});                  ->($token->{attributes}->{manifest}->{value});
3630              ## ISSUE: No relative reference resolution?              ## ISSUE: Spec is unclear on relative references.
3631                ## According to Hixie (#whatwg 2008-03-19), it should be
3632                ## resolved against the base URI of the document in HTML
3633                ## or xml:base of the element in XHTML.
3634            } else {            } else {
3635              !!!cp ('t25');              !!!cp ('t25');
3636              $self->{application_cache_selection}->(undef);              $self->{application_cache_selection}->(undef);
3637            }            }
3638    
3639              !!!nack ('t25c');
3640    
3641            !!!next-token;            !!!next-token;
3642            return; ## Go to the "before head" insertion mode.            return; ## Go to the "before head" insertion mode.
3643          } else {          } else {
# Line 2718  sub _tree_construction_root_element ($) Line 3654  sub _tree_construction_root_element ($)
3654          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
3655        }        }
3656    
3657      my $root_element; !!!create-element ($root_element, 'html');      my $root_element;
3658        !!!create-element ($root_element, $HTML_NS, 'html',, $token);
3659      $self->{document}->append_child ($root_element);      $self->{document}->append_child ($root_element);
3660      push @{$self->{open_elements}}, [$root_element, 'html'];      push @{$self->{open_elements}}, [$root_element, $el_category->{html}];
3661    
3662      $self->{application_cache_selection}->(undef);      $self->{application_cache_selection}->(undef);
3663    
3664      ## NOTE: Reprocess the token.      ## NOTE: Reprocess the token.
3665        !!!ack-later;
3666      return; ## Go to the "before head" insertion mode.      return; ## Go to the "before head" insertion mode.
3667    
3668      ## ISSUE: There is an issue in the spec      ## ISSUE: There is an issue in the spec
# Line 2748  sub _reset_insertion_mode ($) { Line 3686  sub _reset_insertion_mode ($) {
3686        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {        if ($self->{open_elements}->[0]->[0] eq $node->[0]) {
3687          $last = 1;          $last = 1;
3688          if (defined $self->{inner_html_node}) {          if (defined $self->{inner_html_node}) {
3689            if ($self->{inner_html_node}->[1] eq 'td' or            !!!cp ('t28');
3690                $self->{inner_html_node}->[1] eq 'th') {            $node = $self->{inner_html_node};
3691              !!!cp ('t27');          } else {
3692              #            die "_reset_insertion_mode: t27";
           } else {  
             !!!cp ('t28');  
             $node = $self->{inner_html_node};  
           }  
3693          }          }
3694        }        }
3695              
3696        ## Step 4..13        ## Step 4..14
3697        my $new_mode = {        my $new_mode;
3698          if ($node->[1] & FOREIGN_EL) {
3699            !!!cp ('t28.1');
3700            ## NOTE: Strictly spaking, the line below only applies to MathML and
3701            ## SVG elements.  Currently the HTML syntax supports only MathML and
3702            ## SVG elements as foreigners.
3703            $new_mode = IN_BODY_IM | IN_FOREIGN_CONTENT_IM;
3704          } elsif ($node->[1] & TABLE_CELL_EL) {
3705            if ($last) {
3706              !!!cp ('t28.2');
3707              #
3708            } else {
3709              !!!cp ('t28.3');
3710              $new_mode = IN_CELL_IM;
3711            }
3712          } else {
3713            !!!cp ('t28.4');
3714            $new_mode = {
3715                        select => IN_SELECT_IM,                        select => IN_SELECT_IM,
3716                        ## NOTE: |option| and |optgroup| do not set                        ## NOTE: |option| and |optgroup| do not set
3717                        ## insertion mode to "in select" by themselves.                        ## insertion mode to "in select" by themselves.
                       td => IN_CELL_IM,  
                       th => IN_CELL_IM,  
3718                        tr => IN_ROW_IM,                        tr => IN_ROW_IM,
3719                        tbody => IN_TABLE_BODY_IM,                        tbody => IN_TABLE_BODY_IM,
3720                        thead => IN_TABLE_BODY_IM,                        thead => IN_TABLE_BODY_IM,
# Line 2776  sub _reset_insertion_mode ($) { Line 3725  sub _reset_insertion_mode ($) {
3725                        head => IN_BODY_IM, # not in head!                        head => IN_BODY_IM, # not in head!
3726                        body => IN_BODY_IM,                        body => IN_BODY_IM,
3727                        frameset => IN_FRAMESET_IM,                        frameset => IN_FRAMESET_IM,
3728                       }->{$node->[1]};                       }->{$node->[0]->manakai_local_name};
3729          }
3730        $self->{insertion_mode} = $new_mode and return if defined $new_mode;        $self->{insertion_mode} = $new_mode and return if defined $new_mode;
3731                
3732        ## Step 14        ## Step 15
3733        if ($node->[1] eq 'html') {        if ($node->[1] & HTML_EL) {
3734          unless (defined $self->{head_element}) {          unless (defined $self->{head_element}) {
3735            !!!cp ('t29');            !!!cp ('t29');
3736            $self->{insertion_mode} = BEFORE_HEAD_IM;            $self->{insertion_mode} = BEFORE_HEAD_IM;
# Line 2794  sub _reset_insertion_mode ($) { Line 3744  sub _reset_insertion_mode ($) {
3744          !!!cp ('t31');          !!!cp ('t31');
3745        }        }
3746                
3747        ## Step 15        ## Step 16
3748        $self->{insertion_mode} = IN_BODY_IM and return if $last;        $self->{insertion_mode} = IN_BODY_IM and return if $last;
3749                
3750        ## Step 16        ## Step 17
3751        $i--;        $i--;
3752        $node = $self->{open_elements}->[$i];        $node = $self->{open_elements}->[$i];
3753                
3754        ## Step 17        ## Step 18
3755        redo S3;        redo S3;
3756      } # S3      } # S3
3757    
# Line 2913  sub _tree_construction_main ($) { Line 3863  sub _tree_construction_main ($) {
3863      ## Step 1      ## Step 1
3864      my $start_tag_name = $token->{tag_name};      my $start_tag_name = $token->{tag_name};
3865      my $el;      my $el;
3866      !!!create-element ($el, $start_tag_name, $token->{attributes});      !!!create-element ($el, $HTML_NS, $start_tag_name, $token->{attributes}, $token);
3867    
3868      ## Step 2      ## Step 2
3869      $insert->($el);      $insert->($el);
# Line 2924  sub _tree_construction_main ($) { Line 3874  sub _tree_construction_main ($) {
3874    
3875      ## Step 4      ## Step 4
3876      my $text = '';      my $text = '';
3877        !!!nack ('t40.1');
3878      !!!next-token;      !!!next-token;
3879      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing      while ($token->{type} == CHARACTER_TOKEN) { # or until stop tokenizing
3880        !!!cp ('t40');        !!!cp ('t40');
# Line 2950  sub _tree_construction_main ($) { Line 3901  sub _tree_construction_main ($) {
3901        ## NOTE: An end-of-file token.        ## NOTE: An end-of-file token.
3902        if ($content_model_flag == CDATA_CONTENT_MODEL) {        if ($content_model_flag == CDATA_CONTENT_MODEL) {
3903          !!!cp ('t43');          !!!cp ('t43');
3904          !!!parse-error (type => 'in CDATA:#'.$token->{type});          !!!parse-error (type => 'in CDATA:#eof', token => $token);
3905        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {        } elsif ($content_model_flag == RCDATA_CONTENT_MODEL) {
3906          !!!cp ('t44');          !!!cp ('t44');
3907          !!!parse-error (type => 'in RCDATA:#'.$token->{type});          !!!parse-error (type => 'in RCDATA:#eof', token => $token);
3908        } else {        } else {
3909          die "$0: $content_model_flag in parse_rcdata";          die "$0: $content_model_flag in parse_rcdata";
3910        }        }
# Line 2963  sub _tree_construction_main ($) { Line 3914  sub _tree_construction_main ($) {
3914    
3915    my $script_start_tag = sub () {    my $script_start_tag = sub () {
3916      my $script_el;      my $script_el;
3917      !!!create-element ($script_el, 'script', $token->{attributes});      !!!create-element ($script_el, $HTML_NS, 'script', $token->{attributes}, $token);
3918      ## TODO: mark as "parser-inserted"      ## TODO: mark as "parser-inserted"
3919    
3920      $self->{content_model} = CDATA_CONTENT_MODEL;      $self->{content_model} = CDATA_CONTENT_MODEL;
3921      delete $self->{escape}; # MUST      delete $self->{escape}; # MUST
3922            
3923      my $text = '';      my $text = '';
3924        !!!nack ('t45.1');
3925      !!!next-token;      !!!next-token;
3926      while ($token->{type} == CHARACTER_TOKEN) {      while ($token->{type} == CHARACTER_TOKEN) {
3927        !!!cp ('t45');        !!!cp ('t45');
# Line 2989  sub _tree_construction_main ($) { Line 3941  sub _tree_construction_main ($) {
3941        ## Ignore the token        ## Ignore the token
3942      } else {      } else {
3943        !!!cp ('t48');        !!!cp ('t48');
3944        !!!parse-error (type => 'in CDATA:#'.$token->{type});        !!!parse-error (type => 'in CDATA:#eof', token => $token);
3945        ## ISSUE: And ignore?        ## ISSUE: And ignore?
3946        ## TODO: mark as "already executed"        ## TODO: mark as "already executed"
3947      }      }
# Line 3017  sub _tree_construction_main ($) { Line 3969  sub _tree_construction_main ($) {
3969    my $open_tables = [[$self->{open_elements}->[0]->[0]]];    my $open_tables = [[$self->{open_elements}->[0]->[0]]];
3970    
3971    my $formatting_end_tag = sub {    my $formatting_end_tag = sub {
3972      my $tag_name = shift;      my $end_tag_token = shift;
3973        my $tag_name = $end_tag_token->{tag_name};
3974    
3975      ## NOTE: The adoption agency algorithm (AAA).      ## NOTE: The adoption agency algorithm (AAA).
3976    
# Line 3026  sub _tree_construction_main ($) { Line 3979  sub _tree_construction_main ($) {
3979        my $formatting_element;        my $formatting_element;
3980        my $formatting_element_i_in_active;        my $formatting_element_i_in_active;
3981        AFE: for (reverse 0..$#$active_formatting_elements) {        AFE: for (reverse 0..$#$active_formatting_elements) {
3982          if ($active_formatting_elements->[$_]->[1] eq $tag_name) {          if ($active_formatting_elements->[$_]->[0] eq '#marker') {
3983              !!!cp ('t52');
3984              last AFE;
3985            } elsif ($active_formatting_elements->[$_]->[0]->manakai_local_name
3986                         eq $tag_name) {
3987            !!!cp ('t51');            !!!cp ('t51');
3988            $formatting_element = $active_formatting_elements->[$_];            $formatting_element = $active_formatting_elements->[$_];
3989            $formatting_element_i_in_active = $_;            $formatting_element_i_in_active = $_;
3990            last AFE;            last AFE;
         } elsif ($active_formatting_elements->[$_]->[0] eq '#marker') {  
           !!!cp ('t52');  
           last AFE;  
3991          }          }
3992        } # AFE        } # AFE
3993        unless (defined $formatting_element) {        unless (defined $formatting_element) {
3994          !!!cp ('t53');          !!!cp ('t53');
3995          !!!parse-error (type => 'unmatched end tag:'.$tag_name);          !!!parse-error (type => 'unmatched end tag', text => $tag_name, token => $end_tag_token);
3996          ## Ignore the token          ## Ignore the token
3997          !!!next-token;          !!!next-token;
3998          return;          return;
# Line 3055  sub _tree_construction_main ($) { Line 4009  sub _tree_construction_main ($) {
4009              last INSCOPE;              last INSCOPE;
4010            } else { # in open elements but not in scope            } else { # in open elements but not in scope
4011              !!!cp ('t55');              !!!cp ('t55');
4012              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
4013                                text => $token->{tag_name},
4014                                token => $end_tag_token);
4015              ## Ignore the token              ## Ignore the token
4016              !!!next-token;              !!!next-token;
4017              return;              return;
4018            }            }
4019          } elsif ({          } elsif ($node->[1] & SCOPING_EL) {
                   applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                   button => 1, marquee => 1, object => 1, html => 1,  
                  }->{$node->[1]}) {  
4020            !!!cp ('t56');            !!!cp ('t56');
4021            $in_scope = 0;            $in_scope = 0;
4022          }          }
4023        } # INSCOPE        } # INSCOPE
4024        unless (defined $formatting_element_i_in_open) {        unless (defined $formatting_element_i_in_open) {
4025          !!!cp ('t57');          !!!cp ('t57');
4026          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
4027                            text => $token->{tag_name},
4028                            token => $end_tag_token);
4029          pop @$active_formatting_elements; # $formatting_element          pop @$active_formatting_elements; # $formatting_element
4030          !!!next-token; ## TODO: ok?          !!!next-token; ## TODO: ok?
4031          return;          return;
4032        }        }
4033        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {        if (not $self->{open_elements}->[-1]->[0] eq $formatting_element->[0]) {
4034          !!!cp ('t58');          !!!cp ('t58');
4035          !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);          !!!parse-error (type => 'not closed',
4036                            text => $self->{open_elements}->[-1]->[0]
4037                                ->manakai_local_name,
4038                            token => $end_tag_token);
4039        }        }
4040                
4041        ## Step 2        ## Step 2
# Line 3085  sub _tree_construction_main ($) { Line 4043  sub _tree_construction_main ($) {
4043        my $furthest_block_i_in_open;        my $furthest_block_i_in_open;
4044        OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4045          my $node = $self->{open_elements}->[$_];          my $node = $self->{open_elements}->[$_];
4046          if (not $formatting_category->{$node->[1]} and          if (not ($node->[1] & FORMATTING_EL) and
4047              #not $phrasing_category->{$node->[1]} and              #not $phrasing_category->{$node->[1]} and
4048              ($special_category->{$node->[1]} or              ($node->[1] & SPECIAL_EL or
4049               $scoping_category->{$node->[1]})) { ## Scoping is redundant, maybe               $node->[1] & SCOPING_EL)) { ## Scoping is redundant, maybe
4050            !!!cp ('t59');            !!!cp ('t59');
4051            $furthest_block = $node;            $furthest_block = $node;
4052            $furthest_block_i_in_open = $_;            $furthest_block_i_in_open = $_;
# Line 3174  sub _tree_construction_main ($) { Line 4132  sub _tree_construction_main ($) {
4132        } # S7          } # S7  
4133                
4134        ## Step 8        ## Step 8
4135        if ({        if ($common_ancestor_node->[1] & TABLE_ROWS_EL) {
            table => 1, tbody => 1, tfoot => 1, thead => 1, tr => 1,  
           }->{$common_ancestor_node->[1]}) {  
4136          my $foster_parent_element;          my $foster_parent_element;
4137          my $next_sibling;          my $next_sibling;
4138                           OE: for (reverse 0..$#{$self->{open_elements}}) {          OE: for (reverse 0..$#{$self->{open_elements}}) {
4139                             if ($self->{open_elements}->[$_]->[1] eq 'table') {            if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4140                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4141                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4142                                 !!!cp ('t65.1');                                 !!!cp ('t65.1');
# Line 3253  sub _tree_construction_main ($) { Line 4209  sub _tree_construction_main ($) {
4209    
4210    my $insert_to_foster = sub {    my $insert_to_foster = sub {
4211      my $child = shift;      my $child = shift;
4212      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]}) {  
4213        # MUST        # MUST
4214        my $foster_parent_element;        my $foster_parent_element;
4215        my $next_sibling;        my $next_sibling;
4216                           OE: for (reverse 0..$#{$self->{open_elements}}) {        OE: for (reverse 0..$#{$self->{open_elements}}) {
4217                             if ($self->{open_elements}->[$_]->[1] eq 'table') {          if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
4218                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                               my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
4219                               if (defined $parent and $parent->node_type == 1) {                               if (defined $parent and $parent->node_type == 1) {
4220                                 !!!cp ('t70');                                 !!!cp ('t70');
# Line 3285  sub _tree_construction_main ($) { Line 4239  sub _tree_construction_main ($) {
4239      }      }
4240    }; # $insert_to_foster    }; # $insert_to_foster
4241    
4242    B: {    B: while (1) {
4243      if ($token->{type} == DOCTYPE_TOKEN) {      if ($token->{type} == DOCTYPE_TOKEN) {
4244        !!!cp ('t73');        !!!cp ('t73');
4245        !!!parse-error (type => 'DOCTYPE in the middle');        !!!parse-error (type => 'in html:#DOCTYPE', token => $token);
4246        ## Ignore the token        ## Ignore the token
4247        ## Stay in the phase        ## Stay in the phase
4248        !!!next-token;        !!!next-token;
4249        redo B;        next B;
4250      } elsif ($token->{type} == START_TAG_TOKEN and      } elsif ($token->{type} == START_TAG_TOKEN and
4251               $token->{tag_name} eq 'html') {               $token->{tag_name} eq 'html') {
4252        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {        if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
4253          !!!cp ('t79');          !!!cp ('t79');
4254          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4255          $self->{insertion_mode} = AFTER_BODY_IM;          $self->{insertion_mode} = AFTER_BODY_IM;
4256        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {        } elsif ($self->{insertion_mode} == AFTER_HTML_FRAMESET_IM) {
4257          !!!cp ('t80');          !!!cp ('t80');
4258          !!!parse-error (type => 'after html:html');          !!!parse-error (type => 'after html', text => 'html', token => $token);
4259          $self->{insertion_mode} = AFTER_FRAMESET_IM;          $self->{insertion_mode} = AFTER_FRAMESET_IM;
4260        } else {        } else {
4261          !!!cp ('t81');          !!!cp ('t81');
4262        }        }
4263    
4264        !!!cp ('t82');        !!!cp ('t82');
4265        !!!parse-error (type => 'not first start tag');        !!!parse-error (type => 'not first start tag', token => $token);
4266        my $top_el = $self->{open_elements}->[0]->[0];        my $top_el = $self->{open_elements}->[0]->[0];
4267        for my $attr_name (keys %{$token->{attributes}}) {        for my $attr_name (keys %{$token->{attributes}}) {
4268          unless ($top_el->has_attribute_ns (undef, $attr_name)) {          unless ($top_el->has_attribute_ns (undef, $attr_name)) {
# Line 3318  sub _tree_construction_main ($) { Line 4272  sub _tree_construction_main ($) {
4272               $token->{attributes}->{$attr_name}->{value});               $token->{attributes}->{$attr_name}->{value});
4273          }          }
4274        }        }
4275          !!!nack ('t84.1');
4276        !!!next-token;        !!!next-token;
4277        redo B;        next B;
4278      } elsif ($token->{type} == COMMENT_TOKEN) {      } elsif ($token->{type} == COMMENT_TOKEN) {
4279        my $comment = $self->{document}->create_comment ($token->{data});        my $comment = $self->{document}->create_comment ($token->{data});
4280        if ($self->{insertion_mode} & AFTER_HTML_IMS) {        if ($self->{insertion_mode} & AFTER_HTML_IMS) {
# Line 3333  sub _tree_construction_main ($) { Line 4288  sub _tree_construction_main ($) {
4288          $self->{open_elements}->[-1]->[0]->append_child ($comment);          $self->{open_elements}->[-1]->[0]->append_child ($comment);
4289        }        }
4290        !!!next-token;        !!!next-token;
4291        redo B;        next B;
4292      } elsif ($self->{insertion_mode} & HEAD_IMS) {      } elsif ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
4293          if ($token->{type} == CHARACTER_TOKEN) {
4294            !!!cp ('t87.1');
4295            $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
4296            !!!next-token;
4297            next B;
4298          } elsif ($token->{type} == START_TAG_TOKEN) {
4299            if ((not {mglyph => 1, malignmark => 1}->{$token->{tag_name}} and
4300                 $self->{open_elements}->[-1]->[1] & FOREIGN_FLOW_CONTENT_EL) or
4301                not ($self->{open_elements}->[-1]->[1] & FOREIGN_EL) or
4302                ($token->{tag_name} eq 'svg' and
4303                 $self->{open_elements}->[-1]->[1] & MML_AXML_EL)) {
4304              ## NOTE: "using the rules for secondary insertion mode"then"continue"
4305              !!!cp ('t87.2');
4306              #
4307            } elsif ({
4308                      b => 1, big => 1, blockquote => 1, body => 1, br => 1,
4309                      center => 1, code => 1, dd => 1, div => 1, dl => 1, dt => 1,
4310                      em => 1, embed => 1, font => 1, h1 => 1, h2 => 1, h3 => 1,
4311                      h4 => 1, h5 => 1, h6 => 1, head => 1, hr => 1, i => 1,
4312                      img => 1, li => 1, listing => 1, menu => 1, meta => 1,
4313                      nobr => 1, ol => 1, p => 1, pre => 1, ruby => 1, s => 1,
4314                      small => 1, span => 1, strong => 1, strike => 1, sub => 1,
4315                      sup => 1, table => 1, tt => 1, u => 1, ul => 1, var => 1,
4316                     }->{$token->{tag_name}}) {
4317              !!!cp ('t87.2');
4318              !!!parse-error (type => 'not closed',
4319                              text => $self->{open_elements}->[-1]->[0]
4320                                  ->manakai_local_name,
4321                              token => $token);
4322    
4323              pop @{$self->{open_elements}}
4324                  while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4325    
4326              $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4327              ## Reprocess.
4328              next B;
4329            } else {
4330              my $nsuri = $self->{open_elements}->[-1]->[0]->namespace_uri;
4331              my $tag_name = $token->{tag_name};
4332              if ($nsuri eq $SVG_NS) {
4333                $tag_name = {
4334                   altglyph => 'altGlyph',
4335                   altglyphdef => 'altGlyphDef',
4336                   altglyphitem => 'altGlyphItem',
4337                   animatecolor => 'animateColor',
4338                   animatemotion => 'animateMotion',
4339                   animatetransform => 'animateTransform',
4340                   clippath => 'clipPath',
4341                   feblend => 'feBlend',
4342                   fecolormatrix => 'feColorMatrix',
4343                   fecomponenttransfer => 'feComponentTransfer',
4344                   fecomposite => 'feComposite',
4345                   feconvolvematrix => 'feConvolveMatrix',
4346                   fediffuselighting => 'feDiffuseLighting',
4347                   fedisplacementmap => 'feDisplacementMap',
4348                   fedistantlight => 'feDistantLight',
4349                   feflood => 'feFlood',
4350                   fefunca => 'feFuncA',
4351                   fefuncb => 'feFuncB',
4352                   fefuncg => 'feFuncG',
4353                   fefuncr => 'feFuncR',
4354                   fegaussianblur => 'feGaussianBlur',
4355                   feimage => 'feImage',
4356                   femerge => 'feMerge',
4357                   femergenode => 'feMergeNode',
4358                   femorphology => 'feMorphology',
4359                   feoffset => 'feOffset',
4360                   fepointlight => 'fePointLight',
4361                   fespecularlighting => 'feSpecularLighting',
4362                   fespotlight => 'feSpotLight',
4363                   fetile => 'feTile',
4364                   feturbulence => 'feTurbulence',
4365                   foreignobject => 'foreignObject',
4366                   glyphref => 'glyphRef',
4367                   lineargradient => 'linearGradient',
4368                   radialgradient => 'radialGradient',
4369                   #solidcolor => 'solidColor', ## NOTE: Commented in spec (SVG1.2)
4370                   textpath => 'textPath',  
4371                }->{$tag_name} || $tag_name;
4372              }
4373    
4374              ## "adjust SVG attributes" (SVG only) - done in insert-element-f
4375    
4376              ## "adjust foreign attributes" - done in insert-element-f
4377    
4378              !!!insert-element-f ($nsuri, $tag_name, $token->{attributes}, $token);
4379    
4380              if ($self->{self_closing}) {
4381                pop @{$self->{open_elements}};
4382                !!!ack ('t87.3');
4383              } else {
4384                !!!cp ('t87.4');
4385              }
4386    
4387              !!!next-token;
4388              next B;
4389            }
4390          } elsif ($token->{type} == END_TAG_TOKEN) {
4391            ## NOTE: "using the rules for secondary insertion mode" then "continue"
4392            !!!cp ('t87.5');
4393            #
4394          } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4395            !!!cp ('t87.6');
4396            !!!parse-error (type => 'not closed',
4397                            text => $self->{open_elements}->[-1]->[0]
4398                                ->manakai_local_name,
4399                            token => $token);
4400    
4401            pop @{$self->{open_elements}}
4402                while $self->{open_elements}->[-1]->[1] & FOREIGN_EL;
4403    
4404            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
4405            ## Reprocess.
4406            next B;
4407          } else {
4408            die "$0: $token->{type}: Unknown token type";        
4409          }
4410        }
4411    
4412        if ($self->{insertion_mode} & HEAD_IMS) {
4413        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
4414          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {          if ($token->{data} =~ s/^([\x09\x0A\x0B\x0C\x20]+)//) {
4415            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {            unless ($self->{insertion_mode} == BEFORE_HEAD_IM) {
# Line 3344  sub _tree_construction_main ($) { Line 4419  sub _tree_construction_main ($) {
4419              !!!cp ('t88.1');              !!!cp ('t88.1');
4420              ## Ignore the token.              ## Ignore the token.
4421              !!!next-token;              !!!next-token;
4422              redo B;              next B;
4423            }            }
4424            unless (length $token->{data}) {            unless (length $token->{data}) {
4425              !!!cp ('t88');              !!!cp ('t88');
4426              !!!next-token;              !!!next-token;
4427              redo B;              next B;
4428            }            }
4429          }          }
4430    
4431          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4432            !!!cp ('t89');            !!!cp ('t89');
4433            ## As if <head>            ## As if <head>
4434            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4435            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});            $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4436            push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            push @{$self->{open_elements}},
4437                  [$self->{head_element}, $el_category->{head}];
4438    
4439            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4440            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3368  sub _tree_construction_main ($) { Line 4444  sub _tree_construction_main ($) {
4444            !!!cp ('t90');            !!!cp ('t90');
4445            ## As if </noscript>            ## As if </noscript>
4446            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
4447            !!!parse-error (type => 'in noscript:#character');            !!!parse-error (type => 'in noscript:#text', token => $token);
4448                        
4449            ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4450            ## As if </head>            ## As if </head>
# Line 3384  sub _tree_construction_main ($) { Line 4460  sub _tree_construction_main ($) {
4460            !!!cp ('t92');            !!!cp ('t92');
4461          }          }
4462    
4463              ## "after head" insertion mode          ## "after head" insertion mode
4464              ## As if <body>          ## As if <body>
4465              !!!insert-element ('body');          !!!insert-element ('body',, $token);
4466              $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
4467              ## reprocess          ## reprocess
4468              redo B;          next B;
4469            } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
4470              if ($token->{tag_name} eq 'head') {          if ($token->{tag_name} eq 'head') {
4471                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {            if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4472                  !!!cp ('t93');              !!!cp ('t93');
4473                  !!!create-element ($self->{head_element}, $token->{tag_name}, $token->{attributes});              !!!create-element ($self->{head_element}, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
4474                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});              $self->{open_elements}->[-1]->[0]->append_child
4475                  push @{$self->{open_elements}}, [$self->{head_element}, $token->{tag_name}];                  ($self->{head_element});
4476                  $self->{insertion_mode} = IN_HEAD_IM;              push @{$self->{open_elements}},
4477                  !!!next-token;                  [$self->{head_element}, $el_category->{head}];
4478                  redo B;              $self->{insertion_mode} = IN_HEAD_IM;
4479                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {              !!!nack ('t93.1');
4480                  !!!cp ('t94');              !!!next-token;
4481                  #              next B;
4482                } else {            } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4483                  !!!cp ('t95');              !!!cp ('t93.2');
4484                  !!!parse-error (type => 'in head:head'); # or in head noscript              !!!parse-error (type => 'after head', text => 'head',
4485                  ## Ignore the token                              token => $token);
4486                  !!!next-token;              ## Ignore the token
4487                  redo B;              !!!nack ('t93.3');
4488                }              !!!next-token;
4489              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              next B;
4490                !!!cp ('t96');            } else {
4491                ## As if <head>              !!!cp ('t95');
4492                !!!create-element ($self->{head_element}, 'head');              !!!parse-error (type => 'in head:head',
4493                $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                              token => $token); # or in head noscript
4494                push @{$self->{open_elements}}, [$self->{head_element}, 'head'];              ## Ignore the token
4495                !!!nack ('t95.1');
4496                !!!next-token;
4497                next B;
4498              }
4499            } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4500              !!!cp ('t96');
4501              ## As if <head>
4502              !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4503              $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4504              push @{$self->{open_elements}},
4505                  [$self->{head_element}, $el_category->{head}];
4506    
4507                $self->{insertion_mode} = IN_HEAD_IM;            $self->{insertion_mode} = IN_HEAD_IM;
4508                ## Reprocess in the "in head" insertion mode...            ## Reprocess in the "in head" insertion mode...
4509              } else {          } else {
4510                !!!cp ('t97');            !!!cp ('t97');
4511              }          }
4512    
4513              if ($token->{tag_name} eq 'base') {              if ($token->{tag_name} eq 'base') {
4514                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4515                  !!!cp ('t98');                  !!!cp ('t98');
4516                  ## As if </noscript>                  ## As if </noscript>
4517                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4518                  !!!parse-error (type => 'in noscript:base');                  !!!parse-error (type => 'in noscript', text => 'base',
4519                                    token => $token);
4520                                
4521                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4522                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
# Line 3439  sub _tree_construction_main ($) { Line 4527  sub _tree_construction_main ($) {
4527                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4528                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4529                  !!!cp ('t100');                  !!!cp ('t100');
4530                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4531                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4532                    push @{$self->{open_elements}},
4533                        [$self->{head_element}, $el_category->{head}];
4534                } else {                } else {
4535                  !!!cp ('t101');                  !!!cp ('t101');
4536                }                }
4537                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4538                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4539                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4540                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4541                  !!!nack ('t101.1');
4542                !!!next-token;                !!!next-token;
4543                redo B;                next B;
4544              } elsif ($token->{tag_name} eq 'link') {              } elsif ($token->{tag_name} eq 'link') {
4545                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4546                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4547                  !!!cp ('t102');                  !!!cp ('t102');
4548                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4549                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4550                    push @{$self->{open_elements}},
4551                        [$self->{head_element}, $el_category->{head}];
4552                } else {                } else {
4553                  !!!cp ('t103');                  !!!cp ('t103');
4554                }                }
4555                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4556                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.                pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
4557                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4558                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4559                  !!!ack ('t103.1');
4560                !!!next-token;                !!!next-token;
4561                redo B;                next B;
4562              } elsif ($token->{tag_name} eq 'meta') {              } elsif ($token->{tag_name} eq 'meta') {
4563                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4564                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4565                  !!!cp ('t104');                  !!!cp ('t104');
4566                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4567                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4568                    push @{$self->{open_elements}},
4569                        [$self->{head_element}, $el_category->{head}];
4570                } else {                } else {
4571                  !!!cp ('t105');                  !!!cp ('t105');
4572                }                }
4573                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4574                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.
4575    
4576                unless ($self->{confident}) {                unless ($self->{confident}) {
4577                  if ($token->{attributes}->{charset}) { ## TODO: And if supported                  if ($token->{attributes}->{charset}) {
4578                    !!!cp ('t106');                    !!!cp ('t106');
4579                      ## NOTE: Whether the encoding is supported or not is handled
4580                      ## in the {change_encoding} callback.
4581                    $self->{change_encoding}                    $self->{change_encoding}
4582                        ->($self, $token->{attributes}->{charset}->{value});                        ->($self, $token->{attributes}->{charset}->{value},
4583                             $token);
4584                                        
4585                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')                    $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
4586                        ->set_user_data (manakai_has_reference =>                        ->set_user_data (manakai_has_reference =>
4587                                             $token->{attributes}->{charset}                                             $token->{attributes}->{charset}
4588                                                 ->{has_reference});                                                 ->{has_reference});
4589                  } elsif ($token->{attributes}->{content}) {                  } elsif ($token->{attributes}->{content}) {
                   ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
4590                    if ($token->{attributes}->{content}->{value}                    if ($token->{attributes}->{content}->{value}
4591                        =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                        =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
4592                            [\x09-\x0D\x20]*=                            [\x09-\x0D\x20]*=
4593                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                            [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
4594                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                            ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
4595                      !!!cp ('t107');                      !!!cp ('t107');
4596                        ## NOTE: Whether the encoding is supported or not is handled
4597                        ## in the {change_encoding} callback.
4598                      $self->{change_encoding}                      $self->{change_encoding}
4599                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                          ->($self, defined $1 ? $1 : defined $2 ? $2 : $3,
4600                               $token);
4601                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')                      $meta_el->[0]->get_attribute_node_ns (undef, 'content')
4602                          ->set_user_data (manakai_has_reference =>                          ->set_user_data (manakai_has_reference =>
4603                                               $token->{attributes}->{content}                                               $token->{attributes}->{content}
# Line 3524  sub _tree_construction_main ($) { Line 4625  sub _tree_construction_main ($) {
4625    
4626                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4627                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4628                  !!!ack ('t110.1');
4629                !!!next-token;                !!!next-token;
4630                redo B;                next B;
4631              } elsif ($token->{tag_name} eq 'title') {              } elsif ($token->{tag_name} eq 'title') {
4632                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4633                  !!!cp ('t111');                  !!!cp ('t111');
4634                  ## As if </noscript>                  ## As if </noscript>
4635                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4636                  !!!parse-error (type => 'in noscript:title');                  !!!parse-error (type => 'in noscript', text => 'title',
4637                                    token => $token);
4638                                
4639                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4640                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4641                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4642                  !!!cp ('t112');                  !!!cp ('t112');
4643                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4644                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4645                    push @{$self->{open_elements}},
4646                        [$self->{head_element}, $el_category->{head}];
4647                } else {                } else {
4648                  !!!cp ('t113');                  !!!cp ('t113');
4649                }                }
# Line 3549  sub _tree_construction_main ($) { Line 4654  sub _tree_construction_main ($) {
4654                $parse_rcdata->(RCDATA_CONTENT_MODEL);                $parse_rcdata->(RCDATA_CONTENT_MODEL);
4655                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4656                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4657                redo B;                next B;
4658              } elsif ($token->{tag_name} eq 'style') {              } elsif ($token->{tag_name} eq 'style' or
4659                         $token->{tag_name} eq 'noframes') {
4660                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and                ## NOTE: Or (scripting is enabled and tag_name eq 'noscript' and
4661                ## insertion mode IN_HEAD_IM)                ## insertion mode IN_HEAD_IM)
4662                ## NOTE: There is a "as if in head" code clone.                ## NOTE: There is a "as if in head" code clone.
4663                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                if ($self->{insertion_mode} == AFTER_HEAD_IM) {
4664                  !!!cp ('t114');                  !!!cp ('t114');
4665                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4666                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4667                    push @{$self->{open_elements}},
4668                        [$self->{head_element}, $el_category->{head}];
4669                } else {                } else {
4670                  !!!cp ('t115');                  !!!cp ('t115');
4671                }                }
4672                $parse_rcdata->(CDATA_CONTENT_MODEL);                $parse_rcdata->(CDATA_CONTENT_MODEL);
4673                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4674                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4675                redo B;                next B;
4676              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4677                if ($self->{insertion_mode} == IN_HEAD_IM) {                if ($self->{insertion_mode} == IN_HEAD_IM) {
4678                  !!!cp ('t116');                  !!!cp ('t116');
4679                  ## NOTE: and scripting is disalbed                  ## NOTE: and scripting is disalbed
4680                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4681                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;                  $self->{insertion_mode} = IN_HEAD_NOSCRIPT_IM;
4682                    !!!nack ('t116.1');
4683                  !!!next-token;                  !!!next-token;
4684                  redo B;                  next B;
4685                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4686                  !!!cp ('t117');                  !!!cp ('t117');
4687                  !!!parse-error (type => 'in noscript:noscript');                  !!!parse-error (type => 'in noscript', text => 'noscript',
4688                                    token => $token);
4689                  ## Ignore the token                  ## Ignore the token
4690                    !!!nack ('t117.1');
4691                  !!!next-token;                  !!!next-token;
4692                  redo B;                  next B;
4693                } else {                } else {
4694                  !!!cp ('t118');                  !!!cp ('t118');
4695                  #                  #
# Line 3588  sub _tree_construction_main ($) { Line 4699  sub _tree_construction_main ($) {
4699                  !!!cp ('t119');                  !!!cp ('t119');
4700                  ## As if </noscript>                  ## As if </noscript>
4701                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4702                  !!!parse-error (type => 'in noscript:script');                  !!!parse-error (type => 'in noscript', text => 'script',
4703                                    token => $token);
4704                                
4705                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4706                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4707                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4708                  !!!cp ('t120');                  !!!cp ('t120');
4709                  !!!parse-error (type => 'after head:'.$token->{tag_name});                  !!!parse-error (type => 'after head',
4710                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                                  text => $token->{tag_name}, token => $token);
4711                    push @{$self->{open_elements}},
4712                        [$self->{head_element}, $el_category->{head}];
4713                } else {                } else {
4714                  !!!cp ('t121');                  !!!cp ('t121');
4715                }                }
# Line 3604  sub _tree_construction_main ($) { Line 4718  sub _tree_construction_main ($) {
4718                $script_start_tag->();                $script_start_tag->();
4719                pop @{$self->{open_elements}} # <head>                pop @{$self->{open_elements}} # <head>
4720                    if $self->{insertion_mode} == AFTER_HEAD_IM;                    if $self->{insertion_mode} == AFTER_HEAD_IM;
4721                redo B;                next B;
4722              } elsif ($token->{tag_name} eq 'body' or              } elsif ($token->{tag_name} eq 'body' or
4723                       $token->{tag_name} eq 'frameset') {                       $token->{tag_name} eq 'frameset') {
4724                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4725                  !!!cp ('t122');                  !!!cp ('t122');
4726                  ## As if </noscript>                  ## As if </noscript>
4727                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4728                  !!!parse-error (type => 'in noscript:'.$token->{tag_name});                  !!!parse-error (type => 'in noscript',
4729                                    text => $token->{tag_name}, token => $token);
4730                                    
4731                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4732                  ## As if </head>                  ## As if </head>
# Line 3628  sub _tree_construction_main ($) { Line 4743  sub _tree_construction_main ($) {
4743                }                }
4744    
4745                ## "after head" insertion mode                ## "after head" insertion mode
4746                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
4747                if ($token->{tag_name} eq 'body') {                if ($token->{tag_name} eq 'body') {
4748                  !!!cp ('t126');                  !!!cp ('t126');
4749                  $self->{insertion_mode} = IN_BODY_IM;                  $self->{insertion_mode} = IN_BODY_IM;
# Line 3638  sub _tree_construction_main ($) { Line 4753  sub _tree_construction_main ($) {
4753                } else {                } else {
4754                  die "$0: tag name: $self->{tag_name}";                  die "$0: tag name: $self->{tag_name}";
4755                }                }
4756                  !!!nack ('t127.1');
4757                !!!next-token;                !!!next-token;
4758                redo B;                next B;
4759              } else {              } else {
4760                !!!cp ('t128');                !!!cp ('t128');
4761                #                #
# Line 3649  sub _tree_construction_main ($) { Line 4765  sub _tree_construction_main ($) {
4765                !!!cp ('t129');                !!!cp ('t129');
4766                ## As if </noscript>                ## As if </noscript>
4767                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4768                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4769                                  text => $token->{tag_name}, token => $token);
4770                                
4771                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4772                ## As if </head>                ## As if </head>
# Line 3668  sub _tree_construction_main ($) { Line 4785  sub _tree_construction_main ($) {
4785    
4786              ## "after head" insertion mode              ## "after head" insertion mode
4787              ## As if <body>              ## As if <body>
4788              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4789              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4790              ## reprocess              ## reprocess
4791              redo B;              !!!ack-later;
4792                next B;
4793            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
4794              if ($token->{tag_name} eq 'head') {              if ($token->{tag_name} eq 'head') {
4795                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4796                  !!!cp ('t132');                  !!!cp ('t132');
4797                  ## As if <head>                  ## As if <head>
4798                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4799                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4800                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  push @{$self->{open_elements}},
4801                        [$self->{head_element}, $el_category->{head}];
4802    
4803                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4804                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4805                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4806                  !!!next-token;                  !!!next-token;
4807                  redo B;                  next B;
4808                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4809                  !!!cp ('t133');                  !!!cp ('t133');
4810                  ## As if </noscript>                  ## As if </noscript>
4811                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4812                  !!!parse-error (type => 'in noscript:/head');                  !!!parse-error (type => 'in noscript:/',
4813                                    text => 'head', token => $token);
4814                                    
4815                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4816                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4817                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4818                  !!!next-token;                  !!!next-token;
4819                  redo B;                  next B;
4820                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {                } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4821                  !!!cp ('t134');                  !!!cp ('t134');
4822                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4823                  $self->{insertion_mode} = AFTER_HEAD_IM;                  $self->{insertion_mode} = AFTER_HEAD_IM;
4824                  !!!next-token;                  !!!next-token;
4825                  redo B;                  next B;
4826                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4827                    !!!cp ('t134.1');
4828                    !!!parse-error (type => 'unmatched end tag', text => 'head',
4829                                    token => $token);
4830                    ## Ignore the token
4831                    !!!next-token;
4832                    next B;
4833                } else {                } else {
4834                  !!!cp ('t135');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
                 #  
4835                }                }
4836              } elsif ($token->{tag_name} eq 'noscript') {              } elsif ($token->{tag_name} eq 'noscript') {
4837                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {                if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
# Line 3713  sub _tree_construction_main ($) { Line 4839  sub _tree_construction_main ($) {
4839                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
4840                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4841                  !!!next-token;                  !!!next-token;
4842                  redo B;                  next B;
4843                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {                } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM or
4844                           $self->{insertion_mode} == AFTER_HEAD_IM) {
4845                  !!!cp ('t137');                  !!!cp ('t137');
4846                  !!!parse-error (type => 'unmatched end tag:noscript');                  !!!parse-error (type => 'unmatched end tag',
4847                                    text => 'noscript', token => $token);
4848                  ## Ignore the token ## ISSUE: An issue in the spec.                  ## Ignore the token ## ISSUE: An issue in the spec.
4849                  !!!next-token;                  !!!next-token;
4850                  redo B;                  next B;
4851                } else {                } else {
4852                  !!!cp ('t138');                  !!!cp ('t138');
4853                  #                  #
# Line 3727  sub _tree_construction_main ($) { Line 4855  sub _tree_construction_main ($) {
4855              } elsif ({              } elsif ({
4856                        body => 1, html => 1,                        body => 1, html => 1,
4857                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
4858                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM or
4859                  !!!cp ('t139');                    $self->{insertion_mode} == IN_HEAD_IM or
4860                  ## 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) {  
4861                  !!!cp ('t140');                  !!!cp ('t140');
4862                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
4863                                    text => $token->{tag_name}, token => $token);
4864                  ## Ignore the token                  ## Ignore the token
4865                  !!!next-token;                  !!!next-token;
4866                  redo B;                  next B;
4867                  } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4868                    !!!cp ('t140.1');
4869                    !!!parse-error (type => 'unmatched end tag',
4870                                    text => $token->{tag_name}, token => $token);
4871                    ## Ignore the token
4872                    !!!next-token;
4873                    next B;
4874                } else {                } else {
4875                  !!!cp ('t141');                  die "$0: $self->{insertion_mode}: Unknown insertion mode";
4876                }                }
4877                              } elsif ($token->{tag_name} eq 'p') {
4878                #                !!!cp ('t142');
4879              } elsif ({                !!!parse-error (type => 'unmatched end tag',
4880                        p => 1, br => 1,                                text => $token->{tag_name}, token => $token);
4881                       }->{$token->{tag_name}}) {                ## Ignore the token
4882                  !!!next-token;
4883                  next B;
4884                } elsif ($token->{tag_name} eq 'br') {
4885                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {                if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4886                  !!!cp ('t142');                  !!!cp ('t142.2');
4887                  ## As if <head>                  ## (before head) as if <head>, (in head) as if </head>
4888                  !!!create-element ($self->{head_element}, 'head');                  !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4889                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});                  $self->{open_elements}->[-1]->[0]->append_child ($self->{head_element});
4890                  push @{$self->{open_elements}}, [$self->{head_element}, 'head'];                  $self->{insertion_mode} = AFTER_HEAD_IM;
4891      
4892                    ## Reprocess in the "after head" insertion mode...
4893                  } elsif ($self->{insertion_mode} == IN_HEAD_IM) {
4894                    !!!cp ('t143.2');
4895                    ## As if </head>
4896                    pop @{$self->{open_elements}};
4897                    $self->{insertion_mode} = AFTER_HEAD_IM;
4898      
4899                    ## Reprocess in the "after head" insertion mode...
4900                  } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4901                    !!!cp ('t143.3');
4902                    ## ISSUE: Two parse errors for <head><noscript></br>
4903                    !!!parse-error (type => 'unmatched end tag',
4904                                    text => 'br', token => $token);
4905                    ## As if </noscript>
4906                    pop @{$self->{open_elements}};
4907                  $self->{insertion_mode} = IN_HEAD_IM;                  $self->{insertion_mode} = IN_HEAD_IM;
4908    
4909                  ## Reprocess in the "in head" insertion mode...                  ## Reprocess in the "in head" insertion mode...
4910                } else {                  ## As if </head>
4911                  !!!cp ('t143');                  pop @{$self->{open_elements}};
4912                }                  $self->{insertion_mode} = AFTER_HEAD_IM;
4913    
4914                #                  ## Reprocess in the "after head" insertion mode...
4915              } else {                } elsif ($self->{insertion_mode} == AFTER_HEAD_IM) {
4916                if ($self->{insertion_mode} == AFTER_HEAD_IM) {                  !!!cp ('t143.4');
                 !!!cp ('t144');  
4917                  #                  #
4918                } else {                } else {
4919                  !!!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;  
4920                }                }
4921    
4922                  ## ISSUE: does not agree with IE7 - it doesn't ignore </br>.
4923                  !!!parse-error (type => 'unmatched end tag',
4924                                  text => 'br', token => $token);
4925                  ## Ignore the token
4926                  !!!next-token;
4927                  next B;
4928                } else {
4929                  !!!cp ('t145');
4930                  !!!parse-error (type => 'unmatched end tag',
4931                                  text => $token->{tag_name}, token => $token);
4932                  ## Ignore the token
4933                  !!!next-token;
4934                  next B;
4935              }              }
4936    
4937              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {              if ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
4938                !!!cp ('t146');                !!!cp ('t146');
4939                ## As if </noscript>                ## As if </noscript>
4940                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
4941                !!!parse-error (type => 'in noscript:/'.$token->{tag_name});                !!!parse-error (type => 'in noscript:/',
4942                                  text => $token->{tag_name}, token => $token);
4943                                
4944                ## Reprocess in the "in head" insertion mode...                ## Reprocess in the "in head" insertion mode...
4945                ## As if </head>                ## As if </head>
# Line 3797  sub _tree_construction_main ($) { Line 4955  sub _tree_construction_main ($) {
4955              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {              } elsif ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4956  ## ISSUE: This case cannot be reached?  ## ISSUE: This case cannot be reached?
4957                !!!cp ('t148');                !!!cp ('t148');
4958                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
4959                                  text => $token->{tag_name}, token => $token);
4960                ## Ignore the token ## ISSUE: An issue in the spec.                ## Ignore the token ## ISSUE: An issue in the spec.
4961                !!!next-token;                !!!next-token;
4962                redo B;                next B;
4963              } else {              } else {
4964                !!!cp ('t149');                !!!cp ('t149');
4965              }              }
4966    
4967              ## "after head" insertion mode              ## "after head" insertion mode
4968              ## As if <body>              ## As if <body>
4969              !!!insert-element ('body');              !!!insert-element ('body',, $token);
4970              $self->{insertion_mode} = IN_BODY_IM;              $self->{insertion_mode} = IN_BODY_IM;
4971              ## reprocess              ## reprocess
4972              redo B;              next B;
4973        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
4974          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {          if ($self->{insertion_mode} == BEFORE_HEAD_IM) {
4975            !!!cp ('t149.1');            !!!cp ('t149.1');
4976    
4977            ## NOTE: As if <head>            ## NOTE: As if <head>
4978            !!!create-element ($self->{head_element}, 'head');            !!!create-element ($self->{head_element}, $HTML_NS, 'head',, $token);
4979            $self->{open_elements}->[-1]->[0]->append_child            $self->{open_elements}->[-1]->[0]->append_child
4980                ($self->{head_element});                ($self->{head_element});
4981            #push @{$self->{open_elements}}, [$self->{head_element}, 'head'];            #push @{$self->{open_elements}},
4982              #    [$self->{head_element}, $el_category->{head}];
4983            #$self->{insertion_mode} = IN_HEAD_IM;            #$self->{insertion_mode} = IN_HEAD_IM;
4984            ## NOTE: Reprocess.            ## NOTE: Reprocess.
4985    
# Line 3841  sub _tree_construction_main ($) { Line 5001  sub _tree_construction_main ($) {
5001          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {          } elsif ($self->{insertion_mode} == IN_HEAD_NOSCRIPT_IM) {
5002            !!!cp ('t149.3');            !!!cp ('t149.3');
5003    
5004            !!!parse-error (type => 'in noscript:#eof');            !!!parse-error (type => 'in noscript:#eof', token => $token);
5005    
5006            ## As if </noscript>            ## As if </noscript>
5007            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
# Line 3860  sub _tree_construction_main ($) { Line 5020  sub _tree_construction_main ($) {
5020          }          }
5021    
5022          ## NOTE: As if <body>          ## NOTE: As if <body>
5023          !!!insert-element ('body');          !!!insert-element ('body',, $token);
5024          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
5025          ## NOTE: Reprocess.          ## NOTE: Reprocess.
5026          redo B;          next B;
5027        } else {        } else {
5028          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
5029        }        }
# Line 3878  sub _tree_construction_main ($) { Line 5038  sub _tree_construction_main ($) {
5038              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});              $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
5039    
5040              !!!next-token;              !!!next-token;
5041              redo B;              next B;
5042            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
5043              if ({              if ({
5044                   caption => 1, col => 1, colgroup => 1, tbody => 1,                   caption => 1, col => 1, colgroup => 1, tbody => 1,
# Line 3886  sub _tree_construction_main ($) { Line 5046  sub _tree_construction_main ($) {
5046                  }->{$token->{tag_name}}) {                  }->{$token->{tag_name}}) {
5047                if ($self->{insertion_mode} == IN_CELL_IM) {                if ($self->{insertion_mode} == IN_CELL_IM) {
5048                  ## have an element in table scope                  ## have an element in table scope
5049                  my $tn;                  for (reverse 0..$#{$self->{open_elements}}) {
                 INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {  
5050                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5051                    if ($node->[1] eq 'td' or $node->[1] eq 'th') {                    if ($node->[1] & TABLE_CELL_EL) {
5052                      !!!cp ('t151');                      !!!cp ('t151');
5053                      $tn = $node->[1];  
5054                      last INSCOPE;                      ## Close the cell
5055                    } elsif ({                      !!!back-token; # <x>
5056                              table => 1, html => 1,                      $token = {type => END_TAG_TOKEN,
5057                             }->{$node->[1]}) {                                tag_name => $node->[0]->manakai_local_name,
5058                                  line => $token->{line},
5059                                  column => $token->{column}};
5060                        next B;
5061                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5062                      !!!cp ('t152');                      !!!cp ('t152');
5063                      last INSCOPE;                      ## ISSUE: This case can never be reached, maybe.
5064                        last;
5065                    }                    }
5066                  } # INSCOPE                  }
5067                    unless (defined $tn) {  
5068                      !!!cp ('t153');                  !!!cp ('t153');
5069  ## TODO: This error type is wrong.                  !!!parse-error (type => 'start tag not allowed',
5070                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      text => $token->{tag_name}, token => $token);
5071                      ## Ignore the token                  ## Ignore the token
5072                      !!!next-token;                  !!!nack ('t153.1');
5073                      redo B;                  !!!next-token;
5074                    }                  next B;
                   
                 !!!cp ('t154');  
                 ## Close the cell  
                 !!!back-token; # <?>  
                 $token = {type => END_TAG_TOKEN, tag_name => $tn};  
                 redo B;  
5075                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5076                  !!!parse-error (type => 'not closed:caption');                  !!!parse-error (type => 'not closed', text => 'caption',
5077                                    token => $token);
5078                                    
5079                  ## As if </caption>                  ## NOTE: As if </caption>.
5080                  ## have a table element in table scope                  ## have a table element in table scope
5081                  my $i;                  my $i;
5082                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5083                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5084                    if ($node->[1] eq 'caption') {                      my $node = $self->{open_elements}->[$_];
5085                      !!!cp ('t155');                      if ($node->[1] & CAPTION_EL) {
5086                      $i = $_;                        !!!cp ('t155');
5087                      last INSCOPE;                        $i = $_;
5088                    } elsif ({                        last INSCOPE;
5089                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5090                             }->{$node->[1]}) {                        !!!cp ('t156');
5091                      !!!cp ('t156');                        last;
5092                      last INSCOPE;                      }
5093                    }                    }
5094    
5095                      !!!cp ('t157');
5096                      !!!parse-error (type => 'start tag not allowed',
5097                                      text => $token->{tag_name}, token => $token);
5098                      ## Ignore the token
5099                      !!!nack ('t157.1');
5100                      !!!next-token;
5101                      next B;
5102                  } # 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;  
                   }  
5103                                    
5104                  ## generate implied end tags                  ## generate implied end tags
5105                  while ({                  while ($self->{open_elements}->[-1]->[1]
5106                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5107                    !!!cp ('t158');                    !!!cp ('t158');
5108                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5109                  }                  }
5110    
5111                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5112                    !!!cp ('t159');                    !!!cp ('t159');
5113                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5114                                      text => $self->{open_elements}->[-1]->[0]
5115                                          ->manakai_local_name,
5116                                      token => $token);
5117                  } else {                  } else {
5118                    !!!cp ('t160');                    !!!cp ('t160');
5119                  }                  }
# Line 3964  sub _tree_construction_main ($) { Line 5125  sub _tree_construction_main ($) {
5125                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5126                                    
5127                  ## reprocess                  ## reprocess
5128                  redo B;                  !!!ack-later;
5129                    next B;
5130                } else {                } else {
5131                  !!!cp ('t161');                  !!!cp ('t161');
5132                  #                  #
# Line 3980  sub _tree_construction_main ($) { Line 5142  sub _tree_construction_main ($) {
5142                  my $i;                  my $i;
5143                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5144                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5145                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5146                      !!!cp ('t163');                      !!!cp ('t163');
5147                      $i = $_;                      $i = $_;
5148                      last INSCOPE;                      last INSCOPE;
5149                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5150                      !!!cp ('t164');                      !!!cp ('t164');
5151                      last INSCOPE;                      last INSCOPE;
5152                    }                    }
5153                  } # INSCOPE                  } # INSCOPE
5154                    unless (defined $i) {                    unless (defined $i) {
5155                      !!!cp ('t165');                      !!!cp ('t165');
5156                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5157                                        text => $token->{tag_name},
5158                                        token => $token);
5159                      ## Ignore the token                      ## Ignore the token
5160                      !!!next-token;                      !!!next-token;
5161                      redo B;                      next B;
5162                    }                    }
5163                                    
5164                  ## generate implied end tags                  ## generate implied end tags
5165                  while ({                  while ($self->{open_elements}->[-1]->[1]
5166                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5167                    !!!cp ('t166');                    !!!cp ('t166');
5168                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5169                  }                  }
5170    
5171                  if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {                  if ($self->{open_elements}->[-1]->[0]->manakai_local_name
5172                            ne $token->{tag_name}) {
5173                    !!!cp ('t167');                    !!!cp ('t167');
5174                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5175                                      text => $self->{open_elements}->[-1]->[0]
5176                                          ->manakai_local_name,
5177                                      token => $token);
5178                  } else {                  } else {
5179                    !!!cp ('t168');                    !!!cp ('t168');
5180                  }                  }
# Line 4021  sub _tree_construction_main ($) { Line 5186  sub _tree_construction_main ($) {
5186                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5187                                    
5188                  !!!next-token;                  !!!next-token;
5189                  redo B;                  next B;
5190                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {                } elsif ($self->{insertion_mode} == IN_CAPTION_IM) {
5191                  !!!cp ('t169');                  !!!cp ('t169');
5192                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5193                                    text => $token->{tag_name}, token => $token);
5194                  ## Ignore the token                  ## Ignore the token
5195                  !!!next-token;                  !!!next-token;
5196                  redo B;                  next B;
5197                } else {                } else {
5198                  !!!cp ('t170');                  !!!cp ('t170');
5199                  #                  #
# Line 4036  sub _tree_construction_main ($) { Line 5202  sub _tree_construction_main ($) {
5202                if ($self->{insertion_mode} == IN_CAPTION_IM) {                if ($self->{insertion_mode} == IN_CAPTION_IM) {
5203                  ## have a table element in table scope                  ## have a table element in table scope
5204                  my $i;                  my $i;
5205                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: {
5206                    my $node = $self->{open_elements}->[$_];                    for (reverse 0..$#{$self->{open_elements}}) {
5207                    if ($node->[1] eq $token->{tag_name}) {                      my $node = $self->{open_elements}->[$_];
5208                      !!!cp ('t171');                      if ($node->[1] & CAPTION_EL) {
5209                      $i = $_;                        !!!cp ('t171');
5210                      last INSCOPE;                        $i = $_;
5211                    } elsif ({                        last INSCOPE;
5212                              table => 1, html => 1,                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5213                             }->{$node->[1]}) {                        !!!cp ('t172');
5214                      !!!cp ('t172');                        last;
5215                      last INSCOPE;                      }
5216                    }                    }
5217    
5218                      !!!cp ('t173');
5219                      !!!parse-error (type => 'unmatched end tag',
5220                                      text => $token->{tag_name}, token => $token);
5221                      ## Ignore the token
5222                      !!!next-token;
5223                      next B;
5224                  } # INSCOPE                  } # INSCOPE
                   unless (defined $i) {  
                     !!!cp ('t173');  
                     !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
                     ## Ignore the token  
                     !!!next-token;  
                     redo B;  
                   }  
5225                                    
5226                  ## generate implied end tags                  ## generate implied end tags
5227                  while ({                  while ($self->{open_elements}->[-1]->[1]
5228                          dd => 1, dt => 1, li => 1, p => 1,                             & END_TAG_OPTIONAL_EL) {
                        }->{$self->{open_elements}->[-1]->[1]}) {  
5229                    !!!cp ('t174');                    !!!cp ('t174');
5230                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5231                  }                  }
5232                                    
5233                  if ($self->{open_elements}->[-1]->[1] ne 'caption') {                  unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5234                    !!!cp ('t175');                    !!!cp ('t175');
5235                    !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                    !!!parse-error (type => 'not closed',
5236                                      text => $self->{open_elements}->[-1]->[0]
5237                                          ->manakai_local_name,
5238                                      token => $token);
5239                  } else {                  } else {
5240                    !!!cp ('t176');                    !!!cp ('t176');
5241                  }                  }
# Line 4079  sub _tree_construction_main ($) { Line 5247  sub _tree_construction_main ($) {
5247                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
5248                                    
5249                  !!!next-token;                  !!!next-token;
5250                  redo B;                  next B;
5251                } elsif ($self->{insertion_mode} == IN_CELL_IM) {                } elsif ($self->{insertion_mode} == IN_CELL_IM) {
5252                  !!!cp ('t177');                  !!!cp ('t177');
5253                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5254                                    text => $token->{tag_name}, token => $token);
5255                  ## Ignore the token                  ## Ignore the token
5256                  !!!next-token;                  !!!next-token;
5257                  redo B;                  next B;
5258                } else {                } else {
5259                  !!!cp ('t178');                  !!!cp ('t178');
5260                  #                  #
# Line 4098  sub _tree_construction_main ($) { Line 5267  sub _tree_construction_main ($) {
5267                ## have an element in table scope                ## have an element in table scope
5268                my $i;                my $i;
5269                my $tn;                my $tn;
5270                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: {
5271                  my $node = $self->{open_elements}->[$_];                  for (reverse 0..$#{$self->{open_elements}}) {
5272                  if ($node->[1] eq $token->{tag_name}) {                    my $node = $self->{open_elements}->[$_];
5273                    !!!cp ('t179');                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5274                    $i = $_;                      !!!cp ('t179');
5275                    last INSCOPE;                      $i = $_;
5276                  } elsif ($node->[1] eq 'td' or $node->[1] eq 'th') {  
5277                    !!!cp ('t180');                      ## Close the cell
5278                    $tn = $node->[1];                      !!!back-token; # </x>
5279                    ## NOTE: There is exactly one |td| or |th| element                      $token = {type => END_TAG_TOKEN, tag_name => $tn,
5280                    ## in scope in the stack of open elements by definition.                                line => $token->{line},
5281                  } elsif ({                                column => $token->{column}};
5282                            table => 1, html => 1,                      next B;
5283                           }->{$node->[1]}) {                    } elsif ($node->[1] & TABLE_CELL_EL) {
5284                    !!!cp ('t181');                      !!!cp ('t180');
5285                    last INSCOPE;                      $tn = $node->[0]->manakai_local_name;
5286                        ## NOTE: There is exactly one |td| or |th| element
5287                        ## in scope in the stack of open elements by definition.
5288                      } elsif ($node->[1] & TABLE_SCOPING_EL) {
5289                        ## ISSUE: Can this be reached?
5290                        !!!cp ('t181');
5291                        last;
5292                      }
5293                  }                  }
5294                } # INSCOPE  
               unless (defined $i) {  
5295                  !!!cp ('t182');                  !!!cp ('t182');
5296                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5297                        text => $token->{tag_name}, token => $token);
5298                  ## Ignore the token                  ## Ignore the token
5299                  !!!next-token;                  !!!next-token;
5300                  redo B;                  next B;
5301                } else {                } # INSCOPE
                 !!!cp ('t183');  
               }  
   
               ## Close the cell  
               !!!back-token; # </?>  
               $token = {type => END_TAG_TOKEN, tag_name => $tn};  
               redo B;  
5302              } elsif ($token->{tag_name} eq 'table' and              } elsif ($token->{tag_name} eq 'table' and
5303                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5304                !!!parse-error (type => 'not closed:caption');                !!!parse-error (type => 'not closed', text => 'caption',
5305                                  token => $token);
5306    
5307                ## As if </caption>                ## As if </caption>
5308                ## have a table element in table scope                ## have a table element in table scope
5309                my $i;                my $i;
5310                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5311                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5312                  if ($node->[1] eq 'caption') {                  if ($node->[1] & CAPTION_EL) {
5313                    !!!cp ('t184');                    !!!cp ('t184');
5314                    $i = $_;                    $i = $_;
5315                    last INSCOPE;                    last INSCOPE;
5316                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5317                    !!!cp ('t185');                    !!!cp ('t185');
5318                    last INSCOPE;                    last INSCOPE;
5319                  }                  }
5320                } # INSCOPE                } # INSCOPE
5321                unless (defined $i) {                unless (defined $i) {
5322                  !!!cp ('t186');                  !!!cp ('t186');
5323                  !!!parse-error (type => 'unmatched end tag:caption');                  !!!parse-error (type => 'unmatched end tag',
5324                                    text => 'caption', token => $token);
5325                  ## Ignore the token                  ## Ignore the token
5326                  !!!next-token;                  !!!next-token;
5327                  redo B;                  next B;
5328                }                }
5329                                
5330                ## generate implied end tags                ## generate implied end tags
5331                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5332                  !!!cp ('t187');                  !!!cp ('t187');
5333                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5334                }                }
5335    
5336                if ($self->{open_elements}->[-1]->[1] ne 'caption') {                unless ($self->{open_elements}->[-1]->[1] & CAPTION_EL) {
5337                  !!!cp ('t188');                  !!!cp ('t188');
5338                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5339                                    text => $self->{open_elements}->[-1]->[0]
5340                                        ->manakai_local_name,
5341                                    token => $token);
5342                } else {                } else {
5343                  !!!cp ('t189');                  !!!cp ('t189');
5344                }                }
# Line 4180  sub _tree_construction_main ($) { Line 5350  sub _tree_construction_main ($) {
5350                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
5351    
5352                ## reprocess                ## reprocess
5353                redo B;                next B;
5354              } elsif ({              } elsif ({
5355                        body => 1, col => 1, colgroup => 1, html => 1,                        body => 1, col => 1, colgroup => 1, html => 1,
5356                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
5357                if ($self->{insertion_mode} & BODY_TABLE_IMS) {                if ($self->{insertion_mode} & BODY_TABLE_IMS) {
5358                  !!!cp ('t190');                  !!!cp ('t190');
5359                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5360                                    text => $token->{tag_name}, token => $token);
5361                  ## Ignore the token                  ## Ignore the token
5362                  !!!next-token;                  !!!next-token;
5363                  redo B;                  next B;
5364                } else {                } else {
5365                  !!!cp ('t191');                  !!!cp ('t191');
5366                  #                  #
# Line 4200  sub _tree_construction_main ($) { Line 5371  sub _tree_construction_main ($) {
5371                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
5372                       $self->{insertion_mode} == IN_CAPTION_IM) {                       $self->{insertion_mode} == IN_CAPTION_IM) {
5373                !!!cp ('t192');                !!!cp ('t192');
5374                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
5375                                  text => $token->{tag_name}, token => $token);
5376                ## Ignore the token                ## Ignore the token
5377                !!!next-token;                !!!next-token;
5378                redo B;                next B;
5379              } else {              } else {
5380                !!!cp ('t193');                !!!cp ('t193');
5381                #                #
5382              }              }
5383        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
5384          for my $entry (@{$self->{open_elements}}) {          for my $entry (@{$self->{open_elements}}) {
5385            if (not {            unless ($entry->[1] & ALL_END_TAG_OPTIONAL_EL) {
             dd => 1, dt => 1, li => 1, p => 1, tbody => 1, td => 1, tfoot => 1,  
             th => 1, thead => 1, tr => 1, body => 1, html => 1,  
           }->{$entry->[1]}) {  
5386              !!!cp ('t75');              !!!cp ('t75');
5387              !!!parse-error (type => 'in body:#eof');              !!!parse-error (type => 'in body:#eof', token => $token);
5388              last;              last;
5389            }            }
5390          }          }
# Line 4237  sub _tree_construction_main ($) { Line 5406  sub _tree_construction_main ($) {
5406            unless (length $token->{data}) {            unless (length $token->{data}) {
5407              !!!cp ('t194');              !!!cp ('t194');
5408              !!!next-token;              !!!next-token;
5409              redo B;              next B;
5410            } else {            } else {
5411              !!!cp ('t195');              !!!cp ('t195');
5412            }            }
5413          }          }
5414    
5415              !!!parse-error (type => 'in table:#character');          !!!parse-error (type => 'in table:#text', token => $token);
5416    
5417              ## As if in body, but insert into foster parent element              ## As if in body, but insert into foster parent element
5418              ## ISSUE: Spec says that "whenever a node would be inserted              ## ISSUE: Spec says that "whenever a node would be inserted
# Line 4251  sub _tree_construction_main ($) { Line 5420  sub _tree_construction_main ($) {
5420              ## result in a new Text node.              ## result in a new Text node.
5421              $reconstruct_active_formatting_elements->($insert_to_foster);              $reconstruct_active_formatting_elements->($insert_to_foster);
5422                            
5423              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]}) {  
5424                # MUST                # MUST
5425                my $foster_parent_element;                my $foster_parent_element;
5426                my $next_sibling;                my $next_sibling;
5427                my $prev_sibling;                my $prev_sibling;
5428                OE: for (reverse 0..$#{$self->{open_elements}}) {                OE: for (reverse 0..$#{$self->{open_elements}}) {
5429                  if ($self->{open_elements}->[$_]->[1] eq 'table') {                  if ($self->{open_elements}->[$_]->[1] & TABLE_EL) {
5430                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;                    my $parent = $self->{open_elements}->[$_]->[0]->parent_node;
5431                    if (defined $parent and $parent->node_type == 1) {                    if (defined $parent and $parent->node_type == 1) {
5432                      !!!cp ('t196');                      !!!cp ('t196');
# Line 4295  sub _tree_construction_main ($) { Line 5461  sub _tree_construction_main ($) {
5461          }          }
5462                            
5463          !!!next-token;          !!!next-token;
5464          redo B;          next B;
5465        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
5466              if ({          if ({
5467                   tr => ($self->{insertion_mode} != IN_ROW_IM),               tr => ($self->{insertion_mode} != IN_ROW_IM),
5468                   th => 1, td => 1,               th => 1, td => 1,
5469                  }->{$token->{tag_name}}) {              }->{$token->{tag_name}}) {
5470                if ($self->{insertion_mode} == IN_TABLE_IM) {            if ($self->{insertion_mode} == IN_TABLE_IM) {
5471                  ## Clear back to table context              ## Clear back to table context
5472                  while ($self->{open_elements}->[-1]->[1] ne 'table' and              while (not ($self->{open_elements}->[-1]->[1]
5473                         $self->{open_elements}->[-1]->[1] ne 'html') {                              & TABLE_SCOPING_EL)) {
5474                    !!!cp ('t201');                !!!cp ('t201');
5475                    pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
5476                  }              }
5477                                
5478                  !!!insert-element ('tbody');              !!!insert-element ('tbody',, $token);
5479                  $self->{insertion_mode} = IN_TABLE_BODY_IM;              $self->{insertion_mode} = IN_TABLE_BODY_IM;
5480                  ## reprocess in the "in table body" insertion mode...              ## reprocess in the "in table body" insertion mode...
5481                }            }
5482              
5483                if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {            if ($self->{insertion_mode} == IN_TABLE_BODY_IM) {
5484                  unless ($token->{tag_name} eq 'tr') {              unless ($token->{tag_name} eq 'tr') {
5485                    !!!cp ('t202');                !!!cp ('t202');
5486                    !!!parse-error (type => 'missing start tag:tr');                !!!parse-error (type => 'missing start tag:tr', token => $token);
5487                  }              }
5488                                    
5489                  ## Clear back to table body context              ## Clear back to table body context
5490                  while (not {              while (not ($self->{open_elements}->[-1]->[1]
5491                    tbody => 1, tfoot => 1, thead => 1, html => 1,                              & TABLE_ROWS_SCOPING_EL)) {
5492                  }->{$self->{open_elements}->[-1]->[1]}) {                !!!cp ('t203');
5493                    !!!cp ('t203');                ## ISSUE: Can this case be reached?
5494                    ## ISSUE: Can this case be reached?                pop @{$self->{open_elements}};
5495                    pop @{$self->{open_elements}};              }
                 }  
5496                                    
5497                  $self->{insertion_mode} = IN_ROW_IM;                  $self->{insertion_mode} = IN_ROW_IM;
5498                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5499                    !!!cp ('t204');                    !!!cp ('t204');
5500                    !!!insert-element ($token->{tag_name}, $token->{attributes});                    !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5501                      !!!nack ('t204');
5502                    !!!next-token;                    !!!next-token;
5503                    redo B;                    next B;
5504                  } else {                  } else {
5505                    !!!cp ('t205');                    !!!cp ('t205');
5506                    !!!insert-element ('tr');                    !!!insert-element ('tr',, $token);
5507                    ## reprocess in the "in row" insertion mode                    ## reprocess in the "in row" insertion mode
5508                  }                  }
5509                } else {                } else {
# Line 4345  sub _tree_construction_main ($) { Line 5511  sub _tree_construction_main ($) {
5511                }                }
5512    
5513                ## Clear back to table row context                ## Clear back to table row context
5514                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5515                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5516                  !!!cp ('t207');                  !!!cp ('t207');
5517                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5518                }                }
5519                                
5520                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5521                $self->{insertion_mode} = IN_CELL_IM;                $self->{insertion_mode} = IN_CELL_IM;
5522    
5523                push @$active_formatting_elements, ['#marker', ''];                push @$active_formatting_elements, ['#marker', ''];
5524                                
5525                  !!!nack ('t207.1');
5526                !!!next-token;                !!!next-token;
5527                redo B;                next B;
5528              } elsif ({              } elsif ({
5529                        caption => 1, col => 1, colgroup => 1,                        caption => 1, col => 1, colgroup => 1,
5530                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
# Line 4370  sub _tree_construction_main ($) { Line 5536  sub _tree_construction_main ($) {
5536                  my $i;                  my $i;
5537                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5538                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5539                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5540                      !!!cp ('t208');                      !!!cp ('t208');
5541                      $i = $_;                      $i = $_;
5542                      last INSCOPE;                      last INSCOPE;
5543                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             html => 1,  
   
                             ## NOTE: This element does not appear here, maybe.  
                             table => 1,  
                            }->{$node->[1]}) {  
5544                      !!!cp ('t209');                      !!!cp ('t209');
5545                      last INSCOPE;                      last INSCOPE;
5546                    }                    }
5547                  } # INSCOPE                  } # INSCOPE
5548                  unless (defined $i) {                  unless (defined $i) {
5549                   !!!cp ('t210');                    !!!cp ('t210');
5550  ## TODO: This type is wrong.  ## TODO: This type is wrong.
5551                   !!!parse-error (type => 'unmacthed end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmacthed end tag',
5552                                      text => $token->{tag_name}, token => $token);
5553                    ## Ignore the token                    ## Ignore the token
5554                      !!!nack ('t210.1');
5555                    !!!next-token;                    !!!next-token;
5556                    redo B;                    next B;
5557                  }                  }
5558                                    
5559                  ## Clear back to table row context                  ## Clear back to table row context
5560                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5561                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5562                    !!!cp ('t211');                    !!!cp ('t211');
5563                    ## ISSUE: Can this case be reached?                    ## ISSUE: Can this case be reached?
5564                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4407  sub _tree_construction_main ($) { Line 5569  sub _tree_construction_main ($) {
5569                  if ($token->{tag_name} eq 'tr') {                  if ($token->{tag_name} eq 'tr') {
5570                    !!!cp ('t212');                    !!!cp ('t212');
5571                    ## reprocess                    ## reprocess
5572                    redo B;                    !!!ack-later;
5573                      next B;
5574                  } else {                  } else {
5575                    !!!cp ('t213');                    !!!cp ('t213');
5576                    ## reprocess in the "in table body" insertion mode...                    ## reprocess in the "in table body" insertion mode...
# Line 4419  sub _tree_construction_main ($) { Line 5582  sub _tree_construction_main ($) {
5582                  my $i;                  my $i;
5583                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5584                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5585                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5586                      !!!cp ('t214');                      !!!cp ('t214');
5587                      $i = $_;                      $i = $_;
5588                      last INSCOPE;                      last INSCOPE;
5589                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5590                      !!!cp ('t215');                      !!!cp ('t215');
5591                      last INSCOPE;                      last INSCOPE;
5592                    }                    }
5593                  } # INSCOPE                  } # INSCOPE
5594                  unless (defined $i) {                  unless (defined $i) {
5595                    !!!cp ('t216');                    !!!cp ('t216');
5596  ## TODO: This erorr type ios wrong.  ## TODO: This erorr type is wrong.
5597                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5598                                      text => $token->{tag_name}, token => $token);
5599                    ## Ignore the token                    ## Ignore the token
5600                      !!!nack ('t216.1');
5601                    !!!next-token;                    !!!next-token;
5602                    redo B;                    next B;
5603                  }                  }
5604    
5605                  ## Clear back to table body context                  ## Clear back to table body context
5606                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5607                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5608                    !!!cp ('t217');                    !!!cp ('t217');
5609                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5610                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4466  sub _tree_construction_main ($) { Line 5626  sub _tree_construction_main ($) {
5626    
5627                if ($token->{tag_name} eq 'col') {                if ($token->{tag_name} eq 'col') {
5628                  ## Clear back to table context                  ## Clear back to table context
5629                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5630                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5631                    !!!cp ('t219');                    !!!cp ('t219');
5632                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5633                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5634                  }                  }
5635                                    
5636                  !!!insert-element ('colgroup');                  !!!insert-element ('colgroup',, $token);
5637                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;                  $self->{insertion_mode} = IN_COLUMN_GROUP_IM;
5638                  ## reprocess                  ## reprocess
5639                  redo B;                  !!!ack-later;
5640                    next B;
5641                } elsif ({                } elsif ({
5642                          caption => 1,                          caption => 1,
5643                          colgroup => 1,                          colgroup => 1,
5644                          tbody => 1, tfoot => 1, thead => 1,                          tbody => 1, tfoot => 1, thead => 1,
5645                         }->{$token->{tag_name}}) {                         }->{$token->{tag_name}}) {
5646                  ## Clear back to table context                  ## Clear back to table context
5647                  while ($self->{open_elements}->[-1]->[1] ne 'table' and                  while (not ($self->{open_elements}->[-1]->[1]
5648                         $self->{open_elements}->[-1]->[1] ne 'html') {                                  & TABLE_SCOPING_EL)) {
5649                    !!!cp ('t220');                    !!!cp ('t220');
5650                    ## ISSUE: Can this state be reached?                    ## ISSUE: Can this state be reached?
5651                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4493  sub _tree_construction_main ($) { Line 5654  sub _tree_construction_main ($) {
5654                  push @$active_formatting_elements, ['#marker', '']                  push @$active_formatting_elements, ['#marker', '']
5655                      if $token->{tag_name} eq 'caption';                      if $token->{tag_name} eq 'caption';
5656                                    
5657                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5658                  $self->{insertion_mode} = {                  $self->{insertion_mode} = {
5659                                             caption => IN_CAPTION_IM,                                             caption => IN_CAPTION_IM,
5660                                             colgroup => IN_COLUMN_GROUP_IM,                                             colgroup => IN_COLUMN_GROUP_IM,
# Line 4502  sub _tree_construction_main ($) { Line 5663  sub _tree_construction_main ($) {
5663                                             thead => IN_TABLE_BODY_IM,                                             thead => IN_TABLE_BODY_IM,
5664                                            }->{$token->{tag_name}};                                            }->{$token->{tag_name}};
5665                  !!!next-token;                  !!!next-token;
5666                  redo B;                  !!!nack ('t220.1');
5667                    next B;
5668                } else {                } else {
5669                  die "$0: in table: <>: $token->{tag_name}";                  die "$0: in table: <>: $token->{tag_name}";
5670                }                }
5671              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5672                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
5673                                  text => $self->{open_elements}->[-1]->[0]
5674                                      ->manakai_local_name,
5675                                  token => $token);
5676    
5677                ## As if </table>                ## As if </table>
5678                ## have a table element in table scope                ## have a table element in table scope
5679                my $i;                my $i;
5680                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5681                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5682                  if ($node->[1] eq 'table') {                  if ($node->[1] & TABLE_EL) {
5683                    !!!cp ('t221');                    !!!cp ('t221');
5684                    $i = $_;                    $i = $_;
5685                    last INSCOPE;                    last INSCOPE;
5686                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           #table => 1,  
                           html => 1,  
                          }->{$node->[1]}) {  
5687                    !!!cp ('t222');                    !!!cp ('t222');
5688                    last INSCOPE;                    last INSCOPE;
5689                  }                  }
# Line 4529  sub _tree_construction_main ($) { Line 5691  sub _tree_construction_main ($) {
5691                unless (defined $i) {                unless (defined $i) {
5692                  !!!cp ('t223');                  !!!cp ('t223');
5693  ## TODO: The following is wrong, maybe.  ## TODO: The following is wrong, maybe.
5694                  !!!parse-error (type => 'unmatched end tag:table');                  !!!parse-error (type => 'unmatched end tag', text => 'table',
5695                                    token => $token);
5696                  ## Ignore tokens </table><table>                  ## Ignore tokens </table><table>
5697                    !!!nack ('t223.1');
5698                  !!!next-token;                  !!!next-token;
5699                  redo B;                  next B;
5700                }                }
5701                                
5702  ## TODO: Followings are removed from the latest spec.  ## TODO: Followings are removed from the latest spec.
5703                ## generate implied end tags                ## generate implied end tags
5704                while ({                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                       dd => 1, dt => 1, li => 1, p => 1,  
                      }->{$self->{open_elements}->[-1]->[1]}) {  
5705                  !!!cp ('t224');                  !!!cp ('t224');
5706                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5707                }                }
5708    
5709                if ($self->{open_elements}->[-1]->[1] ne 'table') {                unless ($self->{open_elements}->[-1]->[1] & TABLE_EL) {
5710                  !!!cp ('t225');                  !!!cp ('t225');
5711  ## ISSUE: Can this case be reached?                  ## NOTE: |<table><tr><table>|
5712                  !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                  !!!parse-error (type => 'not closed',
5713                                    text => $self->{open_elements}->[-1]->[0]
5714                                        ->manakai_local_name,
5715                                    token => $token);
5716                } else {                } else {
5717                  !!!cp ('t226');                  !!!cp ('t226');
5718                }                }
# Line 4557  sub _tree_construction_main ($) { Line 5722  sub _tree_construction_main ($) {
5722    
5723                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5724    
5725                ## reprocess            ## reprocess
5726                redo B;            !!!ack-later;
5727              next B;
5728          } elsif ($token->{tag_name} eq 'style') {          } elsif ($token->{tag_name} eq 'style') {
5729            if (not $open_tables->[-1]->[1]) { # tainted            if (not $open_tables->[-1]->[1]) { # tainted
5730              !!!cp ('t227.8');              !!!cp ('t227.8');
5731              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5732              $parse_rcdata->(CDATA_CONTENT_MODEL);              $parse_rcdata->(CDATA_CONTENT_MODEL);
5733              redo B;              next B;
5734            } else {            } else {
5735              !!!cp ('t227.7');              !!!cp ('t227.7');
5736              #              #
# Line 4574  sub _tree_construction_main ($) { Line 5740  sub _tree_construction_main ($) {
5740              !!!cp ('t227.6');              !!!cp ('t227.6');
5741              ## NOTE: This is a "as if in head" code clone.              ## NOTE: This is a "as if in head" code clone.
5742              $script_start_tag->();              $script_start_tag->();
5743              redo B;              next B;
5744            } else {            } else {
5745              !!!cp ('t227.5');              !!!cp ('t227.5');
5746              #              #
# Line 4585  sub _tree_construction_main ($) { Line 5751  sub _tree_construction_main ($) {
5751                my $type = lc $token->{attributes}->{type}->{value};                my $type = lc $token->{attributes}->{type}->{value};
5752                if ($type eq 'hidden') {                if ($type eq 'hidden') {
5753                  !!!cp ('t227.3');                  !!!cp ('t227.3');
5754                  !!!parse-error (type => 'in table:'.$token->{tag_name});                  !!!parse-error (type => 'in table',
5755                                    text => $token->{tag_name}, token => $token);
5756    
5757                  !!!insert-element ($token->{tag_name}, $token->{attributes});                  !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
5758    
5759                  ## TODO: form element pointer                  ## TODO: form element pointer
5760    
5761                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
5762    
5763                  !!!next-token;                  !!!next-token;
5764                  redo B;                  !!!ack ('t227.2.1');
5765                    next B;
5766                } else {                } else {
5767                  !!!cp ('t227.2');                  !!!cp ('t227.2');
5768                  #                  #
# Line 4612  sub _tree_construction_main ($) { Line 5780  sub _tree_construction_main ($) {
5780            #            #
5781          }          }
5782    
5783          !!!parse-error (type => 'in table:'.$token->{tag_name});          !!!parse-error (type => 'in table', text => $token->{tag_name},
5784                            token => $token);
5785    
5786          $insert = $insert_to_foster;          $insert = $insert_to_foster;
5787          #          #
# Line 4623  sub _tree_construction_main ($) { Line 5792  sub _tree_construction_main ($) {
5792                my $i;                my $i;
5793                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5794                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5795                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_ROW_EL) {
5796                    !!!cp ('t228');                    !!!cp ('t228');
5797                    $i = $_;                    $i = $_;
5798                    last INSCOPE;                    last INSCOPE;
5799                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5800                    !!!cp ('t229');                    !!!cp ('t229');
5801                    last INSCOPE;                    last INSCOPE;
5802                  }                  }
5803                } # INSCOPE                } # INSCOPE
5804                unless (defined $i) {                unless (defined $i) {
5805                  !!!cp ('t230');                  !!!cp ('t230');
5806                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5807                                    text => $token->{tag_name}, token => $token);
5808                  ## Ignore the token                  ## Ignore the token
5809                    !!!nack ('t230.1');
5810                  !!!next-token;                  !!!next-token;
5811                  redo B;                  next B;
5812                } else {                } else {
5813                  !!!cp ('t232');                  !!!cp ('t232');
5814                }                }
5815    
5816                ## Clear back to table row context                ## Clear back to table row context
5817                while (not {                while (not ($self->{open_elements}->[-1]->[1]
5818                  tr => 1, html => 1,                                & TABLE_ROW_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
5819                  !!!cp ('t231');                  !!!cp ('t231');
5820  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5821                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4656  sub _tree_construction_main ($) { Line 5824  sub _tree_construction_main ($) {
5824                pop @{$self->{open_elements}}; # tr                pop @{$self->{open_elements}}; # tr
5825                $self->{insertion_mode} = IN_TABLE_BODY_IM;                $self->{insertion_mode} = IN_TABLE_BODY_IM;
5826                !!!next-token;                !!!next-token;
5827                redo B;                !!!nack ('t231.1');
5828                  next B;
5829              } elsif ($token->{tag_name} eq 'table') {              } elsif ($token->{tag_name} eq 'table') {
5830                if ($self->{insertion_mode} == IN_ROW_IM) {                if ($self->{insertion_mode} == IN_ROW_IM) {
5831                  ## As if </tr>                  ## As if </tr>
# Line 4664  sub _tree_construction_main ($) { Line 5833  sub _tree_construction_main ($) {
5833                  my $i;                  my $i;
5834                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5835                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5836                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5837                      !!!cp ('t233');                      !!!cp ('t233');
5838                      $i = $_;                      $i = $_;
5839                      last INSCOPE;                      last INSCOPE;
5840                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5841                      !!!cp ('t234');                      !!!cp ('t234');
5842                      last INSCOPE;                      last INSCOPE;
5843                    }                    }
# Line 4678  sub _tree_construction_main ($) { Line 5845  sub _tree_construction_main ($) {
5845                  unless (defined $i) {                  unless (defined $i) {
5846                    !!!cp ('t235');                    !!!cp ('t235');
5847  ## TODO: The following is wrong.  ## TODO: The following is wrong.
5848                    !!!parse-error (type => 'unmatched end tag:'.$token->{type});                    !!!parse-error (type => 'unmatched end tag',
5849                                      text => $token->{type}, token => $token);
5850                    ## Ignore the token                    ## Ignore the token
5851                      !!!nack ('t236.1');
5852                    !!!next-token;                    !!!next-token;
5853                    redo B;                    next B;
5854                  }                  }
5855                                    
5856                  ## Clear back to table row context                  ## Clear back to table row context
5857                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5858                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5859                    !!!cp ('t236');                    !!!cp ('t236');
5860  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
5861                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4703  sub _tree_construction_main ($) { Line 5871  sub _tree_construction_main ($) {
5871                  my $i;                  my $i;
5872                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5873                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5874                    if ({                    if ($node->[1] & TABLE_ROW_GROUP_EL) {
                        tbody => 1, thead => 1, tfoot => 1,  
                       }->{$node->[1]}) {  
5875                      !!!cp ('t237');                      !!!cp ('t237');
5876                      $i = $_;                      $i = $_;
5877                      last INSCOPE;                      last INSCOPE;
5878                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5879                      !!!cp ('t238');                      !!!cp ('t238');
5880                      last INSCOPE;                      last INSCOPE;
5881                    }                    }
5882                  } # INSCOPE                  } # INSCOPE
5883                  unless (defined $i) {                  unless (defined $i) {
5884                    !!!cp ('t239');                    !!!cp ('t239');
5885                    !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                    !!!parse-error (type => 'unmatched end tag',
5886                                      text => $token->{tag_name}, token => $token);
5887                    ## Ignore the token                    ## Ignore the token
5888                      !!!nack ('t239.1');
5889                    !!!next-token;                    !!!next-token;
5890                    redo B;                    next B;
5891                  }                  }
5892                                    
5893                  ## Clear back to table body context                  ## Clear back to table body context
5894                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
5895                    tbody => 1, tfoot => 1, thead => 1, html => 1,                                  & TABLE_ROWS_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
5896                    !!!cp ('t240');                    !!!cp ('t240');
5897                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
5898                  }                  }
# Line 4753  sub _tree_construction_main ($) { Line 5918  sub _tree_construction_main ($) {
5918                my $i;                my $i;
5919                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5920                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
5921                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[1] & TABLE_EL) {
5922                    !!!cp ('t241');                    !!!cp ('t241');
5923                    $i = $_;                    $i = $_;
5924                    last INSCOPE;                    last INSCOPE;
5925                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
5926                    !!!cp ('t242');                    !!!cp ('t242');
5927                    last INSCOPE;                    last INSCOPE;
5928                  }                  }
5929                } # INSCOPE                } # INSCOPE
5930                unless (defined $i) {                unless (defined $i) {
5931                  !!!cp ('t243');                  !!!cp ('t243');
5932                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
5933                                    text => $token->{tag_name}, token => $token);
5934                  ## Ignore the token                  ## Ignore the token
5935                    !!!nack ('t243.1');
5936                  !!!next-token;                  !!!next-token;
5937                  redo B;                  next B;
5938                }                }
5939                                    
5940                splice @{$self->{open_elements}}, $i;                splice @{$self->{open_elements}}, $i;
# Line 4778  sub _tree_construction_main ($) { Line 5943  sub _tree_construction_main ($) {
5943                $self->_reset_insertion_mode;                $self->_reset_insertion_mode;
5944                                
5945                !!!next-token;                !!!next-token;
5946                redo B;                next B;
5947              } elsif ({              } elsif ({
5948                        tbody => 1, tfoot => 1, thead => 1,                        tbody => 1, tfoot => 1, thead => 1,
5949                       }->{$token->{tag_name}} and                       }->{$token->{tag_name}} and
# Line 4788  sub _tree_construction_main ($) { Line 5953  sub _tree_construction_main ($) {
5953                  my $i;                  my $i;
5954                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5955                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5956                    if ($node->[1] eq $token->{tag_name}) {                    if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
5957                      !!!cp ('t247');                      !!!cp ('t247');
5958                      $i = $_;                      $i = $_;
5959                      last INSCOPE;                      last INSCOPE;
5960                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5961                      !!!cp ('t248');                      !!!cp ('t248');
5962                      last INSCOPE;                      last INSCOPE;
5963                    }                    }
5964                  } # INSCOPE                  } # INSCOPE
5965                    unless (defined $i) {                    unless (defined $i) {
5966                      !!!cp ('t249');                      !!!cp ('t249');
5967                      !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                      !!!parse-error (type => 'unmatched end tag',
5968                                        text => $token->{tag_name}, token => $token);
5969                      ## Ignore the token                      ## Ignore the token
5970                        !!!nack ('t249.1');
5971                      !!!next-token;                      !!!next-token;
5972                      redo B;                      next B;
5973                    }                    }
5974                                    
5975                  ## As if </tr>                  ## As if </tr>
# Line 4812  sub _tree_construction_main ($) { Line 5977  sub _tree_construction_main ($) {
5977                  my $i;                  my $i;
5978                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                  INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
5979                    my $node = $self->{open_elements}->[$_];                    my $node = $self->{open_elements}->[$_];
5980                    if ($node->[1] eq 'tr') {                    if ($node->[1] & TABLE_ROW_EL) {
5981                      !!!cp ('t250');                      !!!cp ('t250');
5982                      $i = $_;                      $i = $_;
5983                      last INSCOPE;                      last INSCOPE;
5984                    } elsif ({                    } elsif ($node->[1] & TABLE_SCOPING_EL) {
                             table => 1, html => 1,  
                            }->{$node->[1]}) {  
5985                      !!!cp ('t251');                      !!!cp ('t251');
5986                      last INSCOPE;                      last INSCOPE;
5987                    }                    }
5988                  } # INSCOPE                  } # INSCOPE
5989                    unless (defined $i) {                    unless (defined $i) {
5990                      !!!cp ('t252');                      !!!cp ('t252');
5991                      !!!parse-error (type => 'unmatched end tag:tr');                      !!!parse-error (type => 'unmatched end tag',
5992                                        text => 'tr', token => $token);
5993                      ## Ignore the token                      ## Ignore the token
5994                        !!!nack ('t252.1');
5995                      !!!next-token;                      !!!next-token;
5996                      redo B;                      next B;
5997                    }                    }
5998                                    
5999                  ## Clear back to table row context                  ## Clear back to table row context
6000                  while (not {                  while (not ($self->{open_elements}->[-1]->[1]
6001                    tr => 1, html => 1,                                  & TABLE_ROW_SCOPING_EL)) {
                 }->{$self->{open_elements}->[-1]->[1]}) {  
6002                    !!!cp ('t253');                    !!!cp ('t253');
6003  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
6004                    pop @{$self->{open_elements}};                    pop @{$self->{open_elements}};
# Line 4849  sub _tree_construction_main ($) { Line 6013  sub _tree_construction_main ($) {
6013                my $i;                my $i;
6014                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6015                  my $node = $self->{open_elements}->[$_];                  my $node = $self->{open_elements}->[$_];
6016                  if ($node->[1] eq $token->{tag_name}) {                  if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6017                    !!!cp ('t254');                    !!!cp ('t254');
6018                    $i = $_;                    $i = $_;
6019                    last INSCOPE;                    last INSCOPE;
6020                  } elsif ({                  } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6021                    !!!cp ('t255');                    !!!cp ('t255');
6022                    last INSCOPE;                    last INSCOPE;
6023                  }                  }
6024                } # INSCOPE                } # INSCOPE
6025                unless (defined $i) {                unless (defined $i) {
6026                  !!!cp ('t256');                  !!!cp ('t256');
6027                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                  !!!parse-error (type => 'unmatched end tag',
6028                                    text => $token->{tag_name}, token => $token);
6029                  ## Ignore the token                  ## Ignore the token
6030                    !!!nack ('t256.1');
6031                  !!!next-token;                  !!!next-token;
6032                  redo B;                  next B;
6033                }                }
6034    
6035                ## Clear back to table body context                ## Clear back to table body context
6036                while (not {                while (not ($self->{open_elements}->[-1]->[1]
6037                  tbody => 1, tfoot => 1, thead => 1, html => 1,                                & TABLE_ROWS_SCOPING_EL)) {
               }->{$self->{open_elements}->[-1]->[1]}) {  
6038                  !!!cp ('t257');                  !!!cp ('t257');
6039  ## ISSUE: Can this case be reached?  ## ISSUE: Can this case be reached?
6040                  pop @{$self->{open_elements}};                  pop @{$self->{open_elements}};
# Line 4879  sub _tree_construction_main ($) { Line 6042  sub _tree_construction_main ($) {
6042    
6043                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6044                $self->{insertion_mode} = IN_TABLE_IM;                $self->{insertion_mode} = IN_TABLE_IM;
6045                  !!!nack ('t257.1');
6046                !!!next-token;                !!!next-token;
6047                redo B;                next B;
6048              } elsif ({              } elsif ({
6049                        body => 1, caption => 1, col => 1, colgroup => 1,                        body => 1, caption => 1, col => 1, colgroup => 1,
6050                        html => 1, td => 1, th => 1,                        html => 1, td => 1, th => 1,
6051                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM                        tr => 1, # $self->{insertion_mode} == IN_ROW_IM
6052                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM                        tbody => 1, tfoot => 1, thead => 1, # $self->{insertion_mode} == IN_TABLE_IM
6053                       }->{$token->{tag_name}}) {                       }->{$token->{tag_name}}) {
6054                !!!cp ('t258');            !!!cp ('t258');
6055                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6056                ## Ignore the token                            text => $token->{tag_name}, token => $token);
6057                !!!next-token;            ## Ignore the token
6058                redo B;            !!!nack ('t258.1');
6059               !!!next-token;
6060              next B;
6061          } else {          } else {
6062            !!!cp ('t259');            !!!cp ('t259');
6063            !!!parse-error (type => 'in table:/'.$token->{tag_name});            !!!parse-error (type => 'in table:/',
6064                              text => $token->{tag_name}, token => $token);
6065    
6066            $insert = $insert_to_foster;            $insert = $insert_to_foster;
6067            #            #
6068          }          }
6069        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6070          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6071                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6072            !!!parse-error (type => 'in body:#eof');            !!!parse-error (type => 'in body:#eof', token => $token);
6073            !!!cp ('t259.1');            !!!cp ('t259.1');
6074            #            #
6075          } else {          } else {
# Line 4922  sub _tree_construction_main ($) { Line 6089  sub _tree_construction_main ($) {
6089                unless (length $token->{data}) {                unless (length $token->{data}) {
6090                  !!!cp ('t260');                  !!!cp ('t260');
6091                  !!!next-token;                  !!!next-token;
6092                  redo B;                  next B;
6093                }                }
6094              }              }
6095                            
# Line 4931  sub _tree_construction_main ($) { Line 6098  sub _tree_construction_main ($) {
6098            } elsif ($token->{type} == START_TAG_TOKEN) {            } elsif ($token->{type} == START_TAG_TOKEN) {
6099              if ($token->{tag_name} eq 'col') {              if ($token->{tag_name} eq 'col') {
6100                !!!cp ('t262');                !!!cp ('t262');
6101                !!!insert-element ($token->{tag_name}, $token->{attributes});                !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6102                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
6103                  !!!ack ('t262.1');
6104                !!!next-token;                !!!next-token;
6105                redo B;                next B;
6106              } else {              } else {
6107                !!!cp ('t263');                !!!cp ('t263');
6108                #                #
6109              }              }
6110            } elsif ($token->{type} == END_TAG_TOKEN) {            } elsif ($token->{type} == END_TAG_TOKEN) {
6111              if ($token->{tag_name} eq 'colgroup') {              if ($token->{tag_name} eq 'colgroup') {
6112                if ($self->{open_elements}->[-1]->[1] eq 'html') {                if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6113                  !!!cp ('t264');                  !!!cp ('t264');
6114                  !!!parse-error (type => 'unmatched end tag:colgroup');                  !!!parse-error (type => 'unmatched end tag',
6115                                    text => 'colgroup', token => $token);
6116                  ## Ignore the token                  ## Ignore the token
6117                  !!!next-token;                  !!!next-token;
6118                  redo B;                  next B;
6119                } else {                } else {
6120                  !!!cp ('t265');                  !!!cp ('t265');
6121                  pop @{$self->{open_elements}}; # colgroup                  pop @{$self->{open_elements}}; # colgroup
6122                  $self->{insertion_mode} = IN_TABLE_IM;                  $self->{insertion_mode} = IN_TABLE_IM;
6123                  !!!next-token;                  !!!next-token;
6124                  redo B;                              next B;            
6125                }                }
6126              } elsif ($token->{tag_name} eq 'col') {              } elsif ($token->{tag_name} eq 'col') {
6127                !!!cp ('t266');                !!!cp ('t266');
6128                !!!parse-error (type => 'unmatched end tag:col');                !!!parse-error (type => 'unmatched end tag',
6129                                  text => 'col', token => $token);
6130                ## Ignore the token                ## Ignore the token
6131                !!!next-token;                !!!next-token;
6132                redo B;                next B;
6133              } else {              } else {
6134                !!!cp ('t267');                !!!cp ('t267');
6135                #                #
6136              }              }
6137        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6138          if ($self->{open_elements}->[-1]->[1] eq 'html' or          if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6139              @{$self->{open_elements}} == 1) { # redundant, maybe              @{$self->{open_elements}} == 1) { # redundant, maybe
6140            !!!cp ('t270.2');            !!!cp ('t270.2');
6141            ## Stop parsing.            ## Stop parsing.
# Line 4976  sub _tree_construction_main ($) { Line 6146  sub _tree_construction_main ($) {
6146            pop @{$self->{open_elements}}; # colgroup            pop @{$self->{open_elements}}; # colgroup
6147            $self->{insertion_mode} = IN_TABLE_IM;            $self->{insertion_mode} = IN_TABLE_IM;
6148            ## Reprocess.            ## Reprocess.
6149            redo B;            next B;
6150          }          }
6151        } else {        } else {
6152          die "$0: $token->{type}: Unknown token type";          die "$0: $token->{type}: Unknown token type";
6153        }        }
6154    
6155            ## As if </colgroup>            ## As if </colgroup>
6156            if ($self->{open_elements}->[-1]->[1] eq 'html') {            if ($self->{open_elements}->[-1]->[1] & HTML_EL) {
6157              !!!cp ('t269');              !!!cp ('t269');
6158  ## TODO: Wrong error type?  ## TODO: Wrong error type?
6159              !!!parse-error (type => 'unmatched end tag:colgroup');              !!!parse-error (type => 'unmatched end tag',
6160                                text => 'colgroup', token => $token);
6161              ## Ignore the token              ## Ignore the token
6162                !!!nack ('t269.1');
6163              !!!next-token;              !!!next-token;
6164              redo B;              next B;
6165            } else {            } else {
6166              !!!cp ('t270');              !!!cp ('t270');
6167              pop @{$self->{open_elements}}; # colgroup              pop @{$self->{open_elements}}; # colgroup
6168              $self->{insertion_mode} = IN_TABLE_IM;              $self->{insertion_mode} = IN_TABLE_IM;
6169                !!!ack-later;
6170              ## reprocess              ## reprocess
6171              redo B;              next B;
6172            }            }
6173      } elsif ($self->{insertion_mode} & SELECT_IMS) {      } elsif ($self->{insertion_mode} & SELECT_IMS) {
6174        if ($token->{type} == CHARACTER_TOKEN) {        if ($token->{type} == CHARACTER_TOKEN) {
6175          !!!cp ('t271');          !!!cp ('t271');
6176          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});          $self->{open_elements}->[-1]->[0]->manakai_append_text ($token->{data});
6177          !!!next-token;          !!!next-token;
6178          redo B;          next B;
6179        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6180              if ($token->{tag_name} eq 'option') {          if ($token->{tag_name} eq 'option') {
6181                if ($self->{open_elements}->[-1]->[1] eq 'option') {            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6182                  !!!cp ('t272');              !!!cp ('t272');
6183                  ## As if </option>              ## As if </option>
6184                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6185                } else {            } else {
6186                  !!!cp ('t273');              !!!cp ('t273');
6187                }            }
6188    
6189                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6190                !!!next-token;            !!!nack ('t273.1');
6191                redo B;            !!!next-token;
6192              } elsif ($token->{tag_name} eq 'optgroup') {            next B;
6193                if ($self->{open_elements}->[-1]->[1] eq 'option') {          } elsif ($token->{tag_name} eq 'optgroup') {
6194                  !!!cp ('t274');            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6195                  ## As if </option>              !!!cp ('t274');
6196                  pop @{$self->{open_elements}};              ## As if </option>
6197                } else {              pop @{$self->{open_elements}};
6198                  !!!cp ('t275');            } else {
6199                }              !!!cp ('t275');
6200              }
6201    
6202                if ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            if ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6203                  !!!cp ('t276');              !!!cp ('t276');
6204                  ## As if </optgroup>              ## As if </optgroup>
6205                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6206                } else {            } else {
6207                  !!!cp ('t277');              !!!cp ('t277');
6208                }            }
6209    
6210                !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6211                !!!next-token;            !!!nack ('t277.1');
6212                redo B;            !!!next-token;
6213          } elsif ($token->{tag_name} eq 'select' or            next B;
6214                   $token->{tag_name} eq 'input' or          } elsif ({
6215                       select => 1, input => 1, textarea => 1,
6216                     }->{$token->{tag_name}} or
6217                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and                   ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6218                    {                    {
6219                     caption => 1, table => 1,                     caption => 1, table => 1,
# Line 5045  sub _tree_construction_main ($) { Line 6221  sub _tree_construction_main ($) {
6221                     tr => 1, td => 1, th => 1,                     tr => 1, td => 1, th => 1,
6222                    }->{$token->{tag_name}})) {                    }->{$token->{tag_name}})) {
6223            ## TODO: The type below is not good - <select> is replaced by </select>            ## TODO: The type below is not good - <select> is replaced by </select>
6224            !!!parse-error (type => 'not closed:select');            !!!parse-error (type => 'not closed', text => 'select',
6225                              token => $token);
6226            ## NOTE: As if the token were </select> (<select> case) or            ## NOTE: As if the token were </select> (<select> case) or
6227            ## as if there were </select> (otherwise).            ## as if there were </select> (otherwise).
6228                ## have an element in table scope            ## have an element in table scope
6229                my $i;            my $i;
6230                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6231                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6232                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6233                    !!!cp ('t278');                !!!cp ('t278');
6234                    $i = $_;                $i = $_;
6235                    last INSCOPE;                last INSCOPE;
6236                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6237                            table => 1, html => 1,                !!!cp ('t279');
6238                           }->{$node->[1]}) {                last INSCOPE;
6239                    !!!cp ('t279');              }
6240                    last INSCOPE;            } # INSCOPE
6241                  }            unless (defined $i) {
6242                } # INSCOPE              !!!cp ('t280');
6243                unless (defined $i) {              !!!parse-error (type => 'unmatched end tag',
6244                  !!!cp ('t280');                              text => 'select', token => $token);
6245                  !!!parse-error (type => 'unmatched end tag:select');              ## Ignore the token
6246                  ## Ignore the token              !!!nack ('t280.1');
6247                  !!!next-token;              !!!next-token;
6248                  redo B;              next B;
6249                }            }
6250                                
6251                !!!cp ('t281');            !!!cp ('t281');
6252                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6253    
6254                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6255    
6256            if ($token->{tag_name} eq 'select') {            if ($token->{tag_name} eq 'select') {
6257              !!!cp ('t281.2');              !!!nack ('t281.2');
6258              !!!next-token;              !!!next-token;
6259              redo B;              next B;
6260            } else {            } else {
6261              !!!cp ('t281.1');              !!!cp ('t281.1');
6262                !!!ack-later;
6263              ## Reprocess the token.              ## Reprocess the token.
6264              redo B;              next B;
6265            }            }
6266          } else {          } else {
6267            !!!cp ('t282');            !!!cp ('t282');
6268            !!!parse-error (type => 'in select:'.$token->{tag_name});            !!!parse-error (type => 'in select',
6269                              text => $token->{tag_name}, token => $token);
6270            ## Ignore the token            ## Ignore the token
6271              !!!nack ('t282.1');
6272            !!!next-token;            !!!next-token;
6273            redo B;            next B;
6274          }          }
6275        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6276              if ($token->{tag_name} eq 'optgroup') {          if ($token->{tag_name} eq 'optgroup') {
6277                if ($self->{open_elements}->[-1]->[1] eq 'option' and            if ($self->{open_elements}->[-1]->[1] & OPTION_EL and
6278                    $self->{open_elements}->[-2]->[1] eq 'optgroup') {                $self->{open_elements}->[-2]->[1] & OPTGROUP_EL) {
6279                  !!!cp ('t283');              !!!cp ('t283');
6280                  ## As if </option>              ## As if </option>
6281                  splice @{$self->{open_elements}}, -2;              splice @{$self->{open_elements}}, -2;
6282                } elsif ($self->{open_elements}->[-1]->[1] eq 'optgroup') {            } elsif ($self->{open_elements}->[-1]->[1] & OPTGROUP_EL) {
6283                  !!!cp ('t284');              !!!cp ('t284');
6284                  pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
6285                } else {            } else {
6286                  !!!cp ('t285');              !!!cp ('t285');
6287                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6288                  ## Ignore the token                              text => $token->{tag_name}, token => $token);
6289                }              ## Ignore the token
6290                !!!next-token;            }
6291                redo B;            !!!nack ('t285.1');
6292              } elsif ($token->{tag_name} eq 'option') {            !!!next-token;
6293                if ($self->{open_elements}->[-1]->[1] eq 'option') {            next B;
6294                  !!!cp ('t286');          } elsif ($token->{tag_name} eq 'option') {
6295                  pop @{$self->{open_elements}};            if ($self->{open_elements}->[-1]->[1] & OPTION_EL) {
6296                } else {              !!!cp ('t286');
6297                  !!!cp ('t287');              pop @{$self->{open_elements}};
6298                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            } else {
6299                  ## Ignore the token              !!!cp ('t287');
6300                }              !!!parse-error (type => 'unmatched end tag',
6301                !!!next-token;                              text => $token->{tag_name}, token => $token);
6302                redo B;              ## Ignore the token
6303              } elsif ($token->{tag_name} eq 'select') {            }
6304                ## have an element in table scope            !!!nack ('t287.1');
6305                my $i;            !!!next-token;
6306                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            next B;
6307                  my $node = $self->{open_elements}->[$_];          } elsif ($token->{tag_name} eq 'select') {
6308                  if ($node->[1] eq $token->{tag_name}) {            ## have an element in table scope
6309                    !!!cp ('t288');            my $i;
6310                    $i = $_;            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6311                    last INSCOPE;              my $node = $self->{open_elements}->[$_];
6312                  } elsif ({              if ($node->[1] & SELECT_EL) {
6313                            table => 1, html => 1,                !!!cp ('t288');
6314                           }->{$node->[1]}) {                $i = $_;
6315                    !!!cp ('t289');                last INSCOPE;
6316                    last INSCOPE;              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6317                  }                !!!cp ('t289');
6318                } # INSCOPE                last INSCOPE;
6319                unless (defined $i) {              }
6320                  !!!cp ('t290');            } # INSCOPE
6321                  !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            unless (defined $i) {
6322                  ## Ignore the token              !!!cp ('t290');
6323                  !!!next-token;              !!!parse-error (type => 'unmatched end tag',
6324                  redo B;                              text => $token->{tag_name}, token => $token);
6325                }              ## Ignore the token
6326                !!!nack ('t290.1');
6327                !!!next-token;
6328                next B;
6329              }
6330                                
6331                !!!cp ('t291');            !!!cp ('t291');
6332                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6333    
6334                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6335    
6336                !!!next-token;            !!!nack ('t291.1');
6337                redo B;            !!!next-token;
6338              next B;
6339          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and          } elsif ($self->{insertion_mode} == IN_SELECT_IN_TABLE_IM and
6340                   {                   {
6341                    caption => 1, table => 1, tbody => 1,                    caption => 1, table => 1, tbody => 1,
6342                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,                    tfoot => 1, thead => 1, tr => 1, td => 1, th => 1,
6343                   }->{$token->{tag_name}}) {                   }->{$token->{tag_name}}) {
6344  ## TODO: The following is wrong?  ## TODO: The following is wrong?
6345                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
6346                              text => $token->{tag_name}, token => $token);
6347                                
6348                ## have an element in table scope            ## have an element in table scope
6349                my $i;            my $i;
6350                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6351                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6352                  if ($node->[1] eq $token->{tag_name}) {              if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
6353                    !!!cp ('t292');                !!!cp ('t292');
6354                    $i = $_;                $i = $_;
6355                    last INSCOPE;                last INSCOPE;
6356                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
6357                            table => 1, html => 1,                !!!cp ('t293');
6358                           }->{$node->[1]}) {                last INSCOPE;
6359                    !!!cp ('t293');              }
6360                    last INSCOPE;            } # INSCOPE
6361                  }            unless (defined $i) {
6362                } # INSCOPE              !!!cp ('t294');
6363                unless (defined $i) {              ## Ignore the token
6364                  !!!cp ('t294');              !!!nack ('t294.1');
6365                  ## Ignore the token              !!!next-token;
6366                  !!!next-token;              next B;
6367                  redo B;            }
               }  
6368                                
6369                ## As if </select>            ## As if </select>
6370                ## have an element in table scope            ## have an element in table scope
6371                undef $i;            undef $i;
6372                INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6373                  my $node = $self->{open_elements}->[$_];              my $node = $self->{open_elements}->[$_];
6374                  if ($node->[1] eq 'select') {              if ($node->[1] & SELECT_EL) {
6375                    !!!cp ('t295');                !!!cp ('t295');
6376                    $i = $_;                $i = $_;
6377                    last INSCOPE;                last INSCOPE;
6378                  } elsif ({              } elsif ($node->[1] & TABLE_SCOPING_EL) {
                           table => 1, html => 1,  
                          }->{$node->[1]}) {  
6379  ## ISSUE: Can this state be reached?  ## ISSUE: Can this state be reached?
6380                    !!!cp ('t296');                !!!cp ('t296');
6381                    last INSCOPE;                last INSCOPE;
6382                  }              }
6383                } # INSCOPE            } # INSCOPE
6384                unless (defined $i) {            unless (defined $i) {
6385                  !!!cp ('t297');              !!!cp ('t297');
6386  ## TODO: The following error type is correct?  ## TODO: The following error type is correct?
6387                  !!!parse-error (type => 'unmatched end tag:select');              !!!parse-error (type => 'unmatched end tag',
6388                  ## Ignore the </select> token                              text => 'select', token => $token);
6389                  !!!next-token; ## TODO: ok?              ## Ignore the </select> token
6390                  redo B;              !!!nack ('t297.1');
6391                }              !!!next-token; ## TODO: ok?
6392                next B;
6393              }
6394                                
6395                !!!cp ('t298');            !!!cp ('t298');
6396                splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
6397    
6398                $self->_reset_insertion_mode;            $self->_reset_insertion_mode;
6399    
6400                ## reprocess            !!!ack-later;
6401                redo B;            ## reprocess
6402              next B;
6403          } else {          } else {
6404            !!!cp ('t299');            !!!cp ('t299');
6405            !!!parse-error (type => 'in select:/'.$token->{tag_name});            !!!parse-error (type => 'in select:/',
6406                              text => $token->{tag_name}, token => $token);
6407            ## Ignore the token            ## Ignore the token
6408              !!!nack ('t299.3');
6409            !!!next-token;            !!!next-token;
6410            redo B;            next B;
6411          }          }
6412        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6413          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6414                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6415            !!!cp ('t299.1');            !!!cp ('t299.1');
6416            !!!parse-error (type => 'in body:#eof');            !!!parse-error (type => 'in body:#eof', token => $token);
6417          } else {          } else {
6418            !!!cp ('t299.2');            !!!cp ('t299.2');
6419          }          }
# Line 5247  sub _tree_construction_main ($) { Line 6435  sub _tree_construction_main ($) {
6435            unless (length $token->{data}) {            unless (length $token->{data}) {
6436              !!!cp ('t300');              !!!cp ('t300');
6437              !!!next-token;              !!!next-token;
6438              redo B;              next B;
6439            }            }
6440          }          }
6441                    
6442          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6443            !!!cp ('t301');            !!!cp ('t301');
6444            !!!parse-error (type => 'after html:#character');            !!!parse-error (type => 'after html:#text', token => $token);
6445    
6446            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6447          } else {          } else {
# Line 5261  sub _tree_construction_main ($) { Line 6449  sub _tree_construction_main ($) {
6449          }          }
6450                    
6451          ## "after body" insertion mode          ## "after body" insertion mode
6452          !!!parse-error (type => 'after body:#character');          !!!parse-error (type => 'after body:#text', token => $token);
6453    
6454          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6455          ## reprocess          ## reprocess
6456          redo B;          next B;
6457        } elsif ($token->{type} == START_TAG_TOKEN) {        } elsif ($token->{type} == START_TAG_TOKEN) {
6458          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6459            !!!cp ('t303');            !!!cp ('t303');
6460            !!!parse-error (type => 'after html:'.$token->{tag_name});            !!!parse-error (type => 'after html',
6461                              text => $token->{tag_name}, token => $token);
6462                        
6463            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
6464          } else {          } else {
# Line 5277  sub _tree_construction_main ($) { Line 6466  sub _tree_construction_main ($) {
6466          }          }
6467    
6468          ## "after body" insertion mode          ## "after body" insertion mode
6469          !!!parse-error (type => 'after body:'.$token->{tag_name});          !!!parse-error (type => 'after body',
6470                            text => $token->{tag_name}, token => $token);
6471    
6472          $self->{insertion_mode} = IN_BODY_IM;          $self->{insertion_mode} = IN_BODY_IM;
6473            !!!ack-later;
6474          ## reprocess          ## reprocess
6475          redo B;          next B;
6476        } elsif ($token->{type} == END_TAG_TOKEN) {        } elsif ($token->{type} == END_TAG_TOKEN) {
6477          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {          if ($self->{insertion_mode} == AFTER_HTML_BODY_IM) {
6478            !!!cp ('t305');            !!!cp ('t305');
6479            !!!parse-error (type => 'after html:/'.$token->{tag_name});            !!!parse-error (type => 'after html:/',
6480                              text => $token->{tag_name}, token => $token);
6481                        
6482            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
6483            ## Reprocess in the "after body" insertion mode.            ## Reprocess in the "after body" insertion mode.
# Line 5297  sub _tree_construction_main ($) { Line 6489  sub _tree_construction_main ($) {
6489          if ($token->{tag_name} eq 'html') {          if ($token->{tag_name} eq 'html') {
6490            if (defined $self->{inner_html_node}) {            if (defined $self->{inner_html_node}) {
6491              !!!cp ('t307');              !!!cp ('t307');
6492              !!!parse-error (type => 'unmatched end tag:html');              !!!parse-error (type => 'unmatched end tag',
6493                                text => 'html', token => $token);
6494              ## Ignore the token              ## Ignore the token
6495              !!!next-token;              !!!next-token;
6496              redo B;              next B;
6497            } else {            } else {
6498              !!!cp ('t308');              !!!cp ('t308');
6499              $self->{insertion_mode} = AFTER_HTML_BODY_IM;              $self->{insertion_mode} = AFTER_HTML_BODY_IM;
6500              !!!next-token;              !!!next-token;
6501              redo B;              next B;
6502            }            }
6503          } else {          } else {
6504            !!!cp ('t309');            !!!cp ('t309');
6505            !!!parse-error (type => 'after body:/'.$token->{tag_name});            !!!parse-error (type => 'after body:/',
6506                              text => $token->{tag_name}, token => $token);
6507    
6508            $self->{insertion_mode} = IN_BODY_IM;            $self->{insertion_mode} = IN_BODY_IM;
6509            ## reprocess            ## reprocess
6510            redo B;            next B;
6511          }          }
6512        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6513          !!!cp ('t309.2');          !!!cp ('t309.2');
# Line 5330  sub _tree_construction_main ($) { Line 6524  sub _tree_construction_main ($) {
6524            unless (length $token->{data}) {            unless (length $token->{data}) {
6525              !!!cp ('t310');              !!!cp ('t310');
6526              !!!next-token;              !!!next-token;
6527              redo B;              next B;
6528            }            }
6529          }          }
6530                    
6531          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {          if ($token->{data} =~ s/^[^\x09\x0A\x0B\x0C\x20]+//) {
6532            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6533              !!!cp ('t311');              !!!cp ('t311');
6534              !!!parse-error (type => 'in frameset:#character');              !!!parse-error (type => 'in frameset:#text', token => $token);
6535            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {            } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6536              !!!cp ('t312');              !!!cp ('t312');
6537              !!!parse-error (type => 'after frameset:#character');              !!!parse-error (type => 'after frameset:#text', token => $token);
6538            } else { # "after html frameset"            } else { # "after after frameset"
6539              !!!cp ('t313');              !!!cp ('t313');
6540              !!!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');  
6541            }            }
6542                        
6543            ## Ignore the token.            ## Ignore the token.
# Line 5358  sub _tree_construction_main ($) { Line 6548  sub _tree_construction_main ($) {
6548              !!!cp ('t315');              !!!cp ('t315');
6549              !!!next-token;              !!!next-token;
6550            }            }
6551            redo B;            next B;
6552          }          }
6553                    
6554          die qq[$0: Character "$token->{data}"];          die qq[$0: Character "$token->{data}"];
6555        } 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');  
         }  
   
6556          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6557              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6558            !!!cp ('t318');            !!!cp ('t318');
6559            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6560              !!!nack ('t318.1');
6561            !!!next-token;            !!!next-token;
6562            redo B;            next B;
6563          } elsif ($token->{tag_name} eq 'frame' and          } elsif ($token->{tag_name} eq 'frame' and
6564                   $self->{insertion_mode} == IN_FRAMESET_IM) {                   $self->{insertion_mode} == IN_FRAMESET_IM) {
6565            !!!cp ('t319');            !!!cp ('t319');
6566            !!!insert-element ($token->{tag_name}, $token->{attributes});            !!!insert-element ($token->{tag_name}, $token->{attributes}, $token);
6567            pop @{$self->{open_elements}};            pop @{$self->{open_elements}};
6568              !!!ack ('t319.1');
6569            !!!next-token;            !!!next-token;
6570            redo B;            next B;
6571          } elsif ($token->{tag_name} eq 'noframes') {          } elsif ($token->{tag_name} eq 'noframes') {
6572            !!!cp ('t320');            !!!cp ('t320');
6573            ## NOTE: As if in body.            ## NOTE: As if in head.
6574            $parse_rcdata->(CDATA_CONTENT_MODEL);            $parse_rcdata->(CDATA_CONTENT_MODEL);
6575            redo B;            next B;
6576    
6577              ## NOTE: |<!DOCTYPE HTML><frameset></frameset></html><noframes></noframes>|
6578              ## has no parse error.
6579          } else {          } else {
6580            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6581              !!!cp ('t321');              !!!cp ('t321');
6582              !!!parse-error (type => 'in frameset:'.$token->{tag_name});              !!!parse-error (type => 'in frameset',
6583            } else {                              text => $token->{tag_name}, token => $token);
6584              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6585              !!!cp ('t322');              !!!cp ('t322');
6586              !!!parse-error (type => 'after frameset:'.$token->{tag_name});              !!!parse-error (type => 'after frameset',
6587                                text => $token->{tag_name}, token => $token);
6588              } else { # "after after frameset"
6589                !!!cp ('t322.2');
6590                !!!parse-error (type => 'after after frameset',
6591                                text => $token->{tag_name}, token => $token);
6592            }            }
6593            ## Ignore the token            ## Ignore the token
6594              !!!nack ('t322.1');
6595            !!!next-token;            !!!next-token;
6596            redo B;            next B;
6597          }          }
6598        } 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');  
         }  
   
6599          if ($token->{tag_name} eq 'frameset' and          if ($token->{tag_name} eq 'frameset' and
6600              $self->{insertion_mode} == IN_FRAMESET_IM) {              $self->{insertion_mode} == IN_FRAMESET_IM) {
6601            if ($self->{open_elements}->[-1]->[1] eq 'html' and            if ($self->{open_elements}->[-1]->[1] & HTML_EL and
6602                @{$self->{open_elements}} == 1) {                @{$self->{open_elements}} == 1) {
6603              !!!cp ('t325');              !!!cp ('t325');
6604              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
6605                                text => $token->{tag_name}, token => $token);
6606              ## Ignore the token              ## Ignore the token
6607              !!!next-token;              !!!next-token;
6608            } else {            } else {
# Line 5429  sub _tree_construction_main ($) { Line 6612  sub _tree_construction_main ($) {
6612            }            }
6613    
6614            if (not defined $self->{inner_html_node} and            if (not defined $self->{inner_html_node} and
6615                $self->{open_elements}->[-1]->[1] ne 'frameset') {                not ($self->{open_elements}->[-1]->[1] & FRAMESET_EL)) {
6616              !!!cp ('t327');              !!!cp ('t327');
6617              $self->{insertion_mode} = AFTER_FRAMESET_IM;              $self->{insertion_mode} = AFTER_FRAMESET_IM;
6618            } else {            } else {
6619              !!!cp ('t328');              !!!cp ('t328');
6620            }            }
6621            redo B;            next B;
6622          } elsif ($token->{tag_name} eq 'html' and          } elsif ($token->{tag_name} eq 'html' and
6623                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {                   $self->{insertion_mode} == AFTER_FRAMESET_IM) {
6624            !!!cp ('t329');            !!!cp ('t329');
6625            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;            $self->{insertion_mode} = AFTER_HTML_FRAMESET_IM;
6626            !!!next-token;            !!!next-token;
6627            redo B;            next B;
6628          } else {          } else {
6629            if ($self->{insertion_mode} == IN_FRAMESET_IM) {            if ($self->{insertion_mode} == IN_FRAMESET_IM) {
6630              !!!cp ('t330');              !!!cp ('t330');
6631              !!!parse-error (type => 'in frameset:/'.$token->{tag_name});              !!!parse-error (type => 'in frameset:/',
6632            } else {                              text => $token->{tag_name}, token => $token);
6633              } elsif ($self->{insertion_mode} == AFTER_FRAMESET_IM) {
6634                !!!cp ('t330.1');
6635                !!!parse-error (type => 'after frameset:/',
6636                                text => $token->{tag_name}, token => $token);
6637              } else { # "after after html"
6638              !!!cp ('t331');              !!!cp ('t331');
6639              !!!parse-error (type => 'after frameset:/'.$token->{tag_name});              !!!parse-error (type => 'after after frameset:/',
6640                                text => $token->{tag_name}, token => $token);
6641            }            }
6642            ## Ignore the token            ## Ignore the token
6643            !!!next-token;            !!!next-token;
6644            redo B;            next B;
6645          }          }
6646        } elsif ($token->{type} == END_OF_FILE_TOKEN) {        } elsif ($token->{type} == END_OF_FILE_TOKEN) {
6647          unless ($self->{open_elements}->[-1]->[1] eq 'html' and          unless ($self->{open_elements}->[-1]->[1] & HTML_EL and
6648                  @{$self->{open_elements}} == 1) { # redundant, maybe                  @{$self->{open_elements}} == 1) { # redundant, maybe
6649            !!!cp ('t331.1');            !!!cp ('t331.1');
6650            !!!parse-error (type => 'in body:#eof');            !!!parse-error (type => 'in body:#eof', token => $token);
6651          } else {          } else {
6652            !!!cp ('t331.2');            !!!cp ('t331.2');
6653          }          }
# Line 5480  sub _tree_construction_main ($) { Line 6669  sub _tree_construction_main ($) {
6669          !!!cp ('t332');          !!!cp ('t332');
6670          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6671          $script_start_tag->();          $script_start_tag->();
6672          redo B;          next B;
6673        } elsif ($token->{tag_name} eq 'style') {        } elsif ($token->{tag_name} eq 'style') {
6674          !!!cp ('t333');          !!!cp ('t333');
6675          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6676          $parse_rcdata->(CDATA_CONTENT_MODEL);          $parse_rcdata->(CDATA_CONTENT_MODEL);
6677          redo B;          next B;
6678        } elsif ({        } elsif ({
6679                  base => 1, link => 1,                  base => 1, link => 1,
6680                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6681          !!!cp ('t334');          !!!cp ('t334');
6682          ## 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
6683          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6684          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.          pop @{$self->{open_elements}}; ## ISSUE: This step is missing in the spec.
6685            !!!ack ('t334.1');
6686          !!!next-token;          !!!next-token;
6687          redo B;          next B;
6688        } elsif ($token->{tag_name} eq 'meta') {        } elsif ($token->{tag_name} eq 'meta') {
6689          ## 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
6690          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6691          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.
6692    
6693          unless ($self->{confident}) {          unless ($self->{confident}) {
6694            if ($token->{attributes}->{charset}) { ## TODO: And if supported            if ($token->{attributes}->{charset}) {
6695              !!!cp ('t335');              !!!cp ('t335');
6696                ## NOTE: Whether the encoding is supported or not is handled
6697                ## in the {change_encoding} callback.
6698              $self->{change_encoding}              $self->{change_encoding}
6699                  ->($self, $token->{attributes}->{charset}->{value});                  ->($self, $token->{attributes}->{charset}->{value}, $token);
6700                            
6701              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')              $meta_el->[0]->get_attribute_node_ns (undef, 'charset')
6702                  ->set_user_data (manakai_has_reference =>                  ->set_user_data (manakai_has_reference =>
6703                                       $token->{attributes}->{charset}                                       $token->{attributes}->{charset}
6704                                           ->{has_reference});                                           ->{has_reference});
6705            } elsif ($token->{attributes}->{content}) {            } elsif ($token->{attributes}->{content}) {
             ## ISSUE: Algorithm name in the spec was incorrect so that not linked to the definition.  
6706              if ($token->{attributes}->{content}->{value}              if ($token->{attributes}->{content}->{value}
6707                  =~ /\A[^;]*;[\x09-\x0D\x20]*[Cc][Hh][Aa][Rr][Ss][Ee][Tt]                  =~ /[Cc][Hh][Aa][Rr][Ss][Ee][Tt]
6708                      [\x09-\x0D\x20]*=                      [\x09-\x0D\x20]*=
6709                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|                      [\x09-\x0D\x20]*(?>"([^"]*)"|'([^']*)'|
6710                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20]*))/x) {                      ([^"'\x09-\x0D\x20][^\x09-\x0D\x20\x3B]*))/x) {
6711                !!!cp ('t336');                !!!cp ('t336');
6712                  ## NOTE: Whether the encoding is supported or not is handled
6713                  ## in the {change_encoding} callback.
6714                $self->{change_encoding}                $self->{change_encoding}
6715                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3);                    ->($self, defined $1 ? $1 : defined $2 ? $2 : $3, $token);
6716                $meta_el->[0]->get_attribute_node_ns (undef, 'content')                $meta_el->[0]->get_attribute_node_ns (undef, 'content')
6717                    ->set_user_data (manakai_has_reference =>                    ->set_user_data (manakai_has_reference =>
6718                                         $token->{attributes}->{content}                                         $token->{attributes}->{content}
# Line 5543  sub _tree_construction_main ($) { Line 6736  sub _tree_construction_main ($) {
6736            }            }
6737          }          }
6738    
6739            !!!ack ('t338.1');
6740          !!!next-token;          !!!next-token;
6741          redo B;          next B;
6742        } elsif ($token->{tag_name} eq 'title') {        } elsif ($token->{tag_name} eq 'title') {
6743          !!!cp ('t341');          !!!cp ('t341');
6744          ## NOTE: This is an "as if in head" code clone          ## NOTE: This is an "as if in head" code clone
6745          $parse_rcdata->(RCDATA_CONTENT_MODEL);          $parse_rcdata->(RCDATA_CONTENT_MODEL);
6746          redo B;          next B;
6747        } elsif ($token->{tag_name} eq 'body') {        } elsif ($token->{tag_name} eq 'body') {
6748          !!!parse-error (type => 'in body:body');          !!!parse-error (type => 'in body', text => 'body', token => $token);
6749                                
6750          if (@{$self->{open_elements}} == 1 or          if (@{$self->{open_elements}} == 1 or
6751              $self->{open_elements}->[1]->[1] ne 'body') {              not ($self->{open_elements}->[1]->[1] & BODY_EL)) {
6752            !!!cp ('t342');            !!!cp ('t342');
6753            ## Ignore the token            ## Ignore the token
6754          } else {          } else {
# Line 5568  sub _tree_construction_main ($) { Line 6762  sub _tree_construction_main ($) {
6762              }              }
6763            }            }
6764          }          }
6765            !!!nack ('t343.1');
6766          !!!next-token;          !!!next-token;
6767          redo B;          next B;
6768        } elsif ({        } elsif ({
6769                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
6770                  div => 1, dl => 1, fieldset => 1,                  div => 1, dl => 1, fieldset => 1,
6771                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
6772                  menu => 1, ol => 1, p => 1, ul => 1,                  menu => 1, ol => 1, p => 1, ul => 1,
6773                  pre => 1, listing => 1,                  pre => 1, listing => 1,
6774                    form => 1,
6775                    table => 1,
6776                    hr => 1,
6777                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
6778            if ($token->{tag_name} eq 'form' and defined $self->{form_element}) {
6779              !!!cp ('t350');
6780              !!!parse-error (type => 'in form:form', token => $token);
6781              ## Ignore the token
6782              !!!nack ('t350.1');
6783              !!!next-token;
6784              next B;
6785            }
6786    
6787          ## has a p element in scope          ## has a p element in scope
6788          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6789            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6790              !!!cp ('t344');              !!!cp ('t344');
6791              !!!back-token;              !!!back-token; # <form>
6792              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6793              redo B;                        line => $token->{line}, column => $token->{column}};
6794            } elsif ({              next B;
6795                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6796              !!!cp ('t345');              !!!cp ('t345');
6797              last INSCOPE;              last INSCOPE;
6798            }            }
6799          } # INSCOPE          } # INSCOPE
6800                        
6801          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6802          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {          if ($token->{tag_name} eq 'pre' or $token->{tag_name} eq 'listing') {
6803              !!!nack ('t346.1');
6804            !!!next-token;            !!!next-token;
6805            if ($token->{type} == CHARACTER_TOKEN) {            if ($token->{type} == CHARACTER_TOKEN) {
6806              $token->{data} =~ s/^\x0A//;              $token->{data} =~ s/^\x0A//;
# Line 5607  sub _tree_construction_main ($) { Line 6813  sub _tree_construction_main ($) {
6813            } else {            } else {
6814              !!!cp ('t348');              !!!cp ('t348');
6815            }            }
6816          } else {          } elsif ($token->{tag_name} eq 'form') {
6817            !!!cp ('t347');            !!!cp ('t347.1');
6818              $self->{form_element} = $self->{open_elements}->[-1]->[0];
6819    
6820              !!!nack ('t347.2');
6821            !!!next-token;            !!!next-token;
6822          }          } elsif ($token->{tag_name} eq 'table') {
6823          redo B;            !!!cp ('t382');
6824        } elsif ($token->{tag_name} eq 'form') {            push @{$open_tables}, [$self->{open_elements}->[-1]->[0]];
6825          if (defined $self->{form_element}) {            
6826            !!!cp ('t350');            $self->{insertion_mode} = IN_TABLE_IM;
6827            !!!parse-error (type => 'in form:form');  
6828            ## Ignore the token            !!!nack ('t382.1');
6829              !!!next-token;
6830            } elsif ($token->{tag_name} eq 'hr') {
6831              !!!cp ('t386');
6832              pop @{$self->{open_elements}};
6833            
6834              !!!nack ('t386.1');
6835            !!!next-token;            !!!next-token;
           redo B;  
6836          } else {          } else {
6837            ## 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 ({  
                       applet => 1, 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];  
6838            !!!next-token;            !!!next-token;
           redo B;  
6839          }          }
6840        } elsif ($token->{tag_name} eq 'li') {          next B;
6841          } elsif ({li => 1, dt => 1, dd => 1}->{$token->{tag_name}}) {
6842          ## has a p element in scope          ## has a p element in scope
6843          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6844            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6845              !!!cp ('t353');              !!!cp ('t353');
6846              !!!back-token;              !!!back-token; # <x>
6847              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6848              redo B;                        line => $token->{line}, column => $token->{column}};
6849            } elsif ({              next B;
6850                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6851              !!!cp ('t354');              !!!cp ('t354');
6852              last INSCOPE;              last INSCOPE;
6853            }            }
# Line 5661  sub _tree_construction_main ($) { Line 6856  sub _tree_construction_main ($) {
6856          ## Step 1          ## Step 1
6857          my $i = -1;          my $i = -1;
6858          my $node = $self->{open_elements}->[$i];          my $node = $self->{open_elements}->[$i];
6859            my $li_or_dtdd = {li => {li => 1},
6860                              dt => {dt => 1, dd => 1},
6861                              dd => {dt => 1, dd => 1}}->{$token->{tag_name}};
6862          LI: {          LI: {
6863            ## Step 2            ## Step 2
6864            if ($node->[1] eq 'li') {            if ($li_or_dtdd->{$node->[0]->manakai_local_name}) {
6865              if ($i != -1) {              if ($i != -1) {
6866                !!!cp ('t355');                !!!cp ('t355');
6867                !!!parse-error (type => 'end tag missing:'.                !!!parse-error (type => 'not closed',
6868                                $self->{open_elements}->[-1]->[1]);                                text => $self->{open_elements}->[-1]->[0]
6869                                      ->manakai_local_name,
6870                                  token => $token);
6871              } else {              } else {
6872                !!!cp ('t356');                !!!cp ('t356');
6873              }              }
# Line 5678  sub _tree_construction_main ($) { Line 6878  sub _tree_construction_main ($) {
6878            }            }
6879                        
6880            ## Step 3            ## Step 3
6881            if (not $formatting_category->{$node->[1]} and            if (not ($node->[1] & FORMATTING_EL) and
6882                #not $phrasing_category->{$node->[1]} and                #not $phrasing_category->{$node->[1]} and
6883                ($special_category->{$node->[1]} or                ($node->[1] & SPECIAL_EL or
6884                 $scoping_category->{$node->[1]}) and                 $node->[1] & SCOPING_EL) and
6885                $node->[1] ne 'address' and $node->[1] ne 'div') {                not ($node->[1] & ADDRESS_EL) and
6886                  not ($node->[1] & DIV_EL)) {
6887              !!!cp ('t358');              !!!cp ('t358');
6888              last LI;              last LI;
6889            }            }
# Line 5694  sub _tree_construction_main ($) { Line 6895  sub _tree_construction_main ($) {
6895            redo LI;            redo LI;
6896          } # LI          } # LI
6897                        
6898          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6899            !!!nack ('t359.1');
6900          !!!next-token;          !!!next-token;
6901          redo B;          next B;
       } elsif ($token->{tag_name} eq 'dd' or $token->{tag_name} eq 'dt') {  
         ## has a p element in scope  
         INSCOPE: for (reverse @{$self->{open_elements}}) {  
           if ($_->[1] eq 'p') {  
             !!!cp ('t360');  
             !!!back-token;  
             $token = {type => END_TAG_TOKEN, tag_name => 'p'};  
             redo B;  
           } elsif ({  
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
             !!!cp ('t361');  
             last INSCOPE;  
           }  
         } # INSCOPE  
             
         ## Step 1  
         my $i = -1;  
         my $node = $self->{open_elements}->[$i];  
         LI: {  
           ## Step 2  
           if ($node->[1] eq 'dt' or $node->[1] eq 'dd') {  
             if ($i != -1) {  
               !!!cp ('t362');  
               !!!parse-error (type => 'end tag missing:'.  
                               $self->{open_elements}->[-1]->[1]);  
             } else {  
               !!!cp ('t363');  
             }  
             splice @{$self->{open_elements}}, $i;  
             last LI;  
           } else {  
             !!!cp ('t364');  
           }  
             
           ## Step 3  
           if (not $formatting_category->{$node->[1]} and  
               #not $phrasing_category->{$node->[1]} and  
               ($special_category->{$node->[1]} or  
                $scoping_category->{$node->[1]}) and  
               $node->[1] ne 'address' and $node->[1] ne 'div') {  
             !!!cp ('t365');  
             last LI;  
           }  
             
           !!!cp ('t366');  
           ## Step 4  
           $i--;  
           $node = $self->{open_elements}->[$i];  
           redo LI;  
         } # LI  
             
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
         !!!next-token;  
         redo B;  
6902        } elsif ($token->{tag_name} eq 'plaintext') {        } elsif ($token->{tag_name} eq 'plaintext') {
6903          ## has a p element in scope          ## has a p element in scope
6904          INSCOPE: for (reverse @{$self->{open_elements}}) {          INSCOPE: for (reverse @{$self->{open_elements}}) {
6905            if ($_->[1] eq 'p') {            if ($_->[1] & P_EL) {
6906              !!!cp ('t367');              !!!cp ('t367');
6907              !!!back-token;              !!!back-token; # <plaintext>
6908              $token = {type => END_TAG_TOKEN, tag_name => 'p'};              $token = {type => END_TAG_TOKEN, tag_name => 'p',
6909              redo B;                        line => $token->{line}, column => $token->{column}};
6910            } elsif ({              next B;
6911                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($_->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$_->[1]}) {  
6912              !!!cp ('t368');              !!!cp ('t368');
6913              last INSCOPE;              last INSCOPE;
6914            }            }
6915          } # INSCOPE          } # INSCOPE
6916                        
6917          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6918                        
6919          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;          $self->{content_model} = PLAINTEXT_CONTENT_MODEL;
6920                        
6921            !!!nack ('t368.1');
6922          !!!next-token;          !!!next-token;
6923          redo B;          next B;
6924        } elsif ($token->{tag_name} eq 'a') {        } elsif ($token->{tag_name} eq 'a') {
6925          AFE: for my $i (reverse 0..$#$active_formatting_elements) {          AFE: for my $i (reverse 0..$#$active_formatting_elements) {
6926            my $node = $active_formatting_elements->[$i];            my $node = $active_formatting_elements->[$i];
6927            if ($node->[1] eq 'a') {            if ($node->[1] & A_EL) {
6928              !!!cp ('t371');              !!!cp ('t371');
6929              !!!parse-error (type => 'in a:a');              !!!parse-error (type => 'in a:a', token => $token);
6930                            
6931              !!!back-token;              !!!back-token; # <a>
6932              $token = {type => END_TAG_TOKEN, tag_name => 'a'};              $token = {type => END_TAG_TOKEN, tag_name => 'a',
6933              $formatting_end_tag->($token->{tag_name});                        line => $token->{line}, column => $token->{column}};
6934                $formatting_end_tag->($token);
6935                            
6936              AFE2: for (reverse 0..$#$active_formatting_elements) {              AFE2: for (reverse 0..$#$active_formatting_elements) {
6937                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {                if ($active_formatting_elements->[$_]->[0] eq $node->[0]) {
# Line 5810  sub _tree_construction_main ($) { Line 6956  sub _tree_construction_main ($) {
6956                        
6957          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6958    
6959          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6960          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6961    
6962            !!!nack ('t374.1');
6963          !!!next-token;          !!!next-token;
6964          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;  
6965        } elsif ($token->{tag_name} eq 'nobr') {        } elsif ($token->{tag_name} eq 'nobr') {
6966          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
6967    
6968          ## has a |nobr| element in scope          ## has a |nobr| element in scope
6969          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6970            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6971            if ($node->[1] eq 'nobr') {            if ($node->[1] & NOBR_EL) {
6972              !!!cp ('t376');              !!!cp ('t376');
6973              !!!parse-error (type => 'in nobr:nobr');              !!!parse-error (type => 'in nobr:nobr', token => $token);
6974              !!!back-token;              !!!back-token; # <nobr>
6975              $token = {type => END_TAG_TOKEN, tag_name => 'nobr'};              $token = {type => END_TAG_TOKEN, tag_name => 'nobr',
6976              redo B;                        line => $token->{line}, column => $token->{column}};
6977            } elsif ({              next B;
6978                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
6979              !!!cp ('t377');              !!!cp ('t377');
6980              last INSCOPE;              last INSCOPE;
6981            }            }
6982          } # INSCOPE          } # INSCOPE
6983                    
6984          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
6985          push @$active_formatting_elements, $self->{open_elements}->[-1];          push @$active_formatting_elements, $self->{open_elements}->[-1];
6986                    
6987            !!!nack ('t377.1');
6988          !!!next-token;          !!!next-token;
6989          redo B;          next B;
6990        } elsif ($token->{tag_name} eq 'button') {        } elsif ($token->{tag_name} eq 'button') {
6991          ## has a button element in scope          ## has a button element in scope
6992          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
6993            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
6994            if ($node->[1] eq 'button') {            if ($node->[1] & BUTTON_EL) {
6995              !!!cp ('t378');              !!!cp ('t378');
6996              !!!parse-error (type => 'in button:button');              !!!parse-error (type => 'in button:button', token => $token);
6997              !!!back-token;              !!!back-token; # <button>
6998              $token = {type => END_TAG_TOKEN, tag_name => 'button'};              $token = {type => END_TAG_TOKEN, tag_name => 'button',
6999              redo B;                        line => $token->{line}, column => $token->{column}};
7000            } elsif ({              next B;
7001                      applet => 1, table => 1, caption => 1, td => 1, th => 1,            } elsif ($node->[1] & SCOPING_EL) {
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7002              !!!cp ('t379');              !!!cp ('t379');
7003              last INSCOPE;              last INSCOPE;
7004            }            }
# Line 5875  sub _tree_construction_main ($) { Line 7006  sub _tree_construction_main ($) {
7006                        
7007          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7008                        
7009          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7010    
7011          ## TODO: associate with $self->{form_element} if defined          ## TODO: associate with $self->{form_element} if defined
7012    
7013          push @$active_formatting_elements, ['#marker', ''];          push @$active_formatting_elements, ['#marker', ''];
7014    
7015            !!!nack ('t379.1');
7016          !!!next-token;          !!!next-token;
7017          redo B;          next B;
       } elsif ({  
                 applet => 1, marquee => 1, object => 1,  
                }->{$token->{tag_name}}) {  
         !!!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 ({  
                     applet => 1, 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;  
7018        } elsif ({        } elsif ({
7019                  area => 1, basefont => 1, bgsound => 1, br => 1,                  xmp => 1,
7020                  embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,                  iframe => 1,
7021                  image => 1,                  noembed => 1,
7022                    noframes => 1, ## NOTE: This is an "as if in head" code clone.
7023                    noscript => 0, ## TODO: 1 if scripting is enabled
7024                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7025          if ($token->{tag_name} eq 'image') {          if ($token->{tag_name} eq 'xmp') {
7026            !!!cp ('t384');            !!!cp ('t381');
7027            !!!parse-error (type => 'image');            $reconstruct_active_formatting_elements->($insert_to_current);
           $token->{tag_name} = 'img';  
7028          } else {          } else {
7029            !!!cp ('t385');            !!!cp ('t399');
7030          }          }
7031            ## NOTE: There is an "as if in body" code clone.
7032          ## NOTE: There is an "as if <br>" code clone.          $parse_rcdata->(CDATA_CONTENT_MODEL);
7033          $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 ({  
                     applet => 1, 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;  
7034        } elsif ($token->{tag_name} eq 'isindex') {        } elsif ($token->{tag_name} eq 'isindex') {
7035          !!!parse-error (type => 'isindex');          !!!parse-error (type => 'isindex', token => $token);
7036                    
7037          if (defined $self->{form_element}) {          if (defined $self->{form_element}) {
7038            !!!cp ('t389');            !!!cp ('t389');
7039            ## Ignore the token            ## Ignore the token
7040              !!!nack ('t389'); ## NOTE: Not acknowledged.
7041            !!!next-token;            !!!next-token;
7042            redo B;            next B;
7043          } else {          } else {
7044              !!!ack ('t391.1');
7045    
7046            my $at = $token->{attributes};            my $at = $token->{attributes};
7047            my $form_attrs;            my $form_attrs;
7048            $form_attrs->{action} = $at->{action} if $at->{action};            $form_attrs->{action} = $at->{action} if $at->{action};
# Line 5994  sub _tree_construction_main ($) { Line 7052  sub _tree_construction_main ($) {
7052            delete $at->{prompt};            delete $at->{prompt};
7053            my @tokens = (            my @tokens = (
7054                          {type => START_TAG_TOKEN, tag_name => 'form',                          {type => START_TAG_TOKEN, tag_name => 'form',
7055                           attributes => $form_attrs},                           attributes => $form_attrs,
7056                          {type => START_TAG_TOKEN, tag_name => 'hr'},                           line => $token->{line}, column => $token->{column}},
7057                          {type => START_TAG_TOKEN, tag_name => 'p'},                          {type => START_TAG_TOKEN, tag_name => 'hr',
7058                          {type => START_TAG_TOKEN, tag_name => 'label'},                           line => $token->{line}, column => $token->{column}},
7059                            {type => START_TAG_TOKEN, tag_name => 'p',
7060                             line => $token->{line}, column => $token->{column}},
7061                            {type => START_TAG_TOKEN, tag_name => 'label',
7062                             line => $token->{line}, column => $token->{column}},
7063                         );                         );
7064            if ($prompt_attr) {            if ($prompt_attr) {
7065              !!!cp ('t390');              !!!cp ('t390');
7066              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value}};              push @tokens, {type => CHARACTER_TOKEN, data => $prompt_attr->{value},
7067                               #line => $token->{line}, column => $token->{column},
7068                              };
7069            } else {            } else {
7070              !!!cp ('t391');              !!!cp ('t391');
7071              push @tokens, {type => CHARACTER_TOKEN,              push @tokens, {type => CHARACTER_TOKEN,
7072                             data => 'This is a searchable index. Insert your search keywords here: '}; # SHOULD                             data => 'This is a searchable index. Insert your search keywords here: ',
7073                               #line => $token->{line}, column => $token->{column},
7074                              }; # SHOULD
7075              ## TODO: make this configurable              ## TODO: make this configurable
7076            }            }
7077            push @tokens,            push @tokens,
7078                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at},                          {type => START_TAG_TOKEN, tag_name => 'input', attributes => $at,
7079                             line => $token->{line}, column => $token->{column}},
7080                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD                          #{type => CHARACTER_TOKEN, data => ''}, # SHOULD
7081                          {type => END_TAG_TOKEN, tag_name => 'label'},                          {type => END_TAG_TOKEN, tag_name => 'label',
7082                          {type => END_TAG_TOKEN, tag_name => 'p'},                           line => $token->{line}, column => $token->{column}},
7083                          {type => START_TAG_TOKEN, tag_name => 'hr'},                          {type => END_TAG_TOKEN, tag_name => 'p',
7084                          {type => END_TAG_TOKEN, tag_name => 'form'};                           line => $token->{line}, column => $token->{column}},
7085            $token = shift @tokens;                          {type => START_TAG_TOKEN, tag_name => 'hr',
7086                             line => $token->{line}, column => $token->{column}},
7087                            {type => END_TAG_TOKEN, tag_name => 'form',
7088                             line => $token->{line}, column => $token->{column}};
7089            !!!back-token (@tokens);            !!!back-token (@tokens);
7090            redo B;            !!!next-token;
7091              next B;
7092          }          }
7093        } elsif ($token->{tag_name} eq 'textarea') {        } elsif ($token->{tag_name} eq 'textarea') {
7094          my $tag_name = $token->{tag_name};          my $tag_name = $token->{tag_name};
7095          my $el;          my $el;
7096          !!!create-element ($el, $token->{tag_name}, $token->{attributes});          !!!create-element ($el, $HTML_NS, $token->{tag_name}, $token->{attributes}, $token);
7097                    
7098          ## TODO: $self->{form_element} if defined          ## TODO: $self->{form_element} if defined
7099          $self->{content_model} = RCDATA_CONTENT_MODEL;          $self->{content_model} = RCDATA_CONTENT_MODEL;
# Line 6031  sub _tree_construction_main ($) { Line 7102  sub _tree_construction_main ($) {
7102          $insert->($el);          $insert->($el);
7103                    
7104          my $text = '';          my $text = '';
7105            !!!nack ('t392.1');
7106          !!!next-token;          !!!next-token;
7107          if ($token->{type} == CHARACTER_TOKEN) {          if ($token->{type} == CHARACTER_TOKEN) {
7108            $token->{data} =~ s/^\x0A//;            $token->{data} =~ s/^\x0A//;
# Line 6061  sub _tree_construction_main ($) { Line 7133  sub _tree_construction_main ($) {
7133            ## Ignore the token            ## Ignore the token
7134          } else {          } else {
7135            !!!cp ('t398');            !!!cp ('t398');
7136            !!!parse-error (type => 'in RCDATA:#'.$token->{type});            !!!parse-error (type => 'in RCDATA:#eof', token => $token);
7137          }          }
7138          !!!next-token;          !!!next-token;
7139            next B;
7140          } elsif ($token->{tag_name} eq 'rt' or
7141                   $token->{tag_name} eq 'rp') {
7142            ## has a |ruby| element in scope
7143            INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7144              my $node = $self->{open_elements}->[$_];
7145              if ($node->[1] & RUBY_EL) {
7146                !!!cp ('t398.1');
7147                ## generate implied end tags
7148                while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
7149                  !!!cp ('t398.2');
7150                  pop @{$self->{open_elements}};
7151                }
7152                unless ($self->{open_elements}->[-1]->[1] & RUBY_EL) {
7153                  !!!cp ('t398.3');
7154                  !!!parse-error (type => 'not closed',
7155                                  text => $self->{open_elements}->[-1]->[0]
7156                                      ->manakai_local_name,
7157                                  token => $token);
7158                  pop @{$self->{open_elements}}
7159                      while not $self->{open_elements}->[-1]->[1] & RUBY_EL;
7160                }
7161                last INSCOPE;
7162              } elsif ($node->[1] & SCOPING_EL) {
7163                !!!cp ('t398.4');
7164                last INSCOPE;
7165              }
7166            } # INSCOPE
7167    
7168            !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7169    
7170            !!!nack ('t398.5');
7171            !!!next-token;
7172          redo B;          redo B;
7173        } elsif ({        } elsif ($token->{tag_name} eq 'math' or
7174                  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');  
7175          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
           
         !!!insert-element-t ($token->{tag_name}, $token->{attributes});  
7176    
7177          ## TODO: associate with $self->{form_element} if defined          ## "Adjust MathML attributes" ('math' only) - done in insert-element-f
7178    
7179            ## "adjust SVG attributes" ('svg' only) - done in insert-element-f
7180    
7181            ## "adjust foreign attributes" - done in insert-element-f
7182                    
7183          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);
7184              $self->{insertion_mode} & BODY_TABLE_IMS or          
7185              $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {          if ($self->{self_closing}) {
7186            !!!cp ('t400.1');            pop @{$self->{open_elements}};
7187            $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;            !!!ack ('t398.1');
7188          } else {          } else {
7189            !!!cp ('t400.2');            !!!cp ('t398.2');
7190            $self->{insertion_mode} = IN_SELECT_IM;            $self->{insertion_mode} |= IN_FOREIGN_CONTENT_IM;
7191              ## NOTE: |<body><math><mi><svg>| -> "in foreign content" insertion
7192              ## mode, "in body" (not "in foreign content") secondary insertion
7193              ## mode, maybe.
7194          }          }
7195    
7196          !!!next-token;          !!!next-token;
7197          redo B;          next B;
7198        } elsif ({        } elsif ({
7199                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7200                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6101  sub _tree_construction_main ($) { Line 7202  sub _tree_construction_main ($) {
7202                  thead => 1, tr => 1,                  thead => 1, tr => 1,
7203                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7204          !!!cp ('t401');          !!!cp ('t401');
7205          !!!parse-error (type => 'in body:'.$token->{tag_name});          !!!parse-error (type => 'in body',
7206                            text => $token->{tag_name}, token => $token);
7207          ## Ignore the token          ## Ignore the token
7208            !!!nack ('t401.1'); ## NOTE: |<col/>| or |<frame/>| here is an error.
7209          !!!next-token;          !!!next-token;
7210          redo B;          next B;
7211                    
7212          ## ISSUE: An issue on HTML5 new elements in the spec.          ## ISSUE: An issue on HTML5 new elements in the spec.
7213        } else {        } else {
7214          !!!cp ('t402');          if ($token->{tag_name} eq 'image') {
7215              !!!cp ('t384');
7216              !!!parse-error (type => 'image', token => $token);
7217              $token->{tag_name} = 'img';
7218            } else {
7219              !!!cp ('t385');
7220            }
7221    
7222            ## NOTE: There is an "as if <br>" code clone.
7223          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7224                    
7225          !!!insert-element-t ($token->{tag_name}, $token->{attributes});          !!!insert-element-t ($token->{tag_name}, $token->{attributes}, $token);
7226    
7227            if ({
7228                 applet => 1, marquee => 1, object => 1,
7229                }->{$token->{tag_name}}) {
7230              !!!cp ('t380');
7231              push @$active_formatting_elements, ['#marker', ''];
7232              !!!nack ('t380.1');
7233            } elsif ({
7234                      b => 1, big => 1, em => 1, font => 1, i => 1,
7235                      s => 1, small => 1, strile => 1,
7236                      strong => 1, tt => 1, u => 1,
7237                     }->{$token->{tag_name}}) {
7238              !!!cp ('t375');
7239              push @$active_formatting_elements, $self->{open_elements}->[-1];
7240              !!!nack ('t375.1');
7241            } elsif ($token->{tag_name} eq 'input') {
7242              !!!cp ('t388');
7243              ## TODO: associate with $self->{form_element} if defined
7244              pop @{$self->{open_elements}};
7245              !!!ack ('t388.2');
7246            } elsif ({
7247                      area => 1, basefont => 1, bgsound => 1, br => 1,
7248                      embed => 1, img => 1, param => 1, spacer => 1, wbr => 1,
7249                      #image => 1,
7250                     }->{$token->{tag_name}}) {
7251              !!!cp ('t388.1');
7252              pop @{$self->{open_elements}};
7253              !!!ack ('t388.3');
7254            } elsif ($token->{tag_name} eq 'select') {
7255              ## TODO: associate with $self->{form_element} if defined
7256            
7257              if ($self->{insertion_mode} & TABLE_IMS or
7258                  $self->{insertion_mode} & BODY_TABLE_IMS or
7259                  $self->{insertion_mode} == IN_COLUMN_GROUP_IM) {
7260                !!!cp ('t400.1');
7261                $self->{insertion_mode} = IN_SELECT_IN_TABLE_IM;
7262              } else {
7263                !!!cp ('t400.2');
7264                $self->{insertion_mode} = IN_SELECT_IM;
7265              }
7266              !!!nack ('t400.3');
7267            } else {
7268              !!!nack ('t402');
7269            }
7270                    
7271          !!!next-token;          !!!next-token;
7272          redo B;          next B;
7273        }        }
7274      } elsif ($token->{type} == END_TAG_TOKEN) {      } elsif ($token->{type} == END_TAG_TOKEN) {
7275        if ($token->{tag_name} eq 'body') {        if ($token->{tag_name} eq 'body') {
7276          if (@{$self->{open_elements}} > 1 and          ## has a |body| element in scope
7277              $self->{open_elements}->[1]->[1] eq 'body') {          my $i;
7278            for (@{$self->{open_elements}}) {          INSCOPE: {
7279              unless ({            for (reverse @{$self->{open_elements}}) {
7280                         dd => 1, dt => 1, li => 1, p => 1, td => 1,              if ($_->[1] & BODY_EL) {
7281                         th => 1, tr => 1, body => 1, html => 1,                !!!cp ('t405');
7282                       tbody => 1, tfoot => 1, thead => 1,                $i = $_;
7283                      }->{$_->[1]}) {                last INSCOPE;
7284                !!!cp ('t403');              } elsif ($_->[1] & SCOPING_EL) {
7285                !!!parse-error (type => 'not closed:'.$_->[1]);                !!!cp ('t405.1');
7286              } else {                last;
               !!!cp ('t404');  
7287              }              }
7288            }            }
7289    
7290            $self->{insertion_mode} = AFTER_BODY_IM;            !!!parse-error (type => 'start tag not allowed',
7291            !!!next-token;                            text => $token->{tag_name}, token => $token);
7292            redo B;            ## NOTE: Ignore the token.
         } else {  
           !!!cp ('t405');  
           !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});  
           ## Ignore the token  
7293            !!!next-token;            !!!next-token;
7294            redo B;            next B;
7295            } # INSCOPE
7296    
7297            for (@{$self->{open_elements}}) {
7298              unless ($_->[1] & ALL_END_TAG_OPTIONAL_EL) {
7299                !!!cp ('t403');
7300                !!!parse-error (type => 'not closed',
7301                                text => $_->[0]->manakai_local_name,
7302                                token => $token);
7303                last;
7304              } else {
7305                !!!cp ('t404');
7306              }
7307          }          }
7308    
7309            $self->{insertion_mode} = AFTER_BODY_IM;
7310            !!!next-token;
7311            next B;
7312        } elsif ($token->{tag_name} eq 'html') {        } elsif ($token->{tag_name} eq 'html') {
7313          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
7314            ## up-to-date, though it has same effect as speced.
7315            if (@{$self->{open_elements}} > 1 and
7316                $self->{open_elements}->[1]->[1] & BODY_EL) {
7317            ## ISSUE: There is an issue in the spec.            ## ISSUE: There is an issue in the spec.
7318            if ($self->{open_elements}->[-1]->[1] ne 'body') {            unless ($self->{open_elements}->[-1]->[1] & BODY_EL) {
7319              !!!cp ('t406');              !!!cp ('t406');
7320              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[1]->[1]);              !!!parse-error (type => 'not closed',
7321                                text => $self->{open_elements}->[1]->[0]
7322                                    ->manakai_local_name,
7323                                token => $token);
7324            } else {            } else {
7325              !!!cp ('t407');              !!!cp ('t407');
7326            }            }
7327            $self->{insertion_mode} = AFTER_BODY_IM;            $self->{insertion_mode} = AFTER_BODY_IM;
7328            ## reprocess            ## reprocess
7329            redo B;            next B;
7330          } else {          } else {
7331            !!!cp ('t408');            !!!cp ('t408');
7332            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7333                              text => $token->{tag_name}, token => $token);
7334            ## Ignore the token            ## Ignore the token
7335            !!!next-token;            !!!next-token;
7336            redo B;            next B;
7337          }          }
7338        } elsif ({        } elsif ({
7339                  address => 1, blockquote => 1, center => 1, dir => 1,                  address => 1, blockquote => 1, center => 1, dir => 1,
# Line 6173  sub _tree_construction_main ($) { Line 7346  sub _tree_construction_main ($) {
7346          my $i;          my $i;
7347          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7348            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7349            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7350              !!!cp ('t410');              !!!cp ('t410');
7351              $i = $_;              $i = $_;
7352              last INSCOPE;              last INSCOPE;
7353            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7354              !!!cp ('t411');              !!!cp ('t411');
7355              last INSCOPE;              last INSCOPE;
7356            }            }
# Line 6188  sub _tree_construction_main ($) { Line 7358  sub _tree_construction_main ($) {
7358    
7359          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7360            !!!cp ('t413');            !!!cp ('t413');
7361            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7362                              text => $token->{tag_name}, token => $token);
7363              ## NOTE: Ignore the token.
7364          } else {          } else {
7365            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7366            while ({            while ({
7367                      ## END_TAG_OPTIONAL_EL
7368                    dd => ($token->{tag_name} ne 'dd'),                    dd => ($token->{tag_name} ne 'dd'),
7369                    dt => ($token->{tag_name} ne 'dt'),                    dt => ($token->{tag_name} ne 'dt'),
7370                    li => ($token->{tag_name} ne 'li'),                    li => ($token->{tag_name} ne 'li'),
7371                    p => 1,                    p => 1,
7372                   }->{$self->{open_elements}->[-1]->[1]}) {                    rt => 1,
7373                      rp => 1,
7374                     }->{$self->{open_elements}->[-1]->[0]->manakai_local_name}) {
7375              !!!cp ('t409');              !!!cp ('t409');
7376              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7377            }            }
7378    
7379            ## Step 2.            ## Step 2.
7380            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7381                      ne $token->{tag_name}) {
7382              !!!cp ('t412');              !!!cp ('t412');
7383              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7384                                text => $self->{open_elements}->[-1]->[0]
7385                                    ->manakai_local_name,
7386                                token => $token);
7387            } else {            } else {
7388              !!!cp ('t414');              !!!cp ('t414');
7389            }            }
# Line 6219  sub _tree_construction_main ($) { Line 7398  sub _tree_construction_main ($) {
7398                }->{$token->{tag_name}};                }->{$token->{tag_name}};
7399          }          }
7400          !!!next-token;          !!!next-token;
7401          redo B;          next B;
7402        } elsif ($token->{tag_name} eq 'form') {        } elsif ($token->{tag_name} eq 'form') {
7403          undef $self->{form_element};          undef $self->{form_element};
7404    
# Line 6227  sub _tree_construction_main ($) { Line 7406  sub _tree_construction_main ($) {
7406          my $i;          my $i;
7407          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7408            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7409            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & FORM_EL) {
7410              !!!cp ('t418');              !!!cp ('t418');
7411              $i = $_;              $i = $_;
7412              last INSCOPE;              last INSCOPE;
7413            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7414              !!!cp ('t419');              !!!cp ('t419');
7415              last INSCOPE;              last INSCOPE;
7416            }            }
# Line 6242  sub _tree_construction_main ($) { Line 7418  sub _tree_construction_main ($) {
7418    
7419          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7420            !!!cp ('t421');            !!!cp ('t421');
7421            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7422                              text => $token->{tag_name}, token => $token);
7423              ## NOTE: Ignore the token.
7424          } else {          } else {
7425            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7426            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7427              !!!cp ('t417');              !!!cp ('t417');
7428              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7429            }            }
7430                        
7431            ## Step 2.            ## Step 2.
7432            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7433                      ne $token->{tag_name}) {
7434              !!!cp ('t417.1');              !!!cp ('t417.1');
7435              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7436                                text => $self->{open_elements}->[-1]->[0]
7437                                    ->manakai_local_name,
7438                                token => $token);
7439            } else {            } else {
7440              !!!cp ('t420');              !!!cp ('t420');
7441            }              }  
# Line 6265  sub _tree_construction_main ($) { Line 7445  sub _tree_construction_main ($) {
7445          }          }
7446    
7447          !!!next-token;          !!!next-token;
7448          redo B;          next B;
7449        } elsif ({        } elsif ({
7450                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,                  h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,
7451                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
# Line 6273  sub _tree_construction_main ($) { Line 7453  sub _tree_construction_main ($) {
7453          my $i;          my $i;
7454          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7455            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7456            if ({            if ($node->[1] & HEADING_EL) {
                h1 => 1, h2 => 1, h3 => 1, h4 => 1, h5 => 1, h6 => 1,  
               }->{$node->[1]}) {  
7457              !!!cp ('t423');              !!!cp ('t423');
7458              $i = $_;              $i = $_;
7459              last INSCOPE;              last INSCOPE;
7460            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7461              !!!cp ('t424');              !!!cp ('t424');
7462              last INSCOPE;              last INSCOPE;
7463            }            }
# Line 6290  sub _tree_construction_main ($) { Line 7465  sub _tree_construction_main ($) {
7465    
7466          unless (defined $i) { # has an element in scope          unless (defined $i) { # has an element in scope
7467            !!!cp ('t425.1');            !!!cp ('t425.1');
7468            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7469                              text => $token->{tag_name}, token => $token);
7470              ## NOTE: Ignore the token.
7471          } else {          } else {
7472            ## Step 1. generate implied end tags            ## Step 1. generate implied end tags
7473            while ({            while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                   dd => 1, dt => 1, li => 1, p => 1,  
                  }->{$self->{open_elements}->[-1]->[1]}) {  
7474              !!!cp ('t422');              !!!cp ('t422');
7475              pop @{$self->{open_elements}};              pop @{$self->{open_elements}};
7476            }            }
7477                        
7478            ## Step 2.            ## Step 2.
7479            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7480                      ne $token->{tag_name}) {
7481              !!!cp ('t425');              !!!cp ('t425');
7482              !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});              !!!parse-error (type => 'unmatched end tag',
7483                                text => $token->{tag_name}, token => $token);
7484            } else {            } else {
7485              !!!cp ('t426');              !!!cp ('t426');
7486            }            }
# Line 6313  sub _tree_construction_main ($) { Line 7490  sub _tree_construction_main ($) {
7490          }          }
7491                    
7492          !!!next-token;          !!!next-token;
7493          redo B;          next B;
7494        } elsif ($token->{tag_name} eq 'p') {        } elsif ($token->{tag_name} eq 'p') {
7495          ## has an element in scope          ## has an element in scope
7496          my $i;          my $i;
7497          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {          INSCOPE: for (reverse 0..$#{$self->{open_elements}}) {
7498            my $node = $self->{open_elements}->[$_];            my $node = $self->{open_elements}->[$_];
7499            if ($node->[1] eq $token->{tag_name}) {            if ($node->[1] & P_EL) {
7500              !!!cp ('t410.1');              !!!cp ('t410.1');
7501              $i = $_;              $i = $_;
7502              last INSCOPE;              last INSCOPE;
7503            } elsif ({            } elsif ($node->[1] & SCOPING_EL) {
                     applet => 1, table => 1, caption => 1, td => 1, th => 1,  
                     button => 1, marquee => 1, object => 1, html => 1,  
                    }->{$node->[1]}) {  
7504              !!!cp ('t411.1');              !!!cp ('t411.1');
7505              last INSCOPE;              last INSCOPE;
7506            }            }
7507          } # INSCOPE          } # INSCOPE
7508    
7509          if (defined $i) {          if (defined $i) {
7510            if ($self->{open_elements}->[-1]->[1] ne $token->{tag_name}) {            if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7511                      ne $token->{tag_name}) {
7512              !!!cp ('t412.1');              !!!cp ('t412.1');
7513              !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);              !!!parse-error (type => 'not closed',
7514                                text => $self->{open_elements}->[-1]->[0]
7515                                    ->manakai_local_name,
7516                                token => $token);
7517            } else {            } else {
7518              !!!cp ('t414.1');              !!!cp ('t414.1');
7519            }            }
# Line 6343  sub _tree_construction_main ($) { Line 7521  sub _tree_construction_main ($) {
7521            splice @{$self->{open_elements}}, $i;            splice @{$self->{open_elements}}, $i;
7522          } else {          } else {
7523            !!!cp ('t413.1');            !!!cp ('t413.1');
7524            !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});            !!!parse-error (type => 'unmatched end tag',
7525                              text => $token->{tag_name}, token => $token);
7526    
7527            !!!cp ('t415.1');            !!!cp ('t415.1');
7528            ## As if <p>, then reprocess the current token            ## As if <p>, then reprocess the current token
7529            my $el;            my $el;
7530            !!!create-element ($el, 'p');            !!!create-element ($el, $HTML_NS, 'p',, $token);
7531            $insert->($el);            $insert->($el);
7532            ## NOTE: Not inserted into |$self->{open_elements}|.            ## NOTE: Not inserted into |$self->{open_elements}|.
7533          }          }
7534    
7535          !!!next-token;          !!!next-token;
7536          redo B;          next B;
7537        } elsif ({        } elsif ({
7538                  a => 1,                  a => 1,
7539                  b => 1, big => 1, em => 1, font => 1, i => 1,                  b => 1, big => 1, em => 1, font => 1, i => 1,
# Line 6362  sub _tree_construction_main ($) { Line 7541  sub _tree_construction_main ($) {
7541                  strong => 1, tt => 1, u => 1,                  strong => 1, tt => 1, u => 1,
7542                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7543          !!!cp ('t427');          !!!cp ('t427');
7544          $formatting_end_tag->($token->{tag_name});          $formatting_end_tag->($token);
7545          redo B;          next B;
7546        } elsif ($token->{tag_name} eq 'br') {        } elsif ($token->{tag_name} eq 'br') {
7547          !!!cp ('t428');          !!!cp ('t428');
7548          !!!parse-error (type => 'unmatched end tag:br');          !!!parse-error (type => 'unmatched end tag',
7549                            text => 'br', token => $token);
7550    
7551          ## As if <br>          ## As if <br>
7552          $reconstruct_active_formatting_elements->($insert_to_current);          $reconstruct_active_formatting_elements->($insert_to_current);
7553                    
7554          my $el;          my $el;
7555          !!!create-element ($el, 'br');          !!!create-element ($el, $HTML_NS, 'br',, $token);
7556          $insert->($el);          $insert->($el);
7557                    
7558          ## Ignore the token.          ## Ignore the token.
7559          !!!next-token;          !!!next-token;
7560          redo B;          next B;
7561        } elsif ({        } elsif ({
7562                  caption => 1, col => 1, colgroup => 1, frame => 1,                  caption => 1, col => 1, colgroup => 1, frame => 1,
7563                  frameset => 1, head => 1, option => 1, optgroup => 1,                  frameset => 1, head => 1, option => 1, optgroup => 1,
# Line 6391  sub _tree_construction_main ($) { Line 7571  sub _tree_construction_main ($) {
7571                  noscript => 0, ## TODO: if scripting is enabled                  noscript => 0, ## TODO: if scripting is enabled
7572                 }->{$token->{tag_name}}) {                 }->{$token->{tag_name}}) {
7573          !!!cp ('t429');          !!!cp ('t429');
7574          !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});          !!!parse-error (type => 'unmatched end tag',
7575                            text => $token->{tag_name}, token => $token);
7576          ## Ignore the token          ## Ignore the token
7577          !!!next-token;          !!!next-token;
7578          redo B;          next B;
7579                    
7580          ## ISSUE: Issue on HTML5 new elements in spec          ## ISSUE: Issue on HTML5 new elements in spec
7581                    
# Line 6405  sub _tree_construction_main ($) { Line 7586  sub _tree_construction_main ($) {
7586    
7587          ## Step 2          ## Step 2
7588          S2: {          S2: {
7589            if ($node->[1] eq $token->{tag_name}) {            if ($node->[0]->manakai_local_name eq $token->{tag_name}) {
7590              ## Step 1              ## Step 1
7591              ## generate implied end tags              ## generate implied end tags
7592              while ({              while ($self->{open_elements}->[-1]->[1] & END_TAG_OPTIONAL_EL) {
                     dd => 1, dt => 1, li => 1, p => 1,  
                    }->{$self->{open_elements}->[-1]->[1]}) {  
7593                !!!cp ('t430');                !!!cp ('t430');
7594                ## ISSUE: Can this case be reached?                ## NOTE: |<ruby><rt></ruby>|.
7595                  ## ISSUE: <ruby><rt></rt> will also take this code path,
7596                  ## which seems wrong.
7597                pop @{$self->{open_elements}};                pop @{$self->{open_elements}};
7598                  $node_i++;
7599              }              }
7600                    
7601              ## Step 2              ## Step 2
7602              if ($token->{tag_name} ne $self->{open_elements}->[-1]->[1]) {              if ($self->{open_elements}->[-1]->[0]->manakai_local_name
7603                        ne $token->{tag_name}) {
7604                !!!cp ('t431');                !!!cp ('t431');
7605                ## NOTE: <x><y></x>                ## NOTE: <x><y></x>
7606                !!!parse-error (type => 'not closed:'.$self->{open_elements}->[-1]->[1]);                !!!parse-error (type => 'not closed',
7607                                  text => $self->{open_elements}->[-1]->[0]
7608                                      ->manakai_local_name,
7609                                  token => $token);
7610              } else {              } else {
7611                !!!cp ('t432');                !!!cp ('t432');
7612              }              }
7613                            
7614              ## Step 3              ## Step 3
7615              splice @{$self->{open_elements}}, $node_i;              splice @{$self->{open_elements}}, $node_i if $node_i < 0;
7616    
7617              !!!next-token;              !!!next-token;
7618              last S2;              last S2;
7619            } else {            } else {
7620              ## Step 3              ## Step 3
7621              if (not $formatting_category->{$node->[1]} and              if (not ($node->[1] & FORMATTING_EL) and
7622                  #not $phrasing_category->{$node->[1]} and                  #not $phrasing_category->{$node->[1]} and
7623                  ($special_category->{$node->[1]} or                  ($node->[1] & SPECIAL_EL or
7624                   $scoping_category->{$node->[1]})) {                   $node->[1] & SCOPING_EL)) {
7625                !!!cp ('t433');                !!!cp ('t433');
7626                !!!parse-error (type => 'unmatched end tag:'.$token->{tag_name});                !!!parse-error (type => 'unmatched end tag',
7627                                  text => $token->{tag_name}, token => $token);
7628                ## Ignore the token                ## Ignore the token
7629                !!!next-token;                !!!next-token;
7630                last S2;                last S2;
# Line 6453  sub _tree_construction_main ($) { Line 7640  sub _tree_construction_main ($) {
7640            ## Step 5;            ## Step 5;
7641            redo S2;            redo S2;
7642          } # S2          } # S2
7643          redo B;          next B;
7644        }        }
7645      }      }
7646      redo B;      next B;
7647      } continue { # B
7648        if ($self->{insertion_mode} & IN_FOREIGN_CONTENT_IM) {
7649          ## NOTE: The code below is executed in cases where it does not have
7650          ## to be, but it it is harmless even in those cases.
7651          ## has an element in scope
7652          INSCOPE: {
7653            for (reverse 0..$#{$self->{open_elements}}) {
7654              my $node = $self->{open_elements}->[$_];
7655              if ($node->[1] & FOREIGN_EL) {
7656                last INSCOPE;
7657              } elsif ($node->[1] & SCOPING_EL) {
7658                last;
7659              }
7660            }
7661            
7662            ## NOTE: No foreign element in scope.
7663            $self->{insertion_mode} &= ~ IN_FOREIGN_CONTENT_IM;
7664          } # INSCOPE
7665        }
7666    } # B    } # B
7667    
7668    ## Stop parsing # MUST    ## Stop parsing # MUST
# Line 6464  sub _tree_construction_main ($) { Line 7670  sub _tree_construction_main ($) {
7670    ## TODO: script stuffs    ## TODO: script stuffs
7671  } # _tree_construct_main  } # _tree_construct_main
7672    
7673  sub set_inner_html ($$$) {  sub set_inner_html ($$$;$) {
7674    my $class = shift;    my $class = shift;
7675    my $node = shift;    my $node = shift;
7676    my $s = \$_[0];    my $s = \$_[0];
7677    my $onerror = $_[1];    my $onerror = $_[1];
7678      my $get_wrapper = $_[2] || sub ($) { return $_[0] };
7679    
7680    ## ISSUE: Should {confident} be true?    ## ISSUE: Should {confident} be true?
7681    
# Line 6487  sub set_inner_html ($$$) { Line 7694  sub set_inner_html ($$$) {
7694      }      }
7695    
7696      ## Step 3, 4, 5 # MUST      ## Step 3, 4, 5 # MUST
7697      $class->parse_string ($$s => $node, $onerror);      $class->parse_char_string ($$s => $node, $onerror, $get_wrapper);
7698    } elsif ($nt == 1) {    } elsif ($nt == 1) {
7699      ## TODO: If non-html element      ## TODO: If non-html element
7700    
7701      ## NOTE: Most of this code is copied from |parse_string|      ## NOTE: Most of this code is copied from |parse_string|
7702    
7703    ## TODO: Support for $get_wrapper
7704    
7705      ## Step 1 # MUST      ## Step 1 # MUST
7706      my $this_doc = $node->owner_document;      my $this_doc = $node->owner_document;
7707      my $doc = $this_doc->implementation->create_document;      my $doc = $this_doc->implementation->create_document;
# Line 6502  sub set_inner_html ($$$) { Line 7711  sub set_inner_html ($$$) {
7711    
7712      ## Step 8 # MUST      ## Step 8 # MUST
7713      my $i = 0;      my $i = 0;
7714      my $line = 1;      $p->{line_prev} = $p->{line} = 1;
7715      my $column = 0;      $p->{column_prev} = $p->{column} = 0;
7716      $p->{set_next_char} = sub {      $p->{set_next_char} = sub {
7717        my $self = shift;        my $self = shift;
7718    
# Line 6512  sub set_inner_html ($$$) { Line 7721  sub set_inner_html ($$$) {
7721    
7722        $self->{next_char} = -1 and return if $i >= length $$s;        $self->{next_char} = -1 and return if $i >= length $$s;
7723        $self->{next_char} = ord substr $$s, $i++, 1;        $self->{next_char} = ord substr $$s, $i++, 1;
7724        $column++;  
7725          ($p->{line_prev}, $p->{column_prev}) = ($p->{line}, $p->{column});
7726          $p->{column}++;
7727    
7728        if ($self->{next_char} == 0x000A) { # LF        if ($self->{next_char} == 0x000A) { # LF
7729          $line++;          $p->{line}++;
7730          $column = 0;          $p->{column} = 0;
7731          !!!cp ('i1');          !!!cp ('i1');
7732        } elsif ($self->{next_char} == 0x000D) { # CR        } elsif ($self->{next_char} == 0x000D) { # CR
7733          $i++ if substr ($$s, $i, 1) eq "\x0A";          $i++ if substr ($$s, $i, 1) eq "\x0A";
7734          $self->{next_char} = 0x000A; # LF # MUST          $self->{next_char} = 0x000A; # LF # MUST
7735          $line++;          $p->{line}++;
7736          $column = 0;          $p->{column} = 0;
7737          !!!cp ('i2');          !!!cp ('i2');
7738        } elsif ($self->{next_char} > 0x10FFFF) {        } elsif ($self->{next_char} > 0x10FFFF) {
7739          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
# Line 6531  sub set_inner_html ($$$) { Line 7742  sub set_inner_html ($$$) {
7742          !!!cp ('i4');          !!!cp ('i4');
7743          !!!parse-error (type => 'NULL');          !!!parse-error (type => 'NULL');
7744          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST          $self->{next_char} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
7745          } elsif ($self->{next_char} <= 0x0008 or
7746                   (0x000E <= $self->{next_char} and
7747                    $self->{next_char} <= 0x001F) or
7748                   (0x007F <= $self->{next_char} and
7749                    $self->{next_char} <= 0x009F) or
7750                   (0xD800 <= $self->{next_char} and
7751                    $self->{next_char} <= 0xDFFF) or
7752                   (0xFDD0 <= $self->{next_char} and
7753                    $self->{next_char} <= 0xFDDF) or
7754                   {
7755                    0xFFFE => 1, 0xFFFF => 1, 0x1FFFE => 1, 0x1FFFF => 1,
7756                    0x2FFFE => 1, 0x2FFFF => 1, 0x3FFFE => 1, 0x3FFFF => 1,
7757                    0x4FFFE => 1, 0x4FFFF => 1, 0x5FFFE => 1, 0x5FFFF => 1,
7758                    0x6FFFE => 1, 0x6FFFF => 1, 0x7FFFE => 1, 0x7FFFF => 1,
7759                    0x8FFFE => 1, 0x8FFFF => 1, 0x9FFFE => 1, 0x9FFFF => 1,
7760                    0xAFFFE => 1, 0xAFFFF => 1, 0xBFFFE => 1, 0xBFFFF => 1,
7761                    0xCFFFE => 1, 0xCFFFF => 1, 0xDFFFE => 1, 0xDFFFF => 1,
7762                    0xEFFFE => 1, 0xEFFFF => 1, 0xFFFFE => 1, 0xFFFFF => 1,
7763                    0x10FFFE => 1, 0x10FFFF => 1,
7764                   }->{$self->{next_char}}) {
7765            !!!cp ('i4.1');
7766            if ($self->{next_char} < 0x10000) {
7767              !!!parse-error (type => 'control char',
7768                              text => (sprintf 'U+%04X', $self->{next_char}));
7769            } else {
7770              !!!parse-error (type => 'control char',
7771                              text => (sprintf 'U-%08X', $self->{next_char}));
7772            }
7773        }        }
7774      };      };
7775      $p->{prev_char} = [-1, -1, -1];      $p->{prev_char} = [-1, -1, -1];
# Line 6538  sub set_inner_html ($$$) { Line 7777  sub set_inner_html ($$$) {
7777            
7778      my $ponerror = $onerror || sub {      my $ponerror = $onerror || sub {
7779        my (%opt) = @_;        my (%opt) = @_;
7780        warn "Parse error ($opt{type}) at line $opt{line} column $opt{column}\n";        my $line = $opt{line};
7781          my $column = $opt{column};
7782          if (defined $opt{token} and defined $opt{token}->{line}) {
7783            $line = $opt{token}->{line};
7784            $column = $opt{token}->{column};
7785          }
7786          warn "Parse error ($opt{type}) at line $line column $column\n";
7787      };      };
7788      $p->{parse_error} = sub {      $p->{parse_error} = sub {
7789        $ponerror->(@_, line => $line, column => $column);        $ponerror->(line => $p->{line}, column => $p->{column}, @_);
7790      };      };
7791            
7792      $p->_initialize_tokenizer;      $p->_initialize_tokenizer;
# Line 6565  sub set_inner_html ($$$) { Line 7810  sub set_inner_html ($$$) {
7810          unless defined $p->{content_model};          unless defined $p->{content_model};
7811          ## ISSUE: What is "the name of the element"? local name?          ## ISSUE: What is "the name of the element"? local name?
7812    
7813      $p->{inner_html_node} = [$node, $node_ln];      $p->{inner_html_node} = [$node, $el_category->{$node_ln}];
7814          ## TODO: Foreign element OK?
7815    
7816      ## Step 3      ## Step 3
7817      my $root = $doc->create_element_ns      my $root = $doc->create_element_ns
# Line 6575  sub set_inner_html ($$$) { Line 7821  sub set_inner_html ($$$) {
7821      $doc->append_child ($root);      $doc->append_child ($root);
7822    
7823      ## Step 5 # MUST      ## Step 5 # MUST
7824      push @{$p->{open_elements}}, [$root, 'html'];      push @{$p->{open_elements}}, [$root, $el_category->{html}];
7825    
7826      undef $p->{head_element};      undef $p->{head_element};
7827    
# Line 6621  sub set_inner_html ($$$) { Line 7867  sub set_inner_html ($$$) {
7867      ## ISSUE: mutation events?      ## ISSUE: mutation events?
7868    
7869      $p->_terminate_tree_constructor;      $p->_terminate_tree_constructor;
7870    
7871        delete $p->{parse_error}; # delete loop
7872    } else {    } else {
7873      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";
7874    }    }

Legend:
Removed from v.1.106  
changed lines
  Added in v.1.170

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24